保存解码的Protobuf内容

huangapple go评论63阅读模式
英文:

Saving decoded Protobuf content

问题

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

Sample code for now:

import mitmproxy

def response(flow):
    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)
英文:

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)

答案1

得分: 0

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

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

英文:

Eh, I'm not sure this helps, but what happens if you don't open the file in binary mode

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

?

答案2

得分: 0

Sure, here's the translated text:

嗨,

我找到了一些基本的东西。
尝试将

f.write(flow.request.content)

替换为

f.write(flow.request.text)

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

请阅读并尝试这个方法来获取请求和响应的组装。
https://stackoverflow.com/questions/28626213/mitm-proxy-getting-entire-request-and-response-string

祝你的项目顺利。

英文:

Hy,

some basic things that I found.
Try replacing

f.write(flow.request.content)

with

f.write(flow.request.text)

I read it on this website
https://discourse.mitmproxy.org/t/modifying-https-response-body-not-working/645/3

Please read and try this to get the requests and responses assembled.
https://stackoverflow.com/questions/28626213/mitm-proxy-getting-entire-request-and-response-string

Best of luck with your project.

答案3

得分: 0

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)

(Note: This code is provided as-is, and you may need to adjust it for your specific use case.)

英文:

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)

huangapple
  • 本文由 发表于 2020年1月3日 21:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定