使用mitmproxy更改HTTPResponse中的图像

问题描述 投票:-1回答:1

对于学校项目,我们在Kali Linux上设置了mitmproxy。最后,我们能够安装mitmproxy,我们现在可以拦截客户端设备的HTTPS包,这些设备浏览网站作为我们Raspberry Pi的WLAN客户端。我们的目标之一是在HTTPS包中更改图像,我们真的希望在我们的项目中实现这一点。这就是重点,我不会使用Python 3内联脚本。这就是我目前所处的位置。

#!/usr/bin/python3
# modify_response.py
import sys
import os
from io import StringIO
from mitmproxy.net.http import encoding
from mitmproxy.net.http import headers
from mitmproxy.net import http
from PIL.Image import core as _imaging

def response(flow):
    flow.response.headers["newheader"] = "response-flow"

    if flow.response.headers.get("content-type", "").startswith("image"):
        decoded_response = decode(flow.response)
        with decoded(flow.respnse):
        print('OK')
        os.system('"./script2.py" "Decoded response: {}"'.format(decoded_response))
        try:
            img = cStringIO.StringIO(open('6868132.png', 'rb').read())
            flow.response.content = img.getvalue()
        except:
            os.system('"./script2.py" "Error occured"')

不幸的是,即使是名为“content-type”的标题的值以“image”开头的请求,似乎if条件也不正确。

我在这里引用这个网站https://sunu.in/manipulating-http-traffic-with-mitmproxy/,因为我想实现他们在那里做的同样的事情。但他们可能使用了相当旧的mitmproxy版本,我们正在使用2.0.2(如果我没有记错的话)。

我是Python的新手,花了几个小时做在线教程,以便我能理解我的代码所做的事情。你可以帮我改变HTTPResponse中的图像吗?

python python-3.x mitmproxy
1个回答
1
投票

我在mitmproxy论坛上发布了同样的问题,并在那里得到了答案。提供的答案包含我正在寻找的python脚本行:

def response(flow):
    if flow.response.headers.get("content-type", "").startswith("image"):
        img = open("file.png", "rb").read()
        flow.response.content = img
        flow.response.headers["content-type"] = "image/png"
© www.soinside.com 2019 - 2024. All rights reserved.