forked from neex/http2smugl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect.go
149 lines (132 loc) · 3.41 KB
/
detect.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
package main
import (
"errors"
"fmt"
"log"
"net"
"net/url"
"time"
)
type DetectParams struct {
Target string
RequestMethod string
DetectMethod DetectMethod
SmugglingMethod SmugglingMethod
SmugglingVariant SmugglingVariant
PaddingMethod PaddingMethod
}
func (p *DetectParams) String() string {
return fmt.Sprintf(
"target=%s, http_method=%s, detect_method=%s, "+
"smuggling_method=%s, smuggling_variant=%s, padding_method=%s",
p.Target,
p.RequestMethod,
p.DetectMethod,
p.SmugglingMethod,
p.SmugglingVariant,
p.PaddingMethod,
)
}
type DetectResult int
const (
Indistinguishable DetectResult = iota
DistinguishableByTiming
DistinguishableNotByTiming
)
func (r DetectResult) String() string {
switch r {
case Indistinguishable:
return "indistinguishable"
case DistinguishableByTiming:
return "distinguishable by timing"
case DistinguishableNotByTiming:
return "distinguishable not by timing"
default:
return fmt.Sprintf("unknown result value: %#v", r)
}
}
func Detect(params *DetectParams, connectTo string, timeout time.Duration, verbose bool) (DetectResult, error) {
u, err := url.Parse(params.Target)
if err != nil {
return Indistinguishable, err
}
prefixHeaders := params.PaddingMethod.Headers()
valid, invalid := params.DetectMethod.GetRequests(params.SmugglingMethod, u, params.SmugglingVariant)
valid.AdditionalHeaders = prefixHeaders.Combine(valid.AdditionalHeaders)
invalid.AdditionalHeaders = prefixHeaders.Combine(invalid.AdditionalHeaders)
validResponses := &ResponseSet{}
invalidResponses := &ResponseSet{}
var rawRequest string
for i := 0; i < 14 && validResponses.DistinguishableFrom(invalidResponses); i++ {
// The order is: V I ( V V V I I I ) x 4
doValid := i == 0 || (i >= 2 && ((i-2)/3)%2 == 0)
request := valid
if !doValid {
request = invalid
}
var (
response *HTTPMessage
err = errNotStarted
triesLeft = 3
)
for err != nil && mayResolveInFuture(err) && triesLeft > 0 {
triesLeft--
rawRequest, response, err = DoRequest(&RequestParams{
Target: u,
Method: params.RequestMethod,
ConnectAddr: connectTo,
Headers: request.AdditionalHeaders,
Body: request.Body,
Timeout: timeout,
})
}
if verbose{
if err != nil && (!isRSTError(err) && !isTimeoutError(err)) {
log.Printf("request: %v, error: %v", params, err)
}
}
if doValid {
validResponses.AccountResponse(response, isTimeoutError(err))
} else {
invalidResponses.AccountResponse(response, isTimeoutError(err))
}
}
result := Indistinguishable
//fmt.Println(rawRequest)
if validResponses.DistinguishableFrom(invalidResponses) {
// todo
fmt.Println(rawRequest)
if validResponses.AllResponsesAreTimeouts() || invalidResponses.AllResponsesAreTimeouts() {
result = DistinguishableByTiming
} else {
result = DistinguishableNotByTiming
}
}
if verbose {
log.Printf("%s: valid=%s, invalid=%s, result=%v",
params, validResponses, invalidResponses, result)
}
return result, nil
}
func isRSTError(err error) bool {
_, ok := err.(ConnDropError)
return ok
}
func isTimeoutError(err error) bool {
n, ok := err.(net.Error)
if !ok {
return false
}
return n.Timeout()
}
var errNotStarted = errors.New("not started")
func mayResolveInFuture(err error) bool {
if err == errNotStarted {
return true
}
n, ok := err.(net.Error)
if !ok {
return false
}
return n.Timeout() || n.Temporary()
}