-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.py
78 lines (59 loc) · 2.15 KB
/
events.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
import logger
import psutil
import time
import os
import socket
import subprocess
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
def USBEvents(serverSocket):
oldDevices = psutil.disk_partitions()
logger.log("Listening to partition mount events", serverSocket)
while True:
newDevices = psutil.disk_partitions()
if(len(newDevices) > len(oldDevices)):
logger.log("New Partition Mounted", serverSocket)
elif(len(newDevices) < len(oldDevices)):
logger.log("Partition Unmounted", serverSocket)
oldDevices = newDevices
time.sleep(0.5)
def networkEvents(serverSocket):
logger.log("Listening to network events", serverSocket)
ip = get_ip()
env = dict(os.environ) # make a copy of the environment
lp_key = 'LD_LIBRARY_PATH' # for Linux and *BSD.
lp_orig = env.get(lp_key + '_ORIG')
if lp_orig is not None:
env[lp_key] = lp_orig # restore the original, unmodified value
else:
# This happens when LD_LIBRARY_PATH was not set.
# Remove the env var as a last resort:
env.pop(lp_key, None)
dumpFilter = f"ip and host {ip} and port not 53 and port not 138 and port not 5353 and ((tcp[tcpflags] & tcp-syn != 0) and not udp)"
dumpProcess = subprocess.Popen(("tcpdump", "-l", "-nn", dumpFilter), stdout=subprocess.PIPE, env=env)
for row in iter(dumpProcess.stdout.readline, b''):
log = row.decode("utf8")
tokenisedLog = log.split()
src = tokenisedLog[2]
dest = tokenisedLog[4].rstrip(":")
try:
srcService = socket.getservbyport(int(src.split(".")[-1]))
src = f"{srcService.upper()} {'.'.join(src.split('.')[0:-1])}"
except:
split = src.split(".")
src = ".".join(split[0:-1]) + ":" + split[-1]
try:
destService = socket.getservbyport(int(dest.split(".")[-1]))
dest = f"{destService.upper()} {'.'.join(dest.split('.')[0:-1])}"
except:
split = src.split(".")
src = ".".join(split[0:-1]) + ":" + split[-1]
logger.log(f"{src} --> {dest}", serverSocket)