-
Notifications
You must be signed in to change notification settings - Fork 66
/
custom.go
331 lines (309 loc) · 12.3 KB
/
custom.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
323
324
325
326
327
328
329
330
331
// Copyright 2015, Yahoo Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// custom tests
package webseclab
import (
"errors"
"fmt"
ht "html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
// CustomMap returns a map of entrypoint to handling functions
func CustomMap() (mp map[string]func(http.ResponseWriter, *http.Request) *LabResp) {
mp = make(map[string]func(http.ResponseWriter, *http.Request) *LabResp)
mp["/xss/reflect/backslash1"] = XSSBackslash
mp["/xss/reflect/doubq1"] = XSSDoubq
mp["/xss/reflect/enc2"] = XSSEnc
mp["/xss/reflect/enc2_fp"] = XSSEncFp
mp["/xss/reflect/full_cookies1"] = XSSFullCookies
mp["/xss/reflect/full_headers1"] = XSSFullHeaders
mp["/xss/reflect/full_useragent1"] = XSSFullUseragent
mp["/xss/reflect/inredirect1_fp"] = XSSInRedirectFp
mp["/xss/reflect/post1"] = XSSPost
mp["/xss/reflect/refer1"] = XSSReferer
mp["/xss/reflect/rs1"] = XSSRs
return mp
}
// filterMap returns a map of entrypoint to a slice of filtering options
func filterMap() (mp map[string][]filter) {
mp = make(map[string][]filter)
mp["/misc/escapeexample_nogt"] = []filter{GreaterThanOff}
mp["/misc/escapeexample_nogt_noquotes"] = []filter{QuotesOff, GreaterThanOff}
mp["/xss/reflect/enc2"] = []filter{TagsOff, DoubleQuotesBackslashEscape}
mp["/xss/reflect/enc2_fp"] = []filter{TagsOff, DoubleQuotesBackslashEscape, BackslashEscape}
mp["/xss/reflect/js3"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/js3_fp"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/js3_notags"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/js3_notags_fp"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/js3_search_fp"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/js4_dq"] = []filter{TagsOff, SingleQuotesOff}
mp["/xss/reflect/js4_dq_fp"] = []filter{TagsOff, BackslashEscapeDoubleQuotesAndBackslash}
mp["/xss/reflect/js6_bug7208690"] = []filter{TagsOff, DoubleQuotesOff}
mp["/xss/reflect/js6_sq"] = []filter{TagsOff, DoubleQuotesOff}
mp["/xss/reflect/js6_sq_combo1"] = []filter{TagsOff, DoubleQuotesOff}
mp["/xss/reflect/js6_sq_fp"] = []filter{TagsOff, DoubleQuotesOff}
mp["/xss/reflect/js_script_close"] = []filter{QuotesOff}
mp["/xss/reflect/oneclick1"] = []filter{QuotesOff, TagsOff}
mp["/xss/reflect/onmouseover"] = []filter{TagsOff}
mp["/xss/reflect/onmouseover_div_unquoted"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/onmouseover_div_unquoted_fp"] = []filter{TagsOff, QuotesOff, SpacesOff}
mp["/xss/reflect/onmouseover_unquoted"] = []filter{TagsOff, QuotesOff}
mp["/xss/reflect/onmouseover_unquoted_fp"] = []filter{TagsOff, QuotesOff, SpacesOff}
mp["/xss/reflect/raw1_fp"] = []filter{QuotesOff, ScriptOff}
mp["/xss/reflect/textarea1"] = []filter{TagsOffUntilTextareaClose}
mp["/xss/reflect/textarea1_fp"] = []filter{TextareaCloseOff}
mp["/xss/reflect/textarea2"] = []filter{NoOp}
mp["/xss/reflect/textarea2_fp"] = []filter{TextareaSafe}
return
}
// XSSRs filters output to produce request splitting (injection of HEAD/BODY separator).
func XSSRs(w http.ResponseWriter, r *http.Request) *LabResp {
r.Header.Write(os.Stdout)
in := r.FormValue("in")
if in == "" {
w.Header().Set("Content-type", "text/html; charset=utf-8")
w.Write([]byte(`No "in" cgi parameter passed - nothing to inject!`))
return &LabResp{Err: nil, Code: http.StatusOK}
}
safe := strings.Replace(in, "\x0A", "%0a", -1)
safe = strings.Replace(safe, "\x0D", "%0d", -1)
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
return &LabResp{Err: errors.New("Bad Error in Hijacking (00)"), Code: http.StatusInternalServerError}
}
conn, bufrw, err := hj.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return &LabResp{Err: errors.New("Bad Error in Hijacking (01)"), Code: http.StatusInternalServerError}
}
// Don't forget to close the connection:
defer conn.Close()
bufrw.WriteString("HTTP/1.1 200\n")
bufrw.WriteString("Content-type: text/html; charset=utf-8\n")
bufrw.WriteString("X-Test: filtered value in the HTTP header (CR and LF removed): " + safe + "\n")
if !strings.HasSuffix(r.URL.Path, ".ok") {
bufrw.WriteString("X-Foo-Unsafe: unfiltered value in the HTTP header (CR and LF not removed): " + in + "\n")
}
bufrw.WriteString("X-Hopefully-Header: with the correct input handling , this should still be in the HTTP header\n")
bufrw.WriteByte(10)
bufrw.WriteByte(13)
bufrw.WriteByte(10)
bufrw.WriteByte(13)
tmplsrc := `<!doctype html><H1>Webseclab</H1><p><H2>Request splitting test</H2><p>The input parameters in the request body should be propertly filtered: <BR>in = {{.In}}</html>`
tmpl, err := ht.New("test").Parse(string(tmplsrc))
input := InData{In: in}
err = tmpl.Execute(bufrw, input)
if err != nil {
panic(err)
}
bufrw.WriteString("</body></html>\n")
bufrw.Flush()
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSFullCookies is a wrapper around standard handler (non-filtered output echo)
// but requires the presence of a cookie with value "awesome"
func XSSFullCookies(w http.ResponseWriter, r *http.Request) *LabResp {
for _, ck := range r.Cookies() {
if strings.Contains(ck.Value, "awesome") {
return DoLabTestStandard(w, r)
}
}
return &LabResp{Err: errors.New("sorry your cookies are no good - please put the word awesome into a cookie value and try again"),
Code: http.StatusForbidden}
}
// XSSFullHeaders is a wrapper around standard handler (non-filtered output echo)
// but requires the presence of HTTP Header X-Letmein with the value 1.
func XSSFullHeaders(w http.ResponseWriter, r *http.Request) *LabResp {
if r.Header.Get("X-Letmein") != "1" {
return &LabResp{Err: errors.New("missing or invalid value of the X-Letmein HTTP Header - please set to 1 and try again"),
Code: http.StatusForbidden}
}
return DoLabTestStandard(w, r)
}
// XSSFullUseragent is a wrapper around standard handler (non-filtered output echo)
// but requires the presence of a Header "User-Agent" with substring "Mobile" in the value
func XSSFullUseragent(w http.ResponseWriter, r *http.Request) *LabResp {
ua := r.Header.Get("User-Agent")
if !strings.Contains(ua, "Mobile") {
return &LabResp{Err: errors.New("access requires forward-looking thinking - please add Mobile to the User-Agent header and try again"),
Code: http.StatusForbidden}
}
return DoLabTestStandard(w, r)
}
// XSSPost handles POST input.
func XSSPost(w http.ResponseWriter, r *http.Request) *LabResp {
var inp InData
if r.Method == "GET" {
err := DoTemplate(w, r.URL.Path, &InData{In: "wrong Method - expecting POST"})
if err != nil {
log.Printf("Error in XssPost: %s\n", err)
return &LabResp{Err: err, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body in XssPost01: %s\n", err)
return &LabResp{Err: err, Code: http.StatusInternalServerError}
}
bodyUnescaped, err := url.QueryUnescape(string(body))
if err != nil {
log.Printf("Error unescaping request body in XssPost01: %s\n", err)
return &LabResp{Err: err, Code: http.StatusInternalServerError}
}
rawParams := make(map[string][]string)
ParseRawQuery(rawParams, bodyUnescaped)
if in, ok := rawParams["in"]; ok {
inp.In = in[0]
}
err = DoTemplate(w, r.URL.Path, &inp)
if err != nil {
log.Printf("Error in XssPost1: %s\n", err)
return &LabResp{Err: err, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSReferer copies and echoes the Referer header.
func XSSReferer(w http.ResponseWriter, r *http.Request) *LabResp {
inp := &InData{} // placeholder
referer := r.Header.Get("Referer")
inp.InRaw = referer
refererUnescaped, err := url.QueryUnescape(referer)
if err != nil {
log.Printf("Error in XssReferer - unable to QueryUnescape %s - %s\n", referer, err)
inp.In = referer
} else {
inp.In = refererUnescaped
}
err = DoTemplate(w, r.URL.Path, inp)
if err != nil {
log.Printf("Error in XssReferer: %s\n", err)
return &LabResp{Err: err, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSInRedirectFp issues a redirect to Yahoo homepage.
func XSSInRedirectFp(w http.ResponseWriter, r *http.Request) *LabResp {
w.Header().Set("Location", "https://www.yahoo.com")
w.WriteHeader(http.StatusFound)
input := &InData{}
rawParams := make(map[string][]string)
ParseRawQuery(rawParams, r.URL.RawQuery)
inputRaw, ok := rawParams["in"]
if ok && len(inputRaw) > 0 {
input.InRaw = inputRaw[0]
unesc, err := url.QueryUnescape(input.InRaw)
if err != nil {
fmt.Printf("ERROR in url.QueryUnescape on %s\n", input.InRaw)
}
input.In = unesc
}
err := DoTemplate(w, r.URL.Path, input)
if err != nil {
log.Printf("Error in DoTemplate: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSEnc escapes quotes with backslash but does not escape backslash itself
// allowing injection of an unescaped double quote
func XSSEnc(w http.ResponseWriter, r *http.Request) *LabResp {
w.Header().Set("Content-type", "text/html; charset=utf-8")
input := &InData{}
rawParams := make(map[string][]string)
ParseRawQuery(rawParams, r.URL.RawQuery)
inputRaw, ok := rawParams["in"]
if ok && len(inputRaw) > 0 {
input.InRaw = inputRaw[0]
unesc, err := url.QueryUnescape(input.InRaw)
if err != nil {
log.Printf("Error in XssEnc2 / QueryUnescape: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
input.In = Transform(unesc, TagsOff, DoubleQuotesBackslashEscape)
}
err := DoTemplate(w, r.URL.Path, input)
if err != nil {
log.Printf("Error in DoTemplate: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSEncFp escapes quotes and backslash with backslash preventing injection
func XSSEncFp(w http.ResponseWriter, r *http.Request) *LabResp {
input := &InData{}
rawParams := make(map[string][]string)
ParseRawQuery(rawParams, r.URL.RawQuery)
inputRaw, ok := rawParams["in"]
if ok && len(inputRaw) > 0 {
input.InRaw = inputRaw[0]
unesc, err := url.QueryUnescape(input.InRaw)
if err != nil {
log.Printf("Error in XssEnc2 / QueryUnescape: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
input.In = Transform(unesc, TagsOff, QuotesOff)
}
err := DoTemplate(w, r.URL.Path, input)
if err != nil {
log.Printf("Error in DoTemplate: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSDoubq double-unencodes the "in" cgi parameter.
func XSSDoubq(w http.ResponseWriter, r *http.Request) *LabResp {
input := &InData{}
rawParams := make(map[string][]string)
ParseRawQuery(rawParams, r.URL.RawQuery)
inputRaw, ok := rawParams["in"]
if ok && len(inputRaw) > 0 {
// imitate a bad way to do input validation - single level unescape
input.InRaw = inputRaw[0]
// one-level escape
unesc1, err := url.QueryUnescape(inputRaw[0])
if err != nil {
fmt.Printf("ERROR in the first url.QueryUnescape on %s\n", inputRaw[0])
return &LabResp{Err: nil, Code: http.StatusBadRequest}
}
unesc1 = Transform(unesc1, TagsOff, QuotesOff)
unesc, err := url.QueryUnescape(unesc1)
if err != nil {
fmt.Printf("ERROR in the second url.QueryUnescape on %s\n", unesc1)
return &LabResp{Err: nil, Code: http.StatusBadRequest}
}
input.In = unesc
}
err := DoTemplate(w, r.URL.Path, input)
if err != nil {
log.Printf("Error in DoTemplate: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}
// XSSBackslash is a special function that uses UnescapeUnicode to convert
// \u{dd} into the {dd} ASCII character.
func XSSBackslash(w http.ResponseWriter, r *http.Request) *LabResp {
input := &InData{}
rawParams := make(map[string][]string)
ParseRawQuery(rawParams, r.URL.RawQuery)
inputRaw, ok := rawParams["in"]
if ok && len(inputRaw) > 0 {
input.InRaw = inputRaw[0]
input.In = Transform(input.InRaw, TagsOff, QuotesOff)
input.In = UnescapeUnicode(input.In)
}
err := DoTemplate(w, r.URL.Path, input)
if err != nil {
log.Printf("Error in DoTemplate: %s\n", err)
return &LabResp{Err: nil, Code: http.StatusInternalServerError}
}
return &LabResp{Err: nil, Code: http.StatusOK}
}