-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBurpToExd.cs
407 lines (359 loc) · 14.6 KB
/
BurpToExd.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/******************************************************************
* Licensed Materials - Property of HCL
* (c) Copyright HCL Technologies Ltd. 2015, 2021.
* Note to U.S. Government Users Restricted Rights.
******************************************************************/
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Watchfire.HttpProxy;
using Watchfire.InfraTypes;
using Watchfire.ScanTypes;
using Watchfire.ScanTypes.Utility;
using ProxyLite;
namespace BurpTrafficImporter
{
class BurpToExd
{
private ExdWriter _exdWriter;
public BurpToExd()
{
}
public void Convert(string sourceFile, string targetFile)
{
Stream sourceStream = new FileStream(sourceFile, FileMode.Open);
Stream targetStream = new FileStream(targetFile, FileMode.Create);
Convert(sourceStream, targetStream);
sourceStream.Close();
targetStream.Close();
}
public void Convert(string sourceFile, Stream targetStream)
{
Stream sourceStream = new FileStream(sourceFile, FileMode.Open);
Convert(sourceStream, targetStream);
sourceStream.Close();
}
public void Convert(Stream sourceStream, Stream targetStream)
{
_exdWriter = new ExdWriter(new StreamWriter(targetStream));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sourceStream);
XmlNodeList items = xmlDoc.SelectNodes("items/item");
if (items != null)
{
int index = 0;
WFCookieContainer cookiesContainer = new WFCookieContainer();
foreach (XmlNode item in items)
{
XmlNode requestNode = item.SelectSingleNode("request");
XmlNode responseNode = item.SelectSingleNode("response");
XmlNode urlNode = item.SelectSingleNode("url");
if (requestNode == null || responseNode == null || urlNode == null)
{
continue;
}
MemoryStream request = new MemoryStream();
ToMemoryStream(requestNode, ref request);
MemoryStream response = new MemoryStream();
ToMemoryStream(responseNode, ref response);
string url = urlNode.InnerText;
RecordedProxyData data = GetProxyData(request, response, url, index, cookiesContainer);
_exdWriter.AddRequest(data);
index++;
}
}
_exdWriter.Close();
}
private static void ToMemoryStream(XmlNode node, ref MemoryStream stream)
{
if (node.Attributes != null && node.Attributes["base64"].Value == "true")
{
byte[] decoded = System.Convert.FromBase64String(node.InnerText);
stream.Write(decoded, 0, decoded.Length);
}
else
{
byte[] decoded = Encoding.UTF8.GetBytes(node.InnerText);
stream.Write(decoded, 0, decoded.Length);
}
}
private RecordedProxyData GetProxyData(MemoryStream request, MemoryStream response, string url, int requestIndex, WFCookieContainer cookieContainer)
{
HttpRequestInfo requestInfo = new HttpRequestInfo();
// STD-10727
// Adding check to determine if the request is secure or not
requestInfo.IsSecure = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
TrafficResponseInfo responseInfo = new TrafficResponseInfo();
ByteString requestBS = new ByteString(request.GetBuffer(), 0, (int)request.Position);
requestInfo.ParseRequest(requestBS, url, true);
responseInfo.AddResponseChunk(response.GetBuffer(), (int)response.Position);
HttpParameterList parameters = RequestParameterParser.ParseParametersFromRequest(requestInfo);
// parse all the variables from the request
//HttpParameterList parameters = Proxy.RequestParameterParser.ParseParametersFromRequest(requestInfo, postData);
Uri uri = ConstructUrl(requestInfo);
WFCookieContainer reqCookies = new WFCookieContainer();
try
{
List<SiteCookie> siteCookies = reqCookies.AddCookies(uri, requestInfo.Headers);
List<SiteCookie> responseCookies = null;
if (cookieContainer != null)
{
responseCookies = cookieContainer.GetCookies(uri);
}
reqCookies = new WFCookieContainer();
foreach (SiteCookie cookie in siteCookies)
{
bool found = false;
if (responseCookies != null)
{
foreach (SiteCookie respCookie in responseCookies)
{
if (respCookie.Name.UnicodeString == cookie.Name.UnicodeString)
{
found = true;
respCookie.SetValue(cookie.Value);
reqCookies.Add(new SiteCookie(respCookie));
break;
}
}
}
if (!found)
{
reqCookies.Add(cookie);
}
}
}
catch
{
//
}
try
{
if (cookieContainer != null)
{
cookieContainer.AddCookies(uri, responseInfo.Headers);
}
}
catch
{//
}
WFCookieContainer respCookies = new WFCookieContainer();
try
{
respCookies.AddCookies(uri, responseInfo.Headers);
if (cookieContainer != null)
{
cookieContainer.AddCookies(uri, responseInfo.Headers);
}
}
catch
{//
}
//Construct response header
string contentTypeHeader = responseInfo.ContentType;
byte[] responseBinary = new byte[responseInfo.BodyLength];
Array.Copy(response.GetBuffer(), responseInfo.BodyIndex, responseBinary, 0, responseInfo.BodyLength);
Encoding responseEnc= Encoding.UTF8;
//Construct response header
if (responseInfo.Headers.ContainsKey(HeaderList.TransferEncodingHeaderName) && responseInfo.Headers[HeaderList.TransferEncodingHeaderName].Contains("chunked"))
{
responseInfo.Headers[HeaderList.TransferEncodingHeaderName].RemoveAll(value => value != null && value.Equals("chunked"));
if (responseInfo.Headers[HeaderList.TransferEncodingHeaderName].Count == 0)
{
responseInfo.Headers.Remove(HeaderList.TransferEncodingHeaderName);
}
responseInfo.Headers.Set(HeaderList.HeaderContentLength, responseBinary.Length.ToString());
}
string responseHeader = responseInfo.StatusLine + "\n" + responseInfo.Headers;
RecordedProxyData data = new RecordedProxyData(requestInfo.Method, uri.AbsoluteUri, requestInfo.RequestEncoding,
responseEnc, responseHeader, responseInfo.StatusCode, reqCookies, respCookies)
{
Ordinal = requestIndex,
Request = requestBS,
Parameters = parameters,
ResponseBinary = responseBinary
};
return data;
}
/// <summary>
/// Construct URI for the current request
/// </summary>
/// <returns>URI object for the current request</returns>
public static Uri ConstructUrl(HttpRequestInfo requestInfo)
{
// Build the URL to save with Browsed data
UriBuilder builder = new UriBuilder { Host = requestInfo.Host };
if (!requestInfo.IsDefaultPort)
builder.Port = requestInfo.Port;
builder.Path = requestInfo.Path;
if (requestInfo.IsSecure)
builder.Scheme = "https";
return builder.Uri;
}
class TrafficResponseInfo
{
public TrafficResponseInfo()
{
Headers = new HeaderList();
BodyIndex = 0;
}
public void AddResponseChunk(byte[] responseChunck, int length)
{
int eoh = responseChunck.FindEndOfHttpHeader();
if (eoh != -1)
{
BodyIndex = eoh + 4;
BodyLength = length - BodyIndex;
int lineIndex = responseChunck.FindEndOfHttpHeaderLine(0);
StatusLine = Encoding.UTF8.GetString(responseChunck, 0, lineIndex);
int tmp = StatusLine.IndexOf(" ", 9, StringComparison.Ordinal);
StatusCode = int.Parse(StatusLine.Substring(9, tmp - 9));
lineIndex += 2;
int lastIndex = lineIndex;
lineIndex = responseChunck.FindEndOfHttpHeaderLine(lineIndex);
while (lastIndex != lineIndex)
{
string headerLine = Encoding.UTF8.GetString(responseChunck, lastIndex, lineIndex - lastIndex);
int headerIndex = headerLine.IndexOf(":", StringComparison.InvariantCulture);
Headers.Add(headerLine.Substring(0, headerIndex), headerLine.Substring(headerIndex + 1).TrimStart());
lineIndex += 2;
lastIndex = lineIndex;
lineIndex = responseChunck.FindEndOfHttpHeaderLine(lineIndex);
}
}
}
public int BodyIndex { get; private set; }
public int BodyLength { get; private set; }
public HeaderList Headers { get; private set; }
public string StatusLine { get; private set; }
public int StatusCode { get; private set; }
public string ContentType
{
get
{
StringList ct = Headers["Content-Type"];
if (ct != null && ct.Count > 0)
{
return ct[0].ToLower();
}
else
{
return string.Empty;
}
}
}
}
}
public static class CodeExtensionsFunctions
{
/// <summary>
///
/// </summary>
/// <param name="byteArr"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static int FindEndOfHttpHeaderLine(this byte[] byteArr, int startIndex)
{
bool slashR = false;
int length = byteArr.Length;
int offset = -1;
for (int i = startIndex; i < length; i++)
{
if (byteArr[i] == '\r')
{
slashR = true;
continue;
}
else if (byteArr[i] == '\n')
{
if (slashR)
{
offset = i - 1;
break;
}
}
slashR = false;
}
return offset;
}
/// <summary>
///
/// </summary>
/// <param name="byteArr"></param>
/// <returns></returns>
public static int FindEndOfHttpHeader(this byte[] byteArr)
{
int state = 0;
int length = byteArr.Length;
int offset = -1;
for (int i = 0; i < length; i++)
{
if (byteArr[i] == '\r')
{
state++;
continue;
}
else if (byteArr[i] == '\n' && state > 0)
{
state++;
if (state == 4)
{
offset = i - 3;
break;
}
continue;
}
state = 0;
}
return offset;
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="substr"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static IEnumerable<int> AllIndexesOf(this string str, string substr, bool ignoreCase = false)
{
if (string.IsNullOrWhiteSpace(str) || string.IsNullOrWhiteSpace(substr))
{
yield break;
}
int index = 0;
while ((index = str.IndexOf(substr, index, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) != -1)
{
yield return index;
index += 1;
}
}
/// <summary>
///
/// </summary>
/// <param name="strArr"></param>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <param name="sepeartor"></param>
/// <returns></returns>
public static string Join(this string[] strArr, int startIndex = 0, int length = -1, string sepeartor = null)
{
StringBuilder sb = new StringBuilder();
bool sepExist = string.IsNullOrEmpty(sepeartor);
if (length == -1)
{
length = strArr.Length;
}
for (int i = startIndex; i < length; i++)
{
sb.Append(strArr[i]);
if (sepExist)
{
sb.Append(sepeartor);
}
}
return sb.ToString();
}
}
}