Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

Commit

Permalink
Fix some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien (jvoisin) Voisin committed May 25, 2016
1 parent 10076cc commit 230e990
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 35 deletions.
3 changes: 2 additions & 1 deletion nxapi/nxlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import parse_qs
import re


def parse_nxlog(nxlog):
"""
Expand All @@ -22,7 +23,7 @@ def parse_nxlog(nxlog):

end = nxlog.find(", ")
if end < 0:
errors.append('%s is an invalid extlog, string "," not found.' %nxlog)
errors.append('%s is an invalid extlog, string "," not found.' % nxlog)
return errors, ret

# Flatten the dict, since parse_qs is a bit annoying
Expand Down
2 changes: 1 addition & 1 deletion nxapi/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def check_whitelist(rule, nxlog):
if pcre.match(mz_var, nxlog['var_name%d' % nb], pcre.I) and nxlog_zone == mz_zone[1:-6]:
matched = True
elif nxlog['var_name%d' % nb] == mz_var and nxlog_zone == mz_zone[1:-4]:
matched = True
matched = True
elif nxlog_zone in mz: # zone without argument
matched = True

Expand Down
2 changes: 1 addition & 1 deletion nxapi/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def parse_rule(full_str):
errors.append('No closing quotation in your rule')
return errors, warnings, ret

duplicate = [k for k,v in collections.Counter(split).items() if v > 1]
duplicate = [k for k, v in collections.Counter(split).items() if v > 1]
if duplicate:
errors.append("Duplicates elements: %s" % ', '.join(duplicate))
return errors, warnings, ret
Expand Down
4 changes: 2 additions & 2 deletions nxapi/whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __validate_mz(warnings, errors, mz):
return errors, warnings
if not mz[0].startswith('$URL'):
warnings.append('Your three parts matchzone does not starts with $URL')
if 1< len(mz) < 4 and mz[0].startswith('$URL') and (mz[1] == 'NAME'):
if 1 < len(mz) < 4 and mz[0].startswith('$URL') and (mz[1] == 'NAME'):
errors.append('You can not use $URL and NAME')
return errors, warnings
return errors, warnings
Expand Down Expand Up @@ -141,7 +141,7 @@ def __linkify_rule(_rid):
else:
zones = list()
if wil < 0:
zones.append('except the rule {}'.format(__linkify_rule(abs(wil))))
zones.append('except the rule {}'.format(__linkify_rule(abs(wil))))
elif not negative:
zones.append('the rule {}'.format(__linkify_rule(wil)))
ret += ', '.join(zones)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
packages=find_packages(),
version='0.1',
install_requires=['python-pcre']
)
)
8 changes: 4 additions & 4 deletions tests/test_nxlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_parse_nxlog(self):
errors, ret = nxlog.parse_nxlog(_nxlog)
self.assertEqual(errors, list())
self.assertEqual(ret, {'uri': '/phpMyAdmin-2.8.2/scripts/setup.php', 'block': '0',
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '42000227',
'total_processed': '472', 'vers': '0.52'})
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '42000227',
'total_processed': '472', 'vers': '0.52'})
34 changes: 17 additions & 17 deletions tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ class TestProcessing(TestCase):
def test_short_str(self):
whitelist = {'mz': ['$HEADERS_VAR:user-agent'], 'wl': [42000227]}
nxlog = {'uri': '/phpMyAdmin-2.8.2/scripts/setup.php', 'block': '0',
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '42000227',
'total_processed': '472', 'vers': '0.52'}
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '42000227',
'total_processed': '472', 'vers': '0.52'}
self.assertTrue(processing.check_whitelist(whitelist, nxlog))

whitelist = {'mz': ['$HEADERS_VAR:user-agent'], 'wl': [42000227]}
nxlog = {'uri': '/phpMyAdmin-2.8.2/scripts/setup.php', 'block': '0',
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'BODY', 'score0': '8',
'var_name0': 'test', 'cscore0': '$UWA', 'id0': '42000227',
'total_processed': '472', 'vers': '0.52'}
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'BODY', 'score0': '8',
'var_name0': 'test', 'cscore0': '$UWA', 'id0': '42000227',
'total_processed': '472', 'vers': '0.52'}
self.assertFalse(processing.check_whitelist(whitelist, nxlog))

whitelist = {'mz': ['$HEADERS_VAR:user-agent'], 'wl': [42000227, 10]}
nxlog = {'uri': '/phpMyAdmin-2.8.2/scripts/setup.php', 'block': '0',
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '10',
'total_processed': '472', 'vers': '0.52'}
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '10',
'total_processed': '472', 'vers': '0.52'}
self.assertTrue(processing.check_whitelist(whitelist, nxlog))

whitelist = {'mz': ['$HEADERS_VAR:user-agent'], 'wl': [42000227, 11]}
nxlog = {'uri': '/phpMyAdmin-2.8.2/scripts/setup.php', 'block': '0',
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '10',
'total_processed': '472', 'vers': '0.52'}
'total_blocked': '204', 'ip': 'X.X.X.X', 'server': 'Y.Y.Y.Y',
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '10',
'total_processed': '472', 'vers': '0.52'}
self.assertFalse(processing.check_whitelist(whitelist, nxlog))

whitelist = {'mz': ['$HEADERS_VAR_X:^user-agent$'], 'wl': [42000227, 10]}
Expand Down Expand Up @@ -76,4 +76,4 @@ def test_short_str(self):
'learning': '0', 'zone0': 'HEADERS', 'score0': '8',
'var_name0': 'user-agent', 'cscore0': '$UWA', 'id0': '10',
'total_processed': '472', 'vers': '0.52'}
self.assertTrue(processing.check_whitelist(whitelist, nxlog))
self.assertTrue(processing.check_whitelist(whitelist, nxlog))
12 changes: 6 additions & 6 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def test_explain(self):
'The rule number <strong>5</strong> is <strong>not</strong> setting the <strong>$XSS</strong>'
' score to <strong>3</strong> when it finds the string <strong>pif</strong>'
' the <strong>body</strong>.')
rule = {'negative': True, 'detection': 'str:pif', 'msg': 'test msg', 'mz': 'BODY|URL', 'score': '$XSS:3', 'sid': 5}
rule = {'negative': True, 'detection': 'str:pif', 'msg': 'test msg', 'mz': 'BODY|URL', 'score': '$XSS:3',
'sid': 5}
self.assertEqual(rules.explain(rule),
'The rule number <strong>5</strong> is <strong>not</strong> setting the <strong>$XSS</strong>'
' score to <strong>3</strong> when it finds the string <strong>pif</strong>'
Expand All @@ -27,13 +28,15 @@ def test_parse(self):
errors, warnings, ret = rules.parse_rule(rule)
self.assertEqual(errors, [])
self.assertEqual(warnings, ['rule IDs below 10k are reserved (5)'])
self.assertEqual(ret, {'mz:': ['BODY'], 'str:': 'str:a', 's:': '$XSS:3', 'negative': '', 'id:': '5', 'msg:': 't'})
self.assertEqual(ret, {'mz:': ['BODY'], 'str:': 'str:a', 's:': '$XSS:3', 'negative': '', 'id:': '5',
'msg:': 't'})

rule = 'MainRule negative "str:a" "msg:t" "mz:BODY|URL" "s:$XSS:3" id:5 ;'
errors, warnings, ret = rules.parse_rule(rule)
self.assertEqual(errors, [])
self.assertEqual(warnings, ['rule IDs below 10k are reserved (5)'])
self.assertEqual(ret, {'mz:': ['BODY', 'URL'], 'str:': 'str:a', 's:': '$XSS:3', 'negative': '', 'id:': '5', 'msg:': 't'})
self.assertEqual(ret, {'mz:': ['BODY', 'URL'], 'str:': 'str:a', 's:': '$XSS:3', 'negative': '', 'id:': '5',
'msg:': 't'})

rule = 'MainRule negative "rx:^[\da-z_]+$" "mz:$ARGS_VAR:id|$BODY_VAR:id" "s:$LOG_TEST:1" id:42000456;'
errors, warnings, ret = rules.parse_rule(rule)
Expand Down Expand Up @@ -107,8 +110,6 @@ def test_parse(self):
self.assertEqual(errors, ['id:POUET is not numeric', "Parsing of element 'id:POUET' failed."])
self.assertEqual(warnings, [])



def test_validate(self):
rule = {'negative': True, 'detection': 'str:pif', 'msg': 'test msg', 'mz': 'BODY', 'score': '$XSS:3', 'sid': 5}
errors, warnings = rules.validate(rule)
Expand All @@ -121,4 +122,3 @@ def test_validate(self):
print(errors)
self.assertIn("WRONG' is not a known sub-part of mz", str(errors))
self.assertEqual(warnings, [])

5 changes: 3 additions & 2 deletions tests/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_validate(self):
errors, warnings = whitelist.validate(wlist)
self.assertEqual(errors, ['You can not use regexp matchzone with non-regexp one'])

wlist = {'wl': [1000], 'mz': ['$ARGS_VAR_X:lol','$ARGS_VAR_X:lol','$ARGS_VAR_X:lol']}
wlist = {'wl': [1000], 'mz': ['$ARGS_VAR_X:lol', '$ARGS_VAR_X:lol', '$ARGS_VAR_X:lol']}
errors, warnings = whitelist.validate(wlist)
self.assertEqual(errors, ['The last argument of your matchzone with two pipes is not "NAME"'])

Expand Down Expand Up @@ -150,4 +150,5 @@ def test_explain(self):
self.assertEqual(whitelist.explain(wlist), 'Whitelist all rules if matching in $ARGS_VAR:foo in $URL:/bar.')

wlist = {'mz': ['$ARGS_VAR:foo', '$URL:/bar'], 'wl': [-10]}
self.assertEqual(whitelist.explain(wlist), 'Whitelist all rules except the rule 10 if matching in $ARGS_VAR:foo in $URL:/bar.')
self.assertEqual(whitelist.explain(wlist),
'Whitelist all rules except the rule 10 if matching in $ARGS_VAR:foo in $URL:/bar.')

0 comments on commit 230e990

Please sign in to comment.