-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmain.go
More file actions
235 lines (189 loc) · 4.7 KB
/
Copy pathmain.go
File metadata and controls
235 lines (189 loc) · 4.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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
)
type paramCheck struct {
url string
param string
}
var transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: time.Second,
DualStack: true,
}).DialContext,
}
var httpClient = &http.Client{
Transport: transport,
}
func main() {
if err := run(os.Stdin, os.Stdout, os.Stderr); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(in io.Reader, out io.Writer, errOut io.Writer) error {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
sc := bufio.NewScanner(in)
sc.Buffer(make([]byte, 0, 64*1024), 10*1024*1024)
initialChecks := make(chan paramCheck, 40)
appendChecks := makePool(initialChecks, func(c paramCheck, output chan paramCheck) {
reflected, err := checkReflected(c.url)
if err != nil {
fmt.Fprintf(errOut, "error from checkReflected for url %s: %s\n", c.url, err)
return
}
if len(reflected) == 0 {
// TODO: wrap in verbose mode
//fmt.Printf("no params were reflected in %s\n", c.url)
return
}
for _, param := range reflected {
output <- paramCheck{c.url, param}
}
})
charChecks := makePool(appendChecks, func(c paramCheck, output chan paramCheck) {
wasReflected, err := checkAppend(c.url, c.param, "iy3j4h234hjb23234")
if err != nil {
fmt.Fprintf(errOut, "error from checkAppend for url %s with param %s: %s\n", c.url, c.param, err)
return
}
if wasReflected {
output <- paramCheck{c.url, c.param}
}
})
done := makePool(charChecks, func(c paramCheck, output chan paramCheck) {
unfiltered := checkUnfilteredChars(c.url, c.param, errOut)
if len(unfiltered) > 0 {
fmt.Fprintf(out, "URL: %s Param: %s Unfiltered: %v\n", c.url, c.param, unfiltered)
}
})
for sc.Scan() {
targetURL := strings.TrimSpace(sc.Text())
if targetURL == "" {
continue
}
initialChecks <- paramCheck{url: targetURL}
}
close(initialChecks)
<-done
if err := sc.Err(); err != nil {
return fmt.Errorf("error reading stdin: %w", err)
}
return nil
}
func checkUnfilteredChars(targetURL, param string, errOut io.Writer) []string {
unfiltered := make([]string, 0)
for _, char := range []string{"\"", "'", "<", ">", "$", "|", "(", ")", "`", ":", ";", "{", "}"} {
wasReflected, err := checkAppend(targetURL, param, "aprefix"+char+"asuffix")
if err != nil {
fmt.Fprintf(errOut, "error from checkAppend for url %s with param %s with %s: %s\n", targetURL, param, char, err)
continue
}
if wasReflected {
unfiltered = append(unfiltered, char)
}
}
return unfiltered
}
func checkReflected(targetURL string) ([]string, error) {
out := make([]string, 0)
req, err := http.NewRequest("GET", targetURL, nil)
if err != nil {
return out, err
}
// temporary. Needs to be an option
req.Header.Add("User-Agent", "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36")
resp, err := httpClient.Do(req)
if err != nil {
return out, err
}
if resp.Body == nil {
return out, err
}
defer resp.Body.Close()
// always read the full body so we can re-use the tcp connection
b, err := io.ReadAll(resp.Body)
if err != nil {
return out, err
}
// nope (:
if strings.HasPrefix(resp.Status, "3") {
return out, nil
}
// also nope
ct := strings.ToLower(resp.Header.Get("Content-Type"))
if ct != "" && !strings.Contains(ct, "html") {
return out, nil
}
body := string(b)
u, err := url.Parse(targetURL)
if err != nil {
return out, err
}
for key, vv := range u.Query() {
for _, v := range vv {
if !strings.Contains(body, v) {
continue
}
out = append(out, key)
}
}
return out, nil
}
func checkAppend(targetURL, param, suffix string) (bool, error) {
u, err := url.Parse(targetURL)
if err != nil {
return false, err
}
qs := u.Query()
val := qs.Get(param)
//if val == "" {
//return false, nil
//return false, fmt.Errorf("can't append to non-existant param %s", param)
//}
qs.Set(param, val+suffix)
u.RawQuery = qs.Encode()
reflected, err := checkReflected(u.String())
if err != nil {
return false, err
}
for _, r := range reflected {
if r == param {
return true, nil
}
}
return false, nil
}
type workerFunc func(paramCheck, chan paramCheck)
func makePool(input chan paramCheck, fn workerFunc) chan paramCheck {
var wg sync.WaitGroup
output := make(chan paramCheck)
for i := 0; i < 40; i++ {
wg.Add(1)
go func() {
for c := range input {
fn(c, output)
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(output)
}()
return output
}