-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumprules.nim
65 lines (54 loc) · 1.93 KB
/
dumprules.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os, strformat, strutils
import src/modsec
import src/modsec/base
import src/modsec/rules
var ruleset = initRuleset()
proc checkFile(file: string) =
when defined(debugrules) or defined(npegTrace):
echo "processing: " & file
defer: echo "done processing: " & file
for rule in parseRules(readFile(file)):
try:
ruleset.addRule(rule, file)
except ModsecInvalidConfig:
discard
proc dumpCounts =
var
total: int
counts: array[SecDirective, int]
for rule in ruleset.rules:
if paramCount() == 1 and not defined(debugrules):
echo rule
inc counts[rule.kind]
inc total
stderr.writeLine &"""
Total rules parsed:
SecRule .................. {counts[SecRule]:>5}
SecRuleRemoveById ........ {counts[SecRuleRemoveById]:>5}
SecMarker ................ {counts[SecMarker]:>5}
SecAction ................ {counts[SecAction]:>5}
SecDefaultAction ......... {counts[SecDefaultAction]:>5}
SecRuleUpdateTargetbyId .. {counts[SecRuleUpdateTargetbyId]:>5}
SecUnparsed .............. {counts[SecUnparsed]:>5}
total .................... {total:>5}
"""
if paramCount() > 0 and fileExists(paramStr(1)):
checkFile paramStr(1)
elif paramCount() > 0 and dirExists(paramStr(1)):
for file in walkFiles(paramStr(1) & "/*.conf"):
checkFile file
else:
stderr.writeLine &"""usage: {paramStr(0)} <file-or-dir> [<ruleid1> ... <ruleidn>]
Given a file argument, parse it as containing ModSec rules, printing and
tallying each. To troubleshoot a parse failure, compile with -d:npegTrace .
Given IDs as an argument, dump those rules after parsing (and don't normally
dump rules.)
Compile with -d:debugrules to dump malformed rules.
Given a directory argument, apply the previous to every *.conf file in that
directory."""
quit(1)
dumpCounts()
for i in 2 .. paramCount():
for r in ruleset.getRulesById(paramStr(i).parseInt):
stderr.writeLine $r
stderr.writeLine "ruleset errors: " & $ruleset.rules.validate