/
database.py
369 lines (322 loc) · 12.4 KB
/
database.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
'''
LEGION (https://govanguard.io)
Copyright (c) 2018 GoVanguard
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from utilities.stenoLogging import *
log = get_logger('legion', path="./log/legion-db.log", console=False)
log.setLevel(logging.INFO)
from PyQt5.QtCore import QSemaphore
import time
from random import randint
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func, create_engine, Table
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy.orm.scoping import scoped_session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.pool import SingletonThreadPool
from six import u as unicode
Base = declarative_base()
class process(Base):
__tablename__ = 'process'
pid = Column(String)
id = Column(Integer, primary_key = True)
display = Column(String)
name = Column(String)
tabTitle = Column(String)
hostIp = Column(String)
port = Column(String)
protocol = Column(String)
command = Column(String)
startTime = Column(String)
endTime = Column(String)
estimatedRemaining = Column(Integer)
elapsed = Column(Integer)
outputfile = Column(String)
output = relationship("process_output")
status = Column(String)
closed = Column(String)
def __init__(self, pid, *args):
self.display = 'True'
self.pid = pid
self.name = args[0]
self.tabTitle = args[1]
self.hostIp = args[2]
self.port = args[3]
self.protocol = args[4]
self.command = args[5]
self.startTime = args[6]
self.endTime = args[7]
self.outputfile = args[8]
self.output = args[10]
self.status = args[9]
self.closed = 'False'
self.estimatedRemaining = args[11]
self.elapsed = args[12]
# This class holds various info about an nmap scan
class nmapSessionObj(Base):
__tablename__ = 'nmapSessionObj'
filename = Column(String, primary_key = True)
startTime = Column(String)
finish_time = Column(String)
nmapVersion = Column(String)
scanArgs = Column(String)
totalHosts = Column(String)
upHosts = Column(String)
downHosts = Column(String)
def __init__(self, filename, *args, **kwargs):
self.filename = filename
self.startTime = args[0]
self.finish_time = args[1]
self.nmapVersion = kwargs.get('nmapVersion') or 'unknown'
self.scanArgs = kwargs.get('scanArgs') or ''
self.totalHosts = kwargs.get('total_host') or '0'
self.upHosts = kwargs.get('upHosts') or '0'
self.downHosts = kwargs.get('downHosts') or '0'
class osObj(Base):
__tablename__ = 'osObj'
id = Column(Integer, primary_key = True)
name = Column(String)
family = Column(String)
generation = Column(String)
osType = Column(String)
vendor = Column(String)
accuracy = Column(String)
hostId = Column(String, ForeignKey('hostObj.id'))
def __init__(self, name, *args):
self.name = name
self.family = args[0]
self.generation = args[1]
self.osType = args[2]
self.vendor = args[3]
self.accuracy = args[4]
self.hostId = args[5]
class portObj(Base):
__tablename__ = 'portObj'
portId = Column(String)
id = Column(Integer, primary_key = True)
protocol = Column(String)
state = Column(String)
hostId = Column(String, ForeignKey('hostObj.id'))
serviceId = Column(String, ForeignKey('serviceObj.id'))
scriptId = Column(String, ForeignKey('l1ScriptObj.id'))
def __init__(self, portId, protocol, state, host, service = ''):
self.portId = portId
self.protocol = protocol
self.state = state
self.serviceId = service
self.hostId = host
class cve(Base):
__tablename__ = 'cve'
name = Column(String)
id = Column(Integer, primary_key = True)
url = Column(String)
product = Column(String)
severity = Column(String)
source = Column(String)
version = Column(String)
exploitId = Column(Integer)
exploit = Column(String)
exploitUrl = Column(String)
serviceId = Column(String, ForeignKey('serviceObj.id'))
hostId = Column(String, ForeignKey('hostObj.id'))
def __init__(self, name, url, product, hostId, severity = '', source = '', version = '', exploitId = 0, exploit = '', exploitUrl = ''):
self.url = url
self.name = name
self.product = product
self.severity = severity
self.source = source
self.version = version
self.exploitId = exploitId
self.exploit = exploit
self.exploitUrl = exploitUrl
self.hostId = hostId
class appObj(Base):
__tablename__ = 'appObj'
name = Column(String)
id = Column(Integer, primary_key = True)
product = Column(String)
version = Column(String)
extrainfo = Column(String)
fingerprint = Column(String)
cpe = Column(String)
serviceId = Column(String, ForeignKey('serviceObj.id'))
def __init__(self, name = '', product = '', version = '', extrainfo = '', fingerprint = '', cpe = ''):
self.name = name
self.product = product
self.version = version
self.extrainfo = extrainfo
self.fingerprint = fingerprint
self.cpe = cpe
class serviceObj(Base):
__tablename__ = 'serviceObj'
name = Column(String)
id = Column(Integer, primary_key = True)
product = Column(String)
version = Column(String)
extrainfo = Column(String)
fingerprint = Column(String)
port = relationship(portObj)
cves = relationship(cve)
application = relationship(appObj)
def __init__(self, name = '', product = '', version = '', extrainfo = '', fingerprint = ''):
self.name = name
self.product = product
self.version = version
self.extrainfo = extrainfo
self.fingerprint = fingerprint
class l1ScriptObj(Base):
__tablename__ = 'l1ScriptObj'
scriptId = Column(String)
id = Column(Integer, primary_key = True)
output = Column(String)
portId = Column(String, ForeignKey('portObj.id'))
hostId = Column(String, ForeignKey('hostObj.id'))
def __init__(self, scriptId, output, portId, hostId):
self.scriptId = scriptId
self.output = unicode(output)
self.portId = portId
self.hostId = hostId
class l2ScriptObj(Base):
__tablename__ = 'l2ScriptObj'
scriptId = Column(String)
id = Column(Integer, primary_key = True)
output = Column(String)
portId = Column(String, ForeignKey('portObj.id'))
hostId = Column(String, ForeignKey('hostObj.id'))
def __init__(self, scriptId, output, portId, hostId):
self.scriptId = scriptId
self.output = unicode(output)
self.portId = portId
self.hostId = hostId
class hostObj(Base):
__tablename__ = 'hostObj'
# State
state = Column(String)
count = Column(String)
checked = Column(String)
# OS
osMatch = Column(String)
osAccuracy = Column(String)
vendor = Column(String)
uptime = Column(String)
lastboot = Column(String)
# Network
isp = Column(String)
asn = Column(String)
ip = Column(String)
ipv4 = Column(String)
ipv6 = Column(String)
macaddr = Column(String)
status = Column(String)
hostname = Column(String)
# ID
hostId = Column(String)
id = Column(Integer, primary_key = True)
count = Column(String)
# Location
city = Column(String)
countryCode = Column(String)
postalCode = Column(String)
longitude = Column(String)
latitude = Column(String)
distance = Column(String)
# Network
isp = Column(String)
asn = Column(String)
# host relationships
os = relationship(osObj)
ports = relationship(portObj)
cves = relationship(cve)
def __init__(self, **kwargs):
self.checked = kwargs.get('checked') or 'False'
self.osMatch = kwargs.get('osMatch') or 'unknown'
self.osAccuracy = kwargs.get('osAccuracy') or 'NaN'
self.ip = kwargs.get('ip') or 'unknown'
self.ipv4 = kwargs.get('ipv4') or 'unknown'
self.ipv6 = kwargs.get('ipv6') or 'unknown'
self.macaddr = kwargs.get('macaddr') or 'unknown'
self.status = kwargs.get('status') or 'unknown'
self.hostname = kwargs.get('hostname') or 'unknown'
self.hostId = kwargs.get('hostname') or 'unknown'
self.vendor = kwargs.get('vendor') or 'unknown'
self.uptime = kwargs.get('uptime') or 'unknown'
self.lastboot = kwargs.get('lastboot') or 'unknown'
self.distance = kwargs.get('distance') or 'unknown'
self.state = kwargs.get('state') or 'unknown'
self.count = kwargs.get('count') or 'unknown'
self.city = kwargs.get('city') or 'unknown'
self.countryCode = kwargs.get('countryCode') or 'unknown'
self.postalCode = kwargs.get('postalCode') or 'unknown'
self.longitude = kwargs.get('longitude') or 'unknown'
self.latitude = kwargs.get('latitude') or 'unknown'
self.isp = kwargs.get('isp') or 'unknown'
self.asn = kwargs.get('asn') or 'unknown'
class note(Base):
__tablename__ = 'note'
hostId = Column(Integer, ForeignKey('hostObj.id'))
id = Column(Integer, primary_key = True)
text = Column(String)
def __init__(self, hostId, text):
self.text = unicode(text)
self.hostId = hostId
class process_output(Base):
__tablename__ = 'process_output'
processId = Column(Integer, ForeignKey('process.id'))
id = Column(Integer, primary_key = True)
output = Column(String)
def __init__(self):
self.output = unicode('')
class Database:
def __init__(self, dbfilename):
try:
self.name = dbfilename
self.dbsemaphore = QSemaphore(1) # to control concurrent write access to db
self.engine = create_engine('sqlite:///{dbFileName}'.format(dbFileName = dbfilename)) #, poolclass=SingletonThreadPool)
self.session = scoped_session(sessionmaker())
self.session.configure(bind = self.engine, autoflush=False)
self.metadata = Base.metadata
self.metadata.create_all(self.engine)
self.metadata.echo = True
self.metadata.bind = self.engine
except Exception as e:
log.info('Could not create database. Please try again.')
log.info(e)
def openDB(self, dbfilename):
try:
self.name = dbfilename
self.dbsemaphore = QSemaphore(1) # to control concurrent write access to db
self.engine = create_engine('sqlite:///{dbFileName}'.format(dbFileName = dbfilename)) #, poolclass=SingletonThreadPool)
self.session = scoped_session(sessionmaker())
self.session.configure(bind = self.engine, autoflush=False)
self.metadata = Base.metadata
self.metadata.create_all(self.engine)
self.metadata.echo = True
self.metadata.bind = self.engine
except:
log.info('Could not open database file. Is the file corrupted?')
def commit(self):
self.dbsemaphore.acquire()
log.info("DB lock aquired")
try:
session = self.session()
rnd = float(randint(1,99)) / 100.00
log.info("Waiting {0}s before commit...".format(str(rnd)))
time.sleep(rnd)
session.commit()
except Exception as e:
log.error("DB Commit issue")
log.error(str(e))
try:
rnd = float(randint(1,99)) / 100.00
time.sleep(rnd)
log.info("Waiting {0}s before commit...".format(str(rnd)))
session.commit()
except Exception as e:
log.error("DB Commit issue on retry")
log.error(str(e))
pass
self.dbsemaphore.release()
log.info("DB lock released")