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。

Fiddler Script 的官方帮助文档的地址是:http://www.fiddler2.com/Fiddler/dev/ScriptSamples.asp

可以直接编辑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登录过程可正常重定向到测试环境。


相关知识

FiddlerScript中的主要方法、用法及脚本

FiddlerScript md5加密实现方式-论坛

JScript.NET介绍-docs.microsoft(JScript.NET语言语法类似C#和js,编脚本时建议参考microsoftMicrosoft.JScript详细文档,以解决脚本保存时的各种报错)

Fiddler Script 与 HTTP 断点调试

编辑器的工具栏 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[])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值