Search code examples
protocol-buffersmitmproxy

Saving decoded Protobuf content


I'm trying to setup a .py plugin that will save decoded Protobuf responses to file, but whatever I do, the result is always file in byte format (not decoded). I have also tried to do the same by using "w" in Mitmproxy - although on screen I saw decoded data, in the file it was encoded again. Any thoughts how to do it correctly?

Sample code for now:

import mitmproxy
def response(flow):
    # if flow.request.pretty_url.endswith("some-url.com/endpoint"):
    if flow.request.pretty_url.endswith("some-url.com/endpoint"):
        f = open("test.log","ab")
        with decoded(flow.response)
            f.write(flow.request.content)
            f.write(flow.response.content)

Solution

  • I was able to find the way to do that. Seems mitmdump or mitmproxy wasn't able to save raw decoded Protobuf, so I used:

    mitmdump -s decode_script.py
    

    with the following script to save the decoded data to a file:

    import mitmproxy
    import subprocess
    import time
    
    def response(flow):
        if flow.request.pretty_url.endswith("HERE/IS/SOME/API/PATH"):
            protobuffedResponse=flow.response.content
            (out, err) = subprocess.Popen(['protoc', '--decode_raw'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate(protobuffedResponse)
            outStr = str(out, 'utf-8')
            outStr = outStr.replace('\\"', '"')
            timestr = time.strftime("%Y%m%d-%H%M%S")
            with open("decoded_messages/" + timestr + ".decode_raw.log","w") as f:
                f.write(outStr)