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
|
|
|
|
10
|
1 |
|
from spike.model import db |
11
|
1 |
|
from spike.model.naxsi_whitelist import NaxsiWhitelist |
12
|
1 |
|
from spike.model.naxsi_whitelistsets import NaxsiWhitelistSets |
13
|
1 |
|
from spike.model import naxsi_mz |
14
|
|
|
|
15
|
1 |
|
whitelists = Blueprint('whitelists', __name__) |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
@whitelists.route("/") |
19
|
|
|
def index(): |
20
|
1 |
|
_wlist = NaxsiWhitelist.query.order_by(NaxsiWhitelist.wl.desc()).all() |
|
|
|
|
21
|
1 |
|
if not _wlist: |
22
|
|
|
flash("No whitelist found, please create one", "success") |
23
|
|
|
return redirect(url_for('whitelists.new')) |
24
|
1 |
|
return render_template("whitelists/index.html", whitelists=_wlist) |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
@whitelists.route("/plain/<string:wid>", methods=["GET"]) |
28
|
|
|
def plain(wid): |
29
|
1 |
|
_wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == wid).first() |
|
|
|
|
30
|
1 |
|
if not _wlist: |
31
|
1 |
|
flash("No rules found, please create one", "error") |
32
|
1 |
|
return redirect(url_for('whitelists.index')) |
33
|
1 |
|
return Response(str(_wlist), mimetype='text/plain') |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
@whitelists.route("/view/<int:wid>", methods=["GET"]) |
37
|
|
|
def view(wid): |
38
|
1 |
|
_wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == wid).first() |
|
|
|
|
39
|
1 |
|
if _wlist is None: |
40
|
1 |
|
flash("The whitelist %d was not found." % wid, "error") |
41
|
1 |
|
return redirect(url_for('whitelists.index')) |
42
|
|
|
return render_template("whitelists/view.html", whitelist=_wlist) |
43
|
|
|
|
44
|
|
|
|
45
|
1 |
|
@whitelists.route("/edit/<string:wid>", methods=["GET"]) |
46
|
|
|
def edit(wid): |
47
|
|
|
return redirect(url_for('whitelists.new')) |
48
|
|
|
|
49
|
|
|
|
50
|
1 |
View Code Duplication |
@whitelists.route("/del/<string:wid>", methods=["GET"]) |
|
|
|
|
51
|
|
|
def del_sid(wid): |
52
|
1 |
|
_wlist = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == wid).first() |
|
|
|
|
53
|
1 |
|
if not _wlist: |
54
|
1 |
|
return redirect(url_for('whitelists.index')) |
55
|
|
|
|
56
|
1 |
|
db.session.delete(_wlist) |
57
|
1 |
|
db.session.commit() |
58
|
|
|
|
59
|
1 |
|
flash("Successfully deleted %s" % wid, "success") |
60
|
1 |
|
return redirect(url_for('whitelists.index')) |
61
|
|
|
|
62
|
|
|
|
63
|
1 |
|
@whitelists.route("/generate", methods=["GET", "POST"]) |
64
|
|
|
def generate(): |
65
|
1 |
|
if request.method == "GET": |
66
|
1 |
|
return render_template("misc/whitelist_generator.html") |
67
|
1 |
|
nxlogs = request.form.get('nxlogs', '') |
68
|
|
|
|
69
|
1 |
|
if not nxlogs: |
70
|
1 |
|
flash('Please input nxlogs') |
71
|
1 |
|
return render_template("misc/whitelist_generator.html") |
72
|
|
|
|
73
|
1 |
|
whitelist = set() |
74
|
1 |
|
for nxlog in nxlogs.split('\n'): |
75
|
1 |
|
nxlog = nxlog.strip() |
76
|
1 |
|
if not nxlog: |
77
|
|
|
continue |
78
|
1 |
|
start = nxlog.find("ip=") |
79
|
1 |
|
if start < 0: |
80
|
1 |
|
flash('{} is an invalid extlog, string "ip=" not found.'.format(nxlog)) |
81
|
1 |
|
return render_template("misc/whitelist_generator.html", nxlogs=nxlogs) |
82
|
|
|
|
83
|
1 |
|
end = nxlog.find(", ") |
84
|
1 |
|
if end < 0: |
85
|
1 |
|
flash('{} is an invalid extlog, string "," not found.'.format(nxlog)) |
86
|
1 |
|
return render_template("misc/whitelist_generator.html", nxlogs=nxlogs) |
87
|
|
|
|
88
|
|
|
# Flatten the dict, since parse_qs is a bit annoying |
89
|
1 |
|
nxdic = parse_qs(nxlog[start:end]) |
90
|
1 |
|
for key, value in nxdic.items(): |
91
|
1 |
|
nxdic[key] = value[0] |
92
|
|
|
|
93
|
1 |
|
cpt = 0 |
94
|
1 |
|
while "id{}".format(cpt) in nxdic: |
95
|
1 |
|
_id = "id{}".format(cpt) |
96
|
1 |
|
_var_name = "var_name{}".format(cpt) |
97
|
1 |
|
_zone = "zone{}".format(cpt) |
98
|
1 |
|
if nxdic[_zone].endswith('|NAME'): |
99
|
|
|
if "var_name{}".format(cpt) in nxdic: |
100
|
|
|
whitelist.add('BasicRule wl:{} "mz:${}_VAR:{}|NAME"'.format(nxdic[_id], nxdic[_zone][:4], nxdic[_var_name])) |
|
|
|
|
101
|
|
|
else: |
102
|
|
|
whitelist.add('BasicRule wl:{} "mz:{}"'.format(nxdic[_id], nxdic[_zone])) |
103
|
1 |
|
elif "var_name{}".format(cpt) in nxdic: |
104
|
1 |
|
whitelist.add('BasicRule wl:{} "mz:{}:{}"'.format(nxdic[_id], "$"+nxdic[_zone]+"_VAR", nxdic[_var_name])) |
|
|
|
|
105
|
|
|
else: |
106
|
|
|
whitelist.add('BasicRule wl:{} "mz:{}"'.format(nxdic[_id], nxdic[_zone])) |
107
|
1 |
|
cpt += 1 |
108
|
1 |
|
return render_template("misc/whitelist_generator.html", whitelist='<br>'.join(whitelist) + ';', nxlogs=nxlogs) |
109
|
|
|
|
110
|
|
|
|
111
|
1 |
|
@whitelists.route('/new', methods=["GET", "POST"]) |
112
|
|
|
def new(): |
113
|
1 |
|
if request.method == "GET": |
114
|
1 |
|
_whitelistesets = NaxsiWhitelistSets.query.all() |
|
|
|
|
115
|
1 |
|
return render_template('whitelists/new.html', matchzones=naxsi_mz, whitelistsets=_whitelistesets) |
116
|
|
|
|
117
|
1 |
|
logging.debug('Posted new request: %s', request.form) |
118
|
|
|
|
119
|
1 |
|
mz = request.form.getlist("mz") + request.form.getlist("custom_mz_val") |
120
|
1 |
|
wid = request.form.get('wl', '') |
121
|
1 |
|
whitelistset = request.form.get("whitelistset", '') |
122
|
|
|
|
123
|
1 |
|
if not wid: |
124
|
1 |
|
flash('Please enter a wl', category='error') |
125
|
1 |
|
return render_template('whitelists/new.html') |
126
|
1 |
|
elif not whitelistset: |
127
|
1 |
|
flash('Please enter a whitelistset', category='error') |
128
|
1 |
|
return render_template('whitelists/new.html') |
129
|
|
|
|
130
|
1 |
|
wlist = NaxsiWhitelist(wl=wid, timestamp=int(time()), |
131
|
|
|
whitelistset=whitelistset, mz=mz, active=1, |
132
|
|
|
negative=request.form.get("negative", "") == 'checked') |
133
|
1 |
|
errors, warnings = wlist.validate() |
134
|
|
|
|
135
|
1 |
|
if errors: |
136
|
1 |
|
flash(",".join(errors), 'error') |
137
|
1 |
|
return redirect(url_for('whitelists.new')) |
138
|
1 |
|
elif warnings: |
139
|
|
|
flash(",".join(warnings), 'warning') |
140
|
|
|
|
141
|
1 |
|
wlist.mz = '|'.join(wlist.mz) |
142
|
1 |
|
db.session.add(wlist) |
143
|
1 |
|
db.session.commit() |
144
|
|
|
|
145
|
|
|
return render_template('whitelists/index.html') |
146
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.