如何使用mitmproxy捕获HTTP请求/响应头?

sfa*_*tor 6 http-proxy http-headers mitmproxy

我已经能够从智能手机捕获HTTP(s)流量,并使用命令使用mitmdump存储此流量

mitmdump -w outfile
Run Code Online (Sandbox Code Playgroud)

这似乎也HTTP body随之抛弃headers.我有兴趣只捕获标题,最好是单个csv行(或json字符串).我怎样才能做到这一点?

use*_*965 5

另一个基于先前响应并更新为 python3 的派生片段:

def response(flow):
    print("")
    print("="*50)
    #print("FOR: " + flow.request.url)
    print(flow.request.method + " " + flow.request.path + " " + flow.request.http_version)

    print("-"*50 + "request headers:")
    for k, v in flow.request.headers.items():
        print("%-20s: %s" % (k.upper(), v))

    print("-"*50 + "response headers:")
    for k, v in flow.response.headers.items():
        print("%-20s: %s" % (k.upper(), v))
        print("-"*50 + "request headers:")
Run Code Online (Sandbox Code Playgroud)

命令行:

mitmdump -q -v -s parse_headers.py -R http://localhost:9200 -p 30001

输出:

==================================================
GET / HTTP/1.1
--------------------------------------------------request headers:
CONTENT-TYPE        : application/json
ACCEPT              : application/json
USER-AGENT          : Jakarta Commons-HttpClient/3.1
HOST                : localhost
--------------------------------------------------response headers:
CONTENT-TYPE        : application/json; charset=UTF-8
CONTENT-LENGTH      : 327
Run Code Online (Sandbox Code Playgroud)


rva*_*ijk 2

You can extract any header fields you need, e.g., with mitmdump and the flow object (python inline scripts). Inline scripts are documented here: https://mitmproxy.org/doc/scripting/inlinescripts.html

To extract all headers, I used the following command:

$ mitmdump -n -q -s parse_headers.py -r <file>.mitm
Run Code Online (Sandbox Code Playgroud)

The parse_headers.py inline script is as follows:

def response(context, flow):
    request_headers = [{"name": k, "value": v} for k, v in flow.request.headers]
    response_headers = [{"name": k, "value": v} for k, v in flow.response.headers]
    print request_headers
    print response_headers
Run Code Online (Sandbox Code Playgroud)

  • 除了“脚本错误:要解包的值太多脚本错误:要解包的值太多”之外,我没有任何输出...:( (2认同)