pythonhttpsproxymitmproxy

MITM Proxy - intercept & Modify https content through python script


I'm trying to intercept and modify https content using Mitm Proxy.

It works really well using the GUI but I'd like to use a python script.

I tried this script:

from mitmproxy import http


def request(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url == "https://www.facebook.com/?l":
        flow.response = http.HTTPResponse.make(
            200,  # (optional) status code
            b"Hello World, this is a test",  # (optional) content
            {"Content-Type": "text/html"}  # (optional) headers
        )

def response(flow: http.HTTPFlow):

    if flow.response.raw_content and flow.request.pretty_url == "https://www.facebook.com/?lol":
        file = open(b"C:\Users\myuser\PycharmProjects\mitmProx\data.txt", "a")
        file.write(str(flow.response.) + "\n")

However, the content I intercept is encrypted and not in clear despite the fact that the content is clear on the web GUI !

Does anyone have an idea why my script intercepts encrypted content and the web GUI prints the clear data?


Solution

  • It looks like you want to use response.text or response.content, not response.raw_content. raw_content contains the raw compressed HTTP message body, whereas .content contains the uncompressed version.