-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forensic_data_generation.py
76 lines (64 loc) · 2.28 KB
/
Forensic_data_generation.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
import re
import pandas as pd
def generate_logformat_regex(logformat):
headers = []
splitters = re.split(r'(<[^<>]+>)', logformat)
regex = ''
for k in range(len(splitters)):
if k % 2 == 0:
splitter = re.sub(' +', '\\\s+', splitters[k])
regex += splitter
else:
header = splitters[k].strip('<').strip('>')
regex += '(?P<%s>.*?)' % header
headers.append(header)
regex = re.compile('^' + regex + '$')
return headers, regex
def log_to_dataframe(log_file, regex, headers):
log_messages = []
linecount = 0
with open(log_file, 'r') as fin:
logs = fin.readlines()
logs = [j.strip() for j in logs]
for line in logs:
try:
line = line.strip()
match = regex.search(line.strip())
message = [match.group(header) for header in headers]
log_messages.append(message)
linecount += 1
except Exception as e:
print(e)
pass
logdf = pd.DataFrame(log_messages, columns=headers)
logdf.insert(0, 'LineId', None)
logdf['LineId'] = [i + 1 for i in range(linecount)]
return logdf
def separate_forensic_headers(log_file):
base_log_format = '\[<program_pid>\] : <Content>'
headers, regex = generate_logformat_regex(base_log_format)
df_log = log_to_dataframe(log_file, regex, headers)
possible_headers = {
'Level': '\<<Level>\> <Content>',
'Duration': '\[<Duration>\] <Content>'
}
for i in possible_headers:
log_format = possible_headers[i]
headers, regex = generate_logformat_regex(log_format)
log_messages = []
linecount = 0
for line in df_log['Content']:
try:
match = regex.search(line.strip())
message = [match.group(header) for header in headers]
except Exception as e:
message = ["N/A", line]
finally:
linecount += 1
log_messages.append(message)
logdf = pd.DataFrame(log_messages, columns=headers, index=None)
if i == "Duration":
logdf['LineId'] = [j + 1 for j in range(linecount)]
df_log[i] = logdf[i]
df_log['Content'] = logdf['Content']
return df_log