Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture HTTP request / response headers with mitmproxy?

I have been able to capture the HTTP(s) traffic from a smartphone and also stored this traffic using mitmdump using the command

mitmdump -w outfile

This seems to dump the HTTP body along with the headers as well. I am interested in capturing only the headers, prefarably as a single csv row (or json string). How can I do that?

like image 532
sfactor Avatar asked Jul 03 '15 11:07

sfactor


1 Answers

Yet another derived snippet based on previous responses and updated to 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:")

Command line:

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

Output:

==================================================
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
like image 134
user553965 Avatar answered Nov 07 '22 01:11

user553965