-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmedusa.go
360 lines (289 loc) · 6.75 KB
/
medusa.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
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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"net"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/panjf2000/ants"
"github.com/valyala/fasthttp"
"gopkg.in/gookit/color.v1"
)
const (
medusaUA = "medusa/0.2.1"
version = "0.2.1"
)
const (
urlTypeSingle = iota
urlTypeMultiple
defaultSchema = "http"
schemaSlicer = "://"
sameBodyLengthCountAlarm = 3
)
var (
u = flag.String("u", "", "")
ul = flag.String("uL", "", "")
ua = flag.String("ua", medusaUA, "")
cp = flag.String("cP", "200", "")
cn = flag.String("cN", "", "")
e = flag.String("e", "", "")
s = flag.String("s", "", "")
t = flag.Int("t", 10, "")
v = flag.Bool("v", false, "")
insecure = flag.Bool("x", false, "")
recursive = flag.Bool("r", false, "")
wl = flag.String("w", "", "")
conc = flag.Int("conc", 100, "")
cpus = flag.Int("cpus", runtime.GOMAXPROCS(-1), "")
client *fasthttp.Client
p *ants.PoolWithFunc
wg *sync.WaitGroup
pCodes = []string{}
nCodes = []string{}
wordlist = []string{}
urls = []string{}
sameBodyWarningHost, currentBody atomic.Value
sameBodyLengthCount int32
)
var usage = `Usage: medusa [options...]
Options:
-u Single URL
-uL URL list file path (line by line)
-e Extension
-s Force schema (uses default http if does not contains url)
-ua User-agent value (default %s)
-cP Postive status codes (seperate by comma)
-cN Negative status codes (seperate by comma)
-x Bypass SSL verification
-t HTTP response timeout (10s)
-r Enable recursive fuzzing
-w Directory wordlist (line by line)
-v Verbose mode, show logs
-conc Maximum concurrent requests
-cpus Number of used cpu cores.
(default for current machine is %d cores)
`
var header = `
medusa v%s by rizasabuncu
-----------------------------------------
[*] URL/List: %s
[*] CPU Cores: %d
[*] User Agent: %s
[*] Extension: %s
[*] Positive status codes: %s
[*] Negative status codes: %s
[*] Bypass SSL verification %t
[*] Recursive fuzz: %t
[*] Wordlist path: %s
[*] Wordlist length: %d
[*] Max concurrent connection: %d
-----------------------------------------
`
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage, medusaUA, runtime.NumCPU()))
}
flag.Parse()
if flag.NFlag() < 1 {
usageAndExit("")
}
if len(*u)+len(*ul) <= 0 {
usageAndExit("-u or -uL cannot be empty")
}
if len(*wl) <= 0 {
usageAndExit("-w cannot be empty")
}
runtime.GOMAXPROCS(*cpus)
defer ants.Release()
var (
urlType = urlTypeSingle
err error
scanURL = *u
)
pCodes = parseStatusCode(*cp)
nCodes = parseStatusCode(*cn)
if len(*ul) > len(*u) {
urlType = urlTypeMultiple
scanURL = *ul
}
switch urlType {
case urlTypeSingle:
urls = append(urls, scanURL)
break
case urlTypeMultiple:
_, err = os.Stat(scanURL)
if err != nil {
usageAndExit("URL list file not exists")
}
urls, err = readLines(scanURL)
if err != nil {
usageAndExit("URL list load error: " + err.Error())
}
break
}
_, err = os.Stat(*wl)
if err != nil {
usageAndExit("Wordlist file not exists")
}
wordlist, err = readLines(*wl)
if err != nil {
usageAndExit("Wordlist load error: " + err.Error())
}
//header
fmt.Printf(
header, version,
scanURL, *cpus,
*ua, *e,
*cp, *cn,
*insecure,
*recursive,
*wl, len(wordlist),
*conc,
)
wg = &sync.WaitGroup{}
client = &fasthttp.Client{
Dial: func(addr string) (net.Conn, error) {
return fasthttp.DialTimeout(addr, time.Duration(*t)*time.Second)
},
TLSConfig: &tls.Config{InsecureSkipVerify: *insecure},
}
start := time.Now()
p, _ = ants.NewPoolWithFunc(*conc, func(u interface{}) {
check(u)
wg.Done()
})
defer p.Release()
for _, u := range urls {
invokeAndGeneratePath(u)
}
wg.Wait()
took := time.Since(start)
logInfo(fmt.Sprintf("Total time:\t%s", took))
}
func check(i interface{}) {
url := i.(string)
host := getHost(url)
//skip samebody
if sameBodyWarningHost.Load() != nil && host == sameBodyWarningHost.Load().(string) {
return
}
sc, body, err := client.Get(nil, url)
if err != nil {
if err != fasthttp.ErrDialTimeout {
logError(err.Error())
}
}
statusCode := strconv.Itoa(sc)
if !checkStatusCode(nCodes, statusCode) && checkStatusCode(pCodes, statusCode) {
if currentBody.Load() != nil && currentBody.Load().(int) == len(body) {
if sameBodyLengthCount >= sameBodyLengthCountAlarm {
if sameBodyWarningHost.Load() == nil {
sameBodyWarningHost.Store(host)
logInfo("Same body detected, skipping host: " + host)
return
}
if host != sameBodyWarningHost.Load().(string) {
sameBodyWarningHost.Store(host)
}
return
}
atomic.AddInt32(&sameBodyLengthCount, 1)
} else {
if sameBodyLengthCount != 0 {
sameBodyLengthCount = 0
}
if *recursive {
go invokeAndGeneratePath(url)
}
currentBody.Store(len(body))
logFound(url, statusCode, strconv.Itoa(len(body)))
}
}
}
func invokeAndGeneratePath(u string) {
u = generateURL(u)
wg.Add(len(wordlist))
for _, w := range wordlist {
_ = p.Invoke(u + w + *e)
}
}
func parseStatusCode(codes string) []string {
return strings.Split(codes, ",")
}
func getHost(u string) string {
pu, err := url.Parse(u)
if err != nil {
return ""
}
return pu.Host
}
func checkStatusCode(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func generateURL(u string) string {
if !strings.Contains(u, "http") || !strings.Contains(u, "https") {
if len(*s) <= 0 {
u = defaultSchema + schemaSlicer + u
}
}
if len(*s) > 0 {
if strings.Contains(u, "http") {
strings.ReplaceAll(u, "http", *s)
} else if strings.Contains(u, "https") {
strings.ReplaceAll(u, "https", *s)
} else {
u = *s + schemaSlicer + u
}
}
if u[len(u)-1:] != "/" {
u = u + "/"
}
return u
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func logFound(url, status, length string) {
color.Green.Print("[" + status + "] ")
fmt.Println(url + " - " + length)
}
func logInfo(msg string) {
color.Yellow.Print("[+] ")
fmt.Println(msg)
}
func logError(msg string) {
color.Red.Print("[ERR] ")
fmt.Println(msg)
}
func usageAndExit(msg string) {
if msg != "" {
logError(msg)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}