1. 程式人生 > >fiddlerscript 之重定向實踐

fiddlerscript 之重定向實踐

FiddlerScript 是Fiddler 的一項非常強大的功能,它允許你擴充套件Fiddler UI,新增新的特性,修改請求與響應內容等。

FiddlerScript 基於JScript.NET 語言。

前情提要

某測試專案X中有用線上包來進行測試環境測試的需求。

需求分析

重定向線上環境到測試環境。

1、若重定向僅涉及少量url,可以通過配置本地host,或者使用抓包工具charles->Tools->Map Remote、Fiddler->Tools->Hosts。

2、若重定向涉及一些包中的引數值在兩個環境下不同,則需要改包,可以使用charles、Fiddler對應的斷點、改包功能。

3、若重定向中需要改包的引數值不是常量而是變數時,需要指令碼工具(anyroxy、fiddlerscript )來進行更靈活的改包。

專案X需要靈活改包才能實現重定向,fiddlerscript可以方便的檢視改包的情況、效果,所以這裡用fiddlerscript來實現專案X的重定向。

實踐

step1-瞭解需要改的包、引數值

通過抓包分析,並藉助開發提供的url整理文件,明確所有需要修改的包、欄位(舉例如下)

step2-準備好指令碼使用環境:

指令碼檔案CustomRules.js位於..\Fiddler2\Scripts\CustomRules.js 下。

你也可以在Fiddler 中開啟CustomRules.js 檔案, 啟動Fiddler, 點選選單Rules->Customize Rules。

可以直接編輯CustomRules.js檔案,也可以下載 Fiddler Script Editor來編輯,下載的地址是http://www.fiddler2.com/fiddler/fse.asp Fiddler Script Editor 提供了語法高亮,以及智慧提示的功能, 方便編輯。

新版本的fiddler自帶該Editor,點選工具欄->Rules->Customize Rules或者直接Ctrl+R可以開啟編輯器。

step3-編寫指令碼:

舉例如下

import System;

import System.Windows.Forms;

import Fiddler;

class Handlers

{...

//自定義簽名函式

function static function MD5(Text: String){

//var strFKey = Convert.ToBase64String(Convert.FromBase64String(Text));

var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

// FiddlerApplication.Log.LogString(md5.ComputeHash(Text))

var bs = System.Text.Encoding.UTF8.GetBytes(Text);

var md5Hasher: System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create();

var arrHash: byte[] = md5Hasher.ComputeHash(bs);

var s = new System.Text.StringBuilder();

for (var b in arrHash){s.Append(arrHash[b].ToString("x2").ToUpper()); } FiddlerApplication.Log.LogString(s.ToString())

return s.ToString();

}

static function OnBeforeRequest(oSession: Session) {

......



if(oSession.fullUrl.Contains("/login")){ oSession.host = "x.test.com"; }     if(oSession.fullUrl.Contains("/oauth2/auth")){

oSession.host = "x.test.com";

oSession.oRequest["app_id"] = "xxxxxxx";

oSession.oRequest["redirect_uri"] = "https://x.test.com/oauth2/result"; } if(oSession.fullUrl.Contains("/api/oauth2/access_token")){

var strBody=oSession.GetRequestBodyAsString(); strBody=strBody.replace("xxxxxxxxxxx","xxxxxxxxx1");  oSession.utilSetRequestBody(strBody);

oSession.host = "x.test.com"; }

//---------具體的驗籤演算法相關的介面改包,需要在瞭解開發驗籤演算法的前提下進行------------

if(oSession.fullUrl.Contains("/checksign")){

var strBody=oSession.GetRequestBodyAsString(); strBody=strBody.replace("sign=xxxxxxxxxxxxxxxx&sign_type=hmac","sign_type=md5");

var rgx = new System.Text.RegularExpressions.Regex("sign=(.*?)&sign_type=hmac"); strBody = rgx.Replace(strBody, "sign_type=md5")

var strPublicKey="xxxxxxxxxxxxxxxx" + strBody";

var sign = MD5(strPublicKey);

FiddlerApplication.Log.LogString(strPublicKey) strBody=strBody.replace("sign_type=md5","sign="+sign+"&sign_type=md5"); FiddlerApplication.Log.LogString(strBody) oSession.utilSetRequestBody(strBody); oSession.host = "x.test.com";}

// FiddlerApplication.Log.LogString(strBody.ToString())

}

//else

// if (oSession.fullUrl.Contains("b.com")) {oSession.host = "b.test.com"};

// if (oSession.fullUrl.Contains("c.com")) {oSession.host = "c.test.com"};}

}

static function OnPeekAtResponseHeaders(oSession: Session) {...}

static function OnBeforeResponse(oSession: Session){...} ......

step4-儲存指令碼&進行登入:

outh登入過程可正常重定向到測試環境。

相關知識

編輯器的工具欄 Go to 點選可直接跳到指令碼中的幾個常用的方法位置上

// 在這個方法中修改Request的內容,用得最多

static function OnBeforeRequest(oSession: Session)

// 在這個方法中修改Response的內容

static function OnBeforeResponse(oSession: Session)

// 在個方法中包含Fiddler 命令,在Fiddler介面中左下方的QuickExec Box,如果你的指令碼處理了指定的命令,則返回true,否則返回false.

static function OnExecAction(sParams: String[])