mitmproxy
快速开启一个反代克隆站点
mitmproxy --mode reverse:目标 --listen-host 0.0.0.0 --listen-port 端口 --set block_global=false
mitm过滤表达式
| 表达式 | 作用 |
|---|---|
| ~a | 匹配响应中的CSS、JS、图片、字体 |
| ~all | 匹配所有流 |
| ~b | body |
| ~bq | 请求body |
| ~bs | 响应body |
| ~c | http响应码 |
| ~comment | 流注释 |
| ~d | 域 |
| ~dns | 匹配dns流 |
| ~dst | 匹配目标地址 |
| ~e | 匹配错误 |
| ~h | header |
| ~hq | 请求header |
| ~hs | 响应header |
| ~http | 匹配http流 |
| ~m | 方法 |
| ~marked | 匹配被标记的流 |
| ~marker | 匹配被特殊标记标记的流 |
| ~meta | 流元数据 |
| ~q | 匹配没有响应的请求 |
| ~replay | 匹配重放的流 |
| ~replayq | 匹配重放的客户端请求 |
| ~replays | 匹配重放的服务端响应 |
| ~s | 匹配响应 |
| ~src | 匹配源地址 |
| ~t | 匹配Content-Type header |
| ~tcp | 匹配tcp流 |
| ~tq | 匹配请求中的Content-Type |
| ~ts | 匹配响应中的Content-Type |
| ~u | url |
| ~websocket | 匹配ws流 |
| ! | 非 |
| & | 与 |
| | | 或 |
| (…) | 分组 |
mitm查看流选择器
| 选择器 | 作用 |
|---|---|
| @all | 所有流 |
| @focus | 当前关注的流 |
| @shown | 所有当前展示的流 |
| @hidden | 所有当前隐藏的流 |
| @marked | 所有标记的流 |
| @unmarked | 所有没标记的流 |
mitm http生命周期
编写mitmproxy脚本
编写一个 py 文件供 mitmproxy 加载,文件定义了变量 addons,addons 是个数组,每个元素是一个类实例,这些类有若干方法,这些方法实现了某些 mitmproxy 提供的事件,mitmproxy 会在某个事件发生时调用对应的方法。这些类,称为一个个 addon,比如一个叫 Counter 的 addon:
import mitmproxy.http
from mitmproxy import ctx
class Counter:
def __init__(self):
self.num = 0
def request(self, flow: mitmproxy.http.HTTPFlow):
self.num = self.num + 1
ctx.log.info("We've seen %d flows" % self.num)
addons = [
Counter()
]
编写能够在mitmproxy控制台中使用的命令
"""Handle flows as command arguments."""
from collections.abc import Sequence
from mitmproxy import command
from mitmproxy import ctx
from mitmproxy import flow
from mitmproxy import http
class MyAddon:
@command.command("myaddon.addheader") #command修饰器
def addheader(self, flows: Sequence[flow.Flow]) -> None: #->None表示这个函数返回的类型为None
for f in flows:
if isinstance(f, http.HTTPFlow): #HTTPFlow 是表示单个 HTTP 事务的对象集合
f.request.headers["myheader"] = "value"
ctx.log.alert("done")
addons = [MyAddon()]
在mitmproxy控制台中,使用myaddon.addheader来调用命令
证书相关
cloudflare
下载cloudflare的源服务器证书,在中间件配置中使用,然后在dns模块中将cdn代理打开,就能够提供https服务了
mitm
在/root/.mitmproxy下将对应证书复制到/usr/share/ca-certificates里,然后使用dpkg-reconfigure ca-certificates导入
如果出现curl能访问目标但是mitimproxy报证书错误的,尝试添加–ssl-insecure
Caddy相关
占位符
| 简写 | 替换 |
|---|---|
| dir | http.request.uri.path.dir |
| file | http.request.uri.path.file |
| header.* | http.request.header.* |
| host | http.request.host |
| labels.* | http.request.host.labels.* |
| hostport | http.request.hostport |
| port | http.request.port |
| method | http.request.method |
| path | http.request.uri.path |
| path.* | http.request.uri.path.* |
| query | http.request.uri.query |
| query.* | http.request.uri.query.* |
| re.. | http.regexp.. |
| remote | http.request.remote |
| remote_host | http.request.remote.host |
| remote_port | http.request.remote.port |
| scheme | http.request.scheme |
| uri | http.request.uri |
| tls_cipher | http.request.tls.cipher_suite |
| tls_version | http.request.tls.version |
| tls_client_fingerprint | http.request.tls.client.fingerprint |
| tls_client_issuer | http.request.tls.client.issuer |
| tls_client_serial | http.request.tls.client.serial |
| tls_client_subject | http.request.tls.client.subject |
| tls_client_certificate_pem | http.request.tls.client.certificate_pem |
| tls_client_certificate_der_base64 | http.request.tls.client.certificate_der_base64 |
| upstream_hostport | http.reverse_proxy.upstream.hostport |