關於Fiddler插件自定義開發

Fiddler插件開發

基本操作這位博主已經提到了:https://blog.csdn.net/qqaiqqaiww/article/details/92800318
如下介紹關於部分方法使用:

 // MessageBox.Show(oSession.ResponseBody.ToString());
                
               //替換response中的部分內容,也可以是全部內容的方法
           oSession.utilReplaceInResponse("<firmwareVersionInfo>ssss</firmwareVersionInfo>", "<firmwareVersionInfo>X-X-X-X</firmwareVersionInfo>");
                             */
                // 在responsebody中尋找指定字符串,返回索引
                string str_info = oSession.utilFindInResponse("firmwareVersionInfo", true).ToString();
                //在responsebody中預先回復替換字符串,若替換responsebody服務器不識別,則會跳過該聽信息請求
               oSession.utilPrependToResponseBody("sadadsds");
                // 此方法是 一次代替,即之代替在指定字符串出現過的第一次,最後的參數是是否區分大小寫
                oSession.utilReplaceOnceInResponse("<firmwareVersionInfo>ssss</firmwareVersionInfo>", "<firmwareVersionInfo>X-X-X-X</firmwareVersionInfo>", true);
                //此方法是設置resonseBody值,參數爲字符串類型,無返回值
                oSession.utilSetResponseBody("asdasdasdasd");
                //此方法是設置請求體的值,參數爲字符串類型,無返回值
                oSession.utilSetRequestBody("asdasdads");
                //此方法是替換request中body的值,參數1 原始值,參數2  替換值
                oSession.utilReplaceInRequest(searchfor,replacestr)
                //創建一個服務器 即返回 ,現不知道如何使用
                oSession.utilCreateResponseAndBypassServer();
                //現在不知道怎麼用
                oSession.WriteResponseToStream(str_,true);
                //此方法爲保存response返回的值,第二個參數 是否只保存頭部 ,可以自行定義存儲文件的類型
                oSession.SaveResponse("D:\\xxx\\fiddler\\Fiddler2\\Captures\\response",false);
                //此方法爲保存response 中的body返回的值,可以自行定義存儲文件的類型
                oSession.SaveResponseBody("D:\\xxx\\fiddler\\Fiddler2\\Captures\\response");
                //此方法爲保存Request,第一個參數是保存路徑,第二個參數是否保存頭部,第三個參數是否保存請求path,即請求路徑
                oSession.SaveRequest("D:\\xxx\\fiddler\\Fiddler2\\Captures\\response", false, true);
                //此方法爲保存 Request 中的body
                oSession.SaveRequestBody("D:\xxxx\\fiddler\\Fiddler2\\Captures\\response");
                //主要是保存session狀態
                oSession.SaveMetadata("D:\\xxxx\\fiddler\\Fiddler2\\Captures\\response");
                //主要是獲取request中的body內容 並轉關爲字符串類型
                oSession.GetRequestBodyAsString();
                //主要是獲取request 中的body的編碼方法
                oSession.GetRequestBodyEncoding();
                //主要是獲取response 中body的編碼方式
                oSession.GetResponseBodyEncoding();
                //主要是獲取response 中的body內容
                oSession.GetResponseBodyAsString();

以下是相關效果及代碼:
實現功能:對於服務器返回的值進行部分修改及屏蔽--------------進行簡單話操作。

