-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRuleEditor.py
191 lines (154 loc) · 6.72 KB
/
RuleEditor.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!//usr/bin/env python
# -------------------------------------------------------------------------------
# Name: ModSecruity rule editor GUI
# Purpose: GUI to build ModSecurity rules
#
# Author: mleos
#
# Created: 2/10/2017
#
# Copyright: (c) spartantri cybersecurity
# Licence: Apache2
# -------------------------------------------------------------------------------
import sys, codecs, argparse, os, requests
from bs4 import BeautifulSoup as Soup
localReferenceManual = ''.join([os.getcwd(),'/Reference-Manual.html'])
#Legacy https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual
#ModSec 2 https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-%28v2.x%29
#ModSec 3 https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-%28v3.x%29
remoteReferenceManual = 'https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-%28v2.x%29'
actions = variables = operators = transforms = list()
from collections import namedtuple
try:
import wx
except:
next
#parser = argparse.ArgumentParser(description='ModSecurity Rule Builder GUI')
#parser.add_argument('remote', help='Download remote Reference Manual', action='store_true')
#args = vars(parser.parse_args())
args = 'local'
#print args
try:
class RuleEditor(wx.Frame):
def __init__(self, *args, **kwargs):
super(RuleEditor, self).__init__(*args, **kwargs)
self.InitUI()
self.Centre()
self.Show(True)
def InitUI(self):
try:
menubar = wx.MenuBar()
fileMenu = wx.Menu()
fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.OnQuit, fitem)
self.SetSize((1580, 880))
self.SetTitle('Rule Edifor for ModSecurity WAF')
rch = {'c1': 20, 'c2': 60, 'c3': 110, \
'r1': 5, 'r2': 20, \
'h1': 20}
positions = {'id_label': [(rch['c1'], rch['r1'])], \
'id_text': [(rch['c1'], rch['r2']), (80, rch['h1'])], \
'phase_label': [(rch['c3'], rch['r1'])], \
'phase_text': [(rch['c3'], rch['r2']), (120, rch['h1'])]}
panel = wx.Panel(self, -1)
hbox = wx.BoxSizer(wx.HORIZONTAL)
#wx.FlexGridSizer(int rows=1, int cols=0, int vgap=0, int hgap=0)
fgs = wx.FlexGridSizer(20, 12, 9, 25)
id_label = wx.StaticText(panel, label="Rule id", pos=positions[self[0]])
id_text = wx.TextCtrl(panel, pos=positions[self[0]])
phase_label = wx.StaticText(panel, label="Phase", pos=positions[self[0]])
phase_combo = wx.ComboBox(panel, pos=positions[self[0]])
selector_label = wx.StaticText(panel, label="Selector")
selector_combo = wx.ComboBox(panel)
operator_label = wx.StaticText(panel, label="Operator")
operator_combo = wx.ComboBox(panel)
pattern_label = wx.StaticText(panel, label="Rule id")
pattern_text = wx.TextCtrl(panel)
location_label = wx.StaticText(panel, label="Location")
location_combo = wx.ComboBox(panel)
#fgs.AddMany([(id_label), (id_text, 1),
# (phase_label), (phase_combo),
# (selector_label), (selector_combo, wx.EXPAND),
# (operator_label), (operator_combo, wx.EXPAND),
# (pattern_label), (pattern_text, wx.EXPAND),
# (location_label), (location_combo, wx.EXPAND)])
#fgs.AddGrowableRow(2,1)
#fgs.AddGrowableCol(1,1)
#hbox.Add(fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
#panel.SetSizer(hbox)
except:
print "No GUI"
next
def OnQuit(self, e):
self.Close()
except:
print "No GUI"
next
def get_ref_manual():
global localReferenceManual, remoteReferenceManual, args
if args == 'remote':
try:
http = os.environ['HTTP_PROXY']
https = os.environ['HTTPS_PROXY']
proxy_dict = { 'http': http, 'https': https }
except:
next
try:
if proxy_dict:
r = requests.get(remoteReferenceManual, proxies=proxy_dict)
else:
r = requests.get(remoteReferenceManual)
if r.status_code == 200:
print r
soup = Soup(r, "html.parser")
return soup
except:
print '%s not accessible try to use %s local file' % (remoteReferenceManual, localReferenceManual)
next
else:
handler = open(localReferenceManual).read()
soup = Soup(handler, "html.parser")
print localReferenceManual
return soup
def get_ref_section(section, soup):
item = namedtuple(section, ['ref', 'name','comment'])
section = section.replace('_', ' ')
extracted_section = list()
print 'Getting %s' % section
for li in soup.findAll('li'):
if li.a:
if li.a.string == section:
for ul in li.findAll('li'):
if ul.a:
extracted_section.append(item(ref=ul.a['href'], name=ul.a.string, comment='?'))
return extracted_section
def get_ref_subsection(section, soup):
return extracted_section
def put_lists_data():
global directive_types
directive_types = {'Disruptive': ['pass', 'deny', 'block', 'drop', 'allow', 'redirect', 'pause', 'proxy', 'msg'], \
'Persistence': ['setuid', 'setsrc', 'setsid', 'initcol'], \
'Meta-data': ['severity', 'maturity', 'accuracy', 'version', 'rev'], \
'Flow': ['skip', 'skipAfter'], \
'Data': ['status', 'xmlns'], \
'Non-disruptive':['log', 'nolog', 'auditlog', 'noauditlog', 'capture', 'setvar', 'deprecatevar', \
'expirevar', 'setenv', 'multiMatch', 'exec', 'prepend', 'append', 't', 'ctl', \
'logdata', 'sanitiseArg', 'sanitiseMatched', 'sanitiseMatchedBytes', \
'sanitiseRequestHeader', 'sanitiseResponseHeader', 'tag'] \
}
return
def main():
soup = get_ref_manual()
global actions, variables, operators, transforms, directive_types
operators = get_ref_section('Operators', soup)
actions = get_ref_section('Actions', soup)
variables = get_ref_section('Variables', soup)
transforms = get_ref_section('Transformation_functions', soup)
put_lists_data()
#app = wx.App()
#RuleEditor(None)
#app.MainLoop()
if __name__ == '__main__':
main()