Python3自定義http/https請(qǐng)求攔截mitmproxy腳本實(shí)例
腳本內(nèi)容
代碼如下:
from mitmproxy import http, ctx from multiprocessing import Lock class Filter: def __init__(self, filter_info): self.log_info = "" self.mutex = Lock() self.filter_info = filter_info self.response_file = None self.switch_on = False self.log_file = "log.txt" def log(self, info) -> None: self.log_info += f"{info}\n\n" def write_log(self, mode="w+") -> None: self.mutex.acquire() with open(self.log_file, mode) as f: f.write(self.log_info) self.mutex.release() def is_target_flow(self, flow: http.HTTPFlow) -> bool: for info in self.filter_info: if info["str_in_url"] in flow.request.url: self.log_file = info["log_file"] self.switch_on = info["switch_on"] if info["response_file"] != None: self.response_file = info["response_file"] return True else: return False def modify_response(self, flow: http.HTTPFlow) -> http.HTTPFlow: if self.switch_on and self.response_file: with open(self.response_file, "r") as f: flow.response.content = f.read().encode() return flow def request(self, flow: http.HTTPFlow) -> None: if self.is_target_flow(flow): self.log_info = "" self.log(f"——METHOD——\n{flow.request.method}") self.log(f"——HOST——\n{flow.request.pretty_host}") self.log(f"——URL——\n{flow.request.pretty_url}") query = [i + ":" + flow.request.query[i] + "\n" for i in flow.request.query] self.log(f"——QUERY STRING——\n{''.join(query)}") if flow.request.urlencoded_form: form = [i + ":" + flow.request.urlencoded_form[i] + "\n" for i in flow.request.urlencoded_form] self.log(f"——FORM——\n{''.join(form)}") self.write_log() def response(self, flow: http.HTTPFlow) -> None: if self.is_target_flow(flow): self.log_info = "" self.log(f"——RESPONSE before modified——\n{flow.response.content.decode()}") flow = self.modify_response(flow) self.log(f"——RESPONSE after modified——\n{flow.response.content.decode()}") self.write_log(mode="a") filter_info = [ { "str_in_url": "getSimpleNews", "log_file": "getSimpleNews_log.txt", "switch_on": True, "response_file": "getSimpleNews_response.txt", }, { "str_in_url": "getQQNewsComment", "log_file": "getQQNewsComment_log.txt", "switch_on": True, "response_file": None, } ] addons = [ Filter(filter_info) ]
使用方法
運(yùn)行mitmproxy指定使用該腳本和端口號(hào)即可:
mitmproxy -p 6666 -s xxx.py
在mitmproxy運(yùn)行時(shí):
1. 會(huì)攔截url中包含str_in_url字符串的請(qǐng)求
2. 會(huì)把response.content修改為當(dāng)前mitm運(yùn)行所在目錄下的response_file文件中的內(nèi)容
3. 打印信息在當(dāng)前mitm運(yùn)行所在目錄下的log_file文件中
4. 如果無(wú)需修改response設(shè)置switch_on為False即為開(kāi)關(guān)關(guān)閉
5. 如果不修改response的話response_file需要寫(xiě)None
補(bǔ)充知識(shí):mitmproxy 監(jiān)聽(tīng)指定端口
安裝
使用python3的安裝方式
監(jiān)聽(tīng)指定端口
例子:Presto SQL請(qǐng)求的監(jiān)聽(tīng)
Presto地址:http://datacenter4:18080
mitmproxy命令(端口8484)
mitmproxy \
--mode reverse:http://datacenter4:18080 \
--listen-host datacenter4 \
--listen-port 8484 \
--replacements :~s:\/\/datacenter4/:\/\/datacenter4:18080/
然后JDBC訪問(wèn)Presto使用:jdbc:presto://datacenter4:8484
效果
以上這篇Python3自定義http/https請(qǐng)求攔截mitmproxy腳本實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Python如何利用Pandas與NumPy進(jìn)行數(shù)據(jù)清洗
許多數(shù)據(jù)科學(xué)家認(rèn)為獲取和清理數(shù)據(jù)的初始步驟占工作的 80%,花費(fèi)大量時(shí)間來(lái)清理數(shù)據(jù)集并將它們歸結(jié)為可以使用的形式。本文將利用 Python 的 Pandas和 NumPy 庫(kù)來(lái)清理數(shù)據(jù),需要的可以參考一下2022-04-04PyTorch計(jì)算損失函數(shù)對(duì)模型參數(shù)的Hessian矩陣示例
這篇文章主要為大家介紹了PyTorch計(jì)算損失函數(shù)對(duì)模型參數(shù)的Hessian矩陣的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python scatter散點(diǎn)圖用循環(huán)分類法加圖例
這篇文章主要為大家詳細(xì)介紹了python scatter散點(diǎn)圖用循環(huán)分類法加圖例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03python configparser中默認(rèn)值的設(shè)定方式
這篇文章主要介紹了python configparser中默認(rèn)值的設(shè)定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類
這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python機(jī)器學(xué)習(xí)之K-Means聚類實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)之K-Means聚類的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02