fiddler4写插件总结

本文目标

为fiddler做一个插件,按要求截获所感兴趣的关键字url,保存到文件中

下载安装最新版本fiddler

https://www.telerik.com/fiddler

选择安装路径到d盘中(非必须)

关于接口的描述,请参考:https://www.cnblogs.com/weekend001/archive/2013/12/13/3473632.html

配置工程

新建一个工程并引入类库

新建一个C#,Windows类库

在这里插入图片描述

添加前面安装的fiddler.exe作为引用

在这里插入图片描述

在这里插入图片描述
因为要加入一个带窗体的应用程序,因此还需要引入System.Windows.Forms
在这里插入图片描述

新建一个页面控件

在AssemblyInfo.cs中声明fiddler的版本

一般嘛使用4.5.1.2即可解决大多数版本问题

[assembly: Fiddler.RequiredVersion("4.5.1.2")]

在这里插入图片描述

设计页面

在这里插入图片描述

使用fiddler提供的接口,把页面加入到fiddler中

using Fiddler;
using System.Windows.Forms;

namespace FiddlerExtension
{
    public class Class1: IAutoTamper
    {
        private TabPage tabPage; //创建插件的选项卡页
        private UserControl1 myCtrl; //MyControl自定义控件

        public Class1()
        {
            //构造函数中实例化对象
            this.tabPage = new TabPage("MyFiddlerExtension");//选项卡的名字为Test
            this.myCtrl = new UserControl1();
        }

        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()
        {

        }

        // Called before the user can edit a request using the Fiddler Inspectors
        public void AutoTamperRequestBefore(Session oSession)
        {
        }


        // Called after the user has had the chance to edit the request using the Fiddler Inspectors, but before the request is sent
        public void AutoTamperRequestAfter(Session oSession) { }


        // Called before the user can edit a response using the Fiddler Inspectors, unless streaming.
        public void AutoTamperResponseBefore(Session oSession) { }


        // Called after the user edited a response using the Fiddler Inspectors.  Not called when streaming.
        public void AutoTamperResponseAfter(Session oSession) { }


        // Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc)
        public void OnBeforeReturningError(Session oSession) { }
    }
}

为按钮加入一个响应事件

using System;
using System.Windows.Forms;

namespace FiddlerExtension
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

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

生成并测试

把生成后得到的FiddlerExtension.dll,拷贝到fiddler目录下的Scripts文件夹中

在这里插入图片描述

=================================================

可以通过修改生成事件,让其自动复制到指定文件夹中

copy "$(TargetPath)" "D:\Fiddler\Scripts$(TargetFilename)"

在这里插入图片描述

=================================================

运行fiddler,点击页面中的按钮,可以看到弹出提示,证明代码已经正确

在这里插入图片描述

完成目标功能

修改页面控件内容

在这里插入图片描述

补充页面控件的代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace FiddlerExtension
{
    public partial class UserControl1 : UserControl
    {
        public string filter = "";
        private bool isBeginFilter = false;
        public UserControl1()
        {
            InitializeComponent();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (!isBeginFilter) // beginFilter
            {
                isBeginFilter = !isBeginFilter;
                filter = this.textBox2.Text;
                this.button1.Text = "停止捕获";
            }
            else
            {
                isBeginFilter = !isBeginFilter;
                filter = "";
                this.button1.Text = "开始捕获";
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            // 把当前listview中的数据保存到output.txt中
            if (this.listView1.Items.Count == 0)
            {
                MessageBox.Show("列表为空!");
            }
            else
            {
                List<string> list = new List<string>();
                foreach (ListViewItem item in this.listView1.Items)
                {
                    string temp = item.SubItems[2].Text; // 取出url的数据进行保存
                    list.Add(temp);
                }
                Thread thexp = new Thread(() => export(list)) { IsBackground = true };
                thexp.Start();
            }
        }
        private void export(List<string> list)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "url_" + Guid.NewGuid().ToString() + ".txt";
            StringBuilder sb = new StringBuilder();
            foreach (string urlString in list)
            {
                sb.AppendLine(urlString);
            }
            System.IO.File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
            MessageBox.Show("已保存完毕,路径:" + path);
        }
    }
}

对应项目的代码如下(Class1.cs)

using Fiddler;
using System.Windows.Forms;

namespace FiddlerExtension
{
    public class Class1 : IAutoTamper
    {
        private TabPage tabPage; //创建插件的选项卡页
        private UserControl1 myCtrl; //MyControl自定义控件

        public Class1()
        {
            //构造函数中实例化对象
            this.tabPage = new TabPage("MyFiddlerExtension");//选项卡的名字为Test
            this.myCtrl = new UserControl1();
        }

        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()
        {

        }

        // Called before the user can edit a request using the Fiddler Inspectors
        public void AutoTamperRequestBefore(Session oSession)
        {
            //   this.myCtrl.textBox1.Text += oSession.fullUrl + "\r\n";
            // 过滤满足条件的host
            if (this.myCtrl.filter != "")
            {
                string url = oSession.fullUrl;
                if (url.Contains(this.myCtrl.filter))
                {
                    ListViewItem item = new ListViewItem(this.myCtrl.listView1.Items.Count + "");
                    item.SubItems.Add(oSession.host); // 加入host
                    item.SubItems.Add(oSession.fullUrl); // 加入fullUrl
                    //item.SubItems.Add(""); // 加入request参数
                    //item.SubItems.Add(""); // 加入response参数
                    this.myCtrl.listView1.Items.Add(item);
                }
            }
        }

        // Called after the user has had the chance to edit the request using the Fiddler Inspectors, but before the request is sent
        public void AutoTamperRequestAfter(Session oSession) { }


        // Called before the user can edit a response using the Fiddler Inspectors, unless streaming.
        public void AutoTamperResponseBefore(Session oSession) { }


        // Called after the user edited a response using the Fiddler Inspectors.  Not called when streaming.
        public void AutoTamperResponseAfter(Session oSession) { }


        // Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc)
        public void OnBeforeReturningError(Session oSession) { }
    }
}

生成并测试

开始捕获

在这里插入图片描述

保存到文件中

点击后如下

在这里插入图片描述

打开查看内容如下

在这里插入图片描述

以上说明功能完成

示例代码下载

https://download.csdn.net/download/zengraoli/12120294

已在demo上做了修改,url写出到固定文件中,只需要替换本文相应代码即可

参考的链接

  • https://www.cnblogs.com/weekend001/archive/2013/12/13/3473632.html
  • https://www.cnblogs.com/rufus-hua/p/5275980.html
  • https://www.qxqzx.com/contents/623.html
  • https://www.sohu.com/a/190260237_741445
  • https://blog.csdn.net/qqaiqqaiww/article/details/92800318
  • https://www.cnblogs.com/weekend001/p/3472912.html
  • https://blog.csdn.net/x333vxhl/article/details/55188371
  • https://www.qxqzx.com/contents/623.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章