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.naxsi_rules import NaxsiRules |
15
|
1 |
|
from spike.model import naxsi_mz |
16
|
|
|
|
17
|
1 |
|
whitelists = Blueprint('whitelists', __name__) |
18
|
|
|
|
19
|
|
|
|
20
|
1 |
|
@whitelists.route("/") |
21
|
|
|
def index(): |
22
|
1 |
|
_wlist = NaxsiWhitelist.query.order_by(NaxsiWhitelist.wid.desc()).all() |
|
|
|
|
23
|
1 |
|
if not _wlist: |
24
|
1 |
|
flash("No whitelist found, please create one", "success") |
25
|
1 |
|
return redirect(url_for('whitelists.new')) |
26
|
|
|
return render_template("whitelists/index.html", whitelists=_wlist) |
27
|
|
|
|
28
|
|
|
|
29
|
1 |
|
@whitelists.route("/plain/<string:wid>", methods=["GET"]) |
30
|
|
|
def plain(wid): |
31
|
|
|
_wlist = NaxsiWhitelist.query.filter(NaxsiRules.sid == wid).first() |
|
|
|
|
32
|
|
|
if not _wlist: |
33
|
|
|
flash("no rules found, please create one", "error") |
34
|
|
|
return redirect(url_for('whitelists.index')) |
35
|
|
|
return Response(_wlist.fullstr(), mimetype='text/plain') |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
@whitelists.route("/view/<string:wid>", methods=["GET"]) |
39
|
|
|
def view(wid): |
40
|
|
|
_wlist = NaxsiWhitelist.query.filter(NaxsiRules.sid == wid).first() |
|
|
|
|
41
|
|
|
if _wlist is None: |
42
|
|
|
flash("no rules found, please create one", "error") |
43
|
|
|
return redirect(url_for('whitelists.index')) |
44
|
|
|
return render_template("rules/view.html", rule=_wlist, rtext=_wlist) |
45
|
|
|
|
46
|
|
|
|
47
|
1 |
|
@whitelists.route("/edit/<string:wid>", methods=["GET"]) |
48
|
|
|
def edit(wid): |
49
|
|
|
return redirect(url_for('whitelists.new')) |
50
|
|
|
|
51
|
|
|
|
52
|
1 |
|
@whitelists.route("/explain/", methods=["GET", "POST"]) |
53
|
|
|
def explain(): |
54
|
|
|
return redirect(url_for('whitelists.new')) |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
@whitelists.route("/del/<string:wid>", methods=["GET"]) |
58
|
|
|
def del_sid(wid): |
59
|
|
|
return redirect(url_for('whitelists.new')) |
60
|
|
|
|
61
|
|
|
|
62
|
1 |
|
@whitelists.route("/generate", methods=["GET", "POST"]) |
63
|
|
|
def generate(): |
64
|
1 |
|
if request.method == "GET": |
65
|
1 |
|
return render_template("misc/whitelist_generator.html") |
66
|
1 |
|
nxlogs = request.form.get('nxlogs', '') |
67
|
|
|
|
68
|
1 |
|
if not nxlogs: |
69
|
1 |
|
flash('Please input nxlogs') |
70
|
1 |
|
return render_template("misc/whitelist_generator.html") |
71
|
|
|
|
72
|
1 |
|
whitelist = list() |
73
|
1 |
|
for nxlog in nxlogs.split('\n'): |
74
|
1 |
|
if not nxlog: |
75
|
|
|
continue |
76
|
1 |
|
start = nxlog.find("ip=") |
77
|
1 |
|
if start < 0: |
78
|
1 |
|
flash('{} is an invalid extlog, string "ip=" not found.'.format(nxlog)) |
79
|
1 |
|
return render_template("misc/whitelist_generator.html", nxlogs=nxlogs) |
80
|
|
|
|
81
|
1 |
|
end = nxlog.find(", ") |
82
|
1 |
|
if end < 0: |
83
|
1 |
|
flash('{} is an invalid extlog, string "," not found.'.format(nxlog)) |
84
|
1 |
|
return render_template("misc/whitelist_generator.html", nxlogs=nxlogs) |
85
|
|
|
|
86
|
|
|
# Flatten the dict, since parse_qs is a bit annoying |
87
|
1 |
|
nxdic = parse_qs(nxlog[start:end]) |
88
|
1 |
|
for key, value in nxdic.items(): |
89
|
1 |
|
nxdic[key] = value[0] |
90
|
|
|
|
91
|
1 |
|
cpt = 0 |
92
|
1 |
|
while "id{}".format(cpt) in nxdic: |
|
|
|
|
93
|
1 |
|
_id = "id{}".format(cpt) |
94
|
1 |
|
_var_name = "var_name{}".format(cpt) |
95
|
1 |
|
_zone = "zone{}".format(cpt) |
96
|
1 |
|
if "var_name{}".format(cpt) in nxdic: |
97
|
1 |
|
whitelist.append('BasicRule wl:{} "mz:{}:{}"'.format(nxdic[_id], nxdic[_var_name], nxdic[_zone])) |
98
|
|
|
else: |
99
|
|
|
whitelist.append('BasicRule wl:{} "mz:{}"'.format(nxdic[_id], nxdic[_var_name])) |
100
|
1 |
|
cpt += 1 |
101
|
1 |
|
return render_template("misc/whitelist_generator.html", whitelist='\n'.join(whitelist) + ';', nxlogs=nxlogs) |
102
|
|
|
|
103
|
|
|
|
104
|
1 |
|
@whitelists.route('/new', methods=["GET", "POST"]) |
105
|
|
|
def new(): |
106
|
1 |
|
if request.method == "GET": |
107
|
1 |
|
_whitelistesets = NaxsiWhitelistSets.query.all() |
|
|
|
|
108
|
1 |
|
return render_template('whitelists/new.html', matchzones=naxsi_mz, whitelistsets=_whitelistesets) |
109
|
|
|
|
110
|
|
|
logging.debug('Posted new request: %s', request.form) |
111
|
|
|
mz = "|".join(filter(len, request.form.getlist("mz") + request.form.getlist("custom_mz_val"))) |
112
|
|
|
|
113
|
|
|
score = request.form.get("score", "") |
|
|
|
|
114
|
|
|
score += ':' |
115
|
|
|
score += request.form.get("score_%s" % request.form.get("score", ""), "") |
116
|
|
|
|
117
|
|
|
wlist = NaxsiWhitelist(wid=request.form.get("id", ""), timestamp=int(time()), |
118
|
|
|
whitelistset=request.form.get("whitelistset", ""), mz=mz, active=1, |
119
|
|
|
negative=request.form.get("negative", "") == 'checked') |
120
|
|
|
|
121
|
|
|
wlist.validate() |
|
|
|
|
122
|
|
|
|
123
|
|
|
if wlist.error: |
124
|
|
|
flash("ERROR: {0}".format(",".join(wlist.error))) |
125
|
|
|
return redirect("/rules/new") |
126
|
|
|
elif wlist.warnings: |
127
|
|
|
flash("WARNINGS: {0}".format(",".join(wlist.warnings))) |
128
|
|
|
db.session.add(wlist) |
129
|
|
|
|
130
|
|
|
try: |
131
|
|
|
db.session.commit() |
132
|
|
|
flash('Created!') |
133
|
|
|
except SQLAlchemyError as e: |
|
|
|
|
134
|
|
|
flash("Error : %s" % e, "error") |
135
|
|
|
|
136
|
|
|
return redirect(url_for('whitelists.index')) |
137
|
|
|
|
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.