forked from txn2/n2proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rweng.go
219 lines (181 loc) · 5.03 KB
/
rweng.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package rweng
import (
"bytes"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"text/template"
"os"
"github.com/Masterminds/sprig"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
type FilterCfg struct {
Name string `yaml:"name"`
Match string `yaml:"match"`
Template string `yaml:"template"`
}
type FilterTemplate struct {
Name string
Match string
Template *template.Template
}
// EngCfg defines an engine configuration
type EngCfg struct {
UrlWhiteList []string `yaml:"urlWhiteList"`
PostBan []string `yaml:"postBan"`
UrlBan []string `yaml:"urlBan"`
QueryBan []string `yaml:"queryBan"`
Filter []FilterCfg `yaml:"postFilter"`
}
// Eng http.Request rule engine.
type Eng struct {
cfg EngCfg
urlWhiteList []*regexp.Regexp
postBan []*regexp.Regexp
urlBan []*regexp.Regexp
queryBan []*regexp.Regexp
filter map[*regexp.Regexp]FilterTemplate
logger *zap.Logger
}
// ProcessRequest performs any rules on matching requests
func (e *Eng) ProcessRequest(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
// bypass on urlWhitelist
for _, rgx := range e.urlWhiteList {
buri := bytes.ToLower([]byte(r.RequestURI))
if rgx.Match(buri) {
e.logger.Warn("Bypassing: Whitelisted URL found.", zap.String("Regexp", rgx.String()), zap.ByteString("URI", buri))
body := ioutil.NopCloser(bytes.NewReader(b))
r.Body = body
r.ContentLength = int64(len(b))
r.Header.Set("Content-Length", strconv.Itoa(len(b)))
return
}
}
// run filter if there is a body
if len(b) > 0 {
for rgx, filter := range e.filter {
// find the match first and populate data structure
matches := rgx.FindAll(bytes.ToLower(b), len(b))
for _, match := range matches {
filter.Match = string(match)
// send the match to the template
var tplReturn bytes.Buffer
if err := filter.Template.Execute(&tplReturn, filter); err != nil {
// something bad happened
e.logger.Error("Filter failed: " + err.Error())
continue
}
b = rgx.ReplaceAll(b, tplReturn.Bytes())
}
}
}
// search for url path contraband
for _, rgx := range e.urlBan {
buri := bytes.ToLower([]byte(r.RequestURI))
if rgx.Match(buri) {
e.logger.Warn("URL contraband found.", zap.String("Regexp", rgx.String()), zap.ByteString("URI", buri))
r.URL.Path = "/"
r.URL.RawQuery = ""
break
}
}
if len(r.URL.RawQuery) > 0 {
// search for url path contraband
for _, rgx := range e.queryBan {
bq := bytes.ToLower([]byte(r.URL.RawQuery))
if rgx.Match(bq) {
e.logger.Warn("QUERY STRING contraband found.", zap.String("Regexp", rgx.String()), zap.ByteString("QUERY", bq))
r.URL.Path = "/"
r.URL.RawQuery = ""
break
}
}
}
// search for posted contraband
for _, rgx := range e.postBan {
if rgx.Match(bytes.ToLower(b)) {
e.logger.Warn("Posted contraband found.", zap.String("Regexp", rgx.String()), zap.ByteString("PostBody", b))
b = []byte{}
break
}
}
body := ioutil.NopCloser(bytes.NewReader(b))
r.Body = body
r.ContentLength = int64(len(b))
r.Header.Set("Content-Length", strconv.Itoa(len(b)))
}
// regexpCompile
func regexpCompile(rrxp []string) ([]*regexp.Regexp, error) {
crxp := make([]*regexp.Regexp, 0)
for _, r := range rrxp {
rxp, err := regexp.Compile("(?i)" + strings.ToLower(r))
if err != nil {
return crxp, err
}
crxp = append(crxp, rxp)
}
return crxp, nil
}
// NewEngFromYml loads an engine from yaml data
func NewEngFromYml(filename string, logger *zap.Logger) (*Eng, error) {
ymlData, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
engCfg := EngCfg{}
err = yaml.Unmarshal([]byte(ymlData), &engCfg)
if err != nil {
return nil, err
}
urlWhileList, err := regexpCompile(engCfg.UrlWhiteList)
if err != nil {
logger.Error("Error in urlWhileList regex compile: " + err.Error())
os.Exit(1)
}
postBan, err := regexpCompile(engCfg.PostBan)
if err != nil {
logger.Error("Error in postBan regex compile: " + err.Error())
os.Exit(1)
}
urlBan, err := regexpCompile(engCfg.UrlBan)
if err != nil {
logger.Error("Error in urlBan regex compile: " + err.Error())
os.Exit(1)
}
queryBan, err := regexpCompile(engCfg.QueryBan)
if err != nil {
logger.Error("Error in queryBan regex compile: " + err.Error())
os.Exit(1)
}
filter := make(map[*regexp.Regexp]FilterTemplate, 0)
for _, filterCfg := range engCfg.Filter {
rxp, err := regexp.Compile(strings.ToLower(filterCfg.Match))
if err != nil {
logger.Error("Error in filterCfg regex compile: " + err.Error())
os.Exit(1)
}
tmpl, err := template.New(filterCfg.Name).Funcs(sprig.TxtFuncMap()).Parse(filterCfg.Template)
if err != nil {
logger.Error("Template parsing error: " + err.Error())
}
filter[rxp] = FilterTemplate{
Name: filterCfg.Name,
Template: tmpl,
}
}
eng := &Eng{
cfg: engCfg,
urlWhiteList: urlWhileList,
postBan: postBan,
urlBan: urlBan,
queryBan: queryBan,
filter: filter,
logger: logger,
}
return eng, nil
}