保存解码的普罗托布夫内容



我正在尝试设置一个.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)

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

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

Hy,

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

f.write(flow.request.content)

f.write(flow.request.text)

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

请阅读并尝试此操作以收集请求和响应。 MITM 代理,获取整个请求和响应字符串

祝您的项目好运。

我能够找到做到这一点的方法。似乎 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)

最新更新