1
|
1 |
|
try: |
2
|
1 |
|
from urlparse import parse_qs |
3
|
|
|
except ImportError: # python3 |
4
|
|
|
from urllib.parse import parse_qs |
5
|
1 |
|
import logging |
6
|
1 |
|
from time import time |
7
|
|
|
|
8
|
1 |
|
from flask import Blueprint, render_template, request, redirect, flash, Response, url_for |
9
|
1 |
|
from sqlalchemy.exc import SQLAlchemyError |
|
|
|
|
10
|
|
|
|
11
|
1 |
|
from spike.model import db |
12
|
1 |
|
from spike.model.naxsi_whitelist import NaxsiWhitelist |
13
|
1 |
|
from spike.model.naxsi_whitelistsets import NaxsiWhitelistSets |
14
|
1 |
|
from spike.model import naxsi_mz |
15
|
|
|
|
16
|
1 |
|
whitelists = Blueprint('whitelists', __name__) |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
@whitelists.route("/") |
20
|
|
|
def index(): |
21
|
1 |
|
_wlist = NaxsiWhitelist.query.order_by(NaxsiWhitelist.wid.desc()).all() |
|
|
|
|
22
|
1 |
|
if not _wlist: |
23
|
|
|
flash("No whitelist found, please create one", "success") |
24
|
|
|
return redirect(url_for('whitelists.new')) |
25
|
1 |
|
return render_template("whitelists/index.html", whitelists=_wlist) |
26
|
|
|
|
27
|
|
|
|
28
|
1 |
|
@whitelists.route("/plain/<string:wid>", methods=["GET"]) |
29
|
|
|
def plain(wid): |
30
|
1 |
|
_wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == wid).first() |
|
|
|
|
31
|
1 |
|
if not _wlist: |
32
|
1 |
|
flash("No rules found, please create one", "error") |
33
|
1 |
|
return redirect(url_for('whitelists.index')) |
34
|
1 |
|
return Response(str(_wlist), mimetype='text/plain') |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
@whitelists.route("/view/<int:wid>", methods=["GET"]) |
38
|
|
|
def view(wid): |
39
|
1 |
|
_wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == wid).first() |
|
|
|
|
40
|
1 |
|
if _wlist is None: |
41
|
1 |
|
flash("The whitelist %d was not found." % wid, "error") |
42
|
1 |
|
return redirect(url_for('whitelists.index')) |
43
|
|
|
return render_template("whitelists/view.html", whitelist=_wlist) |
44
|
|
|
|
45
|
|
|
|
46
|
1 |
|
@whitelists.route("/edit/<string:wid>", methods=["GET"]) |
47
|
|
|
def edit(wid): |
48
|
|
|
return redirect(url_for('whitelists.new')) |
49
|
|
|
|
50
|
|
|
|
51
|
1 |
View Code Duplication |
@whitelists.route("/del/<string:wid>", methods=["GET"]) |
|
|
|
|
52
|
|
|
def del_sid(wid): |
53
|
1 |
|
_wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == wid).first() |
|
|
|
|
54
|
1 |
|
if not _wlist: |
55
|
1 |
|
return redirect(url_for('whitelists.index')) |
56
|
|
|
|
57
|
1 |
|
db.session.delete(_wlist) |
58
|
|
|
|
59
|
1 |
|
try: |
60
|
1 |
|
db.session.commit() |
61
|
1 |
|
flash("Successfully deleted %s" % wid, "success") |
62
|
|
|
except SQLAlchemyError: |
63
|
|
|
flash("Error while trying to update %s" % wid, "error") |
64
|
|
|
|
65
|
1 |
|
return redirect(url_for('whitelists.index')) |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
@whitelists.route("/generate", methods=["GET", "POST"]) |
69
|
|
|
def generate(): |
70
|
1 |
|
if request.method == "GET": |
71
|
1 |
|
return render_template("misc/whitelist_generator.html") |
72
|
1 |
|
nxlogs = request.form.get('nxlogs', '') |
73
|
|
|
|
74
|
1 |
|
if not nxlogs: |
75
|
1 |
|
flash('Please input nxlogs') |
76
|
1 |
|
return render_template("misc/whitelist_generator.html") |
77
|
|
|
|
78
|
1 |
|
whitelist = list() |
79
|
1 |
|
for nxlog in nxlogs.split('\n'): |
80
|
1 |
|
if not nxlog: |
81
|
|
|
continue |
82
|
1 |
|
start = nxlog.find("ip=") |
83
|
1 |
|
if start < 0: |
84
|
1 |
|
flash('{} is an invalid extlog, string "ip=" not found.'.format(nxlog)) |
85
|
1 |
|
return render_template("misc/whitelist_generator.html", nxlogs=nxlogs) |
86
|
|
|
|
87
|
1 |
|
end = nxlog.find(", ") |
88
|
1 |
|
if end < 0: |
89
|
1 |
|
flash('{} is an invalid extlog, string "," not found.'.format(nxlog)) |
90
|
1 |
|
return render_template("misc/whitelist_generator.html", nxlogs=nxlogs) |
91
|
|
|
|
92
|
|
|
# Flatten the dict, since parse_qs is a bit annoying |
93
|
1 |
|
nxdic = parse_qs(nxlog[start:end]) |
94
|
1 |
|
for key, value in nxdic.items(): |
95
|
1 |
|
nxdic[key] = value[0] |
96
|
|
|
|
97
|
1 |
|
cpt = 0 |
98
|
1 |
View Code Duplication |
while "id{}".format(cpt) in nxdic: |
|
|
|
|
99
|
1 |
|
_id = "id{}".format(cpt) |
100
|
1 |
|
_var_name = "var_name{}".format(cpt) |
101
|
1 |
|
_zone = "zone{}".format(cpt) |
102
|
1 |
|
if "var_name{}".format(cpt) in nxdic: |
103
|
1 |
|
whitelist.append('BasicRule wl:{} "mz:{}:{}"'.format(nxdic[_id], nxdic[_var_name], nxdic[_zone])) |
104
|
|
|
else: |
105
|
|
|
whitelist.append('BasicRule wl:{} "mz:{}"'.format(nxdic[_id], nxdic[_var_name])) |
106
|
1 |
|
cpt += 1 |
107
|
1 |
|
return render_template("misc/whitelist_generator.html", whitelist='\n'.join(whitelist) + ';', nxlogs=nxlogs) |
108
|
|
|
|
109
|
|
|
|
110
|
1 |
|
@whitelists.route('/new', methods=["GET", "POST"]) |
111
|
|
|
def new(): |
112
|
1 |
|
if request.method == "GET": |
113
|
1 |
|
_whitelistesets = NaxsiWhitelistSets.query.all() |
|
|
|
|
114
|
1 |
|
return render_template('whitelists/new.html', matchzones=naxsi_mz, whitelistsets=_whitelistesets) |
115
|
|
|
|
116
|
1 |
|
logging.debug('Posted new request: %s', request.form) |
117
|
|
|
|
118
|
1 |
|
mz = "|".join(filter(len, request.form.getlist("mz") + request.form.getlist("custom_mz_val"))) |
119
|
1 |
|
wid = request.form.get('wid', '') |
120
|
1 |
|
whitelistset = request.form.get("whitelistset", '') |
121
|
|
|
|
122
|
1 |
|
if not wid: |
123
|
1 |
|
flash('Please enter a wid', category='error') |
124
|
1 |
|
return render_template('whitelists/new.html') |
125
|
1 |
|
elif not whitelistset: |
126
|
1 |
|
flash('Please enter a whitelistset', category='error') |
127
|
1 |
|
return render_template('whitelists/new.html') |
128
|
|
|
|
129
|
1 |
|
wlist = NaxsiWhitelist(wid=wid, timestamp=int(time()), |
130
|
|
|
whitelistset=whitelistset, mz=mz, active=1, |
131
|
|
|
negative=request.form.get("negative", "") == 'checked') |
132
|
1 |
|
wlist.validate() |
133
|
|
|
|
134
|
1 |
|
if wlist.error: |
135
|
1 |
|
flash(",".join(wlist.error), 'error') |
136
|
1 |
|
return redirect(url_for('whitelists.new')) |
137
|
1 |
|
elif wlist.warnings: |
138
|
|
|
flash(",".join(wlist.warnings), 'warning') |
139
|
|
|
|
140
|
1 |
|
db.session.add(wlist) |
141
|
|
|
|
142
|
1 |
|
try: |
143
|
1 |
|
db.session.commit() |
144
|
1 |
|
flash('Created!') |
145
|
|
|
except SQLAlchemyError as e: |
|
|
|
|
146
|
|
|
flash("Error : %s" % e, "error") |
147
|
|
|
|
148
|
|
|
return render_template('whitelists/index.html') |
149
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.