-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbro2csv.py
executable file
·64 lines (45 loc) · 2 KB
/
bro2csv.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
#!/usr/bin/python
import os
import glob
import ntpath
import argparse
from parsebrologs import ParseBroLogs
parser = argparse.ArgumentParser(description='Translate bro logs (TSV) to CSV')
parser.add_argument('-i', '--input', dest='input', nargs = '+', required=True, metavar='./bro_logs/http.log', type=str, help='Specific bro log path - individual file or directory. Must be .log')
parser.add_argument('-f', '--fields', type=str, nargs = '+', metavar='host', default=[], required=False, help='Bro output fields')
parser.add_argument('--overwrite', action='store_true', required=False, help='Overwrite any existing files')
parser.add_argument('--stdo', action='store_true', required=False, help='Print to standard out (as csv)')
parser.add_argument('--cwd', action='store_true', required=False, help='Save files to the current working directory instead of beside original files.')
parser.add_argument('--glob', type=str, default='*.log', metavar='"*.log"', required=False, help='Glob for bro logs. Must be quoted, e.g. "*.log" - can\'t be used when the input is a file (obviously...)')
args = parser.parse_args()
files = []
for i in args.input:
if os.path.isdir(i):
files += glob.glob('{}{}'.format(i, args.glob))
else:
files.append(i)
for f in files:
if args.cwd:
new_file = ntpath.basename(f)
else:
new_file = '{}.csv'.format(str(f))
try:
if len(args.fields) == 0:
log_data = ParseBroLogs(f)
else:
log_data = ParseBroLogs(f, fields=args.fields)
# print(dir(log_data))
# exit()
except Exception as e:
print('Error ({}): {}'.format(e,f))
continue
if args.stdo:
print(log_data.to_escaped_csv(safe_headers=True))
else:
if (os.path.isfile(new_file) and args.overwrite) or (not os.path.isfile(new_file)):
print('Parsing: {}'.format(f))
with open(new_file, 'w') as outfile:
outfile.write(log_data.to_escaped_csv(safe_headers=True))
print('Written: {}.csv'.format(f))
else:
print('Not overwriting: {}'.format(new_file))