Loading

fiddler抓包自定义代码示例

请求前示例

static function OnBeforeRequest(oSession: Session) {
  if (oSession.fullUrl.Contains("https://ruoshui.test"))
{
    // 获取请求体
    var jsonString = oSession.GetRequestBodyAsString();
    FiddlerApplication.Log.LogString("Request Body: " + jsonString); // 输出请求体内容
    
    try
    {
        // 尝试解析 JSON 字符串
        var jsonObj = Fiddler.WebFormats.JSON.JsonDecode(jsonString);
        FiddlerApplication.Log.LogString("------------");

        // 输出请求体中的 's' 字段
        var mys = jsonObj.JSONObject['s'];
        FiddlerApplication.Log.LogString("s value: " + mys);
        
        // 检查是否是需要修改的请求
        if (mys == 'App.Order.getOrderByPlan')
        {
            var mycode = jsonObj.JSONObject['js_code'];
            var mylogin = '{"s":"App.PlanCar.planCancel","plan_info_id":114541,"js_code":"' + mycode + '"}'; // 更改后的JSON数据
            
            // 调试输出新构建的 JSON
            FiddlerApplication.Log.LogString("Modified JSON: " + mylogin);
            
            // 将新的 JSON 字符串解析成对象
            var requestJson = Fiddler.WebFormats.JSON.JsonDecode(mylogin);
            
            // 再次序列化为 JSON 字符串
            var reJsonDes = Fiddler.WebFormats.JSON.JsonEncode(requestJson.JSONObject);
            
            // 输出修改后的 JSON 字符串
            FiddlerApplication.Log.LogString("Serialized Modified JSON: " + reJsonDes);
            
            // 设置请求体为修改后的 JSON
            oSession.utilSetRequestBody(mylogin);
        }
    }
    catch (e)
    {
        // 如果解析 JSON 时发生错误,记录日志并跳过该请求
        FiddlerApplication.Log.LogString("Error parsing JSON: " + e.Message);
    }
}
}

请求返回结果修改示例

static function OnBeforeResponse(oSession: Session) {
        FiddlerApplication.Log.LogString("-------1111111111111-----"+oSession.url);
        // 检查请求的URL 
        if (oSession.url.Contains("ruoshui.test:4443/"))
        {
            try
            {
                // 获取响应体内容为字符串
                var responseBody = oSession.GetResponseBodyAsString();
                // 尝试将响应的JSON解析为对象
                var jsonResponse = Fiddler.WebFormats.JSON.JsonDecode(responseBody);
                FiddlerApplication.Log.LogString("-------ret参数是-----"+jsonResponse.JSONObject["ret"]);
                
            // 获取请求体
            var jsonString = oSession.GetRequestBodyAsString();
            FiddlerApplication.Log.LogString("获取请求体" + jsonString); // 输出请求体内容
            // 尝试解析 JSON 字符串
            var jsonObj = Fiddler.WebFormats.JSON.JsonDecode(jsonString);
            FiddlerApplication.Log.LogString("------------");
            // 输出请求体中的 's' 字段
            var mys = jsonObj.JSONObject['s'];
            FiddlerApplication.Log.LogString("s value: " + mys);
                
            // 检查JSON对象中的ret字段是否为200
            if (mys == 'App.Order.getOrderByCarCheckIn')
            {
                FiddlerApplication.Log.LogString("准备该参数:"+jsonObj.JSONObject["s"]);
                var reponseJsonString=oSession.GetResponseBodyAsString();//获取JSON字符串
                var responseJSON=Fiddler.WebFormats.JSON.JsonDecode(reponseJsonString);//转化为JSON数据,可编辑
                var str='{"videos":[{"duration":157,"id":1837,"name":"平均数FiddlerScript","pay":0,'+
                    '"resourceCode":"LLKT_01",'+
                    '"showOrder":1}]}';//自定义JSON
                //responseJSON.JSONObject['data']= Fiddler.WebFormats.JSON.JsonDecode(str).JSONObject ;//转换需要
                responseJSON.JSONObject= Fiddler.WebFormats.JSON.JsonDecode(str).JSONObject ;//转换需要

                //responseJSON.JSONObject['ret']= 100 ;//转换需要
                var myResponseJSON= Fiddler.WebFormats.JSON.JsonEncode(responseJSON.JSONObject);//转换需要
                oSession.utilSetResponseBody(myResponseJSON);//设置ResponseBody中的JSON数据
                   
            }
        }
            catch (e)
            {
                FiddlerApplication.Log.LogString("错误了: " + e.Message);
            }
        }
    
    
        if (m_Hide304s && oSession.responseCode == 304) {
            oSession["ui-hide"] = "true";
        }
    }

一些常用的oSession函数和方法

// 请求host
 oSession.host == "my.test.com";
 // 请求host之后的url是否包含
 oSession.url.Contains("/feed") ;
 // 获取响应内容的字符串
 var logContent = oSession.GetResponseBodyAsString(); 
 // 创建写入流
 var sw : System.IO.StreamWriter; 
if (System.IO.File.Exists(filename)){  //是否有该文件夹  
    sw = System.IO.File.AppendText(filename);   //有添加
    sw.Write(logContent);  
}  
else{  
    sw = System.IO.File.CreateText(filename);  //没有创建
    sw.Write(logContent);  
}  
sw.Close();  //关闭写入流
sw.Dispose();  //销毁写入流

 // 修改session中的显示样式
 oSession["ui-color"] = "orange";
 // 移除http头部中的MQB-X5-Referer字段
 oSession.oRequest.headers.Remove("MQB-X5-Referer");
 // 修改http头部中的Cache-Control字段
 oSession.oRequest["Cache-Control"] = "no-cache";
 // 修改host
 oSession.host = "example.domain"; 
 // 修改Origin字段
 oSession.oRequest["Origin"] = "http://domain";
 // 删除所有的cookie
 oSession.oRequest.headers.Remove("Cookie");
 // 新建cookie
 oSession.oRequest.headers.Add("Cookie", "username=cookiename;");
 // 修改Referer字段
 oSession.oRequest["Referer"] = "https://yoururl";
 
 // 获取Request中的body字符串
 var strBody=oSession.GetRequestBodyAsString();
 // 用正则表达式或者replace方法去修改string
 strBody=strBody.replace("aaaa","bbbbbb");
 // 将修改后的body,重新写回Request中
 oSession.utilSetRequestBody(strBody);
// 判断连接中是否包含字符串str
oSession.uriContains(str)
// 给连接请求添加一个字段TEST
oSession.oRequest["TEST"]="TEST NEW Request";

参考:
https://blog.csdn.net/ljw5888/article/details/89449137
https://blog.csdn.net/2401_84160087/article/details/137703564
https://www.cnblogs.com/i-love-python/p/11505669.html

posted @ 2024-12-24 11:29  王召波  阅读(134)  评论(0)    收藏  举报