forked from cyal1/BurpCollector
/
BurpCollector.py
318 lines (253 loc) · 9.08 KB
/
BurpCollector.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
#!coding:utf-8
"""
======
TEag1e@www.teagle.top
米斯特安全团队@www.hi-ourlife.com
======
"""
import collections
import json
import math
import os
import threading
import time
from burp import IBurpExtender
from burp import IContextMenuFactory
from burp import IExtensionStateListener
from javax.swing import JMenuItem
from MysqlController import MysqlController
__lock__ = threading.Lock()
class BurpExtender(IBurpExtender, IExtensionStateListener, IContextMenuFactory):
def registerExtenderCallbacks(self, callbacks):
# Set extension's name
callbacks.setExtensionName('Burp Collector')
self._callbacks = callbacks
# create Log File
self.createLogFile()
# test the database connection
MysqlController().connectTest()
# retrieve the context menu factories
callbacks.registerContextMenuFactory(self)
# register ourselves as an Extension listener
callbacks.registerExtensionStateListener(self)
t = threading.Thread(target=self.collectPeriodically)
t.setDaemon(True)
t.start()
#
# create log files with current time
#
def createLogFile(self):
# currentTime = Year Month Day Hour Minute
currentTime = time.strftime('%Y%m%d%H%M%S', time.localtime())
# make directory
if not os.path.exists('log'):
os.mkdir('log')
os.chdir('log')
# make log file
self._paramLog = '{}param.log'.format(currentTime)
self._fileLog = '{}file.log'.format(currentTime)
self._pathLog = '{}path.log'.format(currentTime)
if not os.path.exists(self._paramLog):
open(self._paramLog, 'w').close()
if not os.path.exists(self._fileLog):
open(self._fileLog, 'w').close()
if not os.path.exists(self._pathLog):
open(self._pathLog, 'w').close()
#
# implement IContextMenuFactory
#
def createMenuItems(self, invocation):
mainMenu = JMenuItem('History To Log',
actionPerformed=self.menuOnClick)
return [mainMenu]
#
# stores Data when Clicking on Context Menu
#
def menuOnClick(self, event):
self.collectAsync()
#
# implement IExtensionStateListener
#
def extensionUnloaded(self):
self.collectAsync()
def collectPeriodically(self):
while True:
self.collect()
time.sleep(600)
def collectAsync(self):
t = threading.Thread(target=self.collect)
t.setDaemon(True)
t.start()
def collect(self):
with __lock__:
print('[*] collect at ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# extract path,file and param from Proxy History
DataExtractor(self._callbacks, self._pathLog, self._fileLog, self._paramLog)
# stored in MySQL from the log file
MysqlController().coreProcessor(self._pathLog, self._fileLog, self._paramLog)
#
# extract path,file and param from Proxy History
#
class DataExtractor():
def __init__(self, callbacks, pathLog, fileLog, paramLog):
self._callbacks = callbacks
self._helpers = callbacks.getHelpers()
self._pathLog = pathLog
self._fileLog = fileLog
self._paramLog = paramLog
# self._entropyScore = 4.0
with open('../config.ini')as config_f:
self._config = json.load(config_f)
# self._path_max_length = int(self._config.get('options').get('pathMaxLength'))
# self._param_max_length = int(self._config.get('options').get('paramMaxLength'))
# self._file_max_length = int(self._config.get('options').get('fileMaxLength'))
self._entropyScore = self._config.get('options').get('entropy')
self.coreProcessor()
def entropy(self, word):
if word.isdigit() and len(word) > 11:
return 9
inp_str = word
counter_char = collections.Counter(inp_str)
entropy = 0
for c, ctn in counter_char.items():
_p = float(ctn) / len(inp_str)
entropy += -1 * _p * math.log(_p, 2)
return round(entropy, 7)
def coreProcessor(self):
allHistoryMessage = self._callbacks.getProxyHistory()
# define three variable to remove duplicate data
collectionParam = []
collectionFile = []
collectionPath = []
# processing each message
for historyMessage in allHistoryMessage:
httpService = historyMessage.getHttpService()
# invoke host filter
host = httpService.getHost()
if not self.filterHost(host):
continue
requestInfo = self._helpers.analyzeRequest(httpService, historyMessage.getRequest())
path = requestInfo.getUrl().getPath()
path, file = self.formatPathFile(path)
# file to log
# invoke file filter
if self.filterFile(file):
try:
currentFile = '{}\t{}'.format(host, file)
except:
pass
else:
if file and currentFile not in collectionFile:
# print(currentFile)
with open(self._fileLog, 'a')as file_f:
file_f.write(currentFile + '\n')
collectionFile.append(currentFile)
# parameters to log
paramsObject = requestInfo.getParameters()
params = self.processParamsObject(paramsObject)
# UnicodeEncodeError
# invoke param filter
params = self.filterParam(params)
try:
currentParams = '{}\t{}'.format(host, ','.join(params))
except:
pass
else:
if params and currentParams not in collectionParam:
with open(self._paramLog, 'a')as params_f:
params_f.write(currentParams + '\n')
collectionParam.append(currentParams)
# path to log
# invoke path filter
path = self.filterPath(path)
try:
currentPath = '{}\t{}'.format(host, path)
except:
pass
else:
if path and currentPath not in collectionPath:
# print(currentPath)
with open(self._pathLog, 'a')as path_f:
path_f.write(currentPath + '\n')
collectionPath.append(currentPath)
# format path and file
def formatPathFile(self, path):
sepIndex = path.rfind('/')
# EX: http://xxx/?id=1
if path == '/':
file = ''
path = ''
# EX: http://xxx/index.php
# file = index.php
elif sepIndex == 0:
file = path[1:]
path = ''
# EX: http://xxx/x/index.php
# path = /x/
# file = index.php
else:
file = path[sepIndex + 1:]
path = path[:sepIndex + 1]
return path, file
# extractor data from host
def filterHost(self, host):
blackHosts = self._config.get('blackHosts')
for blackHost in blackHosts:
if host.endswith(blackHost):
return
return True
# extractor data from file
def filterFile(self, file):
v = self.entropy(file)
balckPaths = self._config.get('blackExtension')
for blackPath in balckPaths:
if file.lower().endswith(blackPath):
return False
if v > self._entropyScore:
# print("file: {} entropy is {}, ignore".format(file, v))
return False
return True
def filterPath(self, path):
wordlist = path.split("/")[1:-1]
c = 0
for w in wordlist:
v = self.entropy(w)
if v > self._entropyScore:
if c == 0:
return
else:
newpath = '/' + '/'.join(wordlist[0:c]) + '/'
# print("path: {} entropy is {}, extract {}".format(path, v, newpath))
return newpath
c = c + 1
return path
def filterParam(self, params):
newparams = []
for p in params:
e = self.entropy(p)
if p == '':
continue
if e > self._entropyScore:
# print(params)
try:
# print("param: %s entropy is %f, ignore" % (p, e))
pass
except:
# print(p,e)
pass
continue
newparams.append(p)
return newparams
# extract params
def processParamsObject(self, paramsObject):
# get parameters
params = set()
for paramObject in paramsObject:
# don't process Cookie's Pamrams
if paramObject.getType() == 2:
continue
param = paramObject.getName()
if param.startswith('_'):
continue
params.add(param)
return params