zoukankan      html  css  js  c++  java
  • Python3自定义http/https请求拦截mitmproxy脚本

    [本文出自天外归云的博客园]

    脚本内容

    代码如下:

    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}
    
    "
    
        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——
    {flow.request.method}")
                self.log(f"——HOST——
    {flow.request.pretty_host}")
                self.log(f"——URL——
    {flow.request.pretty_url}")
                query = [i + ":" + flow.request.query[i] + "
    " for i in flow.request.query]
                self.log(f"——QUERY STRING——
    {''.join(query)}")
                if flow.request.urlencoded_form:
                    form = [i + ":" + flow.request.urlencoded_form[i] + "
    " for i in flow.request.urlencoded_form]
                    self.log(f"——FORM——
    {''.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——
    {flow.response.content.decode()}")
                flow = self.modify_response(flow)
                self.log(f"——RESPONSE after modified——
    {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)
    ]

    使用方法

    运行mitmproxy指定使用该脚本和端口号即可:

    mitmproxy -p 6666 -s xxx.py

    在mitmproxy运行时:

    1. 会拦截url中包含str_in_url字符串的请求

    2. 会把response.content修改为当前mitm运行所在目录下的response_file文件中的内容

    3. 打印信息在当前mitm运行所在目录下的log_file文件中

    4. 如果无需修改response设置switch_on为False即为开关关闭

    5. 如果不修改response的话response_file需要写None

  • 相关阅读:
    使用NoSQL可视化管理工具时报错The GuidRepresentation for the reader is CSharpLegacy
    git和github连接权限(这是一个简便方法,不是很安全,建议大家还是用ssh解决)
    python模块的使用
    利用python抓取页面数据
    利用递归解决“汉诺塔的移动”问题(使用python来做的,其它语言也行)
    mysql中利用show profile很直观的看到查询缓存的作用。
    MySQL中show profiles的开启
    浅谈依赖注入
    使用laraval框架和前端完成restful风格的请求对接(这里只是讨论restful的概念)
    利用composer安装laraval
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/9591022.html
Copyright © 2011-2022 走看看