1.mitmproxy擴展機制
參考官網:
mitmproxy addons example: https://docs.mitmproxy.org/stable/addons-examples/#example-complexhar_dumppy
2.抓包-map local
以雪球app為例
1)編寫腳本,mock2.json中自選股列表數據加倍
mitmproxydemo.py
from mitmproxy import http def request(flow: http.HTTPFlow) -> None: if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url: with open("/Users/zhaitiantian3/Public/mock2.json") as f: flow.response = http.HTTPResponse.make(200, f.read(), {"Content-Type": "application/json"})
2)手機app連接代理,安裝證書:mit.it
3)運行腳本
mitmdump -s mitmproxydemo.py
刷新自選股列表數據刷新,數據加倍;但是如果是ios手機,我這里使用代理,有的app就會報錯
<< Cannot establish TLS with 182.92.251.113:443 (sni: None): TlsException('Cannot validate certificate hostname without SNI')
因此查找解決方案:
使用命令:
mitmdump -s -k mitmproxydemo.py
mock后的數據:
3.抓包-rewrite
1)編寫python腳本
rewriter.py
import json def response(flow): if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url: data = json.loads(flow.response.content) data['data']['items'][0]['quote']['name'] = data['data']['items'][0]['quote']['name'] + "test" flow.response.text = json.dumps(data)
2)手機app連接代理,安裝證書:mit.it
3)運行腳本
mitmdump -s rewriter.py
4)數據展示如下:
4.自動生成等價類數據
編寫腳本:
import json from mitmproxy import http url_index = dict() arrays = [-5, -3, -1, 0, 1, 3, 5, 100] def response(flow: http.HTTPFlow) -> None: # if "Content-Type" in flow.response.headers.keys() and \ # "json" in flow.response.headers['Content-Type']: if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url: url = flow.request.url.split('.json')[0] if url not in url_index.keys(): url_index[url] = 0 else: url_index[url] += 1 # 去等價類中的某一個,根據訪問次數循環使用 seed = url_index[url] % len(arrays) print(seed) data = json.loads(flow.response.text) # 對數據進行批量修改 data_new = json_travel(data, num=arrays[seed]) json_new = json.dumps(data_new, indent=2) flow.response.text = json_new def json_travel(data, array=None, text=1, num=1): data_new = None # 如果是詞典,對詞典進行遍歷 if isinstance(data, dict): data_new = dict() for k, v in data.items(): data_new[k] = json_travel(v, array, text, num) # 如果是列表,對列表的每一項進行遍歷 elif isinstance(data, list): data_new = list() for item in data: item_new = json_travel(item, array, text, num) if array is None: data_new.append(item_new) elif len(data_new) < array: data_new.append(item_new) else: pass # 如果是字符串 elif isinstance(data, str): data_new = data * text # 如果是int或者float這樣的數字 elif isinstance(data, int) or isinstance(data, float): # 對數字進行一個乘積計算 data_new = data * num # 其他數據類型保持原樣 else: data_new = data return data_new def test_json_travel(): with open("demo.json") as f: data = json.load(f) print(json_travel(data, array=0)) print(json_travel(data, text=5)) print(json_travel(data, num=5))
2)手機app連接代理,安裝證書:mit.it
3)運行腳本
mitmdump -s tstcase.py
根據訪問次數循環乘當前價格,可以使用該方式mock出其他前端的邏輯