forked from Hackmanit/Web-Cache-Vulnerability-Scanner
/
utils.go
264 lines (232 loc) · 6.34 KB
/
utils.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
package pkg
import (
"context"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
)
var (
NoColor = ""
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
Gray = "\033[37m"
White = "\033[97m"
)
func init() {
if runtime.GOOS == "windows" {
Reset = ""
Red = ""
Green = ""
Yellow = ""
Blue = ""
Purple = ""
Cyan = ""
Gray = ""
White = ""
}
}
func removeColors(msg string) string {
msg = strings.ReplaceAll(msg, Reset, "")
msg = strings.ReplaceAll(msg, Red, "")
msg = strings.ReplaceAll(msg, Green, "")
msg = strings.ReplaceAll(msg, Yellow, "")
msg = strings.ReplaceAll(msg, Blue, "")
msg = strings.ReplaceAll(msg, Purple, "")
msg = strings.ReplaceAll(msg, Cyan, "")
msg = strings.ReplaceAll(msg, Gray, "")
msg = strings.ReplaceAll(msg, White, "")
msg = strings.Trim(msg, "\n")
return msg
}
func PrintNewLine() {
Print("\n", NoColor)
}
func PrintVerbose(msg string, color string, threshold int) {
if color == Red {
msg = color + "[ERR] " + Reset + msg
} else if color == Yellow {
msg = color + "[!] " + Reset + msg
} else if color == Green {
msg = color + "[+] " + Reset + msg
} else if color == Cyan {
msg = color + "[*] " + Reset + msg
}
if Config.Verbosity >= threshold {
fmt.Print(msg)
}
log.Print(removeColors(msg))
}
func Print(msg string, color string) {
PrintVerbose(msg, color, 0)
}
func PrintFatal(msg string) {
Print(msg, Red)
os.Exit(1)
}
func ReadLocalFile(path string, name string) []string {
path = strings.TrimPrefix(path, "file:")
if strings.HasPrefix(strings.ToLower(path), "file:") {
PrintFatal("Please make sure that path: is lowercase")
}
w, err := ioutil.ReadFile(path)
if err != nil {
additional := ""
if name == "header" {
additional = "Use the flag \"-hw path/to/wordlist\" to specify the path to a header wordlist\n"
} else if name == "parameter" {
additional = "Use the flag \"-pw path/to/wordlist\" to specify the path to a parameter wordlist\n"
}
PrintFatal("The specified " + name + " file path " + path + " couldn't be found: " + err.Error() + "\n" + additional)
}
return strings.Split(string(w), "\n")
}
func setRequest(req *http.Request, doPost bool, cb string, cookie http.Cookie) {
cache := Config.Website.Cache
if cb != "" && cache.CBisParameter {
var newUrl string
newUrl, _ = addCachebusterParameter(req.URL.String(), cb)
var err error
req.URL, err = url.Parse(newUrl)
if err != nil {
msg := "Converting " + newUrl + " to URL:" + err.Error() + "\n"
Print(msg, Red)
}
}
setRequestHeaders(req, cb)
//TODO config nötig oder nur config.Website.Cookies?
setRequestCookies(req, cb, cookie)
// Content-Type nur hinzufügen, wenn nicht schon vorher geschehen
if doPost {
if req.Header.Get("Content-Type") == "" && Config.ContentType != "" {
req.Header.Add("Content-Type", Config.ContentType)
}
}
}
/* TODO wie bei requestCookies nur die erste occurance eines headers aufnehmen */
func setRequestHeaders(req *http.Request, cb string) {
cache := Config.Website.Cache
req.Header.Set("User-Agent", useragent)
for _, h := range Config.Headers {
h = strings.TrimSuffix(h, "\r")
h = strings.TrimSpace(h)
if h == "" {
continue
} else if !strings.Contains(h, ":") {
msg := "Specified header" + h + "doesn't contain a : and will be skipped"
Print(msg, NoColor)
continue
} else {
hSplitted := strings.SplitN(h, ":", 2)
// is this header the cachebuster?
if cb != "" && cache.CBisHeader && strings.EqualFold(hSplitted[0], cache.CBName) {
req.Header.Set(cache.CBName, cb)
}
req.Header.Set(strings.TrimSpace(hSplitted[0]), strings.TrimSpace(hSplitted[1]))
}
}
}
/* */
func setRequestCookies(req *http.Request, cb string, cookie http.Cookie) {
cache := Config.Website.Cache
for _, c := range Config.Website.Cookies {
// only add first occurence of a cookie to the request
_, err := req.Cookie(c.Name)
if err == http.ErrNoCookie {
if cb != "" && cache.CBisCookie && c.Name == cache.CBName {
c.Value = cb
if c.Name == cookie.Name {
msg := "Can't test cookie " + c.Name + " for Web Cache Poisoning, as it is used as Cachebuster\n"
Print(msg, Yellow)
}
} else if c.Name == cookie.Name {
c = &cookie
}
req.AddCookie(c)
}
}
}
/* if rp.newCookie.Name != "" {
if Config.Website.Cache.CBisCookie && strings.EqualFold(Config.Website.Cache.CBName, rp.newCookie.Name) {
msg = "Can't test cookie " + rp.newCookie.Name + " for Web Cache Poisoning, as it is used as Cachebuster\n"
Print(msg, Yellow)
} else {
removeCookie := http.Cookie
req.AddCookie(&rp.newCookie)
}
*/
func addCachebusterParameter(strUrl string, cb string) (string, string) {
if cb == "" {
cb = randInt()
}
if !strings.Contains(strUrl, "?") {
strUrl += "?" + Config.CacheBuster + "=" + cb
} else {
strUrl += Config.QuerySeperator + Config.CacheBuster + "=" + cb
}
return strUrl, cb
}
/* Create a random long integer */
func randInt() string {
min := 100000000000
max := 999999999999
result := min + rand.Intn(max-min)
return strconv.Itoa(result)
}
func waitLimiter(identifier string) {
err := Config.Limiter.Wait(context.Background())
if err != nil {
msg := identifier + " rate Wait: " + err.Error()
Print(msg, Red)
}
}
func searchBodyHeadersForString(cb string, body string, headers http.Header) bool {
if strings.Contains(body, cb) {
return true
}
for _, h := range headers {
for _, v := range h {
if strings.Contains(v, cb) {
return true
}
}
}
return false
}
// check if cache was hit
func checkCacheHit(value string) bool {
indicator := Config.Website.Cache.Indicator
if strings.EqualFold("age", indicator) {
value = strings.TrimSpace(value)
if value != "0" {
return true
}
} else if strings.EqualFold("x-iinfo", indicator) {
if strings.EqualFold(value[22:23], "C") || strings.EqualFold(value[22:23], "V") {
return true
}
// Cache Hit may have 0,>0 or >0,0 as value. Both responses are cached
} else if strings.EqualFold("x-cache-hits", indicator) {
for _, x := range strings.Split(indicator, ",") {
x = strings.TrimSpace(x)
if x != "0" {
return true
}
}
// Some Headers may have "miss,hit" or "hit,miss" as value. But both are cached responses.
} else if strings.Contains(value, "hit") {
return true
}
return false
}