-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBurpExtender.java
More file actions
322 lines (262 loc) · 11 KB
/
BurpExtender.java
File metadata and controls
322 lines (262 loc) · 11 KB
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
package burp;
import java.awt.Component;
import java.util.ArrayList;
import java.util.*;
import java.io.PrintWriter;
import java.security.MessageDigest;
import javax.xml.bind.DatatypeConverter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class BurpExtender implements IBurpExtender, IMessageEditorTabFactory, IScannerInsertionPointProvider
{
private IBurpExtenderCallbacks callbacks;
private IExtensionHelpers helpers;
private PrintWriter stdout;
//
// implement IBurpExtender
//
@Override
public void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks)
{
// obtain our output stream
stdout = new PrintWriter(callbacks.getStdout(), true);
// keep a reference to our callbacks object
this.callbacks = callbacks;
// obtain an extension helpers object
helpers = callbacks.getHelpers();
// set our extension name
callbacks.setExtensionName("Base64 custom scanner");
// register ourselves as a message editor tab factory
callbacks.registerMessageEditorTabFactory(this);
// register ourselves as a scanner insertion point provider
callbacks.registerScannerInsertionPointProvider(this);
}
//
// implement IMessageEditorTabFactory
//
@Override
public IMessageEditorTab createNewInstance(IMessageEditorController controller, boolean editable)
{
// create a new instance of our custom editor tab
return new Base64InputTab(controller, editable);
}
//
// class implementing IMessageEditorTab
//
class Base64InputTab implements IMessageEditorTab
{
private boolean editable;
private ITextEditor txtInput;
private byte[] currentMessage;
public Base64InputTab(IMessageEditorController controller, boolean editable)
{
this.editable = editable;
// create an instance of Burp's text editor, to display our deserialized data
txtInput = callbacks.createTextEditor();
txtInput.setEditable(editable);
}
//
// implement IMessageEditorTab
//
@Override
public String getTabCaption()
{
return "Base64 values";
}
@Override
public Component getUiComponent()
{
return txtInput.getComponent();
}
@Override
public boolean isEnabled(byte[] content, boolean isRequest)
{
// enable this tab for requests containing a data parameter
return isRequest && null != helpers.getRequestParameter(content, "data");
}
@Override
public void setMessage(byte[] content, boolean isRequest)
{
if (content == null)
{
// clear our display
txtInput.setText(null);
txtInput.setEditable(false);
}
else
{
// retrieve the data parameter
IParameter parameter = helpers.getRequestParameter(content, "data");
// deserialize the parameter value
txtInput.setText(helpers.base64Decode(helpers.urlDecode(parameter.getValue())));
txtInput.setEditable(editable);
}
// remember the displayed content
currentMessage = content;
}
@Override
public byte[] getMessage()
{
// determine whether the user modified the deserialized data
if (txtInput.isTextModified())
{
// reserialize the data
byte[] text = txtInput.getText();
String input = helpers.urlEncode(helpers.base64Encode(text));
// update the request with the new parameter value
return helpers.updateParameter(currentMessage, helpers.buildParameter("data", input, IParameter.PARAM_BODY));
}
else return currentMessage;
}
@Override
public boolean isModified()
{
return txtInput.isTextModified();
}
@Override
public byte[] getSelectedData()
{
return txtInput.getSelectedText();
}
}
@Override
public List<IScannerInsertionPoint> getInsertionPoints(IHttpRequestResponse baseRequestResponse)
{
// retrieve the data parameter
IParameter dataParameter = helpers.getRequestParameter(baseRequestResponse.getRequest(), "data");
if (dataParameter == null)
return null;
// if the parameter is present, add a single custom insertion point for it
List<IScannerInsertionPoint> insertionPoints = new ArrayList<IScannerInsertionPoint>();
String strData=helpers.bytesToString(helpers.base64Decode(helpers.urlDecode(dataParameter.getValue())));
String[] strParameters=strData.split("&");
for(int i=0;i<strParameters.length;i++)
{
stdout.println("Creating insertion point:"+i+" from parameter "+strParameters[i].toString());
insertionPoints.add(new InsertionPoint(baseRequestResponse.getRequest(), strData, strParameters[i]));
}
return insertionPoints;
}
//
// class implementing IScannerInsertionPoint
//
private class InsertionPoint implements IScannerInsertionPoint
{
private byte[] baseRequest;
private String insertionPointPrefix;
private String baseValue;
private String insertionPointSuffix;
private String initialMessageParams;
private String initialParamValue;
private String name;
InsertionPoint(byte[] baseRequest, String allparams,String dataParameter)
{
this.baseRequest = baseRequest;
this.initialMessageParams=allparams;
this.initialParamValue=dataParameter;
this.name=dataParameter.replace("=", "");
// split the name/value pair
int start = dataParameter.indexOf("=") + 1;
insertionPointPrefix = dataParameter.substring(0, start);
int end = dataParameter.length();
baseValue = dataParameter.substring(start, end);
insertionPointSuffix = dataParameter.substring(end, dataParameter.length());
stdout.println("-----------------------------------");
stdout.println("Insertion Point created\n");
stdout.println("insertionPointPrefix="+insertionPointPrefix);
stdout.println("baseValue="+baseValue);
stdout.println("insertionPointSuffix="+insertionPointSuffix);
stdout.println("-----------------------------------");
}
//
// implement IScannerInsertionPoint
//
@Override
public String getInsertionPointName()
{
return this.name+"Base64-wrapped input";
}
@Override
public String getBaseValue()
{
return baseValue;
}
@Override
public byte[] buildRequest(byte[] payload)
{
//SUPPOSE THAT THE BACKEND RECEIVES A DATA PARAM AND ITS MD5DIGEST PARAM
// build the raw data using the provided payload
String newParamValue = insertionPointPrefix + "#CUSTOM-INJECTION#" + helpers.bytesToString(payload) + insertionPointSuffix;
String newPayload = this.initialMessageParams.replace(this.initialParamValue, newParamValue);
stdout.println(newPayload);
// Base64- and URL-encode the data
String newparam_base64=helpers.base64Encode(newPayload);
String md5Hash="";
//RECOMPUTE THE MD5 DIGEST
byte[] finalReq=this.baseRequest;
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(newparam_base64.getBytes());
byte[] digest = md.digest();
md5Hash = DatatypeConverter.printHexBinary(digest).toUpperCase();
finalReq=helpers.addParameter(finalReq, helpers.buildParameter("md5digest", md5Hash, IParameter.PARAM_BODY));
}
catch(Exception e)
{
stdout.println(e.toString());
}
/*RECOMPUTE THE HMAC-SHA512 HASH
in live cases you may need to retrieve the secret key dynamically from the server with a web call
*/
try
{
String hmacdigest=this.hmacDigest(newparam_base64, "thesecretkey", "HmacSHA512");
finalReq=helpers.addParameter(finalReq, helpers.buildParameter("hmac-sha512", hmacdigest, IParameter.PARAM_BODY));
stdout.println("md5:"+md5Hash+" HMAC: "+hmacdigest+" payload: "+newparam_base64);
}
catch(Exception e)
{
stdout.println(e.toString());
}
newPayload= helpers.urlEncode(newparam_base64);
return helpers.updateParameter(finalReq, helpers.buildParameter("data", newPayload, IParameter.PARAM_BODY));
}
public String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (UnsupportedEncodingException e) {
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
}
return digest;
}
@Override
public int[] getPayloadOffsets(byte[] payload)
{
// since the payload is being inserted into a serialized data structure, there aren't any offsets
// into the request where the payload literally appears
return null;
}
@Override
public byte getInsertionPointType()
{
return INS_EXTENSION_PROVIDED;
}
}
}