Fiddler如何自動修改請求和響應包

Charles的Map功能能夠將某個請求進行重定向,用重定向的內容響應請求的內容。這個功能很是方便。在抓包過程中,有時候爲了調試方便,須要將線上的服務定位到內網。好比咱們線上的服務器域名爲 api.example.com,而內網的用於調試的服務器域名爲 test.neiwang.com,那麼就須要將全部域名 api.example.com替換爲 test.neiwang.com,就可使用charles的這個功能,可是charles是收費軟件,使用破解版又可能不安全,因此咱們須要用一款免費抓包工具來代替,fiddler就是個不錯的選擇。可是fiddler並無map功能,不過不要緊,咱們能夠經過編寫腳原本實現這個功能。
Fiddler菜單中,Rules->Custon Rules,或按Ctrl+R鍵,編輯ScriptEditor代碼文件,在OnBeforeRequest函數(static function OnBeforeRequest(oSession: Session))裏面加上幾句代碼:
端口不一樣:css

if (oSession.host.ToLower=="https://api.example.com:8080") {
    oSession.host="http://test.neiwang.com:9090";
}

端口相同:html

if (oSession.HostnameIs("www.bayden.com")) {
  oSession.hostname="test.bayden.com";
}


拓展開來,若是咱們須要修改請求和響應信息,應該怎麼編寫代碼呢?
1.增長請求頭:api

oSession.oRequest["NewHeaderName"] = "New header value";

2.將請求的某個頁面替換爲同一個站點的不一樣頁面安全

if (oSession.PathAndQuery=="/version1.css") {
  oSession.PathAndQuery="/version2.css";
}

3.將請求的某個頁面替換爲不一樣站點的頁面服務器

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


修改響應:static function OnBeforeResponse(oSession: Session)
1.刪除響應頭less

oSession.oResponse.headers.Remove("Set-Cookie");

2.解壓縮和unchunk一個HTTP響應函數

// Remove any compression or chunking from the response in order to make it easier to manipulate
oSession.utilDecodeResponse();

3.在響應的HTML中搜索關鍵詞(不區分大小寫)工具

if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html") && oSession.utilFindInResponse("searchfor", false)>-1){
  oSession["ui-color"] = "red";
}

4.搜索和替換HTML內容ui

if (oSession.HostnameIs("www.bayden.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
  oSession.utilDecodeResponse();
  oSession.utilReplaceInResponse('<b>','<u>');
}

5.移除全部div標籤和標籤中的內容url

// 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);
}

參考連接

https://www.jianshu.com/p/775f83e45a02

https://docs.telerik.com/fiddler/knowledgebase/fiddlerscript/modifyrequestorresponse

相關文章
相關標籤/搜索