1
|
1 |
|
try: |
2
|
1 |
|
from urlparse import parse_qs |
|
|
|
|
3
|
|
|
except ImportError: # python3 |
4
|
|
|
from urllib.parse import parse_qs |
5
|
|
|
|
6
|
1 |
|
from flask import Blueprint, render_template, request, redirect, flash, url_for |
7
|
|
|
|
8
|
1 |
|
from nxapi import nxlog |
|
|
|
|
9
|
|
|
|
10
|
1 |
|
from spike.model.naxsi_rules import NaxsiRules |
11
|
1 |
|
from spike.model.naxsi_whitelist import NaxsiWhitelist |
12
|
|
|
|
13
|
1 |
|
sandbox = Blueprint('sandbox', __name__) |
14
|
|
|
|
15
|
|
|
|
16
|
1 |
|
@sandbox.route("/", methods=["GET"]) |
17
|
|
|
def index(): |
18
|
1 |
|
return render_template("misc/sandbox.html") |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
@sandbox.route("/explain_rule/", methods=["GET", "POST"]) |
22
|
|
|
def explain_rule(): |
23
|
1 |
|
errors = warnings = list() |
24
|
1 |
|
rule_get = request.args.get('rule', '') |
25
|
1 |
|
rule_post = request.form.get("rule", '') |
26
|
1 |
View Code Duplication |
if rule_get.isdigit(): # explain a rule by id |
|
|
|
|
27
|
1 |
|
_rule = NaxsiRules.query.filter(NaxsiRules.sid == rule_get).first() |
|
|
|
|
28
|
1 |
|
if _rule is None: |
29
|
1 |
|
flash('Not rule with id %s' % rule_get) |
30
|
1 |
|
return redirect(url_for("sandbox.index")) |
31
|
1 |
|
elif rule_get is not '': |
32
|
1 |
|
flash('Please provide a numeric id') |
33
|
1 |
|
return redirect(url_for("sandbox.index")) |
34
|
1 |
|
elif not rule_post: |
35
|
1 |
|
flash('Please provide a rule') |
36
|
1 |
|
return redirect(url_for("sandbox.index")) |
37
|
|
|
else: |
38
|
1 |
|
_rule = NaxsiRules() |
39
|
1 |
|
errors, warnings, rdict = _rule.parse_rule(rule_post) |
40
|
1 |
|
_rule = NaxsiRules() |
41
|
1 |
|
_rule.from_dict(rdict) |
42
|
1 |
|
_rule.errors = errors |
43
|
1 |
|
_rule.warnings = warnings |
44
|
|
|
|
45
|
1 |
|
if _rule.errors: |
46
|
1 |
|
flash('You rule is wrong', 'error') |
47
|
1 |
|
return render_template("misc/sandbox.html") |
48
|
|
|
|
49
|
1 |
|
if 'visualise_rule' in request.form: |
50
|
1 |
|
if _rule.detection.startswith('rx:'): |
51
|
|
|
return redirect('https://regexper.com/#' + _rule.detection[3:]) |
52
|
|
|
else: |
53
|
1 |
|
flash('The rule is not a regexp, so you can not visualize it.', category='error') |
54
|
|
|
|
55
|
1 |
|
if errors: |
56
|
|
|
for error in errors: |
57
|
|
|
flash(error, category='error') |
58
|
1 |
|
if warnings: |
59
|
1 |
|
for warnings in warnings: |
60
|
1 |
|
flash(warnings, category='warning') |
61
|
|
|
|
62
|
1 |
|
return render_template("misc/sandbox.html", rule_explaination=_rule.explain(), rule=_rule) |
63
|
|
|
|
64
|
|
|
|
65
|
1 |
|
@sandbox.route("/explain_whitelist/", methods=["GET", "POST"]) |
66
|
|
|
def explain_whitelist(): |
67
|
1 |
|
whitelist_get = request.args.get('whitelist', '') |
68
|
1 |
|
whitelist_post = request.form.get('whitelist', '') |
69
|
1 |
View Code Duplication |
if whitelist_get.isdigit(): # explain a whitelist by id |
|
|
|
|
70
|
1 |
|
_wl = NaxsiWhitelist.query.filter(NaxsiWhitelist.id == whitelist_get).first() |
|
|
|
|
71
|
1 |
|
if _wl is None: |
72
|
1 |
|
flash('Not rule with id %s' % whitelist_get) |
73
|
1 |
|
return redirect(url_for("sandbox.index")) |
74
|
1 |
|
elif whitelist_get is not '': |
75
|
1 |
|
flash('Please provide a numeric id') |
76
|
1 |
|
return redirect(url_for("sandbox.index")) |
77
|
1 |
|
elif not whitelist_post: |
78
|
1 |
|
flash('Please provide a whitelist') |
79
|
1 |
|
return redirect(url_for("sandbox.index")) |
80
|
|
|
else: |
81
|
1 |
|
_wl = NaxsiWhitelist() |
82
|
1 |
|
errors, warnings, rdict = _wl.parse(whitelist_post) |
83
|
1 |
|
_wl = NaxsiWhitelist() |
84
|
1 |
|
_wl.from_dict(rdict) |
85
|
1 |
|
_wl.errors = errors |
86
|
1 |
|
_wl.warnings = warnings |
87
|
|
|
|
88
|
1 |
|
if _wl.errors: |
89
|
1 |
|
for error in _wl.errors: |
90
|
1 |
|
flash(error, category='error') |
91
|
1 |
|
return render_template("misc/sandbox.html", whitelist=_wl) |
92
|
1 |
|
if _wl.warnings: |
93
|
|
|
for warnings in _wl.warnings: |
94
|
|
|
flash(warnings, category='warning') |
95
|
|
|
|
96
|
1 |
|
return render_template("misc/sandbox.html", whitelist_explaination=_wl.explain(), whitelist=_wl) |
97
|
|
|
|
98
|
|
|
|
99
|
1 |
|
@sandbox.route('/explain_nxlog/', methods=["POST"]) |
100
|
|
|
def explain_nxlog(): |
101
|
1 |
|
_nxlog = request.form.get("nxlog", '') |
102
|
1 |
|
if not _nxlog: |
103
|
1 |
|
return redirect(url_for("sandbox.index")) |
104
|
|
|
|
105
|
1 |
|
errors, nxdic = nxlog.parse_nxlog(_nxlog) |
106
|
1 |
|
if errors: |
107
|
1 |
|
flash(''.join(errors)) |
108
|
1 |
|
return redirect(url_for("sandbox.index")) |
109
|
|
|
|
110
|
|
|
return render_template("misc/sandbox.html", nxlog_explaination=nxlog.explain_nxlog(nxdic), nxlog=_nxlog) |
111
|
|
|
|