-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdbhandler.py
217 lines (191 loc) · 5.29 KB
/
dbhandler.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
import sqlite3
from sqlite3 import Error
import hashlib
import subprocess
from subprocess import Popen
import shutil
import os
import time
def dbConnect():
conn = sqlite3.connect('History_Data.db')
return conn
print("Connected Successfully")
def dbCreate():
conn=dbConnect()
conn.execute(''' CREATE TABLE if not exists Bad_Hash (id INTEGER PRIMARY KEY AUTOINCREMENT ,md5 STRING);''')
conn.execute(''' CREATE TABLE if not exists VT (id INTEGER PRIMARY KEY AUTOINCREMENT ,md5 STRING,total INTEGER,pos INTEGER,notinvt INTEGER);''')
conn.execute(''' CREATE TABLE if not exists phashes_q (id INTEGER PRIMARY KEY AUTOINCREMENT ,md5 STRING, UNIQUE(md5));''')
conn.execute(''' CREATE TABLE if not exists File_events (id INTEGER PRIMARY KEY AUTOINCREMENT ,path STRING,md5 STRING, eventType STRING,Time INTEGER,total INTEGER,pos INTEGER,notinvt INTEGER,UNIQUE(md5));''')
def dbInsertp_q(d):
conn=dbConnect()
c=conn.cursor()
c.execute('INSERT OR IGNORE INTO phashes_q(id,md5) values(NULL,"'+d+'");')
conn.commit()
conn.close()
print("Inserted TO DB : "+d)
def dbsearch(d):
conn=dbConnect()
c=conn.cursor()
cout=c.execute('select * from Bad_Hash where md5 = "'+d+'";')
rows = cout.fetchall()
if len(rows) == 0:
return False
else:
return True
def getVT(d):
conn=dbConnect()
c=conn.cursor()
cout=c.execute('select pos from VT where md5 = "'+d+'";')
rows = cout.fetchall()
if len(rows) == 0:
return -1
else:
return rows[0][0]
def md5Checksum(filePath):
with open(filePath, 'rb') as fh:
m = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
def dbInsert_vt(d,t,p,nv):
conn=dbConnect()
c=conn.cursor()
try:
c.execute('INSERT INTO VT(id,md5,total,pos,notinvt) values(NULL,"'+d+'",{},{},{});'.format(t,p,nv))
conn.commit()
except Exception as e:
print(e)
conn.close()
print("Inserted TO DB : "+d)
def dbsearch_vt(d):
conn=dbConnect()
c=conn.cursor()
cout=c.execute('select * from VT where md5 = "'+d+'";')
rows = cout.fetchall()
if len(rows) == 0:
return False
else:
return True
def getpdata():
conn=dbConnect()
c=conn.cursor()
cout=c.execute('select md5 from phashes_q;')
rows = cout.fetchall()
if len(rows) == 0:
return False
else:
return rows
def pqclean():
conn=dbConnect()
c=conn.cursor()
cout=c.execute('DELETE FROM phashes_q WHERE EXISTS( SELECT 1 FROM VT Where phashes_q.md5 = VT.md5)')
print(cout.fetchall())
conn.commit()
conn.close()
return
def insert_fe(pth,md,evt):
conn=dbConnect()
c=conn.cursor()
try:
c.execute('INSERT OR IGNORE INTO File_events(id,path,md5,eventType,Time) values(NULL,"'+pth+'","{}","{}",strftime("%s","now"));'.format(md,evt))
conn.commit()
except Exception as e:
print(e)
conn.close()
print("Inserted TO DB : "+md)
def getfdata():
conn=dbConnect()
c=conn.cursor()
cout=c.execute('select md5 from File_events where notinvt is NULL;')
rows = cout.fetchall()
if len(rows) == 0:
return False
else:
return rows
def updatefhash(h,tot,p,nv):
conn=dbConnect()
c=conn.cursor()
try:
c.execute('update File_events set total = {},pos={},notinvt={} where md5 = "{}"'.format(tot,p,nv,h))
conn.commit()
except Exception as e:
print(e)
conn.close()
print("updated TO DB : "+h)
def dbcheckip(ip):
conn=dbConnect()
cursor=conn.cursor()
cursor.execute('select * from malacious_ips where ip_values="' + str(ip) + '";')
result = cursor.fetchone()
if result:
return True
else:
return False
def execute_command(cmd):
try:
p1 = Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p1.communicate()
return output[0]
except:
print('Exception in Executing Command')
return []
def check_exe(path):
execute_command("sigcheck64.exe -w res.txt -e "+path)
f = open("res.txt", "r")
if f.readlines()[1].find("Signed"):
return True
else:
return False
def check_Imphash(h):
conn=dbConnect()
cursor=conn.cursor()
cursor.execute('select malware_name from Imp_hash where imphash="' + str(h) + '";')
rows = cout.fetchall()
if len(rows) == 0:
return -1
else:
return rows[0][0]
def zip_file(path):
import zipfile
print("Quantine..")
zipfile.ZipFile('quarantine.zip', mode='a').write(path)
time.sleep(2)
print("Removing mailicoius file.."+path)
os.remove(path)
def zipfilebyhash(h):
conn=dbConnect()
cursor=conn.cursor()
cursor.execute('select path from File_events where md5="' + str(h) + '";')
rows = cout.fetchall()
if len(rows) == 0:
return -1
else:
path = rows[0][0]
zip_file(path)
'''
def dbInsert(d):
conn=dbConnect()
c=conn.cursor()
c.execute('INSERT INTO Bad_Hash(id,md5) values(NULL,"'+d+'");')
conn.commit()
conn.close()
print("Inserted TO DB : "+d)
def dbInsert(d,t,p):
conn=dbConnect()
c=conn.cursor()
c.execute('INSERT INTO VT(id,md5,total,pos) values(NULL,"'+d+'",'+t+','+p+');')
conn.commit()
conn.close()
print("Inserted TO DB : "+d)
def unprocessedhandling(h):
conn=dbConnect()
c=conn.cursor()
for hash in h:
c.execute('UPDATE VT set notinvt = {} WHERE md5="{}";'.format(1,hash))
conn.commit()
conn.close()
return
'''