-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimplerules.py
executable file
·57 lines (51 loc) · 1.84 KB
/
simplerules.py
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
#!/usr/bin/env python
# -------------------------------------------------------------------------------
# Name: Modsecurity simple rule generator
# Purpose: Modsecurity simple rule generation from xml and xslt
#
# Author: spartantri
#
# Created: 19/08/2016
# Copyright: (c) spartantri 2016
# License: Apache License Version 2.0
# -------------------------------------------------------------------------------
def transform_xml(input_xml, transformation_xslt, modsecurity_starting_ruleid=9980000, verbose=False):
from lxml import etree
import re
data = open(transformation_xslt)
xslt_content = data.read()
xslt_root = etree.XML(xslt_content)
dom = etree.parse(input_xml)
transform = etree.XSLT(xslt_root)
result = transform(dom)
if verbose == True:
print 'Writing temporary file... (modsec.rules.tmp)'
f = open('modsec.rules.tmp', 'w')
f.write(str(result))
f = open('modsec.rules.tmp', 'r')
rules = open('modsec.rules', 'w')
rule_count = 0
print 'Writing modsecurity rules file... (modsec.rules)'
for line in f:
rule_id = ''.join(['id:', str(modsecurity_starting_ruleid)])
line = line.replace('id:9931733', rule_id)
rules.write(line)
if rule_id in line:
modsecurity_starting_ruleid += 1
if verbose == True:
print 'Generated rule', rule_id
rule_count += 1
else:
writing_id = re.search('(id:\d+)', line)
if writing_id:
if verbose == True:
print 'Generated rule', writing_id.group(1)
rule_count += 1
print 'Generated {0} rules'.format(rule_count)
f.close()
rules.close()
def main():
transform_xml('test.xml', 'SimpleTransformation.xslt', 9980000, False)
pass
if __name__ == '__main__':
main()