mitmdump

- -q:屏蔽mitmdump默認的控制台日志,只顯示自己腳本中的
- -s:入口腳本文件
- -p:更改端口,默認為8080
- 修改腳本文件時,不用重啟也會生效
針對 HTTP 生命周期的事件
- 請求:def request(self, flow: mitmproxy.http.HTTPFlow):
- 響應:def response(self, flow: mitmproxy.http.HTTPFlow):
- 其它:
- def http_connect(self, flow: mitmproxy.http.HTTPFlow):
- def requestheaders(self, flow: mitmproxy.http.HTTPFlow):
- def responseheaders(self, flow: mitmproxy.http.HTTPFlow):
- def error(self, flow: mitmproxy.http.HTTPFlow):
請求:def request(flow:flow)
- flow.request.headers
- 獲取所有頭信息,包含Host、User-Agent、Content-type等字段
- flow.request.url
- 完整的請求地址,包含域名及請求參數,但是不包含放在body里面的請求參數
- flow.request.host
- flow.request.method
- flow.request.scheme
- flow.request.path
- flow.request.get_text()
- 請求中body的內容,有一些http會把請求參數放在body里面,可通過此方法獲取,返回字典類型
- flow.request.get_content()
- 結果如flow.request.get_text(),返回bytes類型
- flow.request.raw_content
- 結果如flow.request.get_content(),返回bytes類型
- flow.request.urlencoded_form
- MultiDictView,content-type:application/x-www-form-urlencoded的請求參數,不包含url直接帶的鍵值參數
- flow.request.multipart_form
- MultiDictView,content-type:multipart/form-data
- flow.request.query
- 返回MultiDictView類型的數據,URL的鍵值參數
- flow.request.query.get('wd')
- flow.request.query.keys()
- flow.request.query.set_all(key,[value])
from mitmproxy.http import flow
def request(flow:flow):
# 獲取所有頭信息,包含Host、User-Agent、Content-type等字段
# print(flow.request.headers)
# 域名
# print(flow.request.host)
# 請求方式:POST、GET等
# print(flow.request.method)
# 請求類型:http、https
# print(flow.request.scheme)
# 請求的路徑,URL除域名之外的內容
# print(flow.request.path)
# 請求中body的內容,有一些http會把請求參數放在body里面,可通過此方法獲取,返回字典類型
# print(flow.request.get_text())
# 返回MultiDictView類型的數據,URL的鍵值參數
# print(flow.request.query)
# 完整的請求地址,包含域名及請求參數,但是不包含放在body里面的請求參數
if 'https://www.baidu.com' in flow.request.url:
# 取得請求參數wd的值
# print(flow.request.query.get('wd'))
# 取得所有請求參數
print(list(flow.request.query.keys()))
# 修改請求參數
flow.request.query.set_all('wd',['python'])
# 打印修改過后的參數
print(flow.request.query.get('wd'))
響應:def response(flow: flow)
- flow.response.status_code
- flow.response.text
- flow.response.content
- flow.response.get_text()
- flow.response.set_text()
- flow.response = flow.response.make(404)
from mitmproxy.http import flow
import json
import re
def response(flow: flow):
# 狀態碼
# print(flow.response.status_code)
# 返回內容,已解碼
# print(flow.response.text)
# 返回內容,Bytes類型
# print(flow.response.content)
# 取得響應的文本
# print(flow.response.get_text())
# 修改響應的文本
# flow.response.set_text('123')
# 返回404
# flow.response = flow.response.make(404)
# 修改淘寶對selenium的js檢測文件
targetUrl = 'https://g.alicdn.com/AWSC/uab/122.js'
if targetUrl in flow.request.url:
taobao_js =flow.response.get_text()
taobao_js = taobao_js.replace('!function(){function','!function (){Object.defineProperties(navigator,{webdriver: {get: () => false}})function')
flow.response.set_text(taobao_js)
print('已修改')
# 淘寶搜索商品時,自動打印商品信息
if 'https://s.taobao.com/search' in flow.request.url:
start = flow.response.text.strip().index('{')
end = -2
print(json.loads(flow.response.text.strip()[start: end])['mods']['itemlist']['data']['auctions'])
# 空氣質量網,修改檢測F12的JS
# https://www.aqistudy.cn/historydata
if 'https://www.aqistudy.cn/historydata/monthdata.php' in flow.request.url:
js = flow.response.text
js = re.sub(r'endebug.*?}\);','',js,flags=re.S)
flow.response.set_text(js)
print('已正常')