Fiddler篇 通过FiddlerScript自动拦截修改请求

进行自定义修改web请求和响应,使用FiddlerScript添加规则Fiddler的OnBeforeRequest或OnBeforeResponse函数。 哪个函数是合适取决于你的代码使用的对象:OnBeforeRequest被称为每个请求之前,OnBeforeResponse就是在每个响应。 注意:

它是不可能的访问内部的响应对象OnBeforeRequest因为他们还没有创建。
它是可能的使用对象的请求OnBeforeResponse; 然而,您所作的改变这些对象将不会被服务器,因为它已经接收到请求。
添加一个请求头

    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隧道

    // Redirect traffic, including HTTPS tunnels
    if (oSession.HTTPMethodIs("CONNECT") && (oSession.PathAndQuery == "www.example.com:443")) { 
        oSession.PathAndQuery = "beta.example.com:443"; 
    }

    if (oSession.HostnameIs("www.example.com")) oSession.hostname = "beta.example.com"; 

模拟Windows主机文件,通过指向一个主机名到一个不同的IP地址。(gdp8 %不改变请求的主机头)

    // All requests for subdomain.example.com should be directed to the development server at 128.123.133.123
    if (oSession.HostnameIs("subdomain.example.com")){
    oSession.bypassGateway = true;                   // Prevent this request from going through an upstream proxy
    oSession["x-overrideHost"] = "128.123.133.123";  // DNS name or IP address of target server
    }

gdp8 %的请求一个页面到另一个页面,可能在不同的服务器上。(gdp8 %通过更改请求的主机头)

    if (oSession.url=="www.example.com/live.js") {
      oSession.url = "dev.example.com/workinprogress.js";
    }

防止上传HTTP cookie

    oSession.oRequest.headers.Remove("Cookie");

减压和unchunk HTTP响应,如果需要更新标题

    // Remove any compression or chunking from the response in order to make it easier to manipulate
    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标签(内容DIV标签内)

    // If content-type is HTML, then remove all DIV tags
    if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html")){
      // Remove any compression or chunking
      oSession.utilDecodeResponse();
      var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

      // Replace all instances of the DIV tag with an empty string
      var oRegEx = /<div[^>]*>(.*?)<\/div>/gi;
      oBody = oBody.replace(oRegEx, "");

      // Set the response body to the div-less string
      oSession.utilSetResponseBody(oBody); 
    }

假装你的浏览器是GoogleBot webcrawler

    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 blocked CSS file");
    }

模拟HTTP基本身份验证(需要用户输入密码之前显示网页内容。)

    if ((oSession.HostnameIs("www.example.com")) && 
     !oSession.oRequest.headers.Exists("Authorization")) 
    {
    // Prevent IE's "Friendly Errors Messages" from hiding the error message by making response body longer than 512 chars.
    var oBody = "<html><body>[Fiddler] Authentication Required.<BR>".PadRight(512, ' ') + "</body></html>";
    oSession.utilSetResponseBody(oBody); 
    // Build up the headers
    oSession.oResponse.headers.HTTPResponseCode = 401;
    oSession.oResponse.headers.HTTPResponseStatus = "401 Auth Required";
    oSession.oResponse["WWW-Authenticate"] = "Basic realm=\"Fiddler (just hit Ok)\"";
    oResponse.headers.Add("Content-Type", "text/html");
    }

响应一个请求的文件加载捕获文件夹的反应(可以放在OnBeforeRequest或OnBeforeResponse函数)

    if (oSession.PathAndQuery=="/version1.css") {
      oSession["x-replywithfile"] ="version2.css";
    }

引用链接
fiddler Modifying a Request or Response

原创文章,作者:VAY冬冬,如若转载,请注明出处:https://blog.vay1314.top/archives/39

(0)
VAY冬冬的头像VAY冬冬
上一篇 2020年6月25日
下一篇 2020年7月19日

相关推荐

0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论