forked from PaytmLabs/nerve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
156 lines (125 loc) · 4.71 KB
/
main.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
import config
import os
import re
import sys
from core.redis import rds
from core.workers import start_workers
from flask import Flask
from flask_restful import Api
from version import VERSION
from werkzeug.security import generate_password_hash
# Import Blueprints
from views.view_index import index
from views.view_docs import documentation
from views.view_dashboard import dashboard
from views.view_reports import reports
from views.view_assessment import assessment
from views.view_topology import topology
from views.view_assets import assets
from views.view_welcome import welcome
from views.view_qs import qs
from views.view_login import login
from views.view_console import console
from views.view_logout import logout
from views.view_download import download
from views.view_stream import stream
from views.view_settings import settings
from views.view_scan import scan
from views.view_vulns import vulns
from views.view_alert import alert
from views.view_startover import startover
# Import REST API Endpoints
from views_api.api_health import Health
from views_api.api_scan import Scan
from views_api.api_update import Update
from views_api.api_exclusions import Exclusion
app = Flask(__name__)
# Initialize Blueprints
app.register_blueprint(index)
app.register_blueprint(login)
app.register_blueprint(logout)
app.register_blueprint(welcome)
app.register_blueprint(download)
app.register_blueprint(assets)
app.register_blueprint(stream)
app.register_blueprint(console)
app.register_blueprint(documentation)
app.register_blueprint(dashboard)
app.register_blueprint(qs)
app.register_blueprint(reports)
app.register_blueprint(assessment)
app.register_blueprint(topology)
app.register_blueprint(vulns)
app.register_blueprint(settings)
app.register_blueprint(scan)
app.register_blueprint(alert)
app.register_blueprint(startover)
app.config.update(
SESSION_COOKIE_SAMESITE='Strict',
)
app.secret_key = os.urandom(24)
api = Api(app)
api.add_resource(Health, '/health')
api.add_resource(Update, '/api/update', '/api/update/<string:component>')
api.add_resource(Scan, '/api/scan', '/api/scan/<string:action>')
api.add_resource(Exclusion, '/api/exclusion', '/api/exclusion')
# Set Security Headers
@app.after_request
def add_security_headers(response):
if config.WEB_SECURITY:
response.headers['Content-Security-Policy'] = config.WEB_SEC_HEADERS['CSP']
response.headers['X-Content-Type-Options'] = config.WEB_SEC_HEADERS['CTO']
response.headers['X-XSS-Protection'] = config.WEB_SEC_HEADERS['XSS']
response.headers['X-Frame-Options'] = config.WEB_SEC_HEADERS['XFO']
response.headers['Referrer-Policy'] = config.WEB_SEC_HEADERS['RP']
response.headers['Server'] = config.WEB_SEC_HEADERS['Server']
return response
# Context Processors
@app.context_processor
def status():
result = {'status': 'Ready'}
session_state = rds.get_session_state()
if session_state == 'created':
result['status'] = 'Initializing...'
elif session_state == 'running':
progress = rds.get_scan_progress()
result['status'] = f'Scanning... [QUEUE:{progress}]' if progress > 0 else 'Busy...'
return result
@app.context_processor
def show_version():
return dict(version=VERSION)
@app.context_processor
def show_frequency():
result = {'frequency': None}
config = rds.get_scan_config()
if config:
result['frequency'] = config['config']['frequency']
return result
@app.context_processor
def show_vuln_count():
return dict(vuln_count=len(rds.get_vuln_data()))
@app.context_processor
def inject_config_vars():
return {
'APP_NAME': config.APP_NAME if bool(re.match(r'^[a-zA-Z0-9 _]{,10}$', config.APP_NAME)) else 'NERVIUM',
'APP_EXTENDED_NAME': config.APP_EXTENDED_NAME if bool(re.match(r'^[a-zA-Z0-9 _,]+$', config.APP_EXTENDED_NAME)) else '',
}
@app.template_filter('utf8_decode')
def utf8_decode(value):
return value.decode('utf-8') if isinstance(value, bytes) else value
if __name__ == '__main__':
if not (config.WEB_USER or config.WEB_PASSW):
reason = "The username or password environment variables are not set correctly."
service_filepath = "/lib/systemd/system/nervium.service"
if os.path.isfile(service_filepath):
reason += f"""
If you are running NERVIUM as a systemd service,
please edit {service_filepath} in order to set valid username and password.
Once done, remember to reload and restart NERVIUM:
systemctl daemon-reload && systemctl restart nervium.service
"""
sys.exit(reason)
config.WEB_PASSW_HASH = generate_password_hash(config.WEB_PASSW)
rds.initialize()
start_workers()
app.run(debug=config.WEB_DEBUG, host=config.WEB_HOST, port=config.WEB_PORT, threaded=True, use_evalex=False)