首页 > 其他分享 >利用fiddlercore,修改http/https请求与响应

利用fiddlercore,修改http/https请求与响应

时间:2023-02-08 18:36:31浏览次数:52  
标签:Fiddler Console System fiddlercore static https using http oS

fiddler抓包工具,相信很多人都用过,很好用的一款抓包工具。

 fiddlercore是官方提供给开发者调用的,用来处理所有的http/https请求,功能就如Fiddler一样强大,fiddlercore官方网站:https://www.telerik.com/fiddlercore

下面我们利用fiddlercore来修改浏览器的响应数据。

自动设置https证书,所有请求都返回“二十有为”

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using Fiddler;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Demo
{
    class Program
    {
        static Proxy oSecureEndpoint;
        static string sSecureEndpointHostname = "localhost";
        static int iSecureEndpointPort = 9876;

        static void Main(string[] args)
        {
            Console.WriteLine("Starting ...");
            Fiddler.CertMaker.createRootCert();
            X509Certificate2 oRootCert = Fiddler.CertMaker.GetRootCertificate();
            SetMachineTrust(oRootCert);
            Fiddler.FiddlerApplication.oDefaultClientCertificate = oRootCert;
            List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();

            #region AttachEventListeners
            Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
            {
                oS.bBufferResponse = true;
                HTTPRequestHeaders rHeads = oS.oRequest.headers;
                //获取cookie
                string cookie = rHeads.AllValues("cookie");
                if ((oS.oRequest.pipeClient.LocalPort == iSecureEndpointPort) && (oS.hostname == sSecureEndpointHostname))
                {
                    oS.utilCreateResponseAndBypassServer();
                    oS.oResponse.headers.HTTPResponseStatus = "200 Ok";
                    oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";
                    oS.oResponse["Cache-Control"] = "private, max-age=0";
                    oS.utilSetResponseBody("<html><body>show!</body></html>");
                }
            };

            Fiddler.FiddlerApplication.BeforeResponse += new Fiddler.SessionStateHandler(FiddlerApplication_BeforeResponse);

            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
            #endregion AttachEventListeners

            Fiddler.CONFIG.IgnoreServerCertErrors = true;
            FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", false);

            FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
            int iPort = 0;
            Fiddler.FiddlerApplication.Startup(iPort, oFCSF);

            oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iSecureEndpointPort, true, sSecureEndpointHostname);
            if (null != oSecureEndpoint)
            {
                WriteCommandResponse("success!");
            }

            bool bDone = false;
            do
            {
                Console.WriteLine("\nEnter h or q:");
                Console.Write(">");
                ConsoleKeyInfo cki = Console.ReadKey();
                Console.WriteLine();
                switch (cki.KeyChar)
                {
                    case 'q':
                    case 'Q':
                        bDone = true;
                        DoQuit();
                        break;
                }
            } while (!bDone);
        }

        static void FiddlerApplication_BeforeResponse(Fiddler.Session oSession)
        {
            if (oSession.isHTTPS)
            {
                string hostname = oSession.hostname;
                int stateCode = oSession.oResponse.headers.HTTPResponseCode;
                string pathAndQuery = oSession.PathAndQuery;
                //获取服务器返回的html
                string body = oSession.GetResponseBodyAsString();
                //修改body
                body = "二十有为";
                oSession.utilDecodeResponse();
                oSession.utilSetResponseBody(body);
            }
            else
            {
                string body = oSession.GetResponseBodyAsString();
            }
        }

        private static bool SetMachineTrust(X509Certificate2 oRootCert)
        {
            try
            {
                System.Security.Cryptography.X509Certificates.X509Store certStore = new System.Security.Cryptography.X509Certificates.X509Store(StoreName.Root, StoreLocation.LocalMachine);
                certStore.Open(OpenFlags.ReadWrite);
                try
                {
                    certStore.Add(oRootCert);
                }
                finally
                {
                    certStore.Close();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            DoQuit();
        }

        private static void WriteCommandResponse(string s)
        {
            ConsoleColor oldColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(s);
            Console.ForegroundColor = oldColor;
        }

        private static void DoQuit()
        {
            WriteCommandResponse("Shutting down...");
            if (null != oSecureEndpoint) oSecureEndpoint.Dispose();
            Fiddler.FiddlerApplication.Shutdown();
            Thread.Sleep(500);
        }

    }
}

 

Demo下载地址:https://pan.baidu.com/s/1_s7ywb3O6zuMto5SDH4Jng

提取码:czun

标签:Fiddler,Console,System,fiddlercore,static,https,using,http,oS
From: https://www.cnblogs.com/youzilong202/p/17102912.html

相关文章

  • 695~696 HTTP请求消息。请求 行,头,体
     HTTP请求消息数据格式:1.请求行请求行请求url请求协议/版本GET/login.html/HTTP/1.1请求方式HTTP协议有七种申......
  • python中aiohttp库如何理解
    1、aiohttp是一个基于asyncio模块的异步HTTP客户端/服务端框架。2、如无需保留请求的对话状态,请求将通过aiohttp.request直接发送回复。3、aiohttp使用字典、list传递参数或......
  • 693~694servlet_urlpartten配置 AND HTTP概述
    Servlet相关配置1.Urlpartten:Servlet访问路径1.一个Servlet可以定义多个访问路径:@WebServlet({"/访问1","/"访问2,"/访问3"})2.路径定义的规则:......
  • HTTP协议调试工具汇总 AnyProxy whistle
     Java1.BurpSuite英国PortSwigger团队开发,用起来很顺手,安全行业占有率很高,闭源、收费。最近又出了款企业版:独特的办公环境:2.Charles俗称花瓶,Mac下前端工程师......
  • httprunner 生成allure报告
    一、环境准备python3.7.7httprunner3.1.4安装的时候,会自动安装pytest和pytest-html插件,对应的版本号是pytest5.4.3pytest-html2.1.1HTTPrunner 默认并未安装 al......
  • 002-从mavenhttps配置到https基础
    1.idea配置mavenhttpsidea配置maven默认不进行https校验Build,Execution.Deployment-->BuildTools-->Maven-->Importing:VMoptionsforimporter增加-D......
  • http - 三次握手四次挥手
     通俗地讲:三次握手第一次握手:客户端要和服务端进行通信,首先要告知服务端一声,遂发出一个SYN=1的连接请求信号,”服务端,我想给你发送数据”。第二次握手:当服务端接收到......
  • httprunner 基本使用
    一、Httprunner简单介绍httprunner是一个面向https协议的接口自动化测试框架。它只需要维护一份json,yaml文件那么就可以实现自动化测试,性能测试,线上监控,持续集成。HttpRu......
  • http学习
    TCP/IPTCP/IP:互联网相关联的协议集合起来总称为TCP/IP。也有说法认为,TCP/IP是指TCP和IP这两种协议。还有一种说法认为,TCP/IP是在IP协议的通信过程中,使用到的协议族的统称......
  • Jmeter 解决 NoHttpResponseException: ip:80 failed to respond
      在 /Users/chenjun/jmeter/apache-jmeter-5.5/bin/jmeter.properties:修改一下httpclient4.retrycount=100httpclient4.request_sent_retry_enabled=trueht......