|
1
|
|
|
from time import strftime, localtime |
|
|
|
|
|
|
2
|
|
|
import re |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
from spike import create_app |
|
5
|
|
|
from spike.model import db |
|
6
|
|
|
from spike.model.naxsi_rules import NaxsiRules |
|
7
|
|
|
|
|
8
|
|
|
try: |
|
9
|
|
|
from urlparse import urlparse |
|
10
|
|
|
except ImportError: # python3 |
|
11
|
|
|
from urllib.parse import urlparse |
|
12
|
|
|
|
|
13
|
|
|
import unittest |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class FlaskrTestCase(unittest.TestCase): |
|
17
|
|
|
def setUp(self): |
|
18
|
|
|
app = create_app() |
|
19
|
|
|
db.init_app(app) |
|
20
|
|
|
app.config['TESTING'] = True |
|
21
|
|
|
self.app = app.test_client() |
|
22
|
|
|
self.created_rules = list() |
|
23
|
|
|
|
|
24
|
|
|
def test_sandbox_rule(self): |
|
25
|
|
|
rv = self.app.get('/sandbox/rule') |
|
26
|
|
|
self.assertEqual(rv.status_code, 200) |
|
27
|
|
|
|
|
28
|
|
|
def test_sandbox_visualize(self): |
|
29
|
|
|
data = {'rule': 'MainRule "rx:^POUET$" "msg: sqli" "mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie" "s:$SQL:8" id:1005;', |
|
30
|
|
|
'visualise_rule': '1'} |
|
31
|
|
|
rv = self.app.post('/sandbox/rule', data=data) |
|
32
|
|
|
self.assertEqual(rv.status_code, 302) |
|
33
|
|
|
self.assertIn('https://regexper.com/#^POUET$', str(rv.data)) |
|
34
|
|
|
|
|
35
|
|
|
del data['visualise_rule'] |
|
36
|
|
|
data['explain_rule'] = 1 |
|
37
|
|
|
rv = self.app.post('/sandbox/rule', data=data) |
|
38
|
|
|
_rule = NaxsiRules('sqli', 'rx:^POUET$', 'BODY|URL|ARGS|$HEADERS_VAR:Cookie', '$SQL:8', '1005', "", "sqli") |
|
39
|
|
|
self.assertIn(str(_rule.explain()), str(rv.data).replace('\\', '')) |
|
40
|
|
|
|
|
41
|
|
|
def test_explain_rule(self): |
|
42
|
|
|
rv = self.app.get('/sandbox/explain_rule/') |
|
43
|
|
|
self.assertEqual(rv.status_code, 302) |
|
44
|
|
|
self.assertEqual(urlparse(rv.location).path, '/rules/') |
|
45
|
|
|
|
|
46
|
|
|
_rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first() |
|
|
|
|
|
|
47
|
|
|
rv = self.app.get('/sandbox/explain_rule/?rule={0}'.format(_rule.sid + 1), follow_redirects=True) |
|
48
|
|
|
self.assertIn('Not rule with id {0}'.format(_rule.sid + 1), str(rv.data)) |
|
49
|
|
|
|
|
50
|
|
|
rv = self.app.get('/sandbox/explain_rule/?rule={0}'.format(_rule.sid)) |
|
51
|
|
|
self.assertEqual(rv.status_code, 200) |
|
52
|
|
|
self.assertIn(_rule.explain(), str(rv.data)) |
|
53
|
|
|
|
|
54
|
|
|
def test_explain_nxlog(self): |
|
55
|
|
|
rv = self.app.get('/sandbox/explain_nxlog/') |
|
56
|
|
|
self.assertEqual(rv.status_code, 405) # we only accept POST there. |
|
57
|
|
|
|
|
58
|
|
|
rv = self.app.post('/sandbox/explain_nxlog/') |
|
59
|
|
|
self.assertEqual(rv.status_code, 302) |
|
60
|
|
|
|
|
61
|
|
|
rv = self.app.post('/sandbox/explain_nxlog/', data={'nxlog': '1234, lol'}) |
|
62
|
|
|
self.assertEqual(rv.status_code, 302) |
|
63
|
|
|
|
|
64
|
|
|
rv = self.app.post('/sandbox/explain_nxlog/', data={'nxlog': 'ip=1234'}) |
|
65
|
|
|
self.assertEqual(rv.status_code, 302) |
|
66
|
|
|
|
|
67
|
|
|
nxlog = '2013/11/10 07:36:19 [error] 8278#0: *5932 NAXSI_FMT: ip=X.X.X.X&server=Y.Y.Y.Y&' |
|
68
|
|
|
nxlog += 'uri=/phpMyAdmin-2.8.2/scripts/setup.php&learning=0&vers=0.52&total_processed=472&total_blocked=204&' |
|
69
|
|
|
nxlog += 'block=0&cscore0=$UWA&score0=8&zone0=HEADERS&id0=42000227&var_name0=user-agent, client: X.X.X.X,' |
|
70
|
|
|
nxlog += 'server: blog.memze.ro, request: "GET /phpMyAdmin-2.8.2/scripts/setup.php HTTP/1.1", host: "X.X.X.X"' |
|
71
|
|
|
|
|
72
|
|
|
rv = self.app.post('/sandbox/explain_nxlog/', data={'nxlog': nxlog}) |
|
73
|
|
|
|
|
74
|
|
|
self.assertIn('performed a request to', str(rv.data)) |
|
|
|
|
|