FiddlerScript允许你在Fiddler Classic中修改Web请求和响应。你可以通过添加规则到OnBeforeRequest
或OnBeforeResponse
函数来实现自定义修改。
选择哪个函数取决于你的代码使用的对象类型:
OnBeforeRequest
在每个请求之前被调用。OnBeforeResponse
在每个响应之前被调用。
需要注意的是,在OnBeforeRequest
中无法访问响应对象,因为此时它们尚未创建。同时,虽然可以在OnBeforeResponse
中使用请求对象,但是对这些对象所做的任何更改都不会被服务器看到,因为它已经接收了请求。
下面是一些示例操作,你可以使用FiddlerScript来执行:
- 添加请求头:
oSession.oRequest["NewHeaderName"] = "New header value";
- 删除响应头:
oSession.oResponse.headers.Remove("Set-Cookie");
- 改变请求页面:
if (oSession.PathAndQuery=="/version1.css") { oSession.PathAndQuery="/version2.css"; }
- 重定向所有请求至另一服务器的相同端口:
if (oSession.HostnameIs("www.bayden.com")) { oSession.hostname="test.bayden.com"; }
- 重定向特定端口的所有请求至另一服务器的不同端口:
if (oSession.host=="www.bayden.com:8080") { oSession.host="test.bayden.com:9090"; }
- 重定向所有请求至另一服务器,包括HTTPS隧道:
if (oSession.HTTPMethodIs("CONNECT") && (oSession.PathAndQuery == "www.example.com:443")) { oSession.PathAndQuery = "beta.example.com:443"; }
- 模拟Windows HOSTS文件,指向不同的IP地址:
if (oSession.HostnameIs("subdomain.example.com")){ oSession.bypassGateway = true; oSession["x-overrideHost"] = "128.123.133.123"; }
- 重定向特定页面的请求至另一服务器:
if (oSession.url=="www.example.com/live.js") { oSession.url = "dev.example.com/workinprogress.js"; }
- 阻止HTTP Cookies上传:
oSession.oRequest.headers.Remove("Cookie");
- 解压和取消分块HTTP响应:
oSession.utilDecodeResponse();
- 在HTML中搜索和替换:
if (oSession.HostnameIs("www.bayden.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){ oSession.utilDecodeResponse(); oSession.utilReplaceInResponse('<b>','<u>'); }
- HTML中不区分大小写的搜索:
if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html") && oSession.utilFindInResponse("searchfor", false)>-1){ oSession["ui-color"] = "red"; }
- 移除所有DIV标签及其内容:
if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html")){ oSession.utilDecodeResponse(); var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes); var oRegEx = /<div[^>]*>(.*?)<\/div>/gi; oBody = oBody.replace(oRegEx, ""); oSession.utilSetResponseBody(oBody); }
- 模拟GoogleBot Web爬虫:
oSession.oRequest["User-Agent"]="Googlebot/2.X (+http://www.googlebot.com/bot.html)";
- 请求希伯来语内容:
oSession.oRequest["Accept-Language"]="he";
- 拒绝CSS请求:
if (oSession.uriContains(".css")){ oSession["ui-color"]="orange"; oSession["ui-bold"]="true"; oSession.oRequest.FailSession(404, "Blocked", "Fiddler Classic blocked CSS file"); }
- 模拟HTTP基本认证(要求用户在显示网页内容前输入密码):
if ((oSession.HostnameIs("www.example.com")) && !oSession.oRequest.headers.Exists("Authorization")){...}
- 响应请求时使用\Captures\Responses文件夹中的文件:
if (oSession.PathAndQuery=="/version1.css") { oSession["x-replywithfile"] ="version2.css"; }
这些脚本示例可以帮助你根据需求修改网络流量。请确保在实施这些修改时考虑所有相关的安全性和功能影响。