-
Notifications
You must be signed in to change notification settings - Fork 0
/
ransomware_monitor.py
273 lines (211 loc) · 8.08 KB
/
ransomware_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import pathlib
import re
import time
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class FullHandler(FileSystemEventHandler):
def on_modified(self, event):
path = str(event.src_path)
path=path[2:]
path=replase_char_with_space(path,"~","")
if "txt" in path and path not in "english.txt" and path not in "log.txt":
print(f'time: {time.ctime()}event type: {event.event_type} path : {path}')
check_dictinary(path)
check_chars(path)
class LightHandler(FileSystemEventHandler):
def on_modified(self, event):
path = str(event.src_path)
path=path[2:]
path=replase_char_with_space(path,"~","")
if "txt" in path and path not in "english.txt" and path not in "log.txt":
print(f'time: {time.ctime()}event type: {event.event_type} path : {path}')
check_chars(path)
def check_chars(path):
log = open('log.txt', 'a')
with open(path) as file: # Use file to refer to the file object
# remove all the check in wordlist
data = file.read()
# check for iligal charecters
data = data.replace("\n", "")
data = data.replace("\t", "")
data = data.replace("\r", "")
data = data.replace(" ", "")
if (not data.isalnum()):
log = open('log.txt', 'a')
warning = ("\n"+str(time.ctime())+" Warning "+ path+" has illigal chrecters check it for Ransomware.\n")
print(warning)
log.write(warning)
log.close()
# delimeter_to_delete - a str with chars you want to delete.
# delimeter_to_space - a str with chars you want to replase withe space.
def replase_char_with_space(word,delimeter_to_delete,delimeter_to_space):
word=str(word)
for d in delimeter_to_delete:
word=word.replace(d,"")
for d in delimeter_to_space:
word=word.replace(d," ")
return word
def check_dictinary(path):
log = open('log.txt', 'a')
with open(path) as file: # Use file to refer to the file object
# remove all the check in wordlist
data = file.read()
data=data.lower()
data =replase_char_with_space(data,":'\"-/!?()[]'‘",",.;_")
with open("english.txt") as file:
english_list = file.read()
english_list=english_list.lower()
english_list= replase_char_with_space(english_list,"'",".,;:")
splited_data = data.split()
english_list_splited = english_list.split()
illigal_words = []
count= 0
first_length = True
for word in splited_data:
if len(word)>25:
if first_length:
first_length=False
log = open('log.txt', 'a')
warning= (str(time.ctime()) + " Warning " + path + " :\nhas more than word with a length bigger than 25 chars check it for Ransomware.\nThe word is :"+word)
print(warning)
log.write(warning)
log.close()
log.close()
isIn = False
for dict in english_list_splited:
if word in dict:
isIn = True
if isIn ==False and len(word)>1 and not word.isnumeric():
count+=1
illigal_words.append(word)
if (len(illigal_words) > (len(splited_data)/20)):
log = open("log.txt",'a')
warning = (str(time.ctime())+" Warning "+ path+" has more than 5% unrecognised word's that don't appere in the dictinary check it for Ransomware.")
details = ("The unrecognised words"+str(illigal_words[0:10]))
print(warning)
print(details)
log.write(warning)
log.write(details)
log.close()
class full_monitor(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.sleep_time = 1
event_handler = FullHandler()
self.observer = Observer()
self.observer.schedule(event_handler, path='.', recursive=False)
self.observer.start()
def run(self):
try:
while True:
time.sleep(self.sleep_time)
path = pathlib.Path('.')
except KeyboardInterrupt:
self.observer.stop()
self.observer.join()
class light_monitor(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.sleep_time = 1
event_handler = LightHandler()
self.observer = Observer()
self.observer.schedule(event_handler, path='.', recursive=False)
self.observer.start()
def run(self):
try:
while True:
time.sleep(self.sleep_time)
except KeyboardInterrupt:
self.observer.stop()
self.observer.join()
def main() :
print("Ransomware Protector created by Simon Pikalov")
print('''----------------------//\\
---------------------// ¤ \\
---------------------\\ ¤ //
---------------------- \\//
-------------------- (___)
---------------------(___)
---------------------(___)
---------------------(___)_________
--------\_____/\__/----\__/\_____/
------------\ _°_[------------]_ _° /
----------------\_°_¤ ---- ¤_°_/
--------------------\ __°__ /
---------------------|\_°_/|
---------------------[|\_/|]
---------------------[|[¤]|]
---------------------[|;¤;|]
---------------------[;;¤;;]
--------------------;;;;¤]|]\\
-------------------;;;;;¤]|]-\\
------------------;;;;;[¤]|]--\\
-----------------;;;;;|[¤]|]---\\
----------------;;;;;[|[¤]|]|---|
---------------;;;;;[|[¤]|]|---|
----------------;;;;[|[¤]|/---/
-----------------;;;[|[¤]/---/
------------------;;[|[¤/---/
-------------------;[|[/---/
--------------------[|/---/
---------------------/---/
--------------------/---/|]
-------------------/---/]|];
------------------/---/¤]|];;
-----------------|---|[¤]|];;;
-----------------|---|[¤]|];;;
------------------\--|[¤]|];;
-------------------\-|[¤]|];
---------------------\|[¤]|]
----------------------\\¤//
-----------------------\|/
------------------------V
''')
isLightRunning = False
isFulllRunning = False
menuMessege = "please choose a action mode\nfor manual type manual , for full mode type full and for light mode tight light"
print(menuMessege)
while(True):
mode = input("\nchoose an action \n")
if mode == "full" or mode == "f" :
if isFulllRunning == True:
print("\nThe program is already running at full mode \n")
else:
print("full Mode ")
isFulllRunning == True
sleepTime = input("\nchoose an Interval Time for the program in seconds: \n")
try:
val = float(sleepTime)
if val < 0:
print("Not valid Input")
else:
full = full_monitor()
full.sleep_time = val
full.start()
isFulllRunning = True
except ValueError:
print("Not valid Input")
elif mode == "light" or mode == "l" :
if isLightRunning == True:
print("\nThe program is already running\n")
else:
sleepTime = input("\nchoose an Interval Time for the program in seconds: \n")
try:
val = float(sleepTime)
if val<0:
print("Not valid Input")
else:
light = light_monitor()
light.sleep_time = val
light.start()
isLightRunning = True
except ValueError:
print("Not valid Input")
elif mode == "help" :
print(
"\n\n\n@author Simon Pikalov\nThis program helps monitor folder txt files by whriting a log file,\nthat contain all the changes in the file that may be causing encryption of your data\n"
"for more information read the readme at https://github.com/simon-pikalov/Ransomware_Protector.\n" + menuMessege)
elif mode == "exit":
exit()
main()