1
|
1 |
|
import logging |
2
|
1 |
|
import re |
3
|
1 |
|
import string |
4
|
|
|
|
5
|
1 |
|
from time import time |
6
|
1 |
|
from flask import Blueprint, render_template, request, redirect, flash, Response |
|
|
|
|
7
|
1 |
|
from sqlalchemy.exc import SQLAlchemyError |
|
|
|
|
8
|
|
|
|
9
|
1 |
|
from spike.model import db |
10
|
1 |
|
from spike.model.naxsi_rules import NaxsiRules |
11
|
1 |
|
from spike.model.naxsi_rulesets import NaxsiRuleSets |
12
|
1 |
|
from spike.model import naxsi_mz, naxsi_score |
13
|
|
|
|
14
|
1 |
|
rules = Blueprint('rules', __name__) |
15
|
|
|
|
16
|
|
|
|
17
|
1 |
|
@rules.route("/") |
18
|
|
|
def index(): |
19
|
1 |
|
_rules = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).all() |
20
|
1 |
|
if not _rules: |
21
|
|
|
flash("no rules found, please create one", "success") |
22
|
|
|
return redirect("/rules/new") |
23
|
1 |
|
return render_template("rules/index.html", rules=_rules) |
24
|
|
|
|
25
|
|
|
|
26
|
1 |
|
@rules.route("/plain/<int:sid>", methods=["GET"]) |
27
|
|
|
def plain(sid): |
28
|
1 |
|
_rule = NaxsiRules.query.filter(NaxsiRules.sid == sid).first() |
29
|
1 |
|
if not _rule: |
30
|
|
|
flash("no rules found, please create one", "error") |
31
|
|
|
return redirect("/rules/new") |
32
|
|
|
|
33
|
1 |
|
return Response(__get_textual_representation_rule(_rule), mimetype='text/plain') |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
@rules.route("/view/<int:sid>", methods=["GET"]) |
37
|
|
|
def view(sid): |
38
|
1 |
|
_rule = NaxsiRules.query.filter(NaxsiRules.sid == sid).first() |
39
|
1 |
|
if _rule is None: |
40
|
1 |
|
flash("no rules found, please create one", "error") |
41
|
1 |
|
return redirect("/rules/") |
42
|
|
|
|
43
|
1 |
|
return render_template("rules/view.html", rule=_rule, rtext=__get_textual_representation_rule(_rule, full=0)) |
44
|
|
|
|
45
|
|
|
|
46
|
1 |
|
@rules.route("/search/", methods=["GET"]) |
47
|
|
|
def search(): |
48
|
1 |
|
terms = request.args.get('s', '') |
49
|
|
|
|
50
|
1 |
|
if len(terms) < 2: |
51
|
1 |
|
return redirect('/rules') |
52
|
|
|
|
53
|
|
|
# No fancy injections |
54
|
1 |
|
whitelist = set(string.ascii_letters + string.digits + ':-_ ') |
55
|
1 |
|
filtered = ''.join(filter(whitelist.__contains__, terms)) |
56
|
|
|
|
57
|
1 |
|
if filtered.isdigit(): # get rule by id |
58
|
1 |
|
_rules = db.session.query(NaxsiRules).filter(NaxsiRules.sid == int(filtered)).all() |
59
|
|
|
else: |
60
|
1 |
|
expression = '%' + filtered + '%' |
61
|
1 |
|
_rules = db.session.query(NaxsiRules).filter( |
62
|
|
|
db.or_( |
63
|
|
|
NaxsiRules.msg.like(expression), |
64
|
|
|
NaxsiRules.rmks.like(expression), |
65
|
|
|
NaxsiRules.detection.like(expression) |
66
|
|
|
) |
67
|
|
|
).order_by(NaxsiRules.sid.desc()).all() |
68
|
1 |
|
return render_template("rules/index.html", rules=_rules, selection="Search: %s" % filtered, lsearch=terms) |
69
|
|
|
|
70
|
|
|
|
71
|
1 |
|
@rules.route("/new", methods=["GET", "POST"]) |
72
|
|
|
def new(): |
73
|
1 |
|
latest_sid = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first() |
74
|
1 |
|
if latest_sid is None: |
75
|
|
|
sid = 200001 |
76
|
|
|
else: |
77
|
1 |
|
sid = latest_sid.sid + 1 |
78
|
|
|
|
79
|
1 |
|
if request.method == "GET": |
80
|
1 |
|
_rulesets = NaxsiRuleSets.query.all() |
81
|
1 |
|
return render_template("rules/new.html", mz=naxsi_mz, rulesets=_rulesets, score=naxsi_score, latestn=sid) |
82
|
|
|
|
83
|
|
|
# create new rule |
84
|
1 |
|
logging.debug('Posted new request: %s', request.form) |
85
|
1 |
|
mz = "|".join(filter(len, request.form.getlist("mz") + request.form.getlist("custom_mz_val"))) |
86
|
1 |
|
score = "{}:{}".format(request.form["score"], request.form["score_%s" % request.form["score"]]) |
87
|
1 |
|
nrule = NaxsiRules(request.form["msg"], request.form["detection"], mz, score, sid, request.form["ruleset"], |
88
|
|
|
request.form["rmks"], "1", request.form['negative'], int(time())) |
89
|
1 |
|
nrule.validate() |
90
|
1 |
|
if len(nrule.error): |
91
|
|
|
flash("ERROR: {0}".format(",".join(nrule.error))) |
92
|
|
|
return redirect("/rules/new") |
93
|
1 |
|
if len(nrule.warnings): |
94
|
1 |
|
flash("WARNINGS: {0}".format(",".join(nrule.warnings))) |
95
|
1 |
|
db.session.add(nrule) |
96
|
|
|
|
97
|
1 |
|
try: |
98
|
1 |
|
db.session.commit() |
99
|
1 |
|
flash("OK: created %s : %s" % (sid, request.form["msg"]), "success") |
100
|
1 |
|
return redirect("/rules/edit/%s" % sid) |
101
|
|
|
except SQLAlchemyError: |
102
|
|
|
flash("ERROR while trying to create %s : %s" % (sid, request.form["msg"]), "error") |
103
|
|
|
|
104
|
|
|
return redirect("/rules/new") |
105
|
|
|
|
106
|
|
|
|
107
|
1 |
|
@rules.route("/edit/<int:sid>", methods=["GET", "POST"]) |
108
|
|
|
def edit(sid): |
109
|
1 |
|
rinfo = NaxsiRules.query.filter(NaxsiRules.sid == sid).first() |
110
|
1 |
|
if not rinfo: |
111
|
1 |
|
return redirect("/rules/") |
112
|
|
|
|
113
|
1 |
|
_rulesets = NaxsiRuleSets.query.all() |
114
|
1 |
|
rruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.name == rinfo.ruleset).first() |
115
|
1 |
|
custom_mz = "" |
116
|
1 |
|
mz_check = rinfo.mz |
117
|
1 |
|
if re.search(r"^\$[A-Z]+:(.*)\|[A-Z]+", mz_check): |
118
|
|
|
custom_mz = mz_check |
119
|
|
|
rinfo.mz = "custom" |
120
|
1 |
|
return render_template("rules/edit.html", mz=naxsi_mz, rulesets=_rulesets, score=naxsi_score, rules_info=rinfo, |
121
|
|
|
rule_ruleset=rruleset, custom_mz=custom_mz) |
122
|
|
|
|
123
|
|
|
|
124
|
1 |
|
@rules.route("/save/<int:sid>", methods=["POST"]) |
125
|
|
|
def save(sid): |
126
|
|
|
mz = "|".join(filter(len, request.form.getlist("mz") + request.form.getlist("custom_mz_val"))) |
127
|
|
|
score = "{}:{}".format(request.form["score"], request.form["score_%s" % request.form["score"]]) |
128
|
|
|
nrule = NaxsiRules.query.filter(NaxsiRules.sid == sid).first() |
129
|
|
|
nrule.msg = request.form["msg"] |
130
|
|
|
nrule.detection = request.form["detection"] |
131
|
|
|
nrule.mz = mz |
132
|
|
|
nrule.score = score |
133
|
|
|
nrule.ruleset = request.form["ruleset"] |
134
|
|
|
nrule.rmks = request.form["rmks"] |
135
|
|
|
nrule.active = request.form["active"] |
136
|
|
|
nrule.negative = request.form["negative"] |
137
|
|
|
nrule.timestamp = int(time()) |
138
|
|
|
nrule.validate() |
139
|
|
|
if len(nrule.error): |
140
|
|
|
flash("ERROR: {0}".format(",".join(nrule.error))) |
141
|
|
|
logging.debug("ERROR: {0}".format(",".join(nrule.error))) |
142
|
|
|
return redirect("/rules/edit/%s" % sid) |
143
|
|
|
if len(nrule.warnings): |
144
|
|
|
flash("WARNINGS: {0}".format(",".join(nrule.warnings))) |
145
|
|
|
logging.debug("WARNINGS: {0}".format(",".join(nrule.warnings))) |
146
|
|
|
db.session.add(nrule) |
147
|
|
|
try: |
148
|
|
|
db.session.commit() |
149
|
|
|
except SQLAlchemyError: |
150
|
|
|
flash("ERROR while trying to update %s : %s" % (sid, msg), "error") |
|
|
|
|
151
|
|
|
return redirect("/rules/edit/%s" % sid) |
152
|
|
|
|
153
|
|
|
|
154
|
1 |
|
@rules.route("/del/<int:sid>", methods=["GET"]) |
155
|
1 |
|
def del_sid(sid=''): |
156
|
1 |
|
nrule = NaxsiRules.query.filter(NaxsiRules.sid == sid).first() |
157
|
1 |
|
if not nrule: |
158
|
|
|
return redirect("/rules/") |
159
|
|
|
|
160
|
1 |
|
db.session.delete(nrule) |
161
|
1 |
|
try: |
162
|
1 |
|
db.session.commit() |
163
|
1 |
|
flash("OK: deleted %s : %s" % (sid, nrule.msg), "success") |
164
|
|
|
except SQLAlchemyError: |
165
|
|
|
flash("ERROR while trying to update %s : %s" % (sid, nrule.msg), "error") |
166
|
|
|
|
167
|
1 |
|
return redirect("/rules/") |
168
|
|
|
|
169
|
|
|
|
170
|
1 |
|
@rules.route("/deact/<int:sid>", methods=["GET"]) |
171
|
|
|
def deact(sid): |
172
|
1 |
|
nrule = NaxsiRules.query.filter(NaxsiRules.sid == sid).first() |
173
|
1 |
|
if nrule is None: |
174
|
1 |
|
return redirect("/rules/") |
175
|
|
|
|
176
|
1 |
|
fm = 'deactivate' if nrule.active else 'reactivate' |
177
|
1 |
|
nrule.active = not nrule.active |
178
|
|
|
|
179
|
1 |
|
db.session.add(nrule) |
180
|
1 |
|
try: |
181
|
1 |
|
db.session.commit() |
182
|
1 |
|
flash("OK: %s %sd : %s" % (fm, sid, nrule.msg), "success") |
183
|
|
|
except SQLAlchemyError: |
184
|
|
|
flash("ERROR while trying to %s %s : %s" % (fm, sid, nrule.msg), "error") |
185
|
|
|
|
186
|
1 |
|
_rulesets = NaxsiRuleSets.query.all() |
187
|
1 |
|
return render_template("rules/edit.html", mz=naxsi_mz, rulesets=_rulesets, score=naxsi_score, rules_info=nrule) |
188
|
|
|
|
189
|
|
|
|
190
|
1 |
|
def __get_textual_representation_rule(rule, full=1): |
|
|
|
|
191
|
1 |
|
if full == 1: |
192
|
1 |
|
return rule.fullstr() |
193
|
|
|
else: |
194
|
|
|
return str(rule) |
195
|
|
|
|
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.