Using mitmproxy to Analyse HTTP[S] Requests

I’m posting this here primarily so that Future Quinn™ can reference it. Hopefully other folks will find it useful as well.

I’ve recently been playing around with mitmproxy, which is working out well for me. One feature I really like is the ability to programmatically extract information from traces created the

mitmproxy
tool. The workflow looks something like this:
  1. Run

    mitmproxy
    .
  2. Use the

    l
    command to filter the flows it captures. For example,
    ~d example\.com
    will only shows requests to
    example.com
    .
  3. Run my test.

  4. Use the

    w
    command to save the flows to a file.
  5. Extract content from the file programmatically using the script facility built in to

    mitmdump
    . For example:

    $ mitmdump -r test1.flows -s filter.py -n -q … output from my filter …

The filters themselves are written in Python (yay Python!) and have deep access to the flows. For example:

from urlparse import urlparse

def response(context, flow):
    u = urlparse(flow.request.url)
    if u.hostname.endswith("example.com"):
        cookie = flow.request.headers["Cookie"]
        if cookie is None:
            cookie = "-"
        print "%d %s %s" % (
            flow.response.status_code,
            u.path,
            cookie
        )

will print a summary of the status, URL path and request cookies for all the requests to

example.com
.

The nice thing here is that you have access to the full power of Python, so you can write code to extract the data in the most useful way.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"