-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_monitor.py
182 lines (154 loc) · 6.77 KB
/
process_monitor.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
import psutil
import time
import threading
import json
from config import Config
from detect_ransomware import DetectRansomware
def get_current_running_process():
running_process = []
for proc in psutil.process_iter(['pid', 'name']):
try:
# Get process info as named tuple (pid, name)
proc_info = proc.as_dict(attrs=['pid', 'name'])
running_process.append((proc_info['pid'], proc_info['name']))
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return running_process
lock = threading.Lock()
class ProcessesToMonitor:
global lock
def __init__(self):
self.processes = dict()
def add_item(self, pid, process_name):
with lock:
self.processes[pid] = process_name
def remove_item(self, pid):
with lock:
self.processes[pid] = None
def get_processes_to_monitor(self):
with lock:
return self.processes
processes_to_monitor = ProcessesToMonitor()
def monitor_process():
print('[+] Monitoring Process')
global processes_to_monitor
previous_processes = get_current_running_process()
while True:
try:
current_processes = get_current_running_process()
# Find the new and exited processes
new_processes = [p for p in current_processes if p not in previous_processes]
exited_processes = [p for p in previous_processes if p not in current_processes]
# Print the new and exited processes
for pid, name in new_processes:
print("New process started: PID={} Name={}".format(pid, name))
processes_to_monitor.add_item(pid, name)
for pid, name in exited_processes:
print("Process exited: PID={} Name={}".format(pid, name))
processes_to_monitor.remove_item(pid)
# Update the previous list of processes and wait for 1 second before updating again
previous_processes = current_processes
time.sleep(1)
except KeyboardInterrupt:
exit(0)
except (OSError, Exception) as _:
print('[!] Exception in Monitoring Process')
def monitor_resource_activity():
global processes_to_monitor
ransomware_detection = DetectRansomware()
results = dict()
cur_count = 21
while True:
if cur_count > 999999: # To prevent integer overflow
cur_count = 1
if (cur_count % Config.DUMP_INTERVAL) == 0:
if ransomware_detection.resource_activity_detection(results):
ransomware_detection.take_action_on_ransomware_detection()
with open('resource_activity', 'w') as f:
json.dump(results, f)
cur_count += 1
try:
processes = processes_to_monitor.get_processes_to_monitor()
for pid, name in processes.items():
if name is None:
continue
try:
process_obj = psutil.Process(pid)
except (psutil.NoSuchProcess, Exception):
continue
process_key = str(pid) + ':' + name
if not results.get(process_key, False):
results[process_key] = dict({
'cpu_percent': [process_obj.cpu_percent()],
'ram': [process_obj.memory_info().rss],
'read_count': [process_obj.io_counters().read_count],
'write_count': [process_obj.io_counters().write_count],
'read_bytes': [process_obj.io_counters().read_bytes],
'write_bytes': [process_obj.io_counters().write_bytes]
})
else:
results[process_key]['cpu_percent'].append(process_obj.cpu_percent())
results[process_key]['ram'].append(process_obj.memory_info().rss)
results[process_key]['read_count'].append(process_obj.io_counters().read_count)
results[process_key]['write_count'].append(process_obj.io_counters().write_count)
results[process_key]['read_bytes'].append(process_obj.io_counters().read_bytes)
results[process_key]['write_bytes'].append(process_obj.io_counters().write_bytes)
time.sleep(1)
except KeyboardInterrupt:
exit(0)
except Exception as ex:
print('[!] Exception in monitoring resource activity')
print(ex)
def monitor_network_activity():
ransomware_detection = DetectRansomware()
global processes_to_monitor
results = dict()
cur_count = 1
while True:
if cur_count > 999999: # To prevent integer overflow
cur_count = 1
if (cur_count % Config.DUMP_INTERVAL) == 0:
if ransomware_detection.network_based_activity_detection(results):
ransomware_detection.take_action_on_ransomware_detection()
with open('network_activity', 'w') as f:
json.dump(results, f)
cur_count += 1
try:
processes = processes_to_monitor.get_processes_to_monitor()
for pid, name in processes.items():
if name is None:
continue
try:
process_obj = psutil.Process(pid)
except (psutil.NoSuchProcess, Exception):
continue
connections = process_obj.connections()
for conn in connections:
process_key = str(pid) + ':' + name
network_activity = dict({
'family': str(conn.family),
'type': str(conn.type),
'laddr': str(conn.laddr),
'raddr': str(conn.raddr),
'status': conn.status
})
if not results.get(process_key, False):
results[process_key] = [network_activity]
else:
results[process_key].append(network_activity)
time.sleep(10)
except KeyboardInterrupt:
exit(0)
except Exception as ex:
print('[!] Exception in monitoring network activity')
print(ex)
if __name__ == '__main__':
t1 = threading.Thread(target=monitor_process)
t2 = threading.Thread(target=monitor_resource_activity)
t3 = threading.Thread(target=monitor_network_activity)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()