界面功能:
1.雙擊請求數據後 會將request/response上的數據顯示到RequestBody/ ResponseBody下面的texterea中,自定義接受的請求數據可以不斷刷新(一條一條的)。
2.部分包含:若勾選則請求中主要contain一部分字符串就會截取,修改response回覆內容。
3…設置後則會過濾並修改,若要取消則重新點擊即可。
說明:具體實現方式可以查看代理。
MyControl.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassLibrary2
{
    public partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("you clicked me");

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }


        public string[] UpdateDGV(Fiddler.Session oSession)
        {
            if (oSession != null)
            {
                string url = this.textBox1.Text.Trim();
                this.Invoke(
   new Action(
       delegate
       {
           //if (url != "" && oSession.host.Contains(url))
           //{
           //    this.dataGridView1.Rows.Add(
           //        oSession.host,
           //        oSession.PathAndQuery,
           //        oSession.RequestMethod,
           //        oSession.GetRequestBodyAsString(),
           //        oSession.GetResponseBodyAsString()
           //    );
           //}
           //else if (url == "")
           //{
               this.dataGridView1.Rows.Add(
            oSession.host,
            oSession.PathAndQuery,
            oSession.RequestMethod,
            oSession.GetRequestBodyAsString(),
            oSession.GetResponseBodyAsString()
);
           //}
       }

)
);
            }
            //string host = oSession.host;
            //string path = oSession.PathAndQuery;
            //string request_url = "http://" + host + path;
            string check = "";
            if (this.checkBox1.Checked == true)
            {
                check = "1";

            }
            else {

                check = "0";
            
            }
            string[] info = new string[] { this.textBox1.Text, this.button1.Text, this.textBox3.Text,check }; 
            return info;
        }


        private void MyControl_Load(object sendewr, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            //將獲取到的URL顯示到UI上
            string Request = this.dataGridView1.Rows[e.RowIndex].Cells["Request"].Value.ToString();
            string Response = this.dataGridView1.Rows[e.RowIndex].Cells["Response"].Value.ToString();
            this.textBox2.Text = Request;
            this.textBox3.Text = Response;
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                dataGridView1.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click_2(object sender, EventArgs e)
        {


            if (this.button1.Text == "設置")
            {

                this.button1.Text = "已設置";

            }
            else {

                this.button1.Text = "設置";
            }

        }
        public void OnBeforeUnload()
        {
        }

        public void OnLoad()
        {
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }


}

MyExtention.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fiddler;
using System.Windows.Forms;
namespace ClassLibrary2
{
    public class MyExtension : IFiddlerExtension, IAutoTamper
    {
        private TabPage tabPage; //創建插件的選項卡頁
        private MyControl myCtrl; //MyControl自定義控件

        public MyExtension()
        {
            //構造函數中實例化對象
            this.tabPage = new TabPage("屏蔽功能");//選項卡的名字爲Test
            this.myCtrl = new MyControl();
        }
        public void OnLoad()
        {//加載完成後才運行以下代碼
            //將用戶控件添加到選項卡中
            this.tabPage.Controls.Add(this.myCtrl);
            //爲選項卡添加icon圖標,這裏使用Fiddler 自帶的
            this.tabPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline;
            //將tabTage選項卡添加到Fidder UI的Tab 頁集合中
            FiddlerApplication.UI.tabsViews.TabPages.Add(this.tabPage);
        }
        public void OnBeforeUnload()
        {//工具關閉後才運行代碼
        }
        public void AutoTamperRequestAfter(Session oSession)
        {//發送請求之前
        }
        public void AutoTamperRequestBefore(Session oSession)
        {//發送請求之後
        }
        public void AutoTamperResponseAfter(Session oSession)
        {//得到響應之前
            string [] url = myCtrl.UpdateDGV(oSession);
            string host = oSession.host;
            string path = oSession.PathAndQuery;
              string protocol="";
            if(oSession.isHTTPS){

                 protocol = "https://";
            
            }
            else{
                 protocol = "http://";
            
            }
            string request_url = protocol + host + path;

            if(url[3]=="1"){
                // 此方方爲部分包含  結束包含URL即可
                if (request_url.EndsWith("xx/x/xx需要包含的字符串") && url[1] == "已設置")
                {
                    //MessageBox.Show(url[2]);
                    //設置修改後的request body內容
                    oSession.utilSetResponseBody(url[2]);
                }
                else
                {
                    //MessageBox.Show(url[0] + url[1]);

                }
            }
            else{

                // 此方法爲全部包含  
                if (request_url == url[0] && url[1] == "已設置")
                {
                    //MessageBox.Show(url[2]);
                    //設置修改後的request body內容
                    oSession.utilSetResponseBody(url[2]);
                }
                else
                {
                    //MessageBox.Show(url[0] + url[1]);

                }
            }
    

          
        }
        public void AutoTamperResponseBefore(Session oSession)
        {//得到響應之後
        }
        public void OnBeforeReturningError(Session oSession)
        {
        }


    }
}

MyControl.Designer.cs
UI的實現方式


namespace ClassLibrary2
{
    partial class MyControl
    {
        /// <summary> 
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放託管資源,爲 true;否則爲 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 組件設計器生成的代碼

        /// <summary> 
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        /// 
  
        private void InitializeComponent()
        {
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.HOST = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.PATH = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.METHED = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Request = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Response = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(1109, 25);
            this.toolStrip1.TabIndex = 2;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(28, 29);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 3;
            this.label1.Text = "截取URL:";
            this.label1.Click += new System.EventHandler(this.label1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(102, 26);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(264, 21);
            this.textBox1.TabIndex = 4;
            this.textBox1.Text = "http://172.7.1.10/ISAPI/System/deviceInfo";
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.HOST,
            this.PATH,
            this.METHED,
            this.Request,
            this.Response});
            this.dataGridView1.Location = new System.Drawing.Point(3, 428);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(795, 339);
            this.dataGridView1.TabIndex = 5;
            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
            this.dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);
            // 
            // HOST
            // 
            this.HOST.HeaderText = "Host";
            this.HOST.Name = "HOST";
            // 
            // PATH
            // 
            this.PATH.HeaderText = "Path";
            this.PATH.Name = "PATH";
            // 
            // METHED
            // 
            this.METHED.HeaderText = "Methed";
            this.METHED.Name = "METHED";
            // 
            // Request
            // 
            this.Request.HeaderText = "Request";
            this.Request.Name = "Request";
            // 
            // Response
            // 
            this.Response.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.Response.DividerWidth = 10;
            this.Response.HeaderText = "Response";
            this.Response.MinimumWidth = 20;
            this.Response.Name = "Response";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(393, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "設置";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click_2);
            // 
            // textBox2
            // 
            this.textBox2.ForeColor = System.Drawing.SystemColors.MenuHighlight;
            this.textBox2.Location = new System.Drawing.Point(16, 149);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textBox2.Size = new System.Drawing.Size(356, 253);
            this.textBox2.TabIndex = 7;
            this.textBox2.Text = "request";
            this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
            // 
            // textBox3
            // 
            this.textBox3.ForeColor = System.Drawing.SystemColors.HotTrack;
            this.textBox3.Location = new System.Drawing.Point(442, 149);
            this.textBox3.Multiline = true;
            this.textBox3.Name = "textBox3";
            this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textBox3.Size = new System.Drawing.Size(356, 253);
            this.textBox3.TabIndex = 8;
            this.textBox3.Text = "response";
            this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋體", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.ForeColor = System.Drawing.SystemColors.MenuHighlight;
            this.label2.Location = new System.Drawing.Point(26, 114);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(119, 19);
            this.label2.TabIndex = 9;
            this.label2.Text = "RequestBody";
            this.label2.Click += new System.EventHandler(this.label2_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋體", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.ForeColor = System.Drawing.SystemColors.MenuHighlight;
            this.label3.Location = new System.Drawing.Point(438, 114);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(129, 19);
            this.label3.TabIndex = 10;
            this.label3.Text = "ResponseBody";
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Checked = true;
            this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBox1.Location = new System.Drawing.Point(510, 24);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(72, 16);
            this.checkBox1.TabIndex = 11;
            this.checkBox1.Text = "部分包含";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // MyControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.toolStrip1);
            this.ForeColor = System.Drawing.SystemColors.HotTrack;
            this.Name = "MyControl";
            this.Size = new System.Drawing.Size(1109, 883);
            this.Load += new System.EventHandler(this.MyControl_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
  
        #endregion

        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn HOST;
        private System.Windows.Forms.DataGridViewTextBoxColumn PATH;
        private System.Windows.Forms.DataGridViewTextBoxColumn METHED;
        private System.Windows.Forms.DataGridViewTextBoxColumn Request;
        private System.Windows.Forms.DataGridViewTextBoxColumn Response;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.CheckBox checkBox1;
        //private System.Windows.Forms.WebBrowser webBrowser1;



    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章