forked from mwielgoszewski/jython-burp-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathburp_extender.py
More file actions
667 lines (512 loc) · 19.9 KB
/
burp_extender.py
File metadata and controls
667 lines (512 loc) · 19.9 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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# -*- coding: utf-8 -*-
'''
BurpExtender
~~~~~~~~~~~~
BurpExtender is a proxied class that implements the burp.IBurpExtender
interface. It is what makes Jython <-> Burp possible.
'''
from java.io import File
from java.lang import AbstractMethodError
from java.net import URL
from org.python.util import JLineConsole, PythonInterpreter
from burp import IBurpExtender, IMenuItemHandler
from threading import Thread
import inspect
import json
import logging
import os
import re
import signal
import site
import sys
import weakref
# Patch dir this file was loaded from into the path
# (Burp doesn't do it automatically)
sys.path.append(os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe()))))
from gds.burp import HttpRequest
from gds.burp.config import Configuration, ConfigSection
from gds.burp.core import Component, ComponentManager
from gds.burp.decorators import callback
from gds.burp.dispatchers import NewScanIssueDispatcher, PluginDispatcher
from gds.burp.monitor import PluginMonitorThread
import gds.burp.settings as settings
logging._srcfile = None
logging.logThreads = 0
logging.logProcesses = 0
class BurpExtender(IBurpExtender, ComponentManager):
_components = ConfigSection('components', '')
_menus = ConfigSection('menus', '')
def __init__(self):
ComponentManager.__init__(self)
self.log = logging.getLogger(self.__class__.__name__)
self.monitoring = {}
def __repr__(self):
return '<BurpExtender at %#x>' % (id(self), )
def __iter__(self):
for request in self.getProxyHistory():
yield request
def _monitor_item(self, obj):
# don't monitor objects initialized in the interpreter
if obj.__module__ == '__main__':
return
mod = obj.__module__
cls = obj.__class__.__name__
# Monitor the actual configuration file rather than the
# module the Configuration class is defined in
if isinstance(obj, Configuration):
filename = obj.filename
elif isinstance(obj, (Component, IMenuItemHandler)):
filename = inspect.getsourcefile(obj.__class__)
elif isinstance(obj, type):
filename = inspect.getsourcefile(obj)
monitoring = self.monitoring.setdefault(filename, [])
monitoring.append({
'class': cls,
'instance': weakref.ref(obj),
'module': mod,
})
return
def componentActivated(self, component):
self.log.debug('Activating component: %r', component)
component.burp = self
component.config = self.config
component.log = self.log
return
def applicationClosing(self):
'''
This method is invoked immediately before Burp Suite exits.
'''
self.log.debug('Shutting down Burp')
return
def registerExtenderCallbacks(self, callbacks):
'''
This method is invoked on startup.
'''
self._callbacks = callbacks
try:
self.setExtensionName(self.getExtensionName())
except Exception:
pass
try:
log_filename = self.loadExtensionSetting(*settings.LOG_FILENAME)
log_format = self.loadExtensionSetting(*settings.LOG_FORMAT)
log_level = self.loadExtensionSetting(*settings.LOG_LEVEL)
self.log.setLevel(log_level)
fileHandler = logging.FileHandler(
log_filename, encoding='utf-8', delay=True)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter(fmt=log_format)
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)
self.log.addHandler(fileHandler)
self.log.addHandler(streamHandler)
self._handler = fileHandler
except Exception:
self.log.exception('Could not load extension logging settings')
try:
config = self.loadExtensionSetting(*settings.CONFIG_FILENAME)
self.config = Configuration(os.path.abspath(config))
except Exception:
self.log.exception('Could not load extension config settings')
try:
from gds.burp.listeners import PluginListener, \
SaveConfigurationOnUnload, \
ScannerListener
SaveConfigurationOnUnload(self)
PluginListener(self)
ScannerListener(self)
except Exception:
self.log.exception('Could not load extension listener')
try:
from gds.burp.ui import ConsoleTab
self._console_tab = ConsoleTab(self)
self.console = self._console_tab.interpreter
except Exception as e:
self.log.exception('Could not load console tab')
for module, _ in self._menus.options():
if self._menus.getbool(module) is True:
for menu in _get_menus(module):
menu(self)
for component, _ in self._components.options():
if self._components.getbool(component) is True:
_get_plugins(component)
self._monitor_item(self.config)
self.monitor = PluginMonitorThread(self)
self.monitor.start()
self.issueAlert('Burp extender ready...')
return
def _check_cb(self):
if hasattr(self, '_callbacks'):
return getattr(self, '_callbacks')
def _check_and_callback(self, method, *args):
cb = self._check_cb()
if not hasattr(cb, method.__name__):
raise Exception("%s() not available in your version of Burp" % (
method.__name__, ))
try:
return getattr(cb, method.__name__)(*args)
except AbstractMethodError:
raise Exception("%s() not available in your version of Burp" % (
method.__name__, ))
cb = property(_check_cb)
@callback
def makeHttpRequest(self, host, port, useHttps, request):
return
@callback
def sendToRepeater(self, host, port, useHttps, request, tabCaption):
return
@callback
def sendToIntruder(self, host, port, useHttps, request, *args):
return
def sendToSpider(self, url):
if not self.isInScope(url):
self.includeInScope(url)
self._check_and_callback(self.sendToSpider, URL(str(url)))
return
@callback
def doActiveScan(self, host, port, useHttps, request, *args):
return
@callback
def doPassiveScan(self, host, port, useHttps, request, response):
return
@callback
def getScanIssues(self, urlPrefix):
return
def registerMenuItem(self, menuItemCaption, menuItemHandler):
'''
This method can be used to register a new menu item which
will appear on the various context menus that are used
throughout Burp Suite to handle user-driven actions.
:param menuItemCaption: The caption to be displayed on the
menu item.
:param menuItemHandler: The handler to be invoked when the
user clicks on the menu item.
'''
self._monitor_item(menuItemHandler)
self._check_and_callback(
self.registerMenuItem, menuItemCaption, menuItemHandler)
return
def newScanIssue(self, issue):
'''
This method is invoked whenever Burp Scanner discovers a new,
unique issue, and can be used to perform customised reporting
or logging of issues.
Plugins should implement the :meth:`~INewScanIssueHandler.newScanIssue`
method of the :class:`INewScanIssueHandler` interface to act upon
new scan issues as they are identified.
:param issue: Details of the new scan issue.
'''
return NewScanIssueDispatcher(self).newScanIssue(issue)
def processHttpMessage(self, toolName, messageIsRequest, messageInfo):
'''
This method is invoked whenever any of Burp's tools makes an HTTP
request or receives a response. It allows extensions to intercept
and modify the HTTP traffic of all Burp tools. For each request,
the method is invoked after the request has been fully processed
by the invoking tool and is about to be made on the network. For
each response, the method is invoked after the response has been
received from the network and before any processing is performed
by the invoking tool.
Plugins should implement the :meth:`processRequest` and/or
:meth:`processResponse` methods of one or more interfaces in
:module:`gds.burp.api`.
A plugin may implement more than one interface, and implement both
`processRequest` and `processResponse` methods. This allows plugins
to only hook certain tools in specific scenarios, such as "only
hook requests sent via Intruder or Scanner, and only hook responses
received via Proxy tool.
An example is provided below that only modifies requests as they
are made via Repeater and Intruder.
.. code-block:: python
class MyPlugin(Component):
implements(IIntruderRequestHandler, IRepeaterRequestHandler)
def processRequest(self, request):
# replace all occurrences of 'somestring' in HTTP
# request with 'anotherstring'.
request.raw = request.raw.replace('somestring',
'anotherstring')
'''
return PluginDispatcher(self).processHttpMessage(
toolName, messageIsRequest, messageInfo)
def getProxyHistory(self, *args):
'''
This method returns a generator of all items in the proxy history.
:params *args: Optional strings to match against url.
'''
if args:
matchers = [re.compile(arg) for arg in args]
for request in self._check_and_callback(self.getProxyHistory):
for matcher in matchers:
if matcher.search(request.getUrl().toString()):
yield HttpRequest(request, _burp=self)
break
else:
for request in self._check_and_callback(self.getProxyHistory):
yield HttpRequest(request, _burp=self)
history = property(lambda burp: list(burp.getProxyHistory()))
@callback
def addToSiteMap(self, item):
return
def getSiteMap(self, *urlPrefixes):
'''
This method returns a generator of details of items in the site map.
:params *urlPrefixes: Optional URL prefixes, in order to extract
a specific subset of the site map. The method performs a simple
case-sensitive text match, returning all site map items whose URL
begins with the specified prefix. If this parameter is null,
the entire site map is returned.
'''
for urlPrefix in urlPrefixes or ('http', ):
for item in self._check_and_callback(self.getSiteMap, urlPrefix):
yield HttpRequest(item, _burp=self)
def excludeFromScope(self, url):
self._check_and_callback(self.excludeFromScope, URL(str(url)))
return
def includeInScope(self, url):
self._check_and_callback(self.includeInScope, URL(str(url)))
return
def isInScope(self, url):
return self._check_and_callback(self.isInScope, URL(str(url)))
@callback
def issueAlert(self, message):
'''
This method can be used to display a specified message in
the Burp Suite alerts tab.
:param message: The alert message to display.
'''
return
def restoreState(self, filename):
'''
This method can be used to restore Burp's state from a
specified saved state file.
:param filename: The filename containing Burp's saved state.
'''
return self._check_and_callback(self.restoreState, File(filename))
def saveState(self, filename):
'''
This method can be used to save Burp's state to a specified
file. This method blocks until the save operation is completed,
and must not be called from the event thread.
:param filename: The filename to save Burp's state in.
'''
return self._check_and_callback(self.saveState, File(filename))
@callback
def loadConfig(self, config):
'''
This method causes Burp to load a new configuration from a
dictionary of key/value pairs provided. Any settings not
specified in the dict will be restored to their default values.
To selectively update only some settings and leave the rest
unchanged, you should first call saveConfig to obtain Burp's
current configuration, modify the relevant items in the dict,
and then call loadConfig with the same dict.
:param config: A dict of key/value pairs to use as Burp's new
configuration.
'''
return
def saveConfig(self):
'''
This method causes Burp to return its current configuration
as a dictionary of key/value pairs.
'''
return dict(self._check_and_callback(self.saveConfig))
@callback
def setProxyInterceptionEnabled(self, enabled):
'''
This method sets the interception mode for Burp Proxy.
:param enabled: Indicates whether interception of proxy messages
should be enabled.
'''
return
def getBurpVersion(self):
'''
This method retrieves information about the version of Burp
in which the extension is running. It can be used by extensions
to dynamically adjust their behavior depending on the
functionality and APIs supported by the current version.
'''
return list(self._check_and_callback(self.getBurpVersion))
version = property(getBurpVersion)
def exitSuite(self, promptUser=False):
'''
This method can be used to shut down Burp programmatically,
with an optional prompt to the user. If the method returns,
the user cancelled the shutdown prompt.
:param promptUser: Indicates whether to prompt the user to
confirm the shutdown (default is False: no prompt).
'''
if promptUser is True:
return self._check_and_callback(self.exitSuite, True)
return self._check_and_callback(self.exitSuite, False)
@callback
def addScanIssue(self, issue):
'''
This method is used to register a new Scanner issue.
Note: Wherever possible, extensions should implement custom
Scanner checks using IScannerCheck and report issues via those
checks, so as to integrate with Burp's user-driven workflow,
and ensure proper consolidation of duplicate reported issues.
This method is only designed for tasks outside of the normal
testing workflow, such as importing results from other scanning
tools.
:param issue: An object created by the extension that implements
the IScanIssue interface.
'''
return
@callback
def addSuiteTab(self, tab):
return
@callback
def applyMarkers(self, request, requestMarkers=None, responseMarkers=None):
return
@callback
def createMessageEditor(self, controller, editable):
return
@callback
def createTextEditor(self):
return
@callback
def customizeUiComponent(self, component):
return
@callback
def getHelpers(self):
return
helpers = property(lambda burp: burp.getHelpers())
@callback
def getStderr(self):
return
stderr = property(lambda burp: burp.getStderr())
@callback
def getStdout(self):
return
stdout = property(lambda burp: burp.getStdout())
@callback
def getToolName(self, toolFlag):
return
@callback
def registerContextMenuFactory(self, factory):
return
@callback
def registerExtensionStateListener(self, listener):
return
@callback
def registerHttpListener(self, listener):
return
@callback
def registerIntruderPayloadGeneratorFactory(self, factory):
return
@callback
def registerIntruderPayloadProcessor(self, processor):
return
@callback
def registerMessageEditorTabFactory(self, factory):
return
@callback
def registerProxyListener(self, listener):
return
@callback
def registerScannerCheck(self, check):
return
@callback
def registerScannerInsertionPointProvider(self, provider):
return
@callback
def registerScannerListener(self, listener):
return
@callback
def registerSessionHandlingAction(self, action):
return
@callback
def removeSuiteTab(self, tab):
return
@callback
def saveBuffersToTempFiles(self, request):
return
@callback
def saveToTempFile(self, buffer):
return
@callback
def setExtensionName(self, name):
return
def getExtensionName(self):
return self.loadExtensionSetting(*settings.EXTENSION_NAME)
def loadExtensionSetting(self, name, default=None):
if name.startswith('jython.'):
settings = self._check_and_callback(self.loadExtensionSetting,
'settings')
if settings:
settings = json.loads(settings)
return settings.get(name, default)
return default
value = self._check_and_callback(self.loadExtensionSetting, name)
if not value and default is not None:
return default
return value
def saveExtensionSetting(self, name, value):
if name.startswith('jython.'):
settings = self._check_and_callback(self.loadExtensionSetting,
'settings')
if settings:
settings = json.loads(settings)
else:
settings = {}
settings[name] = value
self._check_and_callback(self.saveExtensionSetting,
'settings', json.dumps(settings))
return
self._check_and_callback(self.saveExtensionSetting, name, value)
return
class ConsoleThread(Thread):
def __init__(self, console):
Thread.__init__(self, name='jython-console')
self.console = console
def run(self):
while True:
try:
self.console.interact()
except Exception:
pass
def _get_menus(menu_module):
module = menu_module.split('.')
klass = module.pop()
try:
m = __import__('.'.join(module), globals(), locals(), module[-1])
except ImportError:
logging.exception('Could not import module %s', '.'.join(module))
return []
if klass == '*':
menus = []
for name, obj in inspect.getmembers(m):
if name == 'MenuItem':
continue
if inspect.isclass(obj) and IMenuItemHandler in inspect.getmro(obj):
menus.append(obj)
return menus
try:
return [getattr(m, klass)]
except AttributeError:
logging.exception('Could not import %s from module %s',
klass, '.'.join(module))
return []
def _get_plugins(plugin_module):
module = plugin_module.split('.')
klass = module.pop()
if klass == '*':
to_import = module[-1:]
else:
to_import = [klass]
try:
__import__('.'.join(module), globals(), locals(), to_import)
except ImportError:
logging.exception('Could not import %s from module %s',
', '.join(to_import), '.'.join(module))
return
def _sigbreak(signum, frame):
'''
Don't do anything upon receiving ^C. Require user to actually exit
via Burp, preventing them from accidentally killing Burp from the
interactive console.
'''
pass
signal.signal(signal.SIGINT, _sigbreak)