-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbmanage.py
237 lines (205 loc) · 8.36 KB
/
dbmanage.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
import os
import pickle
import hashlib
import psutil
import datetime
import subprocess
import time
import signal
import sys
import ctypes
dblist = ["db\WHITELIST", "db\BLACKLIST", "db\SEEN", "db\FAILED"]
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def is_running_as_admin():
'''
Checks if the script is running with administrative privileges.
Returns True if is running as admin, False otherwise.
'''
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def view_db(name):
db = []
if os.path.exists(name):
hash_md5 = md5(name)
with open(name, 'rb') as f:
db = pickle.load(f)
else:
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
hash_md5 = md5(name)
print("\n\n===========================================")
print("\t"+name)
print("===========================================")
i = 1
for e in db:
print(str(i) + ") " + str(e)+"\n")
i += 1
print("===========================================")
print("Total: " + str(len(db)) + " entries.")
print("MD5 HASH: " + hash_md5)
def add_db(name):
print("Please select the type of entry: ")
print("1) Process ID (PID)")
print("2) Executable Path - (NOTE: This will execute and collect info of the executable)")
choice = int(input("Please choose a number: "))
if 0 < choice < 3:
if choice == 1:
ps = int(input("Please enter a PID: "))
p = psutil.Process(ps)
entry = {'pid': ps, 'name': p.name(), 'md5': md5(p.exe()), 'time': str(datetime.datetime.now()), 'exe': p.exe()}
db = []
if os.path.exists(name) and os.path.getsize(name) > 0:
with open(name, 'rb') as f:
db = pickle.load(f)
else:
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
db.append(entry)
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
print("Successfully added " + p.name() + " to " + name + " database.")
print("Total: " + str(len(db)))
print("MD5 HASH: " + md5(name))
if choice == 2:
path = input("Please enter the absolute path of the executable: ")
if os.path.exists(path):
sp = subprocess.Popen(path)
time.sleep(10)
p = psutil.Process(sp.pid)
entry = {'pid': sp.pid, 'name': p.name(), 'md5': md5(p.exe()), 'time': str(datetime.datetime.now()),
'exe': p.exe()}
db = []
if os.path.exists(name) and os.path.getsize(name) > 0:
with open(name, 'rb') as f:
db = pickle.load(f)
else:
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
db.append(entry)
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
print("Successfully added " + p.name() + " to " + name + " database.")
print("Total: " + str(len(db)))
print("MD5 HASH: " + md5(name))
print("Closing application ...")
p.kill()
else:
print("Invalid path, please try again.")
def rm_db(name):
view_db(name)
choice = int(input("Please enter the number of the entry you want to delete: "))
db = []
if os.path.exists(name) and os.path.getsize(name) > 0:
with open(name, 'rb') as f:
db = pickle.load(f)
else:
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
if 0 < choice < len(db)+1:
print(db[choice-1])
if input("Are you sure you want to delete this entry above (Y/N): ").lower() == "y":
db.remove(db[choice-1])
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
print("Successfully removed entry")
print("Total: " + str(len(db)))
print("MD5 HASH: " + md5(name))
def gen_db(name):
procList = psutil.pids()
failed = 0
db = []
failed_db = []
for ps in procList:
try:
p = psutil.Process(ps)
entry = {'pid': ps, 'name': p.name(), 'md5': md5(p.exe()), 'time': str(datetime.datetime.now()),
'exe': p.exe()}
db.append(entry)
print("\rAdding " + p.name(), end=" \t\t\t\t\t\t\t ")
except Exception as e:
p = psutil.Process(ps)
failed_db.append({'pid': p.pid, 'name': p.name(), 'time': str(datetime.datetime.now())})
failed += 1
with open(name, 'wb') as f:
pickle.dump(db, f, pickle.HIGHEST_PROTOCOL)
with open("FAILED.DB", 'wb') as f:
pickle.dump(failed_db, f, pickle.HIGHEST_PROTOCOL)
print("\n\nTotal: " + str(len(db)))
print("MD5 HASH: " + md5(name))
print("Failed to add " + str(failed) + " entries. These can be seen in the FAILED database.")
def welcome():
if not is_running_as_admin():
print("Please run as Administrator...\nExiting ....")
sys.exit(1)
print("\n\n=======================================================\n")
print("\t ██╗ ██╗██████╗ ██████╗ ██████╗")
print("\t ██║ ██║╚════██╗██╔══██╗██╔════╝")
print("\t ██║ █╗ ██║ █████╔╝██████╔╝██║")
print("\t ██║███╗██║██╔═══╝ ██╔══██╗██║")
print("\t ╚███╔███╔╝███████╗██║ ██║╚██████╗")
print("\t ╚══╝╚══╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝")
print("\n\t Windows Registry and RAM Collector\n\tDatabase Manager\n"
"\t\t\t -BY AVINASH SINGH")
print("\n=======================================================\n")
print("\nDatabase management system.\n\n")
options()
def options():
print("\nYou have the following options: ")
i = 1
for d in dblist:
print(str(i) + ") " + d + " database.")
i += 1
print(str(i)+") Quit")
def safe_quit(sig, frame):
print("\n\nExisting the database manager ...")
sys.exit(0)
def execute():
try:
welcome()
signal.signal(signal.SIGINT, safe_quit)
while True:
choice = int(input("\n\nPlease enter a number: "))
if 0 < choice < len(dblist)+1:
print("1) View")
print("2) Add")
print("3) Delete")
if choice == 1:
print("4) Generate Whitelist from running processes")
action = int(input("\n\nPlease enter a number: "))
if 0 < action < 5:
if action == 1:
view_db(dblist[choice-1]+".db")
elif action == 2:
add_db(dblist[choice-1]+".db")
elif action == 3:
rm_db(dblist[choice-1]+".db")
elif action == 4:
gen_db(dblist[choice-1]+".db")
else:
print("Invalid option selected, please try again.")
elif choice == len(dblist)+1:
print("Exiting the manger ...")
break
else:
print("Please select a valid option.")
options()
except Exception:
print("An error occurred please try again.")
if __name__ == '__main__':
os.system('cls' if os.name == 'nt' else 'clear')
from ctypes import windll, byref
from ctypes.wintypes import SMALL_RECT
STDOUT = -11
hdl = windll.kernel32.GetStdHandle(STDOUT)
rect = SMALL_RECT(0, 50, 65, 90) # (left, top, right, bottom)
windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
windll.kernel32.SetConsoleCursorPosition(hdl, 0)
print("W2RC database manager starting ...")
execute()