Search code examples
pythonpython-3.xhttpsman-in-the-middlemitmproxy

Save body response from specific URL to file and decode it using mitmproxy


I'm trying to save and decode body response from "https://example.com/orders" to the file using mitmdump. I found a script which saves only response to file, but it seems doesn't work on latest mitmproxy.

from mitmproxy.models import decoded
def response(context, flow):
 with decoded(flow.response):  # automatically decode gzipped responses.
    with open("body.txt","ab") as f:
        f.write(flow.response.content)

When I'm running script error message displaying :

Script error: Traceback (most recent call last):
File "save_body.py", line 1, in <module>
from mitmproxy.models import decoded
ImportError: No module named 'mitmproxy.models'

My Operating System and version:

Mitmproxy version: 2.0.2 (release version) 
Python version: 3.5.2
Platform: Linux-4.10.0-28-generic-x86_64-with-Ubuntu-16.04-xenial
SSL version: OpenSSL 1.0.2g  1 Mar 2016
Linux distro: Ubuntu 16.04 xenial

How can I save decoded body from only one URL, excluding other URLs, to a file? And how to do so: if this request from URL I need repeats, I need mitmdump to overwrite "body.txt"?


Solution

  • I found a solution, this simple python script writes response body to the file, and if request repeats - overwrites it every time.

    import mitmproxy
    def response(flow):
        if flow.request.pretty_url.endswith("example.com/orders"):
            with open("orders.html","wb") as f:
                    f.write(flow.response.content)
    

    Save this script to save_body.py and use it with mitmdump -s save_body.py