-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpersistence.py
253 lines (214 loc) · 10.6 KB
/
persistence.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
import sqlite3
import csv
import threading
import logging
import sys
import datetime
from datetime import datetime
logger = logging.getLogger('rSMBi')
class Database:
def __init__(self, db_file):
self.db_file = db_file
def connect_database(self):
self.conn = sqlite3.connect(self.db_file, check_same_thread=False)
self.cursor = self.conn.cursor()
self.lock = threading.Lock()
def create_database(self):
self.connect_database()
try:
rsmbi_match_table = """ CREATE TABLE IF NOT EXISTS rsmbi (
id integer PRIMARY KEY AUTOINCREMENT,
file text NOT NULL,
share text NOT NULL,
ip text NOT NULL,
tsFirstFound text NOT NULL,
tsLastFound text NOT NULL,
runTag text NOT NULL,
winClickable text NOT NULL
); """
if self.cursor is not None:
self.create_table(rsmbi_match_table)
except Exception as e:
logger.error(
"Encountered error while creating the database: " + str(e))
sys.exit(1)
def exportToCSV(self, tag):
cursor = self.cursor
exportQuery = "SELECT * from rsmbi WHERE runTag = '{tag}\'".format(
tag=tag)
sr = cursor.execute(exportQuery)
with open('rsmbi_results.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(sr)
def commit(self):
self.conn.commit()
def create_table(self, create_table_sql):
try:
self.cursor.execute(create_table_sql)
except Exception as e:
logger.error(e)
def insertFinding(self, filename, share, ip, tag):
now = datetime.now()
date = now.strftime("%d-%m-%Y")
# remove the local path tmp path
filename = '/'.join(filename.split('/')[3:])
clickable = ("\\\\" + ip + "\\" + share +
"\\" + filename).replace('/', '\\')
try:
self.lock.acquire(True)
cursor = self.cursor
cursor.execute('SELECT id,file FROM rsmbi WHERE ip = ? AND share = ? AND file = ?', (
ip, share, filename))
results = cursor.fetchall()
if len(results) == 0:
insertFindingQuery = "INSERT INTO rsmbi (file, share, ip, tsFirstFound, tsLastFound, runTag, winClickable) VALUES (?,?,?,?,?,?,?)"
cursor.execute(insertFindingQuery,
(filename, share, ip, date, date, tag, clickable.replace("/", "\\")))
self.commit()
else:
updateQuery = 'UPDATE rsmbi SET tsLastFound = ? WHERE ip = ? AND share = ? AND file= ?'
cursor.execute(updateQuery, (date, ip, share,
filename))
self.commit()
updateQuery = 'UPDATE rsmbi SET runTag = ? WHERE ip = ? AND share = ? AND file= ?'
cursor.execute(updateQuery, (tag, ip, share,
filename))
except Exception as e:
logger.error("Error while updating database: " + str(e))
self.lock.release()
finally:
self.lock.release()
class DatabaseSMBSR:
def __init__(self, db_file):
self.db_file = db_file
def connect_database(self):
self.conn = sqlite3.connect(self.db_file, check_same_thread=False)
self.cursor = self.conn.cursor()
self.lock = threading.Lock()
def create_database(self):
self.connect_database()
try:
smb_match_table = """ CREATE TABLE IF NOT EXISTS smbsr (
id integer PRIMARY KEY AUTOINCREMENT,
file text NOT NULL,
share text NOT NULL,
ip text NOT NULL,
position text NOT NULL,
matchedWith text NOT NULL,
tsCreated text NOT NULL,
tsModified text NOT NULL,
tsAccessed text NOT NULL,
tsFirstFound text NOT NULL,
tsLastFound text NOT NULL,
runTag text NOT NULL,
extract text NOT NULL,
winClickable text NOT NULL
); """
smb_files_table = """ CREATE TABLE IF NOT EXISTS smbfile (
id integer PRIMARY KEY AUTOINCREMENT,
file text NOT NULL,
share text NOT NULL,
ip text NOT NULL,
tsCreated text NOT NULL,
tsModified text NOT NULL,
tsAccessed text NOT NULL,
tsFirstFound text NOT NULL,
tsLastFound text NOT NULL,
runTag text NOT NULL,
winClickable text NOT NULL
); """
if self.cursor is not None:
self.create_table(smb_match_table)
self.create_table(smb_files_table)
except Exception as e:
logger.error(
"Encountered error while creating the database: " + str(e))
sys.exit(1)
def exportToCSV(self, tag):
cursor = self.cursor
exportQuery = "SELECT * from smbsr WHERE runTag = '{tag}\'".format(
tag=tag)
exportQueryFile = "SELECT * from smbfile WHERE runTag = '{tag}\'".format(
tag=tag)
sr = cursor.execute(exportQuery)
with open('smbsr_results.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(sr)
sf = cursor.execute(exportQueryFile)
with open('smbsrfile_results.csv', 'w') as g:
writer = csv.writer(g)
writer.writerows(sf)
def commit(self):
self.conn.commit()
def create_table(self, create_table_sql):
try:
self.cursor.execute(create_table_sql)
except Exception as e:
logger.error(e)
def insertFinding(self, filename, share, ip, line, matchedwith, times, tag, text):
filename = '/'.join(filename.split('/')[3:])
clickable = ("\\\\" + ip + "\\" + share +
"\\" + filename).replace('/', '\\')
now = datetime.now()
date = now.strftime("%d-%m-%Y")
try:
self.lock.acquire(True)
cursor = self.cursor
results = cursor.execute('SELECT id, extract FROM smbsr WHERE ip = ? AND share = ? AND file = ? AND matchedWith = ? AND position = ?', (
ip, share, filename, matchedwith, line)).fetchall()
if len(results) == 0:
insertFindingQuery = "INSERT INTO smbsr (file, share, ip, position, matchedWith, tsCreated, tsModified, tsAccessed, tsFirstFound, tsLastFound, runTag, extract, winClickable) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"
cursor.execute(insertFindingQuery, (filename, share, ip, line,
matchedwith, times[0], times[1], times[2], date, date, tag, text, clickable))
self.commit()
else:
textOld = ((results[0])[1])
updateQuery = 'UPDATE smbsr SET tsLastFound = ? WHERE ip = ? AND share = ? AND file= ? AND matchedWith = ? AND position = ? '
cursor.execute(updateQuery, (date, ip, share,
filename, matchedwith, line))
self.commit()
if textOld != text:
updateQuery = 'UPDATE smbsr SET extract = ? WHERE ip = ? AND share = ? AND file = ? AND matchedWith = ? AND position = ?'
cursor.execute(updateQuery, (text, ip, share,
filename, matchedwith, line))
self.commit()
updateQuery = 'UPDATE smbsr SET runTag = ? WHERE ip = ? AND share = ? AND file = ? AND matchedWith = ? AND position = ? AND extract = ?'
cursor.execute(updateQuery, (tag, text, ip,
share, filename, matchedwith, line))
self.commit()
except Exception as e:
logger.error(
"Error while updating database for secret match: " + str(e))
self.lock.release()
finally:
self.lock.release()
def insertFileFinding(self, filename, share, ip, times, tag):
now = datetime.now()
filename = '/'.join(filename.split('/')[3:])
date = now.strftime("%d-%m-%Y")
clickable = ("\\\\" + ip + "\\" + share +
"\\" + filename).replace('/', '\\')
try:
self.lock.acquire(True)
cursor = self.cursor
checkQuery = 'SELECT id FROM smbfile WHERE ip = ? AND share = ? AND file = ?'
results = cursor.execute(
checkQuery, (ip, share, filename)).fetchall()
if len(results) == 0:
insertFindingQuery = "INSERT INTO smbfile (file, share, ip, tsCreated, tsModified, tsAccessed, tsFirstFound, tsLastFound, runTag, winClickable) VALUES (?,?,?,?,?,?,?,?,?,?)"
cursor.execute(insertFindingQuery, (filename, share,
ip, times[0], times[1], times[2], date, date, tag, clickable))
self.commit()
else:
updateQuery = 'UPDATE smbfile SET tsLastFound = ? WHERE ip= ? AND share = ? AND file = ?'
cursor.execute(updateQuery, (date, ip, share, filename))
self.commit()
updateQuery = 'UPDATE smbfile SET runTag = ? WHERE ip= ? AND share = ? AND file = ?'
cursor.execute(updateQuery, (tag, ip, share, filename))
self.commit()
except Exception as e:
logger.error(
"Error while updating database for file finding: " + str(e))
self.lock.release()
finally:
self.lock.release()