FiddlerCore是 Telerik 的跨平台 .NET 库,它允许捕获和修改 HTTP/HTTPS 流量。使用 FiddlerCore 的一些最流行的应用程序是 Telerik Fiddler(基于 .NET Framework 并在 Windows 上运行)和 Fiddler Everywhere(基于 .NET Core 并在 Windows、Mac 和 Linux 上运行)。
关键信息:FiddlerCore跨平台.net库,可以捕获http/https
NuGet安装:
界面设计:
启动捕获:
public void startFiddler()
{
if (!FiddlerApplication.IsStarted())
{
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.Startup(8888, true, true, true);
}
else
{
appendtext("fiddler is running...");
}
}
停止捕获:
public void stopFiddler()
{
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}
}
安装证书:
public void install()
{
if (!CertMaker.rootCertExists())
{
CertMaker.createRootCert();
CertMaker.trustRootCert();
}
}
证书移除:
public void remove()
{
if (CertMaker.rootCertExists())
{
CertMaker.removeFiddlerGeneratedCerts();
}
}
两个事件函数:
private void FiddlerApplication_BeforeRequest(Session oSession)
{
if (oSession.RequestMethod == "POST"||oSession.RequestMethod=="GET")
{
string url = oSession.fullUrl;
string body = oSession.GetRequestBodyAsString();
string header = oSession.RequestHeaders.ToString();
appendtext(url);
appendtext(header);
appendtext(body);
}
}
private void FiddlerApplication_AfterSessionComplete(Session oSession)
{
if (oSession.RequestMethod == "POST" || oSession.RequestMethod == "GET")
{
string body = oSession.GetResponseBodyAsString();
appendtext(body);
appendtext(tmp);
}
}
全部代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Fiddler;
namespace Fiddler抓包
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string tmp = "-------------------------------------";
private void btn_start_Click(object sender, EventArgs e)
{
startFiddler();
}
public void startFiddler()
{
if (!FiddlerApplication.IsStarted())
{
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.Startup(8888, true, true, true);
}
else
{
appendtext("fiddler is running...");
}
}
private void FiddlerApplication_BeforeRequest(Session oSession)
{
if (oSession.RequestMethod == "POST"||oSession.RequestMethod=="GET")
{
string url = oSession.fullUrl;
string body = oSession.GetRequestBodyAsString();
string header = oSession.RequestHeaders.ToString();
appendtext(url);
appendtext(header);
appendtext(body);
}
}
private void FiddlerApplication_AfterSessionComplete(Session oSession)
{
if (oSession.RequestMethod == "POST" || oSession.RequestMethod == "GET")
{
string body = oSession.GetResponseBodyAsString();
appendtext(body);
appendtext(tmp);
}
}
public void stopFiddler()
{
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}
}
public void install()
{
if (!CertMaker.rootCertExists())
{
CertMaker.createRootCert();
CertMaker.trustRootCert();
}
}
public void remove()
{
if (CertMaker.rootCertExists())
{
CertMaker.removeFiddlerGeneratedCerts();
}
}
public void appendtext(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action(() =>
{
richTextBox1.AppendText(value + "\r\n");
}));
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
stopFiddler();
}
private void btn_install_Click(object sender, EventArgs e)
{
install();
}
private void btn_remove_Click(object sender, EventArgs e)
{
remove();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
stopFiddler();
if (FiddlerApplication.oProxy != null)
{
if (FiddlerApplication.oProxy.IsAttached)
FiddlerApplication.oProxy.Detach();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.splitContainer1.SplitterDistance = this.Height / 7;
}
}
}
效果图:
文章来源:https://www.toymoban.com/news/detail-525360.html
源码:https://download.csdn.net/download/cyberarmy/87999632文章来源地址https://www.toymoban.com/news/detail-525360.html
到了这里,关于C# 调用FiddlerCore实现http/https抓包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!