-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathBurpExtender.java
585 lines (483 loc) · 19.2 KB
/
BurpExtender.java
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package burp;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import javax.swing.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.*;
import java.util.concurrent.*;
import static burp.Keysmith.getHtmlKeys;
import static burp.Keysmith.getWords;
public class BurpExtender implements IBurpExtender, IExtensionStateListener {
private static final String name = "Param Miner";
private static final String version = "1.07";
private ThreadPoolExecutor taskEngine;
@Override
public void registerExtenderCallbacks(final IBurpExtenderCallbacks callbacks) {
new Utilities(callbacks);
BlockingQueue<Runnable> tasks;
if (Utilities.globalSettings.getBoolean("enable auto-mine")) {
tasks = new PriorityBlockingQueue<>(1000, new RandomComparator());
}
else {
tasks = new LinkedBlockingQueue<>();
}
taskEngine = new ThreadPoolExecutor(Utilities.globalSettings.getInt("thread pool size"), Utilities.globalSettings.getInt("thread pool size"), 10, TimeUnit.MINUTES, tasks);
Utilities.globalSettings.registerListener("thread pool size", value -> {
Utilities.out("Updating active thread pool size to "+value);
try {
taskEngine.setCorePoolSize(Integer.parseInt(value));
taskEngine.setMaximumPoolSize(Integer.parseInt(value));
} catch (IllegalArgumentException e) {
taskEngine.setMaximumPoolSize(Integer.parseInt(value));
taskEngine.setCorePoolSize(Integer.parseInt(value));
}
});
callbacks.setExtensionName(name);
try {
StringUtils.isNumeric("1");
} catch (java.lang.NoClassDefFoundError e) {
Utilities.out("Failed to import the Apache Commons Lang library. You can get it from http://commons.apache.org/proper/commons-lang/");
throw new NoClassDefFoundError();
}
try {
callbacks.getHelpers().analyzeResponseVariations();
} catch (java.lang.NoSuchMethodError e) {
Utilities.out("This extension requires Burp Suite Pro 1.7.10 or later");
throw new NoSuchMethodError();
}
ParamGrabber paramGrabber = new ParamGrabber(taskEngine);
callbacks.registerContextMenuFactory(new OfferParamGuess(callbacks, paramGrabber, taskEngine));
if(Utilities.isBurpPro()) {
callbacks.registerScannerCheck(new GrabScan(paramGrabber));
}
callbacks.registerHttpListener(paramGrabber);
callbacks.registerProxyListener(paramGrabber);
SwingUtilities.invokeLater(new ConfigMenu());
Utilities.callbacks.registerExtensionStateListener(this);
Utilities.out("Loaded " + name + " v" + version);
Utilities.out(" CACHE_ONLY "+Utilities.CACHE_ONLY);
}
public void extensionUnloaded() {
Utilities.log("Aborting all attacks");
Utilities.unloaded.set(true);
taskEngine.getQueue().clear();
taskEngine.shutdown();
}
}
class Fuzzable extends CustomScanIssue {
private final static String DETAIL =
"A unlinked input was identified, based on the following evidence. " +
"Response attributes that only stay consistent in one probe-set are italicised, with the variable attribute starred.";
private final static String REMEDIATION = "This issue does not necessarily indicate a vulnerability; it is merely highlighting behaviour worthy of manual investigation. Try to determine the root cause of the observed behaviour." +
"Refer to <a href='http://blog.portswigger.net/2016/11/backslash-powered-scanning-hunting.html'>Backslash Powered Scanning</a> for further details and guidance interpreting results. ";
Fuzzable(IHttpRequestResponse[] requests, URL url, String title, String detail, boolean reliable, String severity) {
super(requests[0].getHttpService(), url, requests, title, DETAIL + detail, severity, calculateConfidence(reliable), REMEDIATION);
}
private static String calculateConfidence(boolean reliable) {
String confidence = "Tentative";
if (reliable) {
confidence = "Firm";
}
return confidence;
}
}
class CustomScanIssue implements IScanIssue {
private IHttpService httpService;
private URL url;
private IHttpRequestResponse[] httpMessages;
private String name;
private String detail;
private String severity;
private String confidence;
private String remediation;
CustomScanIssue(
IHttpService httpService,
URL url,
IHttpRequestResponse[] httpMessages,
String name,
String detail,
String severity,
String confidence,
String remediation) {
this.name = name;
this.detail = detail;
this.severity = severity;
this.httpService = httpService;
this.url = url;
this.httpMessages = httpMessages;
this.confidence = confidence;
this.remediation = remediation;
}
CustomScanIssue(
IHttpService httpService,
URL url,
IHttpRequestResponse httpMessages,
String name,
String detail,
String severity,
String confidence,
String remediation) {
this.name = name;
this.detail = detail;
this.severity = severity;
this.httpService = httpService;
this.url = url;
this.httpMessages = new IHttpRequestResponse[1];
this.httpMessages[0] = httpMessages;
this.confidence = confidence;
this.remediation = remediation;
}
@Override
public URL getUrl() {
return url;
}
@Override
public String getIssueName() {
return name;
}
@Override
public int getIssueType() {
return 0;
}
@Override
public String getSeverity() {
return severity;
}
@Override
public String getConfidence() {
return confidence;
}
@Override
public String getIssueBackground() {
return null;
}
@Override
public String getRemediationBackground() {
return null;
}
@Override
public String getIssueDetail() {
return detail;
}
@Override
public String getRemediationDetail() {
return remediation;
}
@Override
public IHttpRequestResponse[] getHttpMessages() {
return httpMessages;
}
@Override
public IHttpService getHttpService() {
return httpService;
}
public String getHost() {
return null;
}
public int getPort() {
return 0;
}
public String getProtocol() {
return null;
}
}
class RequestWithOffsets {
private byte[] request;
private int[] offsets;
public RequestWithOffsets(byte[] request, int[] offsets) {
this.request = request;
this.offsets = offsets;
}
}
class ParamInsertionPoint implements IScannerInsertionPoint {
byte[] request;
String name;
String value;
byte type;
ParamInsertionPoint(byte[] request, String name, String value, byte type) {
this.request = request;
this.name = name;
this.value = value;
this.type = type;
}
String calculateValue(String unparsed) {
return unparsed;
}
@Override
public String getInsertionPointName() {
return name;
}
@Override
public String getBaseValue() {
return value;
}
@Override
public byte[] buildRequest(byte[] payload) {
IParameter newParam = Utilities.helpers.buildParameter(name, Utilities.encodeParam(Utilities.helpers.bytesToString(payload)), type);
return Utilities.helpers.updateParameter(request, newParam);
}
@Override
public int[] getPayloadOffsets(byte[] payload) {
//IParameter newParam = Utilities.helpers.buildParameter(name, Utilities.encodeParam(Utilities.helpers.bytesToString(payload)), type);
return new int[]{0, 0};
//return new int[]{newParam.getValueStart(), newParam.getValueEnd()};
}
@Override
public byte getInsertionPointType() {
return type;
//return IScannerInsertionPoint.INS_PARAM_BODY;
// return IScannerInsertionPoint.INS_EXTENSION_PROVIDED;
}
}
class ParamNameInsertionPoint extends ParamInsertionPoint {
String attackID;
String defaultPrefix;
String host;
HashMap<String, String> present;
ParamNameInsertionPoint(byte[] request, String name, String value, byte type, String attackID) {
super(request, name, value, type);
this.attackID = attackID;
ArrayList<String> keys = Keysmith.getAllKeys(request, new HashMap<>());
HashMap<String, Integer> freq = new HashMap<>();
for (String key: keys) {
if (key.contains(":")) {
String object = key.split(":")[0];
freq.put(object, freq.getOrDefault(object, 0) + 1);
}
}
String maxKey = null;
if (Utilities.globalSettings.getBoolean("auto-nest params")) {
int max = 0;
for (Map.Entry<String, Integer> entry : freq.entrySet()) {
if (entry.getValue() > max) {
maxKey = entry.getKey();
max = entry.getValue();
}
}
}
defaultPrefix = maxKey;
if (maxKey != null) {
Utilities.out("Selected default key: "+maxKey);
}
else {
Utilities.log("No default key available");
}
present = new HashMap<>();
List<String> headers = Utilities.helpers.analyzeRequest(request).getHeaders();
for (String header: headers) {
if (header.startsWith("Host: ")) {
host = header.split(": ", 2)[1];
}
header = header.split(": ", 2)[0];
present.put(header.toLowerCase(), header);
}
}
String calculateValue(String unparsed) {
return Utilities.toCanary(unparsed) + attackID + value + Utilities.fuzzSuffix();
}
@Override
public byte[] buildRequest(byte[] payload) {
String bulk = Utilities.helpers.bytesToString(payload);
String[] params = bulk.split("[|]");
ArrayList<String> preppedParams = new ArrayList<>();
for(String key: params) {
if (defaultPrefix != null && !key.contains(":")) {
key = defaultPrefix + ":" + key;
}
preppedParams.add(Keysmith.unparseParam(key));
}
if(type == IParameter.PARAM_URL || type == IParameter.PARAM_BODY || type == IParameter.PARAM_COOKIE || type == Utilities.PARAM_HEADER) {
return buildBulkRequest(preppedParams);
}
return buildBasicRequest(preppedParams);
}
public byte[] buildBulkRequest(ArrayList<String> params) {
String merged = prepBulkParams(params);
String replaceKey = "TCZqBcS13SA8QRCpW";
IParameter newParam = Utilities.helpers.buildParameter(replaceKey, "", type);
byte[] built = Utilities.helpers.updateParameter(request, newParam);
return Utilities.fixContentLength(Utilities.replace(built, Utilities.helpers.stringToBytes(replaceKey+"="), Utilities.helpers.stringToBytes(merged)));
}
String prepBulkParams(ArrayList<String> params) {
ArrayList<String> preppedParams = new ArrayList<>();
String equals;
String join;
String trail;
if(type == IParameter.PARAM_COOKIE) {
equals = "=";
join = "; ";
trail = ";";
}
else if (type == Utilities.PARAM_HEADER) {
equals = ": ";
join ="\r\n";
trail = ""; // \r\n
}
else {
equals = "=";
join = "&";
trail = "";
}
for (String param: params) {
String fullParam[] = getValue(param);
if ("".equals(fullParam[0])) {
continue;
}
preppedParams.add(Utilities.encodeParam(fullParam[0]) + equals + Utilities.encodeParam(fullParam[1]));
}
return String.join(join, preppedParams) + trail;
}
String[] getValue(String name) {
if (name.contains("~")) {
String[] parts = name.split("~", 2);
parts[1] = parts[1].replace("%s", calculateValue(name));
parts[1] = parts[1].replace("%h", host);
return new String[]{parts[0], String.valueOf(Utilities.invert(parts[1]))};
}
else {
return new String[]{name, calculateValue(name)};
}
}
byte[] buildBasicRequest(ArrayList<String> params) {
byte[] built = request;
for (String name: params) {
String[] param = getValue(name);
IParameter newParam = Utilities.helpers.buildParameter(param[0], Utilities.encodeParam(param[1]), type);
built = Utilities.helpers.updateParameter(built, newParam);
}
return built;
}
}
class HeaderNameInsertionPoint extends ParamNameInsertionPoint {
public HeaderNameInsertionPoint(byte[] request, String name, String value, byte type, String attackID) {
super(request, name, value, type, attackID);
}
public byte[] buildBulkRequest(ArrayList<String> params) {
String merged = prepBulkParams(params);
String replaceKey = "TCZqBcS13SA8QRCpW";
byte[] built = Utilities.addOrReplaceHeader(request, replaceKey, "foo");
if (params.isEmpty() || "".equals(merged)) {
return built;
}
Iterator<String> dupeCheck= params.iterator();
while (dupeCheck.hasNext()) {
String param = dupeCheck.next().split("~", 2)[0];
if (present.containsKey(param)) {
String toReplace = present.get(param)+": ";
built = Utilities.replace(built, toReplace.getBytes(), ("old"+toReplace).getBytes());
}
}
return Utilities.setHeader(built, replaceKey, "x\r\n"+merged);
}
}
class JsonParamNameInsertionPoint extends ParamInsertionPoint {
byte[] headers;
byte[] body;
String baseInput;
String attackID;
JsonElement root;
public JsonParamNameInsertionPoint(byte[] request, String name, String value, byte type, String attackID) {
super(request, name, value, type); // Utilities.encodeJSON(value)
int start = Utilities.getBodyStart(request);
this.attackID = attackID;
headers = Arrays.copyOfRange(request, 0, start);
body = Arrays.copyOfRange(request, start, request.length);
baseInput = Utilities.helpers.bytesToString(body);
root = new JsonParser().parse(baseInput);
}
private Object makeNode(ArrayList<String> keys, int i, Object paramValue) {
if (i+1 == keys.size()) {
return paramValue;
}
else if (Utilities.parseArrayIndex(keys.get(i+1)) != -1) {
return new ArrayList(Utilities.parseArrayIndex(keys.get(i+1)));
}
else {
return new HashMap();
}
}
String calculateValue(String unparsed) {
return Utilities.toCanary(unparsed) + attackID + value + Utilities.fuzzSuffix();
}
@Override
@SuppressWarnings("unchecked")
public byte[] buildRequest(byte[] payload) throws RuntimeException {
String[] params = Utilities.helpers.bytesToString(payload).split("[|]");
String lastBuild = baseInput;
try {
for (String unparsed: params) {
Object paramValue;
if (unparsed.contains("~")) {
String[] parts = unparsed.split("~", 2);
unparsed = parts[0];
paramValue = Utilities.invert(parts[1]);
} else {
paramValue = calculateValue(unparsed);
}
ArrayList<String> keys = new ArrayList<>(Arrays.asList(unparsed.split(":")));
boolean isArray = Utilities.parseArrayIndex(keys.get(0)) != -1;
Object base;
if (isArray) {
try {
base = new ObjectMapper().readValue(lastBuild, ArrayList.class);
}
catch (MismatchedInputException e) {
base = new ArrayList();
}
} else {
try {
base = new ObjectMapper().readValue(lastBuild, HashMap.class);
}
catch (MismatchedInputException e) {
base = new HashMap();
}
}
Object next = base;
for (int i = 0; i < keys.size(); i++) {
try {
String key = keys.get(i);
boolean setValue = i + 1 == keys.size();
int index = Utilities.parseArrayIndex(key);
if (index != -1) {
ArrayList injectionPoint = (ArrayList) next;
if (injectionPoint.size() < index + 1) {
for (int k = injectionPoint.size(); k < index; k++) {
injectionPoint.add(Utilities.generateCanary());
}
injectionPoint.add(makeNode(keys, i, paramValue));
} else if (injectionPoint.get(index) == null || setValue) {
injectionPoint.set(index, makeNode(keys, i, paramValue));
}
next = injectionPoint.get(index);
} else {
HashMap injectionPoint = (HashMap) next;
if (!injectionPoint.containsKey(key) || setValue) {
injectionPoint.put(key, makeNode(keys, i, paramValue));
}
next = injectionPoint.get(key);
}
} catch(ClassCastException e) {
//Utilities.out("Cast error"); // todo figure out a sensible action to stop this form occuring
}
}
lastBuild = new ObjectMapper().writeValueAsString(base);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(headers);
outputStream.write(Utilities.helpers.stringToBytes(lastBuild));
return Utilities.fixContentLength(outputStream.toByteArray());
} catch (Exception e) {
Utilities.out("Error with " + String.join(":", params));
e.printStackTrace(new PrintStream(Utilities.callbacks.getStdout()));
return buildRequest(Utilities.helpers.stringToBytes("error_" + String.join(":", params).replace(":", "_")));
// throw new RuntimeException("Request creation unexpectedly failed: "+e.getMessage());
}
}
}