-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_analyzer.py
183 lines (171 loc) · 8.32 KB
/
sys_analyzer.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
#!/usr/bin/python
import re
import sys
import colors
import gzip
import rules
import color_rules
import pathlib
def main(logfile, time):
systemClock = "Unknown (not set) "
linuxVersion = "Unknown (not set) "
hostname = "Unknown (not set) "
logs = {}
USBs = {}
dhcpReq = {}
dhcpPack = {}
inputs = {}
networkManagers = {}
openVPN = {}
daemonStart = {}
cronjobs = {}
avahi_dns = {}
firewall = {}
user_specified = {}
user_rule_names = []
pid = ''
try:
log = gzip.open(logfile, 'r') if '.gz' in logfile else open(logfile, 'r', encoding='utf-8')
except FileNotFoundError:
print("Wrong file or file path")
sys.exit(1)
if pathlib.Path('user_rules').is_file():
user_rules = open('user_rules', 'r', encoding='utf-8')
for line in user_rules:
user_rule_names.append(line.strip("\n"))
user_rules.close()
for line in log:
date = rules.DATE(line).group(0)
if (date > time):
date = colors.TIME + date + colors.CEND
if rules.PID(line):
pid = "[pid:" + rules.PID(line).group(2).strip(" ") + "] "
if rules.NETWORK_MANAGER_ELEMENT(line) and rules.NETWORK_MANAGER_FILTER(line):
element = rules.NETWORK_MANAGER_ELEMENT(line).group(3).strip("\n")
msg = rules.NETWORK_MANAGER_ELEMENT(line).group(4).strip("\n")
msg = colorize(color_rules.PATH, colors.PATH, msg)
if element not in networkManagers:
networkManagers[element] = NetworkManagers(element)
networkManagers[element].messages[msg] = date
elif rules.USB(line):
msg = colorize(color_rules.USB_ID, colors.ID, rules.USB(line).group(0).strip("\n"))
msg = colorize(color_rules.EXITING, colors.OKRED, msg)
USBs[pid + msg] = date
elif rules.DHCP_REQUEST(line):
ip_port = colorize(color_rules.IP, colors.IP, rules.DHCP_IP(line).group(2))
if rules.PORT(line):
ip_port += colors.PORT + ":" + rules.PORT(line).group(2) + colors.CEND
dhcpReq[ip_port] = date
elif rules.DHCP_DHCPACK(line):
ip_port = colorize(color_rules.IP, colors.IP, rules.DHCP_IP(line).group(2))
if rules.PORT(line):
ip_port += " " + colors.PORT + rules.PORT(line).group(0) + colors.CEND
dhcpPack[ip_port] = date
elif rules.INPUT(line):
input_ = rules.INPUT(line).group(3).strip("\n")
input_ = colorize(color_rules.PATH, colors.PATH, input_)
inputs[input_] = date
elif rules.OPENVPN(line):
msg = rules.OPENVPN(line).group(0).strip("\n")
openVPN[pid + msg] = date
elif rules.OPENVPN_ERROR(line):
msg = rules.OPENVPN_ERROR(line).group(5).strip("\n")
openVPN[pid + msg] = date
elif rules.DAEMON_START_EXIT(line):
msg = colorize(color_rules.EXITING, colors.OKRED, rules.DAEMON_START_EXIT(line).group(3).strip("\n"))
msg = pid + colorize(color_rules.STARTING, colors.OKGREEN, msg)
daemonStart[msg] = date
elif rules.CRON_JOBS(line):
msg = rules.CRON_JOBS(line).group(0)
cronjobs[date] = msg
elif rules.AVAHI_DNS(line):
msg = rules.AVAHI_DNS(line).group(4).strip("\n")
msg = colorize(color_rules.IP, colors.IP, msg)
msg = colorize(color_rules.STARTING, colors.OKGREEN, msg)
msg = colorize(color_rules.EXITING, colors.OKRED, msg)
avahi_dns[pid + msg] = date
elif rules.FIREWALL(line):
SRC_IP_PORT = rules.FIREWALL(line).group(4) + ":" + rules.FIREWALL(line).group(10)
DST_IP_PORT = rules.FIREWALL(line).group(7) + ":" + rules.FIREWALL(line).group(13)
msg = "BLOCK " + SRC_IP_PORT + " ---> " + DST_IP_PORT
msg = colorize(color_rules.IP, colors.IP, msg)
msg = colorize(color_rules.PORT, colors.ID, msg)
firewall[msg] = date
elif rules.LINUX_VERSION(line):
linuxVersion = rules.LINUX_VERSION(line).group(0)
elif rules.SYSTEM_CLOCK(line):
systemClock = rules.SYSTEM_CLOCK(line).group(3).strip("\n")
elif rules.HOST_NAME(line):
hostname = rules.HOST_NAME(line).group(2).strip("\n")
for rule in user_rule_names:
if rule not in user_specified and rule is not None:
user_specified[rule] = NetworkManagers(rule)
method_to_call = getattr(rules, rule)
result = method_to_call(line)
if result:
msg = result.group(0).strip("\n")
user_specified[rule].messages[date] = msg
log.close()
print("\n" + colors.HEADER + " *** SUMMARY " + logfile + " *** " + colors.CEND + "\n")
print(colors.USER + "[*] System clock " + colors.CEND + "\n" + systemClock + "\n")
print(colors.USER + "[*] Linux version " + colors.CEND + "\n" + linuxVersion + "\n")
print(colors.USER + "[*] Host name " + colors.CEND + "\n" + hostname )
if len(USBs) > 0:
print("\n" + colors.USER + "[*] USB " + colors.CEND)
for usb in sorted(USBs, key=USBs.get, reverse=True):
print(USBs[usb] + " " + transform(r'\[(.*)\]\s', "", usb))
if (len(dhcpReq) > 0):
print("\n" + colors.USER + "[*] DHCP requests (unique) " + colors.CEND)
for ip in sorted(dhcpReq, key=dhcpReq.get, reverse=True):
print (dhcpReq[ip] + " to " + ip )
if (len(dhcpPack) > 0):
print("\n" + colors.USER + "[*] DHCP packs (unique) " + colors.CEND)
for ip in sorted(dhcpPack, key=dhcpPack.get, reverse=True):
print (dhcpPack[ip] + " from "+ ip )
if (len(inputs) > 0):
print("\n" + colors.USER + "[*] Input " + colors.CEND)
for input_ in sorted(inputs, key=inputs.get, reverse=True):
print (inputs[input_] + " " + input_)
if (len(openVPN) > 0):
print("\n" + colors.USER + "[*] OpenVPN " + colors.CEND)
for vpn in sorted(openVPN, reverse=True):
print(openVPN[vpn] + " " + vpn)
if (len(daemonStart) > 0):
print("\n" + colors.USER + "[*] daemon start and exit " + colors.CEND)
for start in sorted(daemonStart, key=daemonStart.get, reverse=True):
print(daemonStart[start] + " " + start)
if (len(cronjobs) > 0 ):
print("\n" + colors.USER + "[*] cron jobs " + colors.CEND)
for msg in sorted(cronjobs, reverse=True):
print(msg + " " + cronjobs[msg])
if (len(avahi_dns) > 0):
print("\n" + colors.USER + "[*] mDNS/DNS-SD (Avahi daemon) " + colors.CEND)
for msg in sorted(avahi_dns, key=avahi_dns.get, reverse=True):
print(avahi_dns[msg]+ " " + msg)
if (len(firewall) > 0):
print("\n" + colors.USER + "[*] Firewall " + colors.CEND)
for msg in sorted(firewall, key=firewall.get, reverse=True):
print(firewall[msg]+ " " + msg)
if len(networkManagers) > 0:
print("\n" + colors.USER + "[*] Network Manager " + colors.CEND)
for nm in networkManagers:
if len(networkManagers[nm].messages) > 0:
print(colors.BOLD + "[+] " + nm + colors.CEND)
for msg in sorted(networkManagers[nm].messages, key=networkManagers[nm].messages.get, reverse=True):
print(networkManagers[nm].messages[msg] + " " + msg)
if len(user_specified) > 0:
for rule in user_specified:
print("\n" + colors.USER + "[*] " + rule + colors.CEND)
if len(user_specified[rule].messages) > 0:
for msg in sorted(user_specified[rule].messages, reverse=True):
print( msg + " " + user_specified[rule].messages[msg])
def colorize(regex, color, text):
return re.sub(regex, lambda m: color+'{}\x1b[0m'.format(m.group()), text)
def transform(regex, exp, text):
return re.sub(regex, lambda m: exp.format(m.group()), text)
class NetworkManagers:
def __init__(self, element):
self.element = None
self.messages = {}
if __name__ == "__main__":
main()