-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmeth.go
247 lines (214 loc) · 5.53 KB
/
dmeth.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
)
const colorReset = "\033[0m"
const colorRed = "\033[31m"
const colorGreen = "\033[32m"
const colorYellow = "\033[33m"
const colorBlue = "\033[34m"
const colorPurple = "\033[35m"
const colorCyan = "\033[36m"
const colorWhite = "\033[37m"
const bgYellow = "\033[43m"
const banner = `
dP 8888ba.88ba dP dP
88 88 '8b '8b 88 88
.d888b88 88 88 88 .d8888b. d8888P 88d888b.
88' '88 88 88 88 88ooood8 88 88' '88
88. .88 88 88 88 88. ... 88 88 88
'88888P8 dP dP dP '88888P' dP dP dP
==========================================================
dMeth - Concurrently discover allowed HTTP methods in URLs
==========================================================
==> https://github.com/prasant-paudel/dmeth <==
`
const usage = `USAGE:
dmeth [OPTIONS] [TARGET]
OPTIONS:
-s string Allowed status codes
-b string Blocked status codes (default=405,501)
-v Verbose (show all responses)
-h Show help menu
EXAMPLES:
1. dmeth https://example.com
2. dmeth -s 200,301 -m post,delete target_urls.txt
3. echo "https://example.com" | dmeth -b 405 -m post
4. cat target_urls.txt | dmeth
`
var ch = make(chan string)
var methods = []string{
"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS",
"TRACE", "PATCH",
}
var (
targetUrls []string
allowedStatusCodes string
blockedStatusCodes string
allowedMethods string
verbose bool
helpFlag bool
)
type Response struct {
Method string
URL string
StatusCode int
Error error
}
func parseArguments() {
flag.StringVar(&allowedStatusCodes, "s", "", "Allowed status codes")
flag.StringVar(&blockedStatusCodes, "b", "405,501", "Blocked status codes")
flag.StringVar(&allowedMethods, "m", "all", "Allowed HTTP methods to look for")
flag.BoolVar(&verbose, "v", false, "Blocked status codes")
flag.BoolVar(&helpFlag, "h", false, "Show this help menu")
flag.Parse()
if allowedStatusCodes != "" {
blockedStatusCodes = ""
}
targetUrls = getTargets()
if helpFlag || len(targetUrls) == 0 {
fmt.Print(colorPurple, banner, colorReset)
fmt.Print(usage)
os.Exit(0)
}
if allowedMethods != "all" {
var _meths []string
for _, m := range strings.Split(allowedMethods, ",") {
m = strings.TrimSpace(m)
m = strings.ToUpper(m)
_meths = append(_meths, m)
}
methods = _meths
}
}
func main() {
parseArguments()
fmt.Print(colorPurple, banner, colorReset)
s := ParseStatusCodes(allowedStatusCodes)
b := ParseStatusCodes(blockedStatusCodes)
if len(b) > 0 && len(s) > 0 {
log.Fatalln("[!] Flags -s and -b can not be used at the same time.")
}
resCh := make(chan Response)
for _, m := range methods {
requests := CreateRequests(m, targetUrls)
go SendRequests(requests, resCh)
}
for range len(methods) * len(targetUrls) {
res := <-resCh
if verbose || containsInt(s, res.StatusCode) || (len(s) == 0 && !containsInt(b, res.StatusCode)) {
fmt.Print(res.String())
}
}
}
func CreateRequests(method string, urls []string) (requests []*http.Request) {
for _, url := range targetUrls {
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Printf("[!] Error creating '%s' request for %s", method, url)
continue
}
requests = append(requests, req)
}
return
}
func SendRequests(requests []*http.Request, outCh chan<- Response) {
t := &http.Transport{}
for _, req := range requests {
res, err := t.RoundTrip(req)
if err != nil {
outCh <- Response{
Error: fmt.Errorf("[!] Error sending '%s' request for %s", req.Method, req.URL),
}
}
outCh <- Response{
Method: res.Request.Method,
URL: res.Request.URL.String(),
StatusCode: res.StatusCode,
}
}
}
func (r *Response) MethodColor() string {
if r.Method == "GET" || r.Method == "HEAD" {
return colorCyan
} else if r.Method == "POST" {
return colorGreen
} else if r.Method == "DELETE" {
return colorRed
} else if r.Method == "PUT" || r.Method == "PATCH" || r.Method == "UPDATE" {
return colorYellow
} else if r.Method == "OPTIONS" {
return colorPurple
}
return colorReset
}
func (res Response) String() string {
if res.Error != nil {
return res.Error.Error()
}
m := res.Method
output := fmt.Sprintln("[+] "+m+strings.Repeat(" ", 8-len(m))+":", res.StatusCode, " | URL: ", res.URL)
output = res.MethodColor() + output + colorReset
return output
}
func getTargets() []string {
// From stdin
f, _ := os.Stdin.Stat()
if f.Mode()&os.ModeCharDevice == 0 {
return ReadFileByLines(os.Stdin.Name())
}
// From argument
if len(os.Args) > 1 {
t := os.Args[len(os.Args)-1]
// If URL
if strings.HasPrefix(t, "http") {
return []string{t}
}
// If file
return ReadFileByLines(t)
}
return nil
}
func ParseStatusCodes(s string) []int {
var out []int
if s != "" {
splittedCodes := strings.Split(s, ",")
for _, code := range splittedCodes {
var _i int
_, err := fmt.Sscan(code, &_i)
if err != nil {
log.Fatal("Invalid status code: ", code)
}
out = append(out, _i)
}
}
return out
}
func containsInt(arr []int, e int) bool {
for _, i := range arr {
if i == e {
return true
}
}
return false
}
func ReadFileByLines(filePath string) []string {
var lines []string
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, strings.TrimSpace(scanner.Text()))
}
file.Close()
return lines
}