# 介绍
mitmtools 是根据 mitmproxy 封装的工具库
具备如下功能
- 替换响应文件
- 修改部分响应内容
- 移除部分响应内容
- hook 注入
安装
```
pip install mitmtools
```
# 替换响应文件
- ReplaceByFile:通过正则匹配 url,使用文件替换整个响应
- ReplaceByStr:通过正则匹配 url,使用字典替换部分字段
```
ReplaceByFile(pattern='', filepath='', max_times=0) # 注意 max_time 为可选参数,代表匹配次数
ReplaceByStr(pattern='', replace_dict={'':''})
```
# 移除部分响应内容
- RemoveContent:通过正则匹配 url,并去除部分响应内容
```
RemoveContent(pattern='', remove_list=['x']), max_times=0)
```
# hook 注入
- HookHtml:通过正则匹配 url,通过 html 进行插入 script 标签进行注入
- HookJs:通过正则匹配 url,通过 js 进行注入
```
HookHtml(pattern='', filepath='', content='')
HookJs(pattern='', filepath='', content='')
```
## 注意
html 注入属于 xss 攻击,部分会有 csp 防护导致 script 不会执行,从而 hook 失败
友情提醒:任何注入都可能被检测!
# 查看
如果只是想查看请求过程的话,直接使用 Show
```
Show()
```
# 执行
将需要执行的方法单独放一个 .py 文件,并放在 addons 列表中,如下:
```
"""
这只是一个配置的例子
注意:
启动之后,文件是动态的,随时修改随时生效
启动程序要和该程序在同一目录,不然可能找不到文件路径
使用中要注意缓存的影响,停用缓存或清除缓存再尝试
"""
from mitmtools import Show
from mitmtools.remove import RemoveContent
from mitmtools.hook import HookJs, HookHtml
from mitmtools.replace import ReplaceByStr, ReplaceByFile
addons = [
Show(), # 输出请求、响应
# replace
# ReplaceByStr(pattern='https://www.baidu.com/', replace_dict={'百度一下,你就知道': '百度一下,你也不知道'})
# ReplaceByFile(pattern='^https://www.baidu.com.?$', filepath="./mitmtools/static/index.html")
# hook
# HookHtml(pattern='https://www.baidu.com/', filepath='./mitmtools/static/hookCookie.js'),
# HookHtml(pattern='https://www.baidu.com/', content='''
# (function () {
# var gkDocument = document;
# var gkPrint = console.log;
# Object.defineProperty(document, "cookie", {
# set: function (val) {
# gkPrint("正在设置 Cookie:", val)
# debugger;
# cookieTemp = val;
# return val
# },
# get: function () {
# return gkDocument.cookie
# }
# })
# })()
# '''),
HookJs(
pattern="san_b247717.js",
filepath='./mitmtools/static/hookCookie.js'
),
# remove
# RemoveContent(pattern='https://www.baidu.com/', remove_list=['就知道'])
]
```
随后调用如下代码,或者自己通过 mitmproxy 命令行命令自行启动
```
from mitmtools.start import execute, execute_web
execute(filepath='xxx.py', port=8866) # 有其它命令都可以通过 args 传
execute_web(port=8866, args={'-s':filepath}) # 有其它命令都可以通过 args 传
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Leviathangk/mitmtools",
"name": "mitmtools",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "mitmtools",
"author": "\u90ed\u4e00\u4f1a\u513f",
"author_email": "1015295213@qq.com",
"download_url": "https://files.pythonhosted.org/packages/80/0d/8f3a92f00ab14372dd8a46cfde2e98abafc94fc0c9d7d32a24f9b3362cf6/mitmtools-0.0.7.tar.gz",
"platform": "any",
"description": "# \u4ecb\u7ecd\r\n\r\nmitmtools \u662f\u6839\u636e mitmproxy \u5c01\u88c5\u7684\u5de5\u5177\u5e93\r\n\r\n\u5177\u5907\u5982\u4e0b\u529f\u80fd\r\n\r\n- \u66ff\u6362\u54cd\u5e94\u6587\u4ef6\r\n- \u4fee\u6539\u90e8\u5206\u54cd\u5e94\u5185\u5bb9\r\n- \u79fb\u9664\u90e8\u5206\u54cd\u5e94\u5185\u5bb9\r\n- hook \u6ce8\u5165\r\n\r\n\u5b89\u88c5\r\n\r\n```\r\npip install mitmtools\r\n```\r\n\r\n# \u66ff\u6362\u54cd\u5e94\u6587\u4ef6\r\n\r\n- ReplaceByFile\uff1a\u901a\u8fc7\u6b63\u5219\u5339\u914d url\uff0c\u4f7f\u7528\u6587\u4ef6\u66ff\u6362\u6574\u4e2a\u54cd\u5e94\r\n- ReplaceByStr\uff1a\u901a\u8fc7\u6b63\u5219\u5339\u914d url\uff0c\u4f7f\u7528\u5b57\u5178\u66ff\u6362\u90e8\u5206\u5b57\u6bb5\r\n\r\n```\r\nReplaceByFile(pattern='', filepath='', max_times=0) # \u6ce8\u610f max_time \u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u4ee3\u8868\u5339\u914d\u6b21\u6570\r\nReplaceByStr(pattern='', replace_dict={'':''})\r\n```\r\n\r\n# \u79fb\u9664\u90e8\u5206\u54cd\u5e94\u5185\u5bb9\r\n\r\n- RemoveContent\uff1a\u901a\u8fc7\u6b63\u5219\u5339\u914d url\uff0c\u5e76\u53bb\u9664\u90e8\u5206\u54cd\u5e94\u5185\u5bb9\r\n\r\n```\r\nRemoveContent(pattern='', remove_list=['x']), max_times=0)\r\n```\r\n\r\n# hook \u6ce8\u5165\r\n\r\n- HookHtml\uff1a\u901a\u8fc7\u6b63\u5219\u5339\u914d url\uff0c\u901a\u8fc7 html \u8fdb\u884c\u63d2\u5165 script \u6807\u7b7e\u8fdb\u884c\u6ce8\u5165\r\n- HookJs\uff1a\u901a\u8fc7\u6b63\u5219\u5339\u914d url\uff0c\u901a\u8fc7 js \u8fdb\u884c\u6ce8\u5165\r\n\r\n```\r\nHookHtml(pattern='', filepath='', content='')\r\nHookJs(pattern='', filepath='', content='')\r\n```\r\n\r\n## \u6ce8\u610f\r\n\r\nhtml \u6ce8\u5165\u5c5e\u4e8e xss \u653b\u51fb\uff0c\u90e8\u5206\u4f1a\u6709 csp \u9632\u62a4\u5bfc\u81f4 script \u4e0d\u4f1a\u6267\u884c\uff0c\u4ece\u800c hook \u5931\u8d25\r\n\r\n\u53cb\u60c5\u63d0\u9192\uff1a\u4efb\u4f55\u6ce8\u5165\u90fd\u53ef\u80fd\u88ab\u68c0\u6d4b\uff01\r\n\r\n# \u67e5\u770b\r\n\r\n\u5982\u679c\u53ea\u662f\u60f3\u67e5\u770b\u8bf7\u6c42\u8fc7\u7a0b\u7684\u8bdd\uff0c\u76f4\u63a5\u4f7f\u7528 Show\r\n\r\n```\r\nShow()\r\n```\r\n\r\n# \u6267\u884c\r\n\r\n\u5c06\u9700\u8981\u6267\u884c\u7684\u65b9\u6cd5\u5355\u72ec\u653e\u4e00\u4e2a .py \u6587\u4ef6\uff0c\u5e76\u653e\u5728 addons \u5217\u8868\u4e2d\uff0c\u5982\u4e0b\uff1a\r\n\r\n```\r\n\"\"\"\r\n \u8fd9\u53ea\u662f\u4e00\u4e2a\u914d\u7f6e\u7684\u4f8b\u5b50\r\n \r\n \u6ce8\u610f\uff1a\r\n \u542f\u52a8\u4e4b\u540e\uff0c\u6587\u4ef6\u662f\u52a8\u6001\u7684\uff0c\u968f\u65f6\u4fee\u6539\u968f\u65f6\u751f\u6548\r\n \u542f\u52a8\u7a0b\u5e8f\u8981\u548c\u8be5\u7a0b\u5e8f\u5728\u540c\u4e00\u76ee\u5f55\uff0c\u4e0d\u7136\u53ef\u80fd\u627e\u4e0d\u5230\u6587\u4ef6\u8def\u5f84\r\n \u4f7f\u7528\u4e2d\u8981\u6ce8\u610f\u7f13\u5b58\u7684\u5f71\u54cd\uff0c\u505c\u7528\u7f13\u5b58\u6216\u6e05\u9664\u7f13\u5b58\u518d\u5c1d\u8bd5\r\n\"\"\"\r\nfrom mitmtools import Show\r\nfrom mitmtools.remove import RemoveContent\r\nfrom mitmtools.hook import HookJs, HookHtml\r\nfrom mitmtools.replace import ReplaceByStr, ReplaceByFile\r\n\r\naddons = [\r\n Show(), # \u8f93\u51fa\u8bf7\u6c42\u3001\u54cd\u5e94\r\n\r\n # replace\r\n # ReplaceByStr(pattern='https://www.baidu.com/', replace_dict={'\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053': '\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u4e5f\u4e0d\u77e5\u9053'})\r\n # ReplaceByFile(pattern='^https://www.baidu.com.?$', filepath=\"./mitmtools/static/index.html\")\r\n\r\n # hook\r\n # HookHtml(pattern='https://www.baidu.com/', filepath='./mitmtools/static/hookCookie.js'),\r\n # HookHtml(pattern='https://www.baidu.com/', content='''\r\n # (function () {\r\n # var gkDocument = document;\r\n # var gkPrint = console.log;\r\n # Object.defineProperty(document, \"cookie\", {\r\n # set: function (val) {\r\n # gkPrint(\"\u6b63\u5728\u8bbe\u7f6e Cookie\uff1a\", val)\r\n # debugger;\r\n # cookieTemp = val;\r\n # return val\r\n # },\r\n # get: function () {\r\n # return gkDocument.cookie\r\n # }\r\n # })\r\n # })()\r\n # '''),\r\n HookJs(\r\n pattern=\"san_b247717.js\",\r\n filepath='./mitmtools/static/hookCookie.js'\r\n ),\r\n\r\n # remove\r\n # RemoveContent(pattern='https://www.baidu.com/', remove_list=['\u5c31\u77e5\u9053'])\r\n]\r\n```\r\n\r\n\u968f\u540e\u8c03\u7528\u5982\u4e0b\u4ee3\u7801\uff0c\u6216\u8005\u81ea\u5df1\u901a\u8fc7 mitmproxy \u547d\u4ee4\u884c\u547d\u4ee4\u81ea\u884c\u542f\u52a8\r\n\r\n```\r\nfrom mitmtools.start import execute, execute_web\r\n\r\nexecute(filepath='xxx.py', port=8866) # \u6709\u5176\u5b83\u547d\u4ee4\u90fd\u53ef\u4ee5\u901a\u8fc7 args \u4f20\r\nexecute_web(port=8866, args={'-s':filepath}) # \u6709\u5176\u5b83\u547d\u4ee4\u90fd\u53ef\u4ee5\u901a\u8fc7 args \u4f20\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT Licence",
"summary": "\u901a\u8fc7 mitmproxy \u5f00\u53d1\u7684\u4fbf\u6377\u5de5\u5177\u5305",
"version": "0.0.7",
"split_keywords": [
"mitmtools"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "800d8f3a92f00ab14372dd8a46cfde2e98abafc94fc0c9d7d32a24f9b3362cf6",
"md5": "12b4e5511bcbaa17257df3413e080922",
"sha256": "accd2eb6bc95bf347775b97f72e55bd706191748583eadac35ffae8488f0a2fd"
},
"downloads": -1,
"filename": "mitmtools-0.0.7.tar.gz",
"has_sig": false,
"md5_digest": "12b4e5511bcbaa17257df3413e080922",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8026,
"upload_time": "2023-02-10T06:58:26",
"upload_time_iso_8601": "2023-02-10T06:58:26.080185Z",
"url": "https://files.pythonhosted.org/packages/80/0d/8f3a92f00ab14372dd8a46cfde2e98abafc94fc0c9d7d32a24f9b3362cf6/mitmtools-0.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-10 06:58:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Leviathangk",
"github_project": "mitmtools",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "mitmtools"
}