-
Notifications
You must be signed in to change notification settings - Fork 50
/
server.go
322 lines (300 loc) · 7.53 KB
/
server.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package yarx
import (
"bytes"
"context"
"embed"
"github.com/kataras/golog"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"sync"
"text/template"
)
type route struct {
chain *MutationChain
rule *MutationRule
handler http.Handler
}
type RegexpHandler struct {
fileServer http.Handler
routes []*route
chainsBackup []*MutationChain
onRuleMatches []ScanEventHandleFunc
onPocMatches []ScanEventHandleFunc
}
func (h *RegexpHandler) HandleRule(rule *MutationRule) {
h.routes = append(h.routes, &route{
rule: rule,
handler: rule.HTTPHandler(),
})
}
func (h *RegexpHandler) SetStaticDir(path string) {
h.fileServer = http.FileServer(http.Dir(path))
}
type pocInfo struct {
Name string
URI []string
}
//go:embed assets/html
var indexFileFS embed.FS
var indexTemplate = template.Must(template.ParseFS(indexFileFS, "assets/html/*.gohtml"))
func (h *RegexpHandler) handleIndex(w http.ResponseWriter, r *http.Request) {
var data []*pocInfo
for _, chain := range h.chainsBackup {
var info pocInfo
for _, rule := range chain.rules {
info.URI = append(info.URI, rule.Method + " " + rule.ReplacedURI)
}
info.Name = chain.Name
data = append(data, &info)
}
err := indexTemplate.ExecuteTemplate(w, "index.gohtml", data)
if err != nil {
w.Write([]byte(err.Error()))
}
}
func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
uri := SortedURI(r.URL)
golog.Infof("[%s] %s", r.Method, uri)
var body []byte
if r.Method != http.MethodGet {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "write body error", 500)
return
}
body = data
}
NextRoute:
for _, route := range h.routes {
rule := route.rule
if rule.Method == r.Method && rule.URI.MatchString(uri) {
for k, re := range rule.Header {
values := r.Header.Values(k)
if len(values) != 1 {
continue NextRoute
}
if !re.MatchString(values[0]) {
continue NextRoute
}
}
if !rule.Body.Match(body) {
continue NextRoute
}
rule.Chain.Lock()
h.run(route, body, w, r)
rule.Chain.Unlock()
return
}
}
if h.fileServer != nil {
h.fileServer.ServeHTTP(w, r)
} else {
if r.URL.Path == "" || r.URL.Path == "/" {
h.handleIndex(w, r)
} else {
w.WriteHeader(404)
w.Write([]byte("not found"))
}
}
}
func (h *RegexpHandler) run(route *route, body []byte, w http.ResponseWriter, r *http.Request) {
rule := route.rule
if rule.Method != http.MethodGet {
r.Body = ioutil.NopCloser(bytes.NewReader(body))
}
var fakeResp RespMetrics
route.handler.ServeHTTP(&fakeResp, r)
for k, v := range fakeResp.HeaderMap() {
w.Header().Set(k, v)
}
// todo: can this have a conflict?
if len(fakeResp.status) != 0 {
// fix go 302 error:
// if the response is >300 and <400, the location must in response header
status := fakeResp.status[0]
if status > 300 && status < 400 && w.Header().Get("location") == "" {
w.Header().Set("location", r.URL.Path)
}
w.WriteHeader(fakeResp.status[0])
}
_, _ = w.Write(fakeResp.body)
var e *ScanEvent
getEvent := func() *ScanEvent {
(&sync.Once{}).Do(func() {
e = &ScanEvent{
Response: &fakeResp,
PocMatched: rule.Chain.Name,
RuleMatched: rule.Name,
}
newReq := r.Clone(context.Background())
if rule.Method != http.MethodGet {
newReq.Body = ioutil.NopCloser(bytes.NewReader(body))
}
e.Request = newReq
})
return e
}
if len(h.onRuleMatches) != 0 {
for _, fn := range h.onRuleMatches {
fn(getEvent())
}
}
if len(h.onPocMatches) != 0 && rule.Chain.IsLast(rule) {
for _, fn := range h.onPocMatches {
fn(getEvent())
}
}
return
}
func (h *RegexpHandler) Routes() []string {
var routes []string
for _, route := range h.routes {
r := route.rule.URI.String()
routes = append(routes, r)
}
return routes
}
func (y *Yarx) HTTPHandler() *RegexpHandler {
handlerRules := y.Rules()
// It's very important to sort the rules, the more flexible it is, the further back it should be
// thus will help yarx to find the most suitable route for an incoming request
sort.Slice(handlerRules, func(i, j int) bool {
a := handlerRules[i]
b := handlerRules[j]
if len(a.Header) != 0 && len(b.Header) == 0 {
return true
}
if len(a.Header) == 0 && len(b.Header) != 0 {
return false
}
aU, err1 := url.ParseRequestURI(a.ReplacedURI)
bU, err2 := url.ParseRequestURI(b.ReplacedURI)
if err1 == nil && err2 == nil {
aPathHasVar := strings.Contains(aU.Path, "---ko--")
bPathHasVar := strings.Contains(bU.Path, "---ko--")
if !aPathHasVar && bPathHasVar {
return true
}
if aPathHasVar && !bPathHasVar {
return false
}
}
aHasVar := strings.Contains(a.URI.String(), "(?P<")
bHasVar := strings.Contains(b.URI.String(), "(?P<")
if !aHasVar && bHasVar {
return true
}
if aHasVar && !bHasVar {
return false
}
if a.Body.String() != "" && b.Body.String() == "" {
return true
}
if a.Body.String() == "" && b.Body.String() != "" {
return false
}
aSplashCount := strings.Count(a.URI.String(), "/")
bSplashCount := strings.Count(b.URI.String(), "/")
if aSplashCount > bSplashCount {
return true
}
if aSplashCount < bSplashCount {
return false
}
if a.Method != http.MethodGet && b.Method == http.MethodGet {
return true
}
if a.Method == http.MethodGet && b.Method != http.MethodGet {
return false
}
return i < j
})
handlerRules = mergeRules(handlerRules)
handler := &RegexpHandler{}
handler.chainsBackup = y.Chains()
for _, rule := range handlerRules {
handler.HandleRule(rule)
}
return handler
}
func handleError(err error, writer http.ResponseWriter) {
golog.Error(err)
writer.WriteHeader(500)
_, _ = writer.Write([]byte(err.Error()))
}
type stringRule struct {
level int
name string
uri string
method string
body string
}
// merge the rules if uri, body, header, method are all same
func mergeRules(rules []*MutationRule) []*MutationRule {
newRules := append(rules[:0:0], rules...)
var ruleHelper []*stringRule
for _, rule := range newRules {
ruleHelper = append(ruleHelper, &stringRule{
level: rule.level,
name: rule.Name,
uri: rule.URI.String(),
method: rule.Method,
body: rule.Body.String(),
})
}
for i, curRule := range ruleHelper {
if newRules[i] == nil {
continue
}
NextRule:
for j := i + 1; j < len(ruleHelper); j++ {
if newRules[j] == nil {
continue
}
otherRule := ruleHelper[j]
if curRule.uri == otherRule.uri &&
curRule.method == otherRule.method &&
curRule.body == otherRule.body {
baseRule := newRules[i]
targetRule := newRules[j]
// check header collision
if len(baseRule.Header) != len(targetRule.Header) {
continue NextRule
}
for k, v := range baseRule.Header {
if targetRule.Header[k] == nil {
continue NextRule
}
if v.String() != targetRule.Header[k].String() {
continue NextRule
}
}
if baseRule.Status != targetRule.Status {
golog.Warnf("response status collision of %s and %s", baseRule, targetRule)
golog.Warnf("deleting %s", targetRule)
newRules[j] = nil
continue NextRule
}
golog.Infof("trying to merge %s with %s",
baseRule, targetRule)
for _, fn := range targetRule.MutateFuncs {
baseRule.MutateFuncs = append(baseRule.MutateFuncs, fn)
}
// todo: how to do this?
baseRule.Name += "||" + targetRule.Name
baseRule.Chain.Name += "||" + targetRule.Chain.Name
newRules[j] = nil
}
}
}
var finalRules []*MutationRule
for _, rule := range newRules {
if rule != nil {
finalRules = append(finalRules, rule)
}
}
return finalRules
}