FiddlerCore拦截本地请求初学习

首先从官网下载FiddlerCoreAPI
https://www.telerik.com/purchase/fiddlercore

查阅手册,看几个关键的方法和变量:

//获得Request体

oSession.GetRequestBodyAsString()      

//获得Response内容

oSession.GetResponseBodyAsString()

 // 修改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";

 拦截websockets请求

 FiddlerApplication_OnWebSocketMessage  

  public partial class Form1 : Form
    {
        public int port = 8888;
        public Form1()
        {
          //  Iptext = new TextBox();
          //  Iptext.Text = "10"; 
            InitializeComponent();
        }

        private void FiddlerApplication_BeforeRequest(Session oSession)
        {
             if ((!string.IsNullOrEmpty(this.Iptext.Text))&&oSession.m_hostIP.StartsWith(this.Iptext.Text))
            {
                this.treeView1.BeginInvoke((ThreadStart)delegate ()
                {
                    
                    TreeNode tempNode = new TreeNode();
                    tempNode.Text = oSession.url+oSession.m_hostIP;
                    this.treeView1.Nodes.Add(tempNode);
                });
            }
            else
            {
                this.treeView1.BeginInvoke((ThreadStart)delegate ()
                {
                    TreeNode tempNode = new TreeNode();
                    tempNode.Text = oSession.url + oSession.m_hostIP; ;
                    this.treeView1.Nodes.Add(tempNode);
                });
            }
          

        }

 
        private void button1_Click(object sender, EventArgs e)
        {
            this.treeView1.BeginInvoke((ThreadStart)delegate ()
            {
                TreeNode tempNode = new TreeNode();
                tempNode.Text = "开始";
                this.treeView1.Nodes.Add(tempNode);
            });
            if(!Fiddler.FiddlerApplication.IsStarted())
            {
                FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
                Fiddler.FiddlerApplication.Startup(port, true, true);
            }
            else
            {
                this.treeView1.BeginInvoke((ThreadStart)delegate ()
                {
                    TreeNode tempNode = new TreeNode();
                    tempNode.Text = "不能重复开启";
                    this.treeView1.Nodes.Add(tempNode);
                });
            }
 
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.treeView1.BeginInvoke((ThreadStart)delegate ()
            {
                TreeNode tempNode = new TreeNode();
                tempNode.Text = "终止";
                this.treeView1.Nodes.Add(tempNode);
            });
            Fiddler.FiddlerApplication.Shutdown();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
    }

2 获取拦截请求的域名和ip

   private void FiddlerApplication_BeforeRequest(Session oS)
        {
if ((!string.IsNullOrEmpty(this.txt_destinationHost.Text)) && oSession.m_hostIP.StartsWith(this.txt_destinationHost.Text))
             {
            }
        }

3 修改响应内容,修改返回请求头,状态,内容

    private void FiddlerApplication_BeforeRequest(Session oS)
        {
  if ((oS.oRequest.pipeClient.LocalPort == iSecureEndpointPort) && (oS.hostname == sSecureEndpointHostname))
            {
                oS.utilCreateResponseAndBypassServer();
                oS.oResponse.headers.SetStatus(200, "Ok");
                oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";
                oS.oResponse["Cache-Control"] = "private, max-age=0";  
                var Restr="返回内容";
                oS.utilDecodeResponse();
                oS.utilSetResponseBody(Restr);
           }
        }

4 开启FiddlerCore在指定端口的监听。

Fiddler.FiddlerApplication.Startup(iPort, oFCSF);

5 建立在指定端口的HTTPS的监听

oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iSecureEndpointPort, true, sSecureEndpointHostname);

6关闭应用

Fiddler.FiddlerApplication.Shutdown();

7获取请求头信息和请求内容

oS.RequestHeaders.ToByteArray(true, true, !oS.isHTTPS)
oS.RequestBody

8 设置代理网关 提供主机:网关的端口组合,该网关应该用于代理此请求,或直接将请求发送到原始服务器。

x-overrideGateway
 oSession["X-OverrideGateway"] ="socks=127:80"

 

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Fiddler是一款用于网络抓包和分析的工具,可以用来拦截请求并查看请求和响应的内容。使用Fiddler进行拦截请求的方法有多种。一种方法是使用bpu命令来拦截指定域名下的全部网络请求。例如,输入bpu www.baidu.com可以拦截www.baidu.com域名下的所有请求。如果想取消拦截,可以输入bpu回车来取消。这样,在再次请求时,就不会被拦截了。另一种方法是使用篡改包功能,可以修改请求值。通过修改请求值,可以对请求进行修改,以达到篡改请求的目的。通过使用Fiddler抓包,可以查看前端发送的请求和后端返回的响应,以便分析问题。如果请求有问题,那么可能是前端的bug;如果响应有问题,则可能是后端的bug。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Fiddler对数据包的拦截(打断点、设置断点)、改包、伪造(构造)、自动响应](https://blog.csdn.net/chengdiyiyo/article/details/120752069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Fiddler使用之拦截、篡改包、网络重定向](https://blog.csdn.net/yangyaner__/article/details/125497897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值