forked from cve-search/cve-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.py
333 lines (285 loc) · 12.2 KB
/
Config.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Config reader to read the configuration file
#
# Software is free software released under the "GNU Affero General Public License v3.0"
#
# Copyright (c) 2013-2018 Alexandre Dulaunoy - [email protected]
# Copyright (c) 2014-2018 Pieter-Jan Moreels - [email protected]
# imports
import sys
import os
runPath = os.path.dirname(os.path.realpath(__file__))
import pymongo
import redis
import bz2
import configparser
import datetime
import gzip
import re
import ssl
import urllib.parse
import urllib.request as req
import zipfile
from io import BytesIO
class Configuration():
ConfigParser = configparser.ConfigParser()
ConfigParser.read(os.path.join(runPath, "../etc/configuration.ini"))
default = {'redisHost': 'localhost', 'redisPort': 6379,
'redisVendorDB': 10, 'redisNotificationsDB': 11,
'redisRefDB': 12,
'mongoHost': 'localhost', 'mongoPort': 27017,
'mongoDB': "cvedb",
'mongoUsername': '', 'mongoPassword': '',
'flaskHost': "127.0.0.1", 'flaskPort': 5000,
'flaskDebug': True, 'pageLength': 50,
'loginRequired': False, 'listLogin': True,
'ssl': False, 'sslCertificate': "./ssl/cve-search.crt",
'sslKey': "./ssl/cve-search.crt",
'CVEStartYear': 2002,
'logging': True, 'logfile': "./log/cve-search.log",
'maxLogSize': '100MB', 'backlog': 5,
'Indexdir': './indexdir', 'updatelogfile': './log/update.log',
'Tmpdir': './tmp',
'http_proxy': '', 'http_ignore_certs': False,
'plugin_load': './etc/plugins.txt',
'plugin_config': './etc/plugins.ini',
'auth_load': './etc/auth.txt'
}
sources={'cve': "https://nvd.nist.gov/feeds/xml/cve/2.0/",
'cpe': "https://nvd.nist.gov/feeds/xml/cpe/dictionary/official-cpe-dictionary_v2.2.xml.zip",
'cwe': "https://cwe.mitre.org/data/xml/cwec_v2.12.xml.zip",
'capec': "https://capec.mitre.org/data/xml/capec_v2.6.xml",
'via4': "https://www.cve-search.org/feeds/via4.json",
'includecve': True, 'includecapec': True, 'includemsbulletin': True,
'includecpe': True, 'includecwe': True, 'includevia4': True}
@classmethod
def readSetting(cls, section, item, default):
result = default
try:
if type(default) == bool:
result = cls.ConfigParser.getboolean(section, item)
elif type(default) == int:
result = cls.ConfigParser.getint(section, item)
else:
result = cls.ConfigParser.get(section, item)
except:
pass
return result
# Mongo
@classmethod
def getMongoDB(cls):
return cls.readSetting("Mongo", "DB", cls.default['mongoDB'])
@classmethod
def getMongoConnection(cls):
mongoHost = cls.readSetting("Mongo", "Host", cls.default['mongoHost'])
mongoPort = cls.readSetting("Mongo", "Port", cls.default['mongoPort'])
mongoDB = cls.getMongoDB()
mongoUsername = cls.readSetting("Mongo", "Username", cls.default['mongoUsername'])
mongoPassword = cls.readSetting("Mongo", "Password", cls.default['mongoPassword'])
mongoUsername = urllib.parse.quote( mongoUsername )
mongoPassword = urllib.parse.quote( mongoPassword )
try:
if mongoUsername and mongoPassword:
mongoURI = "mongodb://{username}:{password}@{host}:{port}/{db}".format(
username = mongoUsername, password = mongoPassword,
host = mongoHost, port = mongoPort,
db = mongoDB
)
connect = pymongo.MongoClient(mongoURI, connect=False)
else:
connect = pymongo.MongoClient(mongoHost, mongoPort, connect=False)
except:
sys.exit("Unable to connect to Mongo. Is it running on %s:%s?"%(mongoHost,mongoPort))
return connect[mongoDB]
@classmethod
def toPath(cls, path):
return path if os.path.isabs(path) else os.path.join(runPath, "..", path)
# Redis
@classmethod
def getRedisHost(cls):
return cls.readSetting("Redis", "Host", cls.default['redisHost'])
@classmethod
def getRedisPort(cls):
return cls.readSetting("Redis", "Port", cls.default['redisPort'])
@classmethod
def getRedisVendorConnection(cls):
redisHost = cls.getRedisHost()
redisPort = cls.getRedisPort()
redisDB = cls.readSetting("Redis", "VendorsDB", cls.default['redisVendorDB'])
return redis.StrictRedis(host=redisHost, port=redisPort, db=redisDB, charset='utf-8', decode_responses=True)
@classmethod
def getRedisNotificationsConnection(cls):
redisHost = cls.getRedisHost()
redisPort = cls.getRedisPort()
redisDB = cls.readSetting("Redis", "NotificationsDB", cls.default['redisNotificationsDB'])
return redis.StrictRedis(host=redisHost, port=redisPort, db=redisDB, charset="utf-8", decode_responses=True)
@classmethod
def getRedisRefConnection(cls):
redisHost = cls.getRedisHost()
redisPort = cls.getRedisPort()
redisDB = cls.readSetting("Redis", "RefDB", cls.default['redisRefDB'])
return redis.StrictRedis(host=redisHost, port=redisPort, db=redisDB, charset="utf-8", decode_responses=True)
# Flask
@classmethod
def getFlaskHost(cls):
return cls.readSetting("Webserver", "Host", cls.default['flaskHost'])
@classmethod
def getFlaskPort(cls):
return cls.readSetting("Webserver", "Port", cls.default['flaskPort'])
@classmethod
def getFlaskDebug(cls):
return cls.readSetting("Webserver", "Debug", cls.default['flaskDebug'])
# Webserver
@classmethod
def getPageLength(cls):
return cls.readSetting("Webserver", "PageLength", cls.default['pageLength'])
# Authentication
@classmethod
def loginRequired(cls):
return cls.readSetting("Webserver", "LoginRequired", cls.default['loginRequired'])
@classmethod
def listLoginRequired(cls):
return cls.readSetting("Webserver", "ListLoginRequired", cls.default['listLogin'])
@classmethod
def getAuthLoadSettings(cls):
return cls.toPath(cls.readSetting("Webserver", "authSettings", cls.default['auth_load']))
# SSL
@classmethod
def useSSL(cls):
return cls.readSetting("Webserver", "SSL", cls.default['ssl'])
@classmethod
def getSSLCert(cls):
return cls.toPath(cls.readSetting("Webserver", "Certificate", cls.default['sslCertificate']))
@classmethod
def getSSLKey(cls):
return cls.toPath(cls.readSetting("Webserver", "Key", cls.default['sslKey']))
# CVE
@classmethod
def getCVEStartYear(cls):
date = datetime.datetime.now()
year = date.year + 1
score = cls.readSetting("CVE", "StartYear", cls.default['CVEStartYear'])
if score < 2002 or score > year:
print('The year %i is not a valid year.\ndefault year %i will be used.' % (score, cls.default['CVEStartYear']))
score = cls.default['CVEStartYear']
return cls.readSetting("CVE", "StartYear", cls.default['CVEStartYear'])
# Logging
@classmethod
def getLogfile(cls):
return cls.toPath(cls.readSetting("Logging", "Logfile", cls.default['logfile']))
@classmethod
def getUpdateLogFile(cls):
return cls.toPath(cls.readSetting("Logging", "Updatelogfile", cls.default['updatelogfile']))
@classmethod
def getLogging(cls):
return cls.readSetting("Logging", "Logging", cls.default['logging'])
@classmethod
def getMaxLogSize(cls):
size = cls.readSetting("Logging", "MaxSize", cls.default['maxLogSize'])
split = re.findall('\d+|\D+', size)
try:
if len(split) > 2 or len(split) == 0:
raise Exception
base = int(split[0])
if len(split) == 1:
multiplier = 1
else:
multiplier = (split[1]).strip().lower()
if multiplier == "b":
multiplier = 1
elif multiplier == "kb":
multiplier = 1024
elif multiplier == "mb":
multiplier = 1024 * 1024
elif multiplier == "gb":
multiplier = 1024 * 1024 * 1024
else:
# If we cannot interpret the multiplier, we take MB as default
multiplier = 1024 * 1024
return base * multiplier
except Exception as e:
print(e)
return 100 * 1024
@classmethod
def getBacklog(cls):
return cls.readSetting("Logging", "Backlog", cls.default['backlog'])
# Indexing
@classmethod
def getTmpdir(cls):
return cls.toPath(cls.readSetting("dbmgt", "Tmpdir", cls.default['Tmpdir']))
# Indexing
@classmethod
def getIndexdir(cls):
return cls.toPath(cls.readSetting("FulltextIndex", "Indexdir", cls.default['Indexdir']))
# Http Proxy
@classmethod
def getProxy(cls):
return cls.readSetting("Proxy", "http", cls.default['http_proxy'])
@classmethod
def ignoreCerts(cls):
return cls.readSetting("Proxy", "IgnoreCerts", cls.default['http_ignore_certs'])
@classmethod
def getFile(cls, getfile, unpack=True):
if cls.getProxy():
proxy = req.ProxyHandler({'http': cls.getProxy(), 'https': cls.getProxy()})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
if cls.ignoreCerts():
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
opener = req.build_opener(urllib.request.HTTPSHandler(context=ctx))
req.install_opener(opener)
response = req.urlopen(getfile)
data = response
# TODO: if data == text/plain; charset=utf-8, read and decode
if unpack:
if 'gzip' in response.info().get('Content-Type'):
buf = BytesIO(response.read())
data = gzip.GzipFile(fileobj=buf)
elif 'bzip2' in response.info().get('Content-Type'):
data = BytesIO(bz2.decompress(response.read()))
elif 'zip' in response.info().get('Content-Type'):
fzip = zipfile.ZipFile(BytesIO(response.read()), 'r')
if len(fzip.namelist())>0:
data=BytesIO(fzip.read(fzip.namelist()[0]))
return (data, response)
# Feeds
@classmethod
def getFeedData(cls, source, unpack=True):
source = cls.getFeedURL(source)
return cls.getFile(source, unpack) if source else None
@classmethod
def getFeedURL(cls, source):
cls.ConfigParser.clear()
cls.ConfigParser.read(os.path.join(runPath, "../etc/sources.ini"))
return cls.readSetting("Sources", source, cls.sources.get(source, ""))
@classmethod
def includesFeed(cls, feed):
return cls.readSetting("EnabledFeeds", feed, cls.sources.get('include'+feed, False))
# Plugins
@classmethod
def getPluginLoadSettings(cls):
return cls.toPath(cls.readSetting("Plugins", "loadSettings", cls.default['plugin_load']))
@classmethod
def getPluginsettings(cls):
return cls.toPath(cls.readSetting("Plugins", "pluginSettings", cls.default['plugin_config']))
class ConfigReader():
def __init__(self, file):
self.ConfigParser = configparser.ConfigParser()
self.ConfigParser.read(file)
def read(self, section, item, default):
result = default
try:
if type(default) == bool:
result = self.ConfigParser.getboolean(section, item)
elif type(default) == int:
result = self.ConfigParser.getint(section, item)
else:
result = self.ConfigParser.get(section, item)
except:
pass
return result