如何使用 ZAP 将请求重定向到另一台主机?
How to redirect requests to another host using ZAP?
我是 ZAP 的新手,我不太了解它的 js/ecma 脚本。
基本上,我试图将请求重定向到另一台主机。
假设连接到 ZAP 代理的应用程序在 URL:
中发出请求
http://www.somesite.com/path/to/a/file
但我想将 URL 中的主机名更改为:
another.site.com
所以它实际上会请求:http://www.anothersite.com/path/to/a/file
这是我尝试使用的代码,但 URL 在请求中保持不变。
function proxyRequest(msg) {
// Debugging can be done using println like this
var uri = msg.getRequestHeader().getURI().toString()
var host = msg.getRequestHeader().getURI().getHost().toString()
print('proxyResponse called for url=' + uri)
if (host == 'download.qt.io') {
uri = uri.replace('download.qt.io/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
if (host == 'ftp.jaist.ac.jp') {
uri = uri.replace('ftp.jaist.ac.jp/pub/qtproject/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
if (host == 'qtproject.mirror.liquidtelecom.com') {
uri = uri.replace('qtproject.mirror.liquidtelecom.com/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
return true
}
选项 1:替换规则
从市场安装 Replacer 插件:
- 转到“工具”菜单并 select 'Replacer Options'。
- 设置规则,如下图所示。
- Save/Okay视情况而定。
- 现在,当您浏览等时,您的所有流量都将 redirected/rewritten。
选项 2:HttpSender 脚本
- 创建一个新的 HttpSender 脚本,类似于以下示例:
function sendingRequest(msg, initiator, helper) {
var host = msg.getRequestHeader().getURI().getHost();
if (host.equals("www.somesite.com")) {
uri = msg.getRequestHeader().getURI();
uri.setEscapedAuthority("www.anothersite.com");
msg.getRequestHeader().setURI(uri);
}
return msg;
}
function responseReceived(msg, initiator, helper) {}
选项 3:主机文件条目
- 转到命令提示符并
nslookup www.somesite.com
,记下 IP 地址 (w.x.y.z)。
- 在您的主机文件中,添加一个条目,将记录的 IP (w.x.y.z) 与
www.anothersite.com
相关联。
(您可能需要重新启动 ZAP/browsers 才能使此更改生效。在 linux 上您可能需要 sudo 来编辑文件,在 Windows 上您需要将其编辑为管理员用户。)
(WRT 编辑您的主机文件的更多详细信息:https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/)
我是 ZAP 的新手,我不太了解它的 js/ecma 脚本。
基本上,我试图将请求重定向到另一台主机。 假设连接到 ZAP 代理的应用程序在 URL:
中发出请求http://www.somesite.com/path/to/a/file
但我想将 URL 中的主机名更改为: another.site.com
所以它实际上会请求:http://www.anothersite.com/path/to/a/file
这是我尝试使用的代码,但 URL 在请求中保持不变。
function proxyRequest(msg) {
// Debugging can be done using println like this
var uri = msg.getRequestHeader().getURI().toString()
var host = msg.getRequestHeader().getURI().getHost().toString()
print('proxyResponse called for url=' + uri)
if (host == 'download.qt.io') {
uri = uri.replace('download.qt.io/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
if (host == 'ftp.jaist.ac.jp') {
uri = uri.replace('ftp.jaist.ac.jp/pub/qtproject/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
if (host == 'qtproject.mirror.liquidtelecom.com') {
uri = uri.replace('qtproject.mirror.liquidtelecom.com/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
return true
}
选项 1:替换规则
从市场安装 Replacer 插件:
- 转到“工具”菜单并 select 'Replacer Options'。
- 设置规则,如下图所示。
- Save/Okay视情况而定。
- 现在,当您浏览等时,您的所有流量都将 redirected/rewritten。
选项 2:HttpSender 脚本
- 创建一个新的 HttpSender 脚本,类似于以下示例:
function sendingRequest(msg, initiator, helper) {
var host = msg.getRequestHeader().getURI().getHost();
if (host.equals("www.somesite.com")) {
uri = msg.getRequestHeader().getURI();
uri.setEscapedAuthority("www.anothersite.com");
msg.getRequestHeader().setURI(uri);
}
return msg;
}
function responseReceived(msg, initiator, helper) {}
选项 3:主机文件条目
- 转到命令提示符并
nslookup www.somesite.com
,记下 IP 地址 (w.x.y.z)。 - 在您的主机文件中,添加一个条目,将记录的 IP (w.x.y.z) 与
www.anothersite.com
相关联。
(您可能需要重新启动 ZAP/browsers 才能使此更改生效。在 linux 上您可能需要 sudo 来编辑文件,在 Windows 上您需要将其编辑为管理员用户。)
(WRT 编辑您的主机文件的更多详细信息:https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/)