Python3自定义http/https请求拦截mitmproxy脚本

摘要:
[本文出自天外归云的博客园]脚本内容代码如下:frommitmproxyimporthttp,ctxfrommultiprocessingimportLockclassFilter:def__init__(self,filter_info):self.log_info=""self.mutex=Lock()self.filter_info=filter_infoself.response_file

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

脚本内容

代码如下:

from mitmproxy importhttp, ctx
from multiprocessing importLock
classFilter:
    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 inself.filter_info:
            if info["str_in_url"] inflow.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"]
                returnTrue
        else:
            returnFalse
    def modify_response(self, flow: http.HTTPFlow) ->http.HTTPFlow:
        if self.switch_on andself.response_file:
            with open(self.response_file, "r") as f:
                flow.response.content =f.read().encode()
        returnflow
    def request(self, flow: http.HTTPFlow) ->None:
        ifself.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 inflow.request.query]
            self.log(f"——QUERY STRING——
{''.join(query)}")
            ifflow.request.urlencoded_form:
                form = [i + ":" + flow.request.urlencoded_form[i] + "" for i inflow.request.urlencoded_form]
                self.log(f"——FORM——
{''.join(form)}")
            self.write_log()
    def response(self, flow: http.HTTPFlow) ->None:
        ifself.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

免责声明:文章转载自《Python3自定义http/https请求拦截mitmproxy脚本》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VC++的菜单控制和自绘菜单财务软件间的财务接口(转载)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

maven 常见错误解决方法

1. 最重要的一点,使用国内镜像,比如 oschina.net 的镜像; 搜 jar 推荐:http://maven.outofmemory.cn/,速度极快。 2. 错误:Could not resolve artifact org.apache.maven.archetypes:maven-archetype-quickstart:pom:RELEAS...

urllib的使用

urllib的使用 urllib是python内置的HTTP请求库,包含如下四个模块: request:它是最基本HTTP请求模块,可以用来模拟发送请求,就像在浏览器里输入网址然后按回车一样,只需要传入URL以及额外的参数,就可以模拟实现这个过程了。 error:异常处理模块,如果出现请求错误,可以捕获到这些异常,然后进行重试或其他操作以保证程序不会意外终...

Oracle EBS FND User Info API (转) EBS用户账号密码职责相关

1. 与用户信息相关API PKG. --和用户处理有关的API FND_USER_PKG; --和用户密码处理有关的API FND_WEB_SEC; --和用户职责处理有关的API FND_USER_RESP_GROUPS_API; 2. 激活失效用户帐号. --------------------------------处理...

Python爬虫入门教程 51-100 Python3爬虫通过m3u8文件下载ts视频-Python爬虫6操作

什么是m3u8文件 M3U8文件是指UTF-8编码格式的M3U文件。M3U文件是记录了一个索引纯文本文件, 打开它时播放软件并不是播放它,而是根据它的索引找到对应的音视频文件的网络地址进行在线播放。 原视频数据分割为很多个TS流,每个TS流的地址记录在m3u8文件列表中 比如我这里有一个m3u8文件,文件内容如下 #EXTM3U #EXT-X-VERSI...

Tair rdb(redis存储引擎)实现介绍

淘宝那岩曾经在淘宝核心系统团队博客上介绍过Tair ldb的实现,本文将尝试着介绍rdb(redis存储引擎)的实现。 Tair是淘宝开源的分布式KV缓存系统,内部将功能模块化,抽离出底层存储细节,可以接入不同的存储引擎。redis是一个开源的、高效的key-value存储,提供了strings、hashs、lists、sets、sorted sets等多...

仅需4步,轻松升级K3s集群!

Rancher 2.4是Rancher目前最新的版本,在这一版本中你可以通过Rancher UI对K3s集群进行升级管理。 K3s是一个轻量级Kubernetes发行版,借助它你可以几分钟之内设置你的开发Kubernetes环境。它非常适用于生产用例,主要用于IoT和Edge设备。 在Rancher 2.4中,你可以导入K3s集群并且可以通过Rancher...