使用mitmproxy将URL更改为另一个URL [英] Change URL to another URL using mitmproxy
问题描述
我正在尝试使用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
脚本将
- 将请求从
mydomain.com
重定向到newsite.mydomain.com
- 更改请求方法路径(假定类似于
/getjson?
到新的`/getxml - 更改目标主机方案
- 更改目标服务器端口
-
覆盖请求标头
Host
以假装为原始页
- Redirect requesto from
mydomain.com
tonewsite.mydomain.com
- Change the request method path (supposed to be something like
/getjson?
to a new one `/getxml - Change the destination host scheme
- Change the destination server port
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屋!