Mitmproxy + Python安装和使用的一些问题

By | August 7, 2020

安装:

pip3 install mitmproxy

国内:

pip install mitmproxy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

如果’Microsoft Visual C++ Build Tools’错误,可以在https://visualstudio.microsoft.com/zh-hans/downloads/ 直接下载安装即可
如果’Dll load error…..’ 错误,安装VC https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

检查是否安装成功:

mitmdump --version

版本升级:

pip install --upgrade mitmproxy

安装证书
访问 http://mitm.it/ 自动安装
手动: 命令 certutil -addstore root mitmproxy-ca-cert.cer

自定义脚本 : 脚本内容是实时修改生效

mitmdump -s xxx.py

指定代理

--mode upstream:http://192.168.0.1:8800

忽略HOST / 忽略多个host

--ignore-hosts ^google\.com:443$
--ignore-hosts ^baidu\.com:443$  --ignore-hosts ^google\.com:443$

使用
直接看例子使用起来很简单。常用的方法API:requestheaders,request,responseheaders,response,error

from mitmproxy import ctx, flowfilter
 
class Recorder:
 
    def requestheaders(self, flow):
        ctx.log.info('requestheaders')
 
    def request(self, flow):
        # 日志打印
        # ctx.log.info('-----------------------')
        # ctx.log.error('-----------------------')
 
        # request 常用api
        # print(flow.request.host) # 当前请求的host
        # print(flow.request.path) # 请求的接口地址
        # print(flow.request.pretty_url) # 完整的url路径
        # print(flow.request.url) # 完整的url路径
        # print(flow.request.method) # 请求方式
        # print(flow.request.scheme) # http请求还是https请求
        # print(flow.request.headers) # 获取所有头信息,包含Host、User-Agent、Content-type等字段
 
        # GET请求
        # 获取参数
        # flow.request.query 请求参数 数据格式 mitmproxy数据类型 类似列表套着元组
        # flow.request.query.keys() 请求参数key拆分成迭代器,所有keys
        # flow.request.query.items() 同时循环请求参数的key value
        # for k,v in flow.request.query.items():
        #     print(k)
        #     print(v)
        # flow.request.query.values() 请求参数value拆分成迭代器,所有values
        # flow.request.query.get('name') 通过指定key获取value
        # print(flow.request.query.get('name'))
        # flow.request.query.get_all('name') 通过key 获取所有这个key下的value
        # print(flow.request.query.get_all('name'))
        # flow.request.query.fields 元素的方式获取所有的请求参数
        # print(flow.request.query.fields)
 
        # 修改更新参数
        # flow.request.query.set_all('sign',['123']) 修改get请求的参数
        # flow.request.query.update() 修改请求的参数 根据参数名  参数格式要求列表套元组  [('userid',2180),('sign','2beb50f2669b0531d7905e56a6e90bf4')]
        # 增加参数
        # flow.request.query.add('userid',2179) 增加get请求参数 参数1:key 参数2:value
        # 删除参数
        # flow.request.query.pop('userid')  删除指定某个请求参数 参数:key
 
        # POST请求
        # 获取参数
        # print(flow.request.get_text())  # 请求中body内容,那么可通过此方法获取,字符串类型
        # print(flow.request.urlencoded_form)  # content-type:application/x-www-form-urlencoded时的请求参数
        # print(flow.request.multipart_form)  # content-type:multipart/form-data 的请求参数
 
        # 修改更新参数
        # flow.request.set_text('123')  # 设置整体的请求参数 接收字符串
        # print(dir(flow.request))
        pass
 
    def responseheaders(self, flow):
        ctx.log.info('responseheaders')
 
    def response(self, flow):
        # print(flow.response.get_text()) 获取返回值结果 结果类型是字符串
        # print(flow.response.get_content()) 获取返回值结果 结果类型是bytes
        # print(flow.response.content) 获取返回值结果 结果类型是bytes二进制
        # print(flow.response.status_code) 状态码
        # print(flow.response.text) 获取返回值结果 结果类型是字符串
        # flow.response.set_text('123') 修改返回值 需要字符串类型
 
        # print(dir(flow.response))
        pass
 
    def error(self, flow):
        print(f'HTTP Error With [{flow.response.reason}], and body: {flow.response.text}')
 
 
addons = [
    Recorder()
]

参考: https://blog.wolfogre.com/posts/usage-of-mitmproxy/
https://www.liuyixiang.com/post/108653.html

Leave a Reply