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;
}
}