-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
413 lines (332 loc) · 12.7 KB
/
Copy pathmain_test.go
File metadata and controls
413 lines (332 loc) · 12.7 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package main
import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
)
var hiddenParams = map[string]string{
"page": "1",
"query": "search",
"session": "abc123",
"user": "admin",
"token": "xyz789",
"mode": "debug",
}
var loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit quam quis odio interdum, ac bibendum elit tincidunt. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec justo a lacus egestas tincidunt. Curabitur nec nisi laoreet enim tempus vulputate ut et felis. Fusce hendrerit urna lacus, sit amet auctor metus varius id. Curabitur luctus sem vitae ante dapibus ornare. Maecenas dignissim ultrices odio a viverra. Donec fermentum risus ac rutrum fermentum. Pellentesque eu quam iaculis, imperdiet sem ac, posuere dui. Suspendisse consequat dolor nisi, eu semper ligula porttitor ut. Nulla tempus eros erat, ut facilisis enim eleifend non. Praesent accumsan metus est, sed gravida purus placerat in. Curabitur et faucibus arcu. Proin velit urna, vehicula id lacus non, luctus semper diam. Ut porttitor mollis elit, et auctor felis.\nMorbi consequat malesuada mi quis bibendum. Curabitur sed arcu eros. Donec id nunc enim. Sed blandit libero sed sodales viverra. Aenean viverra vitae metus nec finibus. Pellentesque viverra pretium turpis, quis feugiat lacus. Cras aliquet eros augue, at dignissim orci accumsan nec. Pellentesque arcu orci, scelerisque eu congue non, aliquet sit amet elit. Cras pretium metus efficitur velit fringilla, id maximus mi euismod."
func createTestMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
response := `<html><body><h1>Normal</h1><form><input name="debug" value="1"><input name="test"/></form></body></html>`
for key, value := range hiddenParams {
if queryParams.Get(key) != "" {
response = `<html><body><h1>Hidden Parameter Detected</h1>` + value + `</body></html>`
break
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
})
mux.HandleFunc("/dynamic", func(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
rnd := rand.New(rand.NewSource(rand.Int63()))
noise := randomString(10 + rnd.Intn(20))
response := `<html><body><h1>Time</h1><p>` + loremIpsum + `</p><div>` + noise + `</div><p>` + loremIpsum + `</p></body></html>`
for key := range hiddenParams {
if queryParams.Get(key) != "" {
response = `<html><body><h2>Secret</h2></body></html>`
break
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
})
mux.HandleFunc("/dynamic-reflected", func(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
rnd := rand.New(rand.NewSource(rand.Int63()))
noise := randomString(10 + rnd.Intn(20))
response := `<html><body><h1>Time</h1> <p>` + loremIpsum + `</p><div>` + noise + `</div><p>` + loremIpsum + `</p><p>` + loremIpsum + `</p></body></html>`
for key, value := range hiddenParams {
if queryParams.Get(key) != "" {
response = `<html><body><h2>Your value</h2>` + value + `<div></div></body></html>`
break
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
})
mux.HandleFunc("/unstable", func(w http.ResponseWriter, r *http.Request) {
rnd := rand.New(rand.NewSource(rand.Int63()))
numElements := rnd.Intn(5) + 1
response := "<html><body>"
for i := 0; i < numElements; i++ {
tags := []string{"h1", "h2", "p", "div", "span", "ul", "ol", "li", "a", "strong"}
tag := tags[rnd.Intn(len(tags))]
contentOptions := []string{
"Random Content " + strconv.Itoa(rnd.Int()),
"Another Random String " + strconv.Itoa(rnd.Int()),
"More Random Text " + strconv.Itoa(rnd.Int()),
loremIpsum[:rnd.Intn(len(loremIpsum))],
randomString(rnd.Intn(100) + 1),
randomString(rnd.Intn(100) + 1),
randomString(rnd.Intn(20) + 1),
randomString(rnd.Intn(50) + 1),
loremIpsum[:rnd.Intn(len(loremIpsum))],
}
content := contentOptions[rnd.Intn(len(contentOptions))]
element := fmt.Sprintf("<%s>%s</%s>", tag, content, tag)
response += element
}
response += "</body></html>"
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
})
mux.HandleFunc("/with-auth", func(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
auth := r.Header.Get("Authorization")
if auth == "" {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
response := `<html><body><h1>Authenticated</h1></body></html>`
for key := range hiddenParams {
if queryParams.Get(key) != "" {
response = `<html><body><h1>Auth + Hidden</h1></body></html>`
break
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
})
return mux
}
func newTestConfig() *Config {
return &Config{
Timeout: 10,
Concurrency: 10,
SimilarityThreshold: 0.9,
httpClient: createHTTPClient(10, false),
}
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func TestDiscoverParams(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
params := []string{"param1", "param2", "param3", "param4", "param5", "param6", "page", "query", "session", "user", "token", "mode", "random1", "random2", "random3", "random4", "random5", "player", "team", "score"}
request := Request{
URL: server.URL,
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
expectedParams := []string{"page", "query", "session", "user", "token", "mode"}
for _, param := range expectedParams {
if !contains(results.Params, param) {
t.Errorf("Expected parameter %s not found. Detected: %s", param, results.Params)
}
}
for _, param := range []string{"debug", "test"} {
if !contains(results.FormParams, param) {
t.Errorf("Expected form parameter %s not found. Detected: %s", param, results.FormParams)
}
}
if contains(results.Params, "test") {
t.Errorf("Reported form parameter 'test' which does not modify the content as valid")
}
}
func TestDiscoverParamsDynamic(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
params := []string{"page", "param1", "param2", "param3", "param4", "param5", "param6", "query", "session", "user", "token", "mode", "random1", "random2", "random3", "random4", "random5", "player", "team", "score"}
request := Request{
URL: server.URL + "/dynamic",
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
expectedParams := []string{"page", "query", "session", "user", "token", "mode"}
for _, param := range expectedParams {
if !contains(results.Params, param) {
t.Errorf("Expected parameter %s not found. Detected: %s", param, results.Params)
}
}
if contains(results.Params, "param1") {
t.Errorf("Reported form parameter 'param1' which does not modify the content as valid")
}
if contains(results.Params, "random1") {
t.Errorf("Reported form parameter 'random1' which does not modify the content as valid")
}
if contains(results.Params, "team") {
t.Errorf("Reported form parameter 'team' which does not modify the content as valid")
}
}
func TestDiscoverParamsDynamicReflected(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
params := []string{"param1", "param2", "param3", "param4", "param5", "param6", "page", "query", "session", "user", "token", "mode", "random1", "random2", "random3", "random4", "random5", "player", "team", "score"}
request := Request{
URL: server.URL + "/dynamic-reflected",
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
expectedParams := []string{"page", "query", "session", "user", "token", "mode"}
for _, param := range expectedParams {
if !contains(results.Params, param) {
t.Errorf("Expected parameter %s not found. Detected: %s", param, results.Params)
}
}
if contains(results.Params, "random1") {
t.Errorf("Reported form parameter 'random1' which does not modify the content as valid")
}
if contains(results.Params, "param6") {
t.Errorf("Reported form parameter 'param6' which does not modify the content as valid")
}
if contains(results.Params, "team") {
t.Errorf("Reported form parameter 'team' which does not modify the content as valid")
}
}
func TestDiscoverParamsDynamicUnstable(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
params := []string{"param1", "param2", "param3", "random1", "random2", "user", "token"}
request := Request{
URL: server.URL + "/unstable",
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
if !results.Aborted {
t.Errorf("Expected the scan to be aborted due to inconsistent responses, but it was not.")
}
if len(results.Params) > 0 || len(results.FormParams) > 0 {
t.Errorf("Expected no parameters to be reported since the scan was aborted, but found Params: %v, FormParams: %v", results.Params, results.FormParams)
}
}
func TestCustomHeaders(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
cfg.Headers = []string{"Authorization: Bearer test-token"}
params := []string{"param1", "page", "query"}
request := Request{
URL: server.URL + "/with-auth",
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
if results.Aborted {
t.Error("Expected scan to succeed with auth header, but it was aborted")
}
for _, expected := range []string{"page", "query"} {
if !contains(results.Params, expected) {
t.Errorf("Expected parameter %s not found with custom headers. Detected: %v", expected, results.Params)
}
}
if contains(results.Params, "param1") {
t.Errorf("False positive: param1 should not be in valid params")
}
}
func TestCustomHeadersWithoutAuth(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
// No auth header set
params := []string{"param1", "page", "query"}
request := Request{
URL: server.URL + "/with-auth",
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
// Without auth, baseline is 401 "Unauthorized"
// Sending hidden params still returns 401, so no params should be detected
// (the server checks auth before checking params)
if len(results.Params) > 0 {
t.Errorf("Expected no valid params without auth header, got: %v", results.Params)
}
}
func TestWordlistDedup(t *testing.T) {
tmpFile, err := os.CreateTemp("", "wordlist-*.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())
content := "param1\nparam2\nparam1\nparam3\nparam2\nparam1\n"
tmpFile.WriteString(content)
tmpFile.Close()
params := loadWordlist(tmpFile.Name())
if len(params) != 3 {
t.Errorf("Expected 3 unique params after dedup, got %d: %v", len(params), params)
}
expected := []string{"param1", "param2", "param3"}
for i, p := range expected {
if params[i] != p {
t.Errorf("Expected params[%d] = %s, got %s", i, p, params[i])
}
}
}
func TestFormParamFalsePositives(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
// Only non-hidden params in wordlist
params := []string{"param1", "param2", "param3"}
request := Request{
URL: server.URL,
Method: "GET",
}
results := DiscoverParams(cfg, request, params, 5)
// Form params should be extracted
for _, fp := range []string{"debug", "test"} {
if !contains(results.FormParams, fp) {
t.Errorf("Expected form param '%s' to be extracted", fp)
}
}
// Neither wordlist params nor form-only params should be valid
// (none of them trigger hidden behavior)
for _, param := range []string{"param1", "param2", "param3", "test", "debug"} {
if contains(results.Params, param) {
t.Errorf("False positive: '%s' should not be in valid params", param)
}
}
}
func TestTotalRequestsAtomic(t *testing.T) {
server := httptest.NewServer(createTestMux())
defer server.Close()
cfg := newTestConfig()
params := []string{"param1", "page"}
request := Request{
URL: server.URL,
Method: "GET",
}
// Reset counter
cfg.totalRequests.Store(0)
DiscoverParams(cfg, request, params, 5)
count := cfg.totalRequests.Load()
if count == 0 {
t.Error("Expected totalRequests to be incremented")
}
// Run again with a fresh config to verify isolation
cfg2 := newTestConfig()
cfg2.totalRequests.Store(0)
DiscoverParams(cfg2, request, params, 5)
count2 := cfg2.totalRequests.Load()
if count2 == 0 {
t.Error("Expected totalRequests to be incremented on second config")
}
// Verify configs are independent
if cfg.totalRequests.Load() != count {
t.Error("First config's totalRequests was modified by second run")
}
}