从 MITM 代理获取“原始”请求\响应

cod*_*ber 5 http httpresponse httprequest python-2.7 mitmproxy

i', 编写 mitmproxy 代理 ( http://mitmproxy.org/index.html ) 以根据其 IP 将 HTTP 和 HTTPS 请求和响应写入文件(然后每个客户端可以访问它自己的请求\响应)以进行单元测试移动的。

就我现在所见,我不能像在 fiddler 中一样使用 str(Flow.request) 或 repr(Flow.request) 来获取响应\请求的“原始”打印,我需要重建它来自 Request 和 Response 对象的内部数据。

有人知道更好的方法吗?我正在使用 :

def response(ScriptContext, Flow):
    Flow.request....
    Flow.response....
Run Code Online (Sandbox Code Playgroud)

要访问被拦截的请求或响应,我没有改变任何东西,只是观察。现在代理在 8080 上,稍后它是 80 和 443 上的透明代理。如果有人之前做过,我会很高兴你能分享一些信息。

Ice*_*erg 5

对于那些想要将请求/响应数据复制到剪贴板而最终到达这里的人:

## export the current request/response as curl/httpie/raw/request/response to clipboard
# press colon : and input one of commands and enter
export.clip curl @focus
export.clip httpie @focus
export.clip raw @focus
export.clip raw_request @focus
export.clip raw_response @focus
Run Code Online (Sandbox Code Playgroud)

中间代理:5.0.1

源代码


cod*_*ber 3

有几件事。首先,您可以使用 str(flow.request.headers) 和 request.httpversion 等自行构建原始响应。然而 _assemble() 和 _assemble_headers() 似乎可以很好地完成这个任务。

所以基本上:

def request(context, flow):
req = flow.request;
try:
    print("Request: -----------------");
    print(req._assemble());
    print("--------------------------");
except Exception as ee:
    print(str(ee));

def response(context, flow):
    res = flow.response;
    try:
        print("Response: -----------------");
    print(res._assemble());

    if res.content:
        size = len(res.content);
        size  = min(size, 20);
        if res.content[0:size] != res.get_decoded_content()[0:size]:
            print("\n\n");
            print(res.get_decoded_content());
    print("--------------------------");
except Exception as ee:
    print(str(ee));
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,解码后的正文是否与未解码的正文不相似(不过我可以检查 gzip 内容类型),我也会打印解码后的消息。这应该根据当前日期保存到文件中,并且每个文件都以从 request\response.client_conn 对象获取的客户端 IP 命名。这几乎解决了我的问题。对 fiddler 的一些检查表明,该请求稍后可以重现,这正是我所需要的。