-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks.go
260 lines (224 loc) · 6.97 KB
/
socks.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
/*
Copyright 2018 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// package socks implements a SOCKS5 handshake.
package socks
import (
"encoding/binary"
"io"
"net"
"strconv"
"github.com/gravitational/teleport"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)
var log = logrus.WithFields(logrus.Fields{
trace.Component: teleport.ComponentSOCKS,
})
const (
socks5Version byte = 0x05
socks5Reserved byte = 0x00
socks5AuthNotRequired byte = 0x00
socks5CommandConnect byte = 0x01
socks5AddressTypeIPv4 byte = 0x01
socks5AddressTypeDomainName byte = 0x03
socks5AddressTypeIPv6 byte = 0x04
socks5Succeeded byte = 0x00
)
// Handshake performs a SOCKS5 handshake with the client and returns
// the remote address to proxy the connection to.
func Handshake(conn net.Conn) (string, error) {
// Read in the version and reject anything other than SOCKS5.
version, err := readByte(conn)
if err != nil {
return "", trace.Wrap(err)
}
if version != socks5Version {
return "", trace.BadParameter("only SOCKS5 is supported")
}
// Read in the authentication method requested by the client and write back
// the method that was selected. At the moment only "no authentication
// required" is supported.
authMethods, err := readAuthenticationMethod(conn)
if err != nil {
return "", trace.Wrap(err)
}
if !byteSliceContains(authMethods, socks5AuthNotRequired) {
return "", trace.BadParameter("only 'no authentication required' is supported")
}
err = writeMethodSelection(conn)
if err != nil {
return "", trace.Wrap(err)
}
// Read in the request from the client and make sure the requested command
// is supported and extract the remote address. If everything is good, write
// out a success response.
remoteAddr, err := readRequest(conn)
if err != nil {
return "", trace.Wrap(err)
}
err = writeReply(conn)
if err != nil {
return "", trace.Wrap(err)
}
return remoteAddr, nil
}
// readAuthenticationMethod reads in the authentication methods the client
// supports.
func readAuthenticationMethod(conn net.Conn) ([]byte, error) {
// Read in the number of authentication methods supported.
nmethods, err := readByte(conn)
if err != nil {
return nil, trace.Wrap(err)
}
// Read nmethods number of bytes from the connection return the list of
// supported authentication methods to the caller.
authMethods := make([]byte, nmethods)
for i := byte(0); i < nmethods; i++ {
method, err := readByte(conn)
if err != nil {
return nil, trace.Wrap(err)
}
authMethods = append(authMethods, method)
}
return authMethods, nil
}
// writeMethodSelection writes out the response to the authentication methods.
// Right now, only SOCKS5 and "no authentication methods" is supported.
func writeMethodSelection(conn net.Conn) error {
message := []byte{socks5Version, socks5AuthNotRequired}
n, err := conn.Write(message)
if err != nil {
return trace.Wrap(err)
}
if n != len(message) {
return trace.BadParameter("wrote: %v wanted to write: %v", n, len(message))
}
return nil
}
// readRequest reads in the SOCKS5 request from the client and returns the
// host:port the client wants to connect to.
func readRequest(conn net.Conn) (string, error) {
// Read in the version and reject anything other than SOCKS5.
version, err := readByte(conn)
if err != nil {
return "", trace.Wrap(err)
}
if version != socks5Version {
return "", trace.BadParameter("only SOCKS5 is supported")
}
// Read in the command the client is requesting. Only CONNECT is supported.
command, err := readByte(conn)
if err != nil {
return "", trace.Wrap(err)
}
if command != socks5CommandConnect {
return "", trace.BadParameter("only CONNECT command is supported")
}
// Read in and throw away the reserved byte.
_, err = readByte(conn)
if err != nil {
return "", trace.Wrap(err)
}
// Read in the address type and determine how many more bytes need to be
// read in to read in the remote host address.
addrLen, err := readAddrType(conn)
if err != nil {
return "", trace.Wrap(err)
}
// Read in the destination address.
destAddr := make([]byte, addrLen)
_, err = io.ReadFull(conn, destAddr)
if err != nil {
return "", trace.Wrap(err)
}
// Read in the destination port.
var destPort uint16
err = binary.Read(conn, binary.BigEndian, &destPort)
if err != nil {
return "", trace.Wrap(err)
}
return net.JoinHostPort(string(destAddr), strconv.Itoa(int(destPort))), nil
}
// readAddrType reads in the address type and returns the length of the dest
// addr field.
func readAddrType(conn net.Conn) (int, error) {
// Read in the type of the remote host.
addrType, err := readByte(conn)
if err != nil {
return 0, trace.Wrap(err)
}
// Based off the type, determine how many more bytes to read in for the
// remote address. For IPv4 it's 4 bytes, for IPv6 it's 16, and for domain
// names read in another byte to determine the length of the field.
switch addrType {
case socks5AddressTypeIPv4:
return net.IPv4len, nil
case socks5AddressTypeIPv6:
return net.IPv6len, nil
case socks5AddressTypeDomainName:
len, err := readByte(conn)
if err != nil {
return 0, trace.Wrap(err)
}
return int(len), nil
default:
return 0, trace.BadParameter("unsupported address type: %v", addrType)
}
}
// Write the response to the client.
func writeReply(conn net.Conn) error {
// Write success reply, similar to OpenSSH only success is written.
// https://github.com/openssh/openssh-portable/blob/5d14019/channels.c#L1442-L1452
message := []byte{
socks5Version,
socks5Succeeded,
socks5Reserved,
socks5AddressTypeIPv4,
}
n, err := conn.Write(message)
if err != nil {
return trace.Wrap(err)
}
if n != len(message) {
return trace.BadParameter("wrote: %v wanted to write: %v", n, len(message))
}
// Reply also requires BND.ADDR and BDN.PORT even though they are ignored
// because Teleport only supports CONNECT.
message = []byte{0, 0, 0, 0, 0, 0}
n, err = conn.Write(message)
if err != nil {
return trace.Wrap(err)
}
if n != len(message) {
return trace.BadParameter("wrote: %v wanted to write: %v", n, len(message))
}
return nil
}
// readByte a single byte from the passed in net.Conn.
func readByte(conn net.Conn) (byte, error) {
b := make([]byte, 1)
_, err := io.ReadFull(conn, b)
if err != nil {
return 0, trace.Wrap(err)
}
return b[0], nil
}
// byteSliceContains checks if the slice a contains the byte b.
func byteSliceContains(a []byte, b byte) bool {
for _, v := range a {
if v == b {
return true
}
}
return false
}