使用 mitmproxy 将 URL 更改为另一个 URL

Mor*_*der 5 python mitmproxy

我正在尝试使用 mitmproxy 和 Python 将一个页面重定向到另一个页面。我可以毫无问题地与 mitmproxy 一起运行我的内联脚本,但是在将 URL 更改为另一个 URL 时我被卡住了。就像如果我去 google.com 它会重定向到 stackoverflow.com

def response(context, flow):
        print("DEBUG")
        if flow.request.url.startswith("http://google.com/"):
            print("It does contain it")
            flow.request.url = "http://stackoverflow/"
Run Code Online (Sandbox Code Playgroud)

这在理论上应该有效。我http://google.com/在 mitmproxy 的 GUI 中看到(作为 GET)但是print("It does contain it")未被解雇。

当我试着把它放在flow.request.url = "http://stackoverflow.com"正下方时print("DEBUG")它也不起作用。

我究竟做错了什么?我还尝试if "google.com" in flow.request.url检查 URL 是否包含google.com但这也不起作用。

谢谢

lor*_*isi 7

以下mitmproxy脚本将

  1. 重定向请求从mydomain.comnewsite.mydomain.com
  2. 更改请求方法路径(应该是类似于/getjson?新的`/getxml
  3. 更改目标主机方案
  4. 更改目标服务器端口
  5. 覆盖请求头Host以伪装成原点

    import mitmproxy
    from mitmproxy.models import HTTPResponse
    from netlib.http import Headers
    def request(flow):
    
        if flow.request.pretty_host.endswith("mydomain.com"):
                mitmproxy.ctx.log( flow.request.path )
                method = flow.request.path.split('/')[3].split('?')[0]
                flow.request.host = "newsite.mydomain.com"
                flow.request.port = 8181
                flow.request.scheme = 'http'
                if method == 'getjson':
                    flow.request.path=flow.request.path.replace(method,"getxml")
                flow.request.headers["Host"] = "newsite.mydomain.com"
    
    Run Code Online (Sandbox Code Playgroud)