forked from danielthatcher/smuggles
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.go
More file actions
215 lines (186 loc) · 4.91 KB
/
Copy pathworker.go
File metadata and controls
215 lines (186 loc) · 4.91 KB
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
package main
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
"sync"
"time"
)
type Worker struct {
Conf Config
Errs chan<- error
ErrCounts *map[string]uint
ErrCountsMux *sync.RWMutex
}
type BaseResult struct {
Time time.Duration
Url *url.URL
}
// BaseTimes fetches urls on a channel and times how long it takes to fetch those URLs
func (w *Worker) BaseTimes(urls <-chan *url.URL, results chan<- BaseResult, done func()) {
for u := range urls {
req := baseReq(u, w.Conf.Headers)
start := time.Now()
_, err, _ := w.SendRequest(req, u, 30*time.Second)
end := time.Now()
duration := end.Sub(start)
if err != nil {
w.Errs <- err
continue
}
results <- BaseResult{duration, u}
}
done()
}
// SmuggleType represents the type of smuggling vulnerability - either CL.TE or TE.CL
type SmuggleType string
const (
SAFE = ""
CLTE = "CL.TE"
TECL = "TE.CL"
)
// SmuggleTest represents the parameters for a test of CL.TE and TE.CL smuggling against
// a single URL using a single method and a single Transfer-Encoding header mutation
type SmuggleTest struct {
// The URL to test
Url *url.URL
// The Method to test
Method string
// The name of the Mutation to use. Should be a key in mutations
Mutation string
// The delay that will indicate the service is vulnerable. Should be calculated off
// of the time take for the base request to this service
Timeout time.Duration
// The type of attack the service is vulnerable to
Status SmuggleType
}
// Equals returns whether two SmuggleTests are equal
func (t SmuggleTest) Equals(s SmuggleTest) bool {
return t.Url.String() == s.Url.String() && t.Method == s.Method && t.Mutation == s.Mutation
}
// smuggleWorker sends requests URLs using the given Transfer-Encoding header,
// and checks for CL.TE then TE.CL vulnerabilities
func (w *Worker) SmuggleTest(tests <-chan SmuggleTest, results chan<- SmuggleTest, done func()) {
for t := range tests {
// Skip test if we've received too many errors for this URL
if w.Conf.MaxErrors > 0 {
w.ErrCountsMux.RLock()
if (*w.ErrCounts)[t.Url.String()] >= w.Conf.MaxErrors {
continue
}
w.ErrCountsMux.RUnlock()
}
// First test for CL.TE
req := clte(t.Method, t.Url, w.Conf.Mutations[t.Mutation], w.Conf.Headers)
_, err, isTimeout := w.SendRequest(req, t.Url, t.Timeout)
if isTimeout {
// Send the verification request
req = clteVerify(t.Method, t.Url, w.Conf.Mutations[t.Mutation], w.Conf.Headers)
_, err, verifyTimeout := w.SendRequest(req, t.Url, t.Timeout)
if !verifyTimeout {
t.Status = CLTE
results <- t
continue
} else if err != nil {
w.ErrCountsMux.Lock()
(*w.ErrCounts)[t.Url.String()]++
w.ErrCountsMux.Unlock()
w.Errs <- err
}
} else if err != nil {
w.ErrCountsMux.Lock()
(*w.ErrCounts)[t.Url.String()]++
w.ErrCountsMux.Unlock()
w.Errs <- err
}
// First test for TE.CL
req = tecl(t.Method, t.Url, w.Conf.Mutations[t.Mutation], w.Conf.Headers)
_, err, isTimeout = w.SendRequest(req, t.Url, t.Timeout)
if isTimeout {
// Send the verification request
req = teclVerify(t.Method, t.Url, w.Conf.Mutations[t.Mutation], w.Conf.Headers)
_, err, verifyTimeout := w.SendRequest(req, t.Url, t.Timeout)
if !verifyTimeout {
t.Status = TECL
results <- t
continue
} else if err != nil {
w.ErrCountsMux.Lock()
(*w.ErrCounts)[t.Url.String()]++
w.ErrCountsMux.Unlock()
w.Errs <- err
}
} else if err != nil {
w.ErrCountsMux.Lock()
(*w.ErrCounts)[t.Url.String()]++
w.ErrCountsMux.Unlock()
w.Errs <- err
}
results <- t
}
done()
}
// sendRequest sends the specified request, but doesn't try to parse the response,
// and instead just returns it
func (w *Worker) SendRequest(req []byte, u *url.URL, timeout time.Duration) (resp []byte, err error, isTimeout bool) {
var cerr error
var conn io.ReadWriteCloser
port := u.Port()
if port == "" {
if u.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
target := fmt.Sprintf("%s:%s", u.Hostname(), port)
if u.Scheme == "https" {
conf := &tls.Config{InsecureSkipVerify: true}
conn, cerr = tls.DialWithDialer(&net.Dialer{
Timeout: timeout,
}, "tcp", target, conf)
} else {
d := net.Dialer{Timeout: timeout}
conn, cerr = d.Dial("tcp", target)
}
if cerr != nil {
err = cerr
return
}
_, err = conn.Write(req)
if err != nil {
return
}
// See if we can read before the timeout
c := make(chan []byte)
e := make(chan error)
go func() {
r, err := ioutil.ReadAll(conn)
if err != nil {
e <- err
} else {
c <- r
}
}()
var start time.Time
if w.Conf.Debug {
start = time.Now()
}
select {
case resp = <-c:
case err = <-e:
case <-time.After(timeout):
isTimeout = true
conn.Close()
}
if w.Conf.Debug {
d := time.Now().Sub(start)
fmt.Printf("Request to %s took %dms (timeout: %t)\n", u.String(), d.Milliseconds(), isTimeout)
fmt.Println(string(req))
fmt.Println("---")
}
return
}