使用mitmproxy将URL更改为另一个URL [英] Change URL to another URL using mitmproxy

查看:497
本文介绍了使用mitmproxy将URL更改为另一个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mitmproxy和Python将页面重定向到另一页面.我可以将我的内联脚本与mitmproxy一起运行,而不会出现问题,但是在将URL更改为另一个URL时遇到了麻烦.就像我去google.com一样,它将重定向到stackoverflow.com

I am trying to redirect one page to another by using mitmproxy and Python. I can run my inline script together with mitmproxy without issues, but I am stuck when it comes to changing the URL to another URL. Like if I went to google.com it would redirect to 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/"

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

This should in theory work. I see http://google.com/ in the GUI of mitmproxy (as GET) but the print("It does contain it") never gets fired.

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

When I try to just put flow.request.url = "http://stackoverflow.com" right under the print("DEBUG") it won't work neither.

我做错了什么?我还尝试过if "google.com" in flow.request.url来检查URL是否包含google.com,但该方法也不起作用.

What am I doing wrong? I have also tried if "google.com" in flow.request.url to check if the URL contains google.com but that won't work either.

谢谢

推荐答案

以下mitmproxy脚本将

  1. 将请求从mydomain.com重定向到newsite.mydomain.com
  2. 更改请求方法路径(假定类似于/getjson?到新的`/getxml
  3. 更改目标主机方案
  4. 更改目标服务器端口
  5. 覆盖请求标头Host以假装为原始页

  1. Redirect requesto from mydomain.com to newsite.mydomain.com
  2. Change the request method path (supposed to be something like /getjson? to a new one `/getxml
  3. Change the destination host scheme
  4. Change the destination server port
  5. Overwrite the request header Host to pretend to be the origi

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"

这篇关于使用mitmproxy将URL更改为另一个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