-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auto-Highlighter.py
439 lines (339 loc) · 13.6 KB
/
Auto-Highlighter.py
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
from burp import IBurpExtender
from burp import IHttpListener
from burp import IContextMenuFactory
from burp import ITab
from java.awt.event import ActionListener
from javax.swing import JPanel
from javax.swing import JLabel
from javax.swing import JComboBox
from javax.swing import Box
from javax.swing import JMenu
from javax.swing import JMenuItem
from collections import OrderedDict
import sys
import re
import zlib
import ast
# TODO
# Setting for keying off URL and verb only
# Setting for normalizing parameter names by removing digits (Ctl01, etc.)
# Burp looks for a class called BurpExtender to instantiate (with no constructor parameters) and then calls registerExtenderCallbacks() on this object passing in a "callbacks" object.
class BurpExtender(IBurpExtender, IHttpListener, IContextMenuFactory, ITab):
# Implement IBurpExtender
def registerExtenderCallbacks(self, callbacks):
# Keep a reference to our callbacks object
self._callbacks = callbacks
# Obtain an extension helpers object
self._helpers = callbacks.getHelpers()
# Connect standard out/err to callback to Burp standard out buffer
sys.stdout = callbacks.getStdout()
sys.stderr = callbacks.getStderr()
# Set our extension name
callbacks.setExtensionName("Auto-Highlighter")
# Register proxy listener to intercept proxy requests
callbacks.registerHttpListener(self)
# Registers ContextMenu Factory
callbacks.registerContextMenuFactory(self)
self.combocolors = [
"red",
"green",
"blue",
"orange",
"yellow",
"pink",
"gray",
"magenta",
"cyan",
]
self.colors = {
"Triaged": "red",
"Interesting": "blue",
"Finding": "cyan",
"Ignore": "gray",
}
self.proxyLookBackLimit = 25000
self.contextMenuKeys = FixSizeOrderedDict()
# Add suite tab (This breaks if not at the end!)
callbacks.addSuiteTab(self)
print("Extension loaded")
print("By Michael Maturi (amarionette) @ Mandiant")
if self._callbacks.loadExtensionSetting("keys"):
self.keys = ast.literal_eval(self._callbacks.loadExtensionSetting("keys"))
else:
self.keys = {}
def getTabCaption(self):
return "Auto-Highlighter"
def getUiComponent(self):
panel = JPanel()
# Create layout structure
boxVertical = Box.createVerticalBox()
boxHorizontal = Box.createHorizontalBox()
boxHorizontal1 = Box.createHorizontalBox()
boxHorizontal2 = Box.createHorizontalBox()
boxHorizontal3 = Box.createHorizontalBox()
boxHorizontal4 = Box.createHorizontalBox()
# Create main UI elements
jlabel = JLabel("Auto-Highlighter Settings")
jlabel1 = JLabel("Triaged Highlight Color")
jlabel2 = JLabel("Interesting Highlight Color")
jlabel3 = JLabel("Finding Highlight Color")
jlabel4 = JLabel("Ignore Highlight Color")
combo = JComboDropDown("Triaged", self).combobox
combo2 = JComboDropDown("Interesting", self).combobox
combo3 = JComboDropDown("Finding", self).combobox
combo4 = JComboDropDown("Ignore", self).combobox
# Add vertical box to panel
panel.add(boxVertical)
boxVertical.add(boxHorizontal)
boxVertical.add(boxHorizontal1)
boxVertical.add(boxHorizontal2)
boxVertical.add(boxHorizontal3)
boxVertical.add(boxHorizontal4)
# Add horizontal boxes to vertical box
boxHorizontal.add(jlabel)
boxHorizontal1.add(jlabel1)
boxHorizontal1.add(combo)
boxHorizontal2.add(jlabel2)
boxHorizontal2.add(combo2)
boxHorizontal3.add(jlabel3)
boxHorizontal3.add(combo3)
boxHorizontal4.add(jlabel4)
boxHorizontal4.add(combo4)
return panel
def keyExists(self,key):
result = False
if key in self.keys:
result = True
return result
def calculateKey(self, baseRequestResponse):
requestInfo = self._helpers.analyzeRequest(baseRequestResponse)
# Handle timed-out requests which will throw an error
if not requestInfo.getUrl():
return None
# Get URL and clean it
method = requestInfo.getMethod()
url = requestInfo.getUrl().toString()
url = self.cleanURL(url)
# Get parameters that are NOT cookies and clean them
parameters = requestInfo.getParameters()
parameters = list(filter(lambda x: (x.getType() != x.PARAM_COOKIE), parameters))
parameterNames = [x.getName().encode("utf-8").strip().lower() for x in parameters]
parameterNamesString = "".join(parameterNames)
# Create a unique identifier using CRC32
key = zlib.crc32(method + url + parameterNamesString)
return key
def removeHiglight(self,key):
history = self._callbacks.getProxyHistory()
for baseRequestResponse in history[self.proxyLookBackLimit:]:
proxyHistoryItemKey = self.calculateKey(baseRequestResponse)
if key == proxyHistoryItemKey:
baseRequestResponse.setHighlight(None)
self.keys.pop(key, None)
return
def doHighlight(self, baseRequestResponse, key):
color = None
# Check all stored keys in 'keys' dictionary (Tag,CRC32Key)
keyValue = self.keys.get(key)[0]
# Look up tag color in colors dictionary, and x-lookup against key's tag to determine color
for tagKey in self.colors:
if tagKey == keyValue:
color = self.colors.get(tagKey)
break
if "Explicit" in keyValue:
color = self.keys.get(key)[1]
baseRequestResponse.setHighlight(color)
def createMenuItems(self, invocation):
items = []
contexts = [
invocation.CONTEXT_MESSAGE_VIEWER_REQUEST,
invocation.CONTEXT_MESSAGE_VIEWER_RESPONSE,
invocation.CONTEXT_MESSAGE_EDITOR_REQUEST,
invocation.CONTEXT_MESSAGE_EDITOR_RESPONSE,
invocation.CONTEXT_PROXY_HISTORY,
invocation.CONTEXT_TARGET_SITE_MAP_TABLE,
invocation.CONTEXT_TARGET_SITE_MAP_TREE,
]
if invocation.getInvocationContext() in contexts:
baseRequestResponse = invocation.getSelectedMessages()[0]
parentMenu = HighlighterParentMenu().menu
key = self.calculateKey(baseRequestResponse)
keyExist = self.keyExists(key)
self.contextMenuKeys[key] = baseRequestResponse
if keyExist:
parentMenu.add(HighlighterRemoveKeyedMenuItem(self, baseRequestResponse, key).menuitem)
else:
parentMenu.add(HighlighterAddKeyedMenuItem(self, baseRequestResponse, key, "Triaged").menuitem)
parentMenu.add(HighlighterAddKeyedMenuItem(self, baseRequestResponse, key, "Interesting").menuitem)
parentMenu.add(HighlighterAddKeyedMenuItem(self, baseRequestResponse, key, "Finding").menuitem)
parentMenu.add(HighlighterAddKeyedMenuItem(self, baseRequestResponse, key, "Ignore").menuitem)
colorMenu = HighlighterAddKeyedMenuItemColor(self, baseRequestResponse, key).menuitem
parentMenu.add(colorMenu)
items.append(parentMenu)
return items
else:
pass
def processHttpMessage(self, toolFlag, messageIsRequest, messageInfo):
if not messageIsRequest:
return
# If tool origin is not proxy, ignore
if toolFlag != self._callbacks.TOOL_PROXY:
return
# Check if a key exists for this item
key = self.calculateKey(messageInfo)
keyExist = self.keyExists(key)
if keyExist:
self.doHighlight(messageInfo, key)
def cleanURL(self, url):
# GUID (Optional Dash), Hashes, Any purely numeric sequence
url = url.split("?")[0]
regexs = [
"{?\w{8}-?\w{4}-?\w{4}-?\w{4}-?\w{12}}?",
"^[a-fA-F0-9]{32}$",
"^[a-fA-F0-9]{40}$",
"^[a-fA-F0-9]{56}$",
"^[a-fA-F0-9]{64}$",
"^[a-fA-F0-9]{96}$",
"^[a-fA-F0-9]{128}$",
"^\d+$",
]
temp = "(?:% s)" % "|".join(regexs)
url = str(url).split("/")
indexes = []
for i, elm in enumerate(url):
if re.match(temp, elm):
indexes.append(i)
for index in sorted(indexes, reverse=True):
url[index] = "<id>"
url = "/".join(url).rstrip("/")
return url
class HighlighterParentMenu(ActionListener):
"""
HighlighterParentMenu creates a new top-level context menu.
"""
def __init__(self):
self.menu = JMenu("Auto-Highlighter")
class HighlighterAddKeyedMenuItem(ActionListener):
"""
HighlighterAddKeyedMenuItem creates a new submenu.
"""
def __init__(
self,
extender,
baseRequestResponse,
key,
tag,
text="Add Highlight From Keyed Parameters",
):
self.extender = extender
self.menuitem = JMenuItem("{0} ({1})".format(text,tag))
self.menuitem.setEnabled(True)
self.menuitem.addActionListener(self)
self.baseRequestResponse = baseRequestResponse
self.key = key
self.tag = tag
def actionPerformed(self, e):
"""
Override the ActionListener method. Usually setup in combination with a menuitem click.
:param e: unused
:return:
"""
self.extender.keys[self.key] = [self.tag]
color = self.extender.colors.get(self.tag)
self.baseRequestResponse.setHighlight(color)
history = self.extender._callbacks.getProxyHistory()
for baseRequestResponse in history[self.extender.proxyLookBackLimit:]:
keyProxyRequest = self.extender.calculateKey(baseRequestResponse)
if self.key == keyProxyRequest:
baseRequestResponse.setHighlight(color)
self.extender._callbacks.saveExtensionSetting("keys", str(self.extender.keys))
class HighlighterAddKeyedMenuItemColor(ActionListener):
"""
HighlighterAddKeyedMenuItem creates a new submenu.
"""
def __init__(
self,
extender,
baseRequestResponse,
key,
text="Add Highlight From Keyed Parameters (Color)",
):
self.extender = extender
self.menuitem = JMenu(text)
self.baseRequestResponse = baseRequestResponse
self.key = key
for color in self.extender.combocolors:
self.submenu = JMenuItem(color)
self.submenu.setEnabled(True)
self.submenu.addActionListener(self)
self.menuitem.add(self.submenu)
self.menuitem.setEnabled(True)
def actionPerformed(self, e):
"""
Override the ActionListener method. Usually setup in combination with a menuitem click.
:param e: unused
:return:
"""
color = e.getSource().getText()
self.extender.keys[self.key] = ["Explicit",color]
self.baseRequestResponse.setHighlight(color)
history = self.extender._callbacks.getProxyHistory()
for baseRequestResponse in history[self.extender.proxyLookBackLimit:]:
keyProxyRequest = self.extender.calculateKey(baseRequestResponse)
if self.key == keyProxyRequest:
baseRequestResponse.setHighlight(color)
self.extender._callbacks.saveExtensionSetting("keys", str(self.extender.keys))
class HighlighterRemoveKeyedMenuItem(ActionListener):
"""
HighlighterRemoveKeyedMenuItem creates a new sub menu.
"""
def __init__(
self,
extender,
baseRequestResponse,
key,
text="Remove Highlight From Keyed Parameters",
):
self.extender = extender
self.menuitem = JMenuItem(text)
self.menuitem.setEnabled(True)
self.menuitem.addActionListener(self)
self.baseRequestResponse = baseRequestResponse
self.key = key
def actionPerformed(self, e):
"""
Override the ActionListener method. Usually setup in combination with a menuitem click.
:param e: unused
:return:
"""
self.extender.removeHiglight(self.key)
self.extender._callbacks.saveExtensionSetting("keys", str(self.extender.keys))
class JComboDropDown(ActionListener):
"""
JComboDropDown creates a new dropdown.
"""
def __init__(self, tag, extender):
self.extender = extender
self.combobox = JComboBox(self.extender.combocolors)
self.combobox.addActionListener(self)
self.tag = tag
# Create combox box color based on saved value or default
setting = self.extender._callbacks.loadExtensionSetting(self.tag)
if setting:
self.combobox.setSelectedItem(setting)
self.extender.colors[self.tag] = setting
else:
self.combobox.setSelectedItem(self.extender.colors.get(self.tag))
def actionPerformed(self, e):
"""
Override the ActionListener method. Usually setup in combination with a menuitem click.
:param e: unused
:return:
"""
selected = self.combobox.getSelectedItem()
self.extender._callbacks.saveExtensionSetting(self.tag, selected)
self.extender.colors[self.tag] = selected
class FixSizeOrderedDict(OrderedDict):
def __setitem__(self, key, value):
OrderedDict.__setitem__(self, key, value)
if len(self) > 50:
self.popitem(False)