-
Notifications
You must be signed in to change notification settings - Fork 9
/
FiddlerCapture.cs
244 lines (192 loc) · 7.44 KB
/
FiddlerCapture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fiddler;
namespace WebSurge
{
public partial class FiddlerCapture : Form
{
private const string Separator = "------------------------------------------------------------------";
private UrlCaptureConfiguration CaptureConfiguration { get; set; }
bool isFirstSslRequest;
public FiddlerCapture()
{
InitializeComponent();
CaptureConfiguration = new UrlCaptureConfiguration(); // this usually comes from configuration settings
}
private void FiddlerCapture_Load(object sender, EventArgs e)
{
tbIgnoreResources.Checked = CaptureConfiguration.IgnoreResources;
txtCaptureDomain.Text = CaptureConfiguration.CaptureDomain;
UpdateButtonStatus();
try
{
var processes = Process.GetProcesses().OrderBy(p => p.ProcessName);
foreach (var process in processes)
{
txtProcessId.Items.Add(process.ProcessName + " - " + process.Id);
}
}
catch { }
}
private void FiddlerApplication_AfterSessionComplete(Session sess)
{
// Ignore HTTPS connect requests
if (sess.RequestMethod == "CONNECT")
return;
if (CaptureConfiguration.ProcessId > 0)
{
if (sess.LocalProcessID != 0 && sess.LocalProcessID != CaptureConfiguration.ProcessId)
return;
}
if (!string.IsNullOrEmpty(CaptureConfiguration.CaptureDomain))
{
if (sess.hostname.ToLower() != CaptureConfiguration.CaptureDomain.Trim().ToLower())
return;
}
if (CaptureConfiguration.IgnoreResources)
{
string url = sess.fullUrl.ToLower();
var extensions = CaptureConfiguration.ExtensionFilterExclusions;
foreach (var ext in extensions)
{
if (url.Contains(ext))
return;
}
var filters = CaptureConfiguration.UrlFilterExclusions;
foreach (var urlFilter in filters)
{
if (url.Contains(urlFilter))
return;
}
}
if (sess == null || sess.oRequest == null || sess.oRequest.headers == null)
return;
string headers = sess.oRequest.headers.ToString();
var reqBody = Encoding.UTF8.GetString(sess.RequestBody);
// if you wanted to capture the response
//string respHeaders = session.oResponse.headers.ToString();
//var respBody = Encoding.UTF8.GetString(session.ResponseBody);
// replace the HTTP line to inject full URL
string firstLine = sess.RequestMethod + " " + sess.fullUrl + " " + sess.oRequest.headers.HTTPVersion;
int at = headers.IndexOf("\r\n");
if (at < 0)
return;
headers = firstLine + "\r\n" + headers.Substring(at + 1);
string output = headers + "\r\n" +
(!string.IsNullOrEmpty(reqBody) ? reqBody + "\r\n" : string.Empty) +
Separator + "\r\n\r\n";
// must marshal to UI thread
BeginInvoke(new Action<string>((text) =>
{
txtCapture.AppendText(text);
UpdateButtonStatus();
}), output);
}
void Start()
{
if (tbIgnoreResources.Checked)
CaptureConfiguration.IgnoreResources = true;
else
CaptureConfiguration.IgnoreResources = false;
string strProcId = txtProcessId.Text;
if (strProcId.Contains('-'))
strProcId = strProcId.Substring(strProcId.IndexOf('-') + 1).Trim();
strProcId = strProcId.Trim();
int procId = 0;
if (!string.IsNullOrEmpty(strProcId))
{
if (!int.TryParse(strProcId, out procId))
procId = 0;
}
CaptureConfiguration.ProcessId = procId;
CaptureConfiguration.CaptureDomain = txtCaptureDomain.Text;
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.Startup(8888, true, true, true);
}
void Stop()
{
FiddlerApplication.AfterSessionComplete -= FiddlerApplication_AfterSessionComplete;
if (FiddlerApplication.IsStarted())
FiddlerApplication.Shutdown();
}
public static bool InstallCertificate()
{
if (!CertMaker.rootCertExists())
{
if (!CertMaker.createRootCert())
return false;
if (!CertMaker.trustRootCert())
return false;
}
return true;
}
public static bool UninstallCertificate()
{
if (CertMaker.rootCertExists())
{
if (!CertMaker.removeFiddlerGeneratedCerts(true))
return false;
}
return true;
}
private void ButtonHandler(object sender, EventArgs e)
{
if (sender == tbCapture)
Start();
else if (sender == tbStop)
Stop();
else if (sender == tbSave)
{
var diag = new SaveFileDialog()
{
AutoUpgradeEnabled = true,
CheckPathExists = true,
DefaultExt = "txt",
Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*",
OverwritePrompt = false,
Title = "Save Fiddler Capture File",
RestoreDirectory = true
};
var res = diag.ShowDialog();
if (res == DialogResult.OK)
{
if (File.Exists(diag.FileName))
File.Delete(diag.FileName);
File.WriteAllText(diag.FileName, txtCapture.Text);
}
}
else if (sender == tbClear)
{
txtCapture.Text = string.Empty;
}
else if (sender == btnInstallSslCert)
{
Cursor = Cursors.WaitCursor;
InstallCertificate();
Cursor = Cursors.Default;
}
else if (sender == btnUninstallSslCert)
UninstallCertificate();
UpdateButtonStatus();
}
private void FiddlerCapture_FormClosing(object sender, FormClosingEventArgs e)
{
Stop();
}
public void UpdateButtonStatus()
{
tbCapture.Enabled = !FiddlerApplication.IsStarted();
tbStop.Enabled = !tbCapture.Enabled;
tbSave.Enabled = txtCapture.Text.Length > 0;
tbClear.Enabled = tbSave.Enabled;
btnInstallSslCert.Enabled = !CertMaker.rootCertExists();
btnUninstallSslCert.Enabled = !btnInstallSslCert.Enabled;
CaptureConfiguration.IgnoreResources = tbIgnoreResources.Checked;
}
}
}