1
|
1 |
|
import re |
2
|
|
|
|
3
|
1 |
|
from spike.model import db |
4
|
1 |
|
from shlex import shlex |
5
|
|
|
|
6
|
1 |
|
from spike.model.naxsi_rules import NaxsiRules |
7
|
1 |
|
from flask import url_for |
8
|
|
|
|
9
|
1 |
|
class NaxsiWhitelist(db.Model): |
10
|
1 |
|
__bind_key__ = 'rules' |
11
|
1 |
|
__tablename__ = 'naxsi_whitelist' |
12
|
|
|
|
13
|
1 |
|
id = db.Column(db.Integer, primary_key=True) |
14
|
1 |
|
wid = db.Column(db.String, nullable=False, unique=True) |
15
|
1 |
|
mz = db.Column(db.String(1024), nullable=False) |
16
|
1 |
|
negative = db.Column(db.Integer, nullable=False, server_default='0') |
17
|
1 |
|
active = db.Column(db.Integer, nullable=False, server_default='1') |
18
|
1 |
|
timestamp = db.Column(db.Integer, nullable=False) |
19
|
1 |
|
whitelistset = db.Column(db.String(1024), nullable=False) |
20
|
|
|
|
21
|
1 |
|
def __init__(self, wid='0', mz='', active=0, negative=0, whitelistset='', timestamp=0): |
22
|
1 |
|
self.wid = wid |
23
|
1 |
|
self.mz = mz |
24
|
1 |
|
self.active = active |
25
|
1 |
|
self.negative = negative |
26
|
1 |
|
self.whitelistset = whitelistset |
27
|
1 |
|
self.timestamp = timestamp |
28
|
1 |
|
self.warnings = [] |
29
|
1 |
|
self.error = [] |
30
|
|
|
|
31
|
1 |
|
def __str__(self): |
32
|
1 |
|
return 'BasicRule {}wl:{} "mz:{}";'.format('negative ' if self.negative else ' ', self.wid, self.mz) |
33
|
|
|
|
34
|
1 |
|
def __validate_id(self, wid): |
35
|
1 |
|
if not re.match(r'(\-?\d+,)*\-?\d+', wid): |
36
|
|
|
self.error.append('Illegal character in the whitelist id.') |
37
|
|
|
return False |
38
|
1 |
|
self.wid = wid |
39
|
1 |
|
return True |
40
|
|
|
|
41
|
1 |
|
def __validate_mz(self, mz): |
42
|
|
|
# Borrow '__validate_matchzone' from naxsi_rules.py ? |
43
|
1 |
|
self.mz = mz |
44
|
1 |
|
return True |
45
|
|
|
|
46
|
1 |
|
def parse(self, str_wl): |
47
|
1 |
|
self.warnings = list() |
48
|
1 |
|
self.error = list() |
49
|
|
|
|
50
|
1 |
|
lexer = shlex(str_wl) |
51
|
1 |
|
lexer.whitespace_split = True |
52
|
1 |
|
split = list(iter(lexer.get_token, '')) |
53
|
|
|
|
54
|
1 |
|
for piece in split: |
55
|
1 |
|
if piece == ';': |
56
|
1 |
|
continue |
57
|
1 |
|
elif piece.startswith(('"', "'")) and (piece[0] == piece[-1]): # remove (double-)quotes |
58
|
1 |
|
piece = piece[1:-1] |
59
|
|
|
|
60
|
1 |
|
if piece == 'BasicRule': |
61
|
1 |
|
continue |
62
|
1 |
|
elif piece.startswith('wl:'): |
63
|
1 |
|
self.__validate_id(piece[3:]) |
64
|
1 |
|
elif piece.startswith('mz:'): |
65
|
1 |
|
self.__validate_mz(piece[3:]) |
66
|
1 |
|
elif piece == 'negative': |
67
|
|
|
self.negative = True |
68
|
|
|
else: |
69
|
1 |
|
self.error.append('Unknown fragment: {}'.format(piece)) |
70
|
1 |
|
return False |
71
|
|
|
|
72
|
1 |
|
if 'BasicRule' not in split: |
73
|
|
|
self.error.append('No "BasicRule" keyword in {}'.format(str_wl)) |
74
|
|
|
return False |
75
|
|
|
|
76
|
1 |
|
return True |
77
|
|
|
|
78
|
1 |
|
def explain(self): |
79
|
1 |
|
def __linkify_rule(rid): |
80
|
1 |
|
if NaxsiRules.query.filter(NaxsiRules.sid == self.wid).first() is None: |
|
|
|
|
81
|
1 |
|
return rid |
82
|
|
|
return '<a href="{}">{}</a>'.format(url_for('rules.view', sid=rid), self.wid) |
83
|
|
|
|
84
|
1 |
|
if self.wid == '0': |
85
|
1 |
|
ret = 'Whitelist all rules' |
86
|
1 |
|
elif self.wid.isdigit(): |
87
|
1 |
|
ret = 'Whitelist the rule {}'.format(__linkify_rule(self.wid)) |
88
|
|
|
else: |
89
|
|
|
zones = list() |
90
|
|
|
for rid in self.wid.split(','): |
91
|
|
|
if rid.startswith('-'): |
92
|
|
|
zones.append('except the rule {}'.format(__linkify_rule(self.wid))) |
93
|
|
|
else: |
94
|
|
|
zones.append('the rule {}'.format(__linkify_rule(self.wid))) |
95
|
|
|
ret = 'Whitelist '+ ', '.join(zones) |
96
|
|
|
|
97
|
1 |
|
if not self.mz: |
98
|
1 |
|
return ret + '.' |
99
|
|
|
|
100
|
|
|
return ret + ' if matching in {}.'.format(self.mz) |
101
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.