-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmain.go
211 lines (170 loc) · 4.17 KB
/
main.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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io/ioutil"
"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() {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
sc := bufio.NewScanner(os.Stdin)
initialChecks := make(chan paramCheck, 40)
appendChecks := makePool(initialChecks, func(c paramCheck, output chan paramCheck) {
reflected, err := checkReflected(c.url)
if err != nil {
//fmt.Fprintf(os.Stderr, "error from checkReflected: %s\n", 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(os.Stderr, "error from checkAppend for url %s with param %s: %s", c.url, c.param, err)
return
}
if wasReflected {
output <- paramCheck{c.url, c.param}
}
})
done := makePool(charChecks, func(c paramCheck, output chan paramCheck) {
output_of_url := []string{c.url, c.param}
for _, char := range []string{"\"", "'", "<", ">", "$", "|", "(", ")", "`", ":", ";", "{", "}"} {
wasReflected, err := checkAppend(c.url, c.param, "aprefix"+char+"asuffix")
if err != nil {
fmt.Fprintf(os.Stderr, "error from checkAppend for url %s with param %s with %s: %s", c.url, c.param, char, err)
continue
}
if wasReflected {
output_of_url = append(output_of_url, char)
}
}
if len(output_of_url) >= 2 {
fmt.Printf("URL: %s Param: %s Unfiltered: %v \n", output_of_url[0] , output_of_url[1],output_of_url[2:])
}
})
for sc.Scan() {
initialChecks <- paramCheck{url: sc.Text()}
}
close(initialChecks)
<-done
}
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 := ioutil.ReadAll(resp.Body)
if err != nil {
return out, err
}
// nope (:
if strings.HasPrefix(resp.Status, "3") {
return out, nil
}
// also nope
ct := 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
}