-
Notifications
You must be signed in to change notification settings - Fork 72
/
http2smugl.go
290 lines (265 loc) · 7.34 KB
/
http2smugl.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
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
func main() {
var (
// root options
timeout time.Duration
connectAddr string
// request subcommand options
bodyFile string
bodyStr []string
requestMethod string
noAutoHeaders bool
noUserAgent bool
autoContentLength bool
bodyToSend [][]byte
bodyLines int
bodyPartsDelay time.Duration
skipBodyEndFlag bool
// detect subcommand options
verbose bool
threads int
targetsFile string
csvLog string
tryHTTP3 bool
)
requestCmd := &cobra.Command{
Use: "request url [header [header...]]",
Short: "make one request with custom headers",
Example: "request https://example.com/ \"transfer-encoding : chunked\"",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if noAutoHeaders && requestMethod != "GET" {
return fmt.Errorf("cannot combine --method and --no-auto-headers")
}
if bodyFile != "" {
if bodyStr != nil {
return errors.New("both --body and --body-str specified")
}
data, err := os.ReadFile(bodyFile)
if err != nil {
return err
}
bodyToSend = append(bodyToSend, data)
} else {
for _, s := range bodyStr {
bodyToSend = append(bodyToSend, []byte(maybeUnquoteArg(s)))
}
}
target, err := url.Parse(args[0])
if err != nil {
return err
}
var headers []Header
for _, h := range args[1:] {
parts := strings.SplitN(h, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid header: %#v", h)
}
if parts[0] == "" && strings.ContainsRune(parts[1], ':') {
parts = strings.SplitN(parts[1], ":", 2)
parts[0] = ":" + parts[0]
}
headers = append(headers, Header{
Name: maybeUnquoteArg(parts[0]),
Value: maybeUnquoteArg(parts[1]),
})
}
doAndPrintRequest(&RequestParams{
Target: target,
Method: maybeUnquoteArg(requestMethod),
ConnectAddr: connectAddr,
Headers: headers,
NoAutoHeaders: noAutoHeaders,
NoUserAgent: noUserAgent,
AddContentLength: autoContentLength,
Body: bodyToSend,
Timeout: timeout,
BodyPartsDelay: bodyPartsDelay,
SkipBodyEndFlag: skipBodyEndFlag,
}, bodyLines)
return nil
},
}
detectCmd := &cobra.Command{
Use: "detect [flags] [url [url...]]",
Short: "detect if an url is vulnerable",
RunE: func(cmd *cobra.Command, args []string) error {
targets := args
if targetsFile != "" {
data, err := ioutil.ReadFile(targetsFile)
if err != nil {
return err
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line != "" {
targets = append(targets, line)
}
}
}
var csvWriter *CSVLogWriter
if csvLog != "" {
var err error
csvWriter, err = NewCSVLogWriter(csvLog)
if err != nil {
return err
}
defer func() {
_ = csvWriter.Close()
}()
}
var targetURLs []string
for i := range targets {
if !strings.Contains(targets[i], "/") {
targetURLs = append(targetURLs, fmt.Sprintf("https://%s/", targets[i]))
if tryHTTP3 {
targetURLs = append(targetURLs, fmt.Sprintf("https+h3://%s/", targets[i]))
}
} else {
targetURLs = append(targetURLs, targets[i])
}
}
return detectMultipleTargets(targetURLs,
connectAddr,
threads,
timeout,
csvWriter,
verbose)
},
}
var rootCmd = &cobra.Command{
Use: "http2smugl",
}
rootCmd.PersistentFlags().DurationVar(&timeout, "timeout", 10*time.Second, "timeout to all requests")
rootCmd.PersistentFlags().StringVar(&connectAddr, "connect-to", "", "override target ip")
requestCmd.Flags().StringVar(&requestMethod, "method", "GET", "request method")
requestCmd.Flags().StringArrayVar(&bodyStr, "body-str", nil, "send this string to body (escape seqs like \\r \\n are supported)")
requestCmd.Flags().StringVar(&bodyFile, "body-file", "", "read request body from this file")
requestCmd.Flags().BoolVar(&noAutoHeaders, "no-auto-headers", false, "don't send pseudo-headers automatically")
requestCmd.Flags().BoolVar(&noUserAgent, "no-user-agent", false, "don't send user-agent")
requestCmd.Flags().BoolVar(&autoContentLength, "auto-content-length", false, "add \"content-length\" header with body size")
requestCmd.Flags().IntVar(&bodyLines, "body-lines", 10, "how many body lines to print (-1 means no limit)")
requestCmd.Flags().DurationVar(&bodyPartsDelay, "body-parts-delay", 0, "delay between body parts")
requestCmd.Flags().BoolVar(&skipBodyEndFlag, "skip-body-end", false, "don't send body end flag (usually results in a timeout)")
detectCmd.Flags().BoolVar(&verbose, "verbose", false, "be more verbose")
detectCmd.Flags().IntVar(&threads, "threads", 100, "number of threads")
detectCmd.Flags().StringVar(&targetsFile, "targets", "", "read targets list from this file")
detectCmd.Flags().StringVar(&csvLog, "csv-log", "", "log results into csv file")
detectCmd.Flags().BoolVar(&tryHTTP3, "try-http3", false, "try HTTP/3 too when no protocol specified in a target")
rootCmd.AddCommand(requestCmd, detectCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
func unquoteArg(s string) (string, error) {
buf := bytes.NewBuffer(nil)
repeatCount := 0
for i := 0; i < len(s); i++ {
var (
toPush byte
skipPush bool
)
if s[i] == '\\' {
i++
if i >= len(s) {
return "", fmt.Errorf("unexpected end of string in escape sequence")
}
switch s[i] {
case 'x', 'X':
i += 2
if i > len(s) {
return "", fmt.Errorf("unexpected end of string in \\x code")
}
b, err := hex.DecodeString(s[i-1 : i+1])
if err != nil {
return "", fmt.Errorf("invalid \\x code: %#v", s[i-1:i+1])
}
toPush = b[0]
case 'r':
toPush = '\r'
case 'n':
toPush = '\n'
case 't':
toPush = '\t'
case 'v':
toPush = '\v'
case 'b':
toPush = '\b'
case 'a':
toPush = '\a'
case 'R':
if repeatCount > 0 {
return "", fmt.Errorf("nested repeat sequences are not supported")
}
i += 1
if i >= len(s) || s[i] < '0' || s[i] > '9' {
return "", fmt.Errorf("invalid repeat count")
}
for ; i < len(s) && s[i] >= '0' && s[i] <= '9'; i++ {
repeatCount = repeatCount*10 + int(s[i]-'0')
}
i--
skipPush = true
default:
toPush = s[i]
}
} else {
toPush = s[i]
}
if !skipPush {
if repeatCount > 0 {
for j := 0; j < repeatCount; j++ {
buf.WriteByte(toPush)
}
repeatCount = 0
} else {
buf.WriteByte(toPush)
}
}
}
if repeatCount > 0 {
return "", fmt.Errorf("repeat sequence is not closed")
}
return buf.String(), nil
}
func maybeUnquoteArg(s string) string {
unquoted, err := unquoteArg(s)
if err != nil {
return s
}
return unquoted
}
func doAndPrintRequest(params *RequestParams, bodyLines int) {
response, err := DoRequest(params)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
if response == nil {
return
}
for _, h := range response.Headers {
fmt.Printf("%s: %s\n", h.Name, h.Value)
}
fmt.Println()
lines := bytes.Split(bytes.Join(response.Body, nil), []byte{'\n'})
for i, l := range lines {
if bodyLines < 0 || i < bodyLines {
fmt.Println(string(l))
} else {
break
}
}
}