【问题标题】:Saving decoded Protobuf content保存解码后的 Protobuf 内容
【发布时间】:2020-01-03 13:49:12
【问题描述】:

我正在尝试设置一个 .py 插件,它将解码的 Protobuf 响应保存到文件中,但无论我做什么,结果始终是字节格式的文件(未解码)。我也尝试通过在 Mitmproxy 中使用“w”来做同样的事情——尽管在屏幕上我看到了解码的数据,但在文件中它又被编码了。 任何想法如何正确地做到这一点?

现在的示例代码:

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)

【问题讨论】:

    标签: protocol-buffers mitmproxy


    【解决方案1】:

    嗯,我不确定这是否有帮助,但是如果您不以二进制模式打开文件会发生什么

    f = open("test.log","a")
    

    ?

    【讨论】:

      【解决方案2】:

      喂,

      我发现了一些基本的东西。 尝试替换

      f.write(flow.request.content)
      

      f.write(flow.request.text)
      

      我在这个网站上读过 https://discourse.mitmproxy.org/t/modifying-https-response-body-not-working/645/3

      请阅读并尝试获取请求和响应。 MITM Proxy, getting entire request and response string

      祝你的项目好运。

      【讨论】:

        【解决方案3】:

        我能够找到方法来做到这一点。似乎 mitmdump 或 mitmproxy 无法保存原始解码的 Protobuf,所以我使用了:

        mitmdump -s decode_script.py
        

        使用以下脚本将解码后的数据保存到文件中:

        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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多