-
Notifications
You must be signed in to change notification settings - Fork 2
/
brolog.py
executable file
·428 lines (379 loc) · 14.2 KB
/
brolog.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
#!/usr/bin/env python
import sys,time,os,threading,Queue,datetime,signal,imp,urlparse,pprint,json,csv
"""
### BEGIN CONFIG
"""
indx = []
stph = []
methods = ['GET','POST','HEAD','OPTIONS']
response_codes = ['200','404','302','301','402','403','500']
"""
Used the downloader and thread handler
from: https://github.com/melpomene/I2P-Pastebin-scraper/blob/master/scrape.py
"""
def print_err(*args):
sys.stderr.write(' '.join(map(str,args)) + '\n')
def print_banner():
# Header Start
color = '\033[91m'
endcolor = '\033[0m'
head = ['','brolog.py - poppin collars not boxin',
'ver: 0.99 [beta]','Written by PLXSERT','','Twitter',
'@PLXSERT','Email: plxsert@prolexic.com','To Exit Press Control-C','']
print_err(color)
print_err('#'*60)
for lines in head:
print_err('#'+lines.center(58,' ')+'#')
print_err('#'*60+endcolor+'\n')
# Header end
def exit(message):
sys.exit(message)
def signal_handler(signal, frame):
print_err("Goooh Bye!")
exit(0)
"""
#########
######### Inputer
#########
"""
def add(weblog_line,config):
if config["input"] == 'sys.stdin':
for lines in sys.stdin.readlines():
weblog_line.put(lines)
else:
try:
file = config["input"].strip()
fhandler = open(file ,'ro')
for lines in fhandler.readlines():
weblog_line.put(lines)
fhandler.close()
except:
exit("0 - bad filename, try fullpath")
"""
#########
######### Parser
#########
"""
def parser(weblog_line,output_log,config):
while True:
line = weblog_line.get()
susp_files = ["/indx.php","configic.php","/inedx.php","/define.inc.php",
"/LICENSE.php","/saerch.php","/error.php","/INSTALL.php"
"/index.inc.php","/settings.class.php"]
for files in susp_files:
if line.find(files) != -1:
response = indx(line)
#response should be an dictionary
output_log.put(response)
if line.find("/stph.php") != -1:
response = stph(line)
#response should be an dictionary
output_log.put(response)
if line.find("/stcp.php") != -1:
response = stph(line)
#response should be an dictionary
output_log.put(response)
if line.find("/stcurl.php") != -1:
response = stph(line)
#response should be an dictionary
output_log.put(response)
if line.find("/startphp.php") != -1:
response = stph(line)
#response should be an dictionary
output_log.put(response)
if line.find("/stmdu.php") != -1:
response = stph(line)
#response should be an dictionary
output_log.put(response)
weblog_line.task_done()
def decode_base64(data):
"""Decode base64, padding being optional.
:param data: Base64 data as an ASCII byte string
:returns: The decoded byte string.
stolen from http://stackoverflow.com/questions/6416131/python-add-new-item-to-dictionary
"""
import base64
missing_padding = 4 - len(data) % 4
if missing_padding:
data += b'='* missing_padding
return base64.decodestring(data)
def decode_page(page):
input = page.encode('rot13')
s_str = input[2:]
e_str = input[0:2]
str(s_str)+str(e_str)
decoded = decode_base64(str(s_str)+str(e_str))
return(decoded)
def parse_epoch(timer):
import datetime
date = datetime.datetime.utcfromtimestamp(float(timer))
isodate = date.strftime("%Y%m%dT%H%M%S")
return(isodate)
def indx(line):
lines = line
line = " oops"
for lookups in methods:
if lines.find(lookups) != -1:
try:
status = lines.strip("""\"""").strip()
url = status[status.find(lookups+" /"):]
url = url[:url.find("HTTP")]
url = url.strip()
url = url[len(lookups)+1:]
bas = urlparse.urlparse(url)
urlq = urlparse.parse_qs((bas.query))
path = bas.path
except:
print "Error: ",lines
try:
ip = lines[0:]
ip = ip[:ip.find(" -")]
except:
ip = None
for response in response_codes:
if lines.find(response) != -1:
res = response
break
else:
res = "UNKNOWN"
method = lookups
try:
action = urlq["action"][0]
except:
action = "None"
try:
status = lines.strip("""\"""").strip()
url = status[status.find(method+" /"):]
url = url[:url.find("HTTP")]
url = url.strip()
url = url[len(method)+1:]
if url.find("?") != -1:
url = url[:url.find("?")]
rev_url = url[::-1]
file = rev_url[:rev_url.find("/")][::-1]
else:
rev_url = url[::-1]
file = rev_url[:rev_url.find("/")][::-1]
except:
file="None"
try:
atime = lines[lines.find("["):]
atime = atime[1:atime.find("]")]
except:
atime = None
data = {"req":method, "full":lines, "action":action,
"srcip":ip,"response":res,"path":path,
"file":file,"format":"0","atime":atime,
"full":lines}
return(data)
def stph(lines):
for lookups in methods:
if lines.find(lookups) != -1:
try:
status = lines.strip("""\"""").strip()
url = status[status.find(lookups+" /"):]
url = url[:url.find("HTTP")]
url = url.strip()
url = url[len(lookups)+1:]
bas = urlparse.urlparse(url)
urlq = urlparse.parse_qs((bas.query))
path = bas.path
except:
print "Error: ",lines
try:
ip = lines[0:]
ip = ip[:ip.find(" -")]
except:
ip = None
try:
atime = lines[lines.find("["):]
atime = atime[1:atime.find("]")]
except:
atime = None
for response in response_codes:
if lines.find(response) != -1:
res = response
break
else:
res = "UNKNOWN"
method = lookups
try:
page = urlq["page"][0]
except:
page = "None"
try:
decoded_page = decode_page(urlq["page"][0])
except:
decoded_page = "None"
try:
try:
stime = parse_epoch(urlq["time_s"][0])
except:
stime = urlq["time_s"][0]
except:
stime = "None"
try:
try:
etime = parse_epoch(urlq["time_e"][0])
except:
etime = urlq["time_e"][0]
except:
etime = "None"
try:
action = urlq["action"][0]
except:
action = "None"
try:
status = lines.strip("""\"""").strip()
url = status[status.find(method+" /"):]
url = url[:url.find("HTTP")]
url = url.strip()
url = url[len(method)+1:]
if url.find("?") != -1:
url = url[:url.find("?")]
rev_url = url[::-1]
file = rev_url[:rev_url.find("/")][::-1]
else:
rev_url = url[::-1]
file = rev_url[:rev_url.find("/")][::-1]
except:
file="None"
data = {"page":page,"stime":stime,"etime":etime,
"atime":atime,"req":method, "full":lines,
"srcip":ip,"response":res,"path":path,
"decoded_page":decoded_page,"action":action,
"file":file,"format":"1"}
return(data)
"""
#########
######### Output Formats
#########
"""
def output(output_log,config):
while True:
#pprint.pprint(output_log.get())
#print json.dumps(output_log.get())
output = output_log.get()
if config["json"] is True:
output_json(output,config)
elif config["csv"] is True:
output_csv(output,config)
else:
output_cymru(output,config)
output_log.task_done()
#print("Queue Size is currently " + str(output_log.qsize()))
def output_csv(output,config):
if output["format"] == "0":
#pprint.pprint(output)
line = [output['atime'], output['response'],output['file'],
output['srcip'],output['req'],
str("v" + output['format'])]
print ",".join(line)
if config["write"] is True:
with open(str(str(config["filename"])+"csv-v0.txt"), 'a') as csvfile:
writer = csv.writer(csvfile,delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(line)
if output["format"] == "1":
line = [output['atime'],output['response'],
output['file'],output['srcip'],
output['decoded_page'],output['action'],
output['stime'],output['etime'],
output['req'],str("v" + output['format'])]
print ",".join(line)
if config["write"] is True:
with open(str(str(config["filename"])+"csv-v1.txt"), 'a') as csvfile:
writer = csv.writer(csvfile,delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(line)
def output_json(output,config):
if config["write"] is True:
handler = open(config["filename"]+"json.txt","a")
handler.write(json.dumps(output))
handler.close()
print json.dumps(output)
def output_cymru(output,config):
if output["format"] == "0":
#pprint.pprint(output)
d = " | " #delieter
line = (output['atime'] + d + output['response'] + \
d + output['file'] + d + output['srcip'] + \
d + output['req'] + d + str("v" + output['format']))
if config["write"] is True:
handler = open(config["filename"]+"default.txt","a")
handler.write(line+"\n")
handler.close()
print line
if output["format"] == "1":
d = " | " #delieter
line = (output['atime'] + d + output['response'] + \
d + output['file'] + d + output['srcip'] + \
d + output['decoded_page'] + d + output['action'] + \
d + output['stime'] + d + output['etime'] + \
d + output['req'] + d + str("v" + output['format']))
if config["write"] is True:
handler = open(config["filename"]+"default.txt","a")
handler.write(line+"\n")
handler.close()
print line
def main(config):
#print config
"""spy vs. spy - black spy wins"""
"""print banner"""
print_banner()
ssecs = time.time()
"""Config"""
weblog_line = Queue.Queue()
output_log = Queue.Queue()
signal.signal(signal.SIGINT, signal_handler)
"""setup collectors"""
num_workers = 1
for i in range(num_workers):
w = threading.Thread(target=add, args=(weblog_line,config,))
w.setDaemon(True)
w.start()
"""start the parsers"""
num_workers = 3
for i in range(num_workers):
p = threading.Thread(target=parser, args=(weblog_line,output_log,config,))
p.setDaemon(True)
p.start()
"""setup output module"""
num_workers = 1
for i in range(num_workers):
o = threading.Thread(target=output, args=(output_log,config,))
o.setDaemon(True)
o.start()
signal.pause()
p.join()
o.join()
w.join()
esecs = time.time()
print str(ssecs - esecs) + str(" seconds to complete")
def config(argys):
import argparse
config = {}
if ("-h" or "--help") in argys:
print_banner()
parser = argparse.ArgumentParser(description='Parse logs from compromised brobots' + \
"default input is standard in" + \
" Ex: cat log.txt | python brolog.py -j" + \
"This example would parse log.txt and output json")
parser.add_argument("-j",'--json',help='Output in JSON Format',
required=False,action='store_true')
parser.add_argument("-c",'--csv',help='Output in CSV Format',
required=False, action='store_true')
parser.add_argument("-w",'--write',
help='write output to file -f to set filename " + \
"(defualt filename base is out.brolog-*.txt',
required=False, action='store_true')
parser.add_argument('-i','--input',
help='Input filename if stdin default is not to be used',
required=False,default="sys.stdin",action="store")
parser.add_argument('-f','--filename',
help='Basefile name for outputting requires -w flag to be set',
required=False,default="out.brolog-",action="store")
config = vars(parser.parse_args(argys))
main(config)
if __name__ == '__main__':
config(sys.argv[1:])