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

时间:2015-07-03 11:10:55

标签: http-headers http-proxy mitmproxy

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

使用mitmdump存储此流量
mitmdump -w outfile

这似乎也会将HTTP bodyheaders一起转储。我有兴趣只捕获标题,最好是单个csv行(或json字符串)。我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

另一个基于先前响应的派生代码段并更新为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:")

命令行:

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

答案 1 :(得分:2)

您可以提取所需的任何标题字段,例如,使用mitmdump和流对象(python内联脚本)。内联脚本在此处记录:https://mitmproxy.org/doc/scripting/inlinescripts.html

要提取所有标题,我使用以下命令:

$ mitmdump -n -q -s parse_headers.py -r <file>.mitm

parse_headers.py内联脚本如下:

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

答案 2 :(得分:1)

你正在使用@rvaneijk,但我收到了以下错误:

Script error: too many values to unpack 
Script error: too many values to unpack

我在'too many values to unpack', iterating over a dict. key=>string, value=>list找到了一个解决方案,并按如下方式更改了代码:

[root@npmjs npmo-server]# cat parse_headers.py
def response(context, flow):
  request_headers = [{"name": k, "value": v} for k, v in flow.request.headers.iteritems()]
  response_headers = [{"name": k, "value": v} for k, v in flow.response.headers.iteritems()]
  print "################################"
  print "FOR: " + flow.request.url
  print flow.request.method + " " + flow.request.path + " " + flow.request.http_version
  print "HTTP REQUEST HEADERS"
  print request_headers
  print "HTTP RESPONSE HEADERS"
  print response_headers
  print ""

输出如下:

10.137.66.4:63870: clientdisconnect

################################
FOR: http://pe2enpmas300.corp.company.net:8081/csv-stringify
GET /csv-stringify HTTP/1.1
HTTP REQUEST HEADERS
[{'name': 'accept-encoding', 'value': 'gzip'}, {'name': 'authorization', 'value': 'Bearer d2e0770656a9726dfb559ea2ddccff3078dba9a0'}, {'name': 'version', 'value': '2.11.2'}, {'name': 'accept', 'value': 'application/json'}, {'name': 'referer', 'value': 'install restify'}, {'name': 'npm-session', 'value': 'a9a4d805c6392599'}, {'name': 'user-agent', 'value': 'npm/2.11.2 node/v0.10.25 linux x64'}, {'name': 'if-none-match', 'value': 'W/"43fb-8/w7tzRZ9CvawCJo5Uiisg"'}, {'name': 'host', 'value': 'registry-e2e.npmjs.intuit.net'}, {'name': 'Connection', 'value': 'keep-alive'}, {'name': 'X-Forwarded-For', 'value': '10.181.70.43'}]
HTTP RESPONSE HEADERS
[{'name': 'X-Powered-By', 'value': 'Express'}, {'name': 'ETag', 'value': 'W/"43fb-8/w7tzRZ9CvawCJo5Uiisg"'}, {'name': 'Date', 'value': 'Tue, 18 Oct 2016 08:04:45 GMT'}, {'name': 'Connection', 'value': 'keep-alive'}]

您可以按如下方式使用Docker:

  1. 在本地创建文件
  2. 运行以下
  3. 确保您对该文件具有读取权限。

    docker run -ti -p 8080:8080 -v $PWD/parse_headers.py:/tmp/parse_headers.py 
        mitmproxy/mitmproxy mitmdump -s /tmp/parse_headers.py 
        -R http://npmjs.corp.company.net:8081 8080
    
相关问题