Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 2e6575a

Browse files
committed
inital commit of regression tests into main branch
1 parent 0b8ab44 commit 2e6575a

File tree

145 files changed

+10770
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+10770
-9
lines changed

util/regression-tests/CRS_Tests.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from ftw import ruleset, logchecker, testrunner
2+
import pytest
3+
import pdb
4+
import sys
5+
import re
6+
import os
7+
import ConfigParser
8+
9+
def test_crs(ruleset, test, logchecker_obj):
10+
runner = testrunner.TestRunner()
11+
for stage in test.stages:
12+
runner.run_stage(stage, logchecker_obj)
13+
14+
class FooLogChecker(logchecker.LogChecker):
15+
16+
def reverse_readline(self, filename):
17+
with open(filename) as f:
18+
f.seek(0, os.SEEK_END)
19+
position = f.tell()
20+
line = ''
21+
while position >= 0:
22+
f.seek(position)
23+
next_char = f.read(1)
24+
if next_char == "\n":
25+
yield line[::-1]
26+
line = ''
27+
else:
28+
line += next_char
29+
position -= 1
30+
yield line[::-1]
31+
32+
def get_logs(self):
33+
import datetime
34+
config = ConfigParser.ConfigParser()
35+
config.read("settings.ini")
36+
log_location = config.get('settings', 'log_location')
37+
our_logs = []
38+
pattern = re.compile(r"\[([A-Z][a-z]{2} [A-z][a-z]{2} \d{1,2} \d{1,2}\:\d{1,2}\:\d{1,2}\.\d+? \d{4})\]")
39+
for lline in self.reverse_readline(log_location):
40+
# Extract dates from each line
41+
match = re.match(pattern,lline)
42+
if match:
43+
log_date = match.group(1)
44+
# Convert our date
45+
log_date = datetime.datetime.strptime(log_date, "%a %b %d %H:%M:%S.%f %Y")
46+
ftw_start = self.start
47+
ftw_end = self.end
48+
# If we have a log date in range
49+
if log_date <= ftw_end and log_date >= ftw_start:
50+
our_logs.append(lline)
51+
# If our log is from before FTW started stop
52+
if(log_date < ftw_start):
53+
break
54+
return our_logs
55+
@pytest.fixture
56+
def logchecker_obj():
57+
return FooLogChecker()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from ftw import ruleset, logchecker, testrunner
2+
import pytest
3+
import sys
4+
import re
5+
import os
6+
import ConfigParser
7+
8+
def test_crs(ruleset, test, logchecker_obj, with_journal, tablename):
9+
runner = testrunner.TestRunner()
10+
for stage in test.stages:
11+
runner.run_stage_with_journal(test.ruleset_meta['name'], test, with_journal, tablename, logchecker_obj)
12+
13+
class FooLogChecker(logchecker.LogChecker):
14+
15+
def reverse_readline(self, filename):
16+
with open(filename) as f:
17+
f.seek(0, os.SEEK_END)
18+
position = f.tell()
19+
line = ''
20+
while position >= 0:
21+
f.seek(position)
22+
next_char = f.read(1)
23+
if next_char == "\n":
24+
yield line[::-1]
25+
line = ''
26+
else:
27+
line += next_char
28+
position -= 1
29+
yield line[::-1]
30+
31+
def get_logs(self):
32+
import datetime
33+
config = ConfigParser.ConfigParser()
34+
config.read("settings.ini")
35+
log_location = config.get('settings', 'log_location')
36+
our_logs = []
37+
pattern = re.compile(r"\[([A-Z][a-z]{2} [A-z][a-z]{2} \d{1,2} \d{1,2}\:\d{1,2}\:\d{1,2}\.\d+? \d{4})\]")
38+
for lline in self.reverse_readline(log_location):
39+
# Extract dates from each line
40+
match = re.match(pattern,lline)
41+
if match:
42+
log_date = match.group(1)
43+
# Convert our date
44+
log_date = datetime.datetime.strptime(log_date, "%a %b %d %H:%M:%S.%f %Y")
45+
ftw_start = self.start
46+
ftw_end = self.end
47+
# If we have a log date in range
48+
if log_date <= ftw_end and log_date >= ftw_start:
49+
our_logs.append(lline)
50+
# If our log is from before FTW started stop
51+
if(log_date < ftw_start):
52+
break
53+
return our_logs
54+
55+
@pytest.fixture
56+
def logchecker_obj():
57+
return FooLogChecker()
Submodule OWASP-CRS-regressions deleted from 947a5ff

util/regression-tests/README

Lines changed: 0 additions & 8 deletions
This file was deleted.

util/regression-tests/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=====================
2+
OWASP-CRS-regressions
3+
=====================
4+
5+
Introduction
6+
============
7+
Welcome to the OWASP Core Rule Set regression testing suite. This suite is meant to test specific rules in OWASP CRS version 3. The suite is designed to uses preconfigured IDs that are specific to this version of CRS. The tests themselves can be run without CRS and one would expect the same elements to be blocked, however one must override the default Output parameter in the tests.
8+
9+
Installation
10+
============
11+
The OWASP Core Rule Set project was part of the effort to develop FTW, the Framework for Testing WAFs. As a result, we use this project in order to run our regression testing. FTW is designed to use existing Python testing frameworks to allow for easy to read web based testing, provided in YAML. You can install FTW by from the repository (at https://github.com/fastly/ftw) or by running pip.
12+
13+
```pip install -r requirements.txt```
14+
15+
This will install FTW as a library. It can also be run natively, see the FTW documentation for more detail.
16+
17+
Requirements
18+
============
19+
There are Three requirements for running the OWASP CRS regressions.
20+
21+
1. You must have ModSecurity specify the location of your error.log, this is done in the settings.ini file
22+
2. ModSecurity must be in DetectionOnly (or anomaly scoring) mode
23+
3. You must disable IP blocking based on previous events
24+
25+
To accomplish 2. and 3. you may use the following rule in your setup.conf:
26+
27+
```
28+
SecAction "id:900005, \
29+
phase:1,\
30+
nolog, \
31+
pass, \
32+
ctl:ruleEngine=DetectionOnly,\
33+
ctl:ruleRemoveById=910000,\
34+
setvar:tx.paranoia_level=4,\
35+
setvar:tx.crs_validate_utf8_encoding=1,\
36+
setvar:tx.arg_name_length=100, \
37+
setvar:tx.arg_length=400"
38+
```
39+
40+
Once these requirements have been met the tests can be run by using pytest.
41+
42+
Running The Tests
43+
=================
44+
45+
On Windows this will look like:
46+
-------------------------------
47+
Single Rule File:
48+
```py.test.exe -v CRS_Tests.py --rule=tests/test.yaml```
49+
The Whole Suite:
50+
```py.test.exe -v CRS_Tests.py --ruledir_recurse=tests/```
51+
52+
On Linux this will look like:
53+
-----------------------------
54+
Single Rule File:
55+
```py.test -v CRS_Tests.py --rule=tests/test.yaml```
56+
The Whole Suite:
57+
```py.test -v CRS_Tests.py --ruledir_recurse=tests/```
58+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ftw

util/regression-tests/settings.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[settings]
2+
#log_location = /var/log/httpd/error_log
3+
log_location = C:\Apache24\logs\error.log

0 commit comments

Comments
 (0)