-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocks.c
290 lines (249 loc) · 7.47 KB
/
socks.c
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
/*
* Copyright (c) 2013-2021 Jan Klemkow <[email protected]>
* Copyright (c) 2015 Stefan Thiemann <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h>
#include <sys/socket.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef USE_LIBBSD
# include <bsd/string.h>
#else
# include <string.h>
#endif
/* enviroment */
extern char **environ;
/* uscpi */
#define READ_FD STDIN_FILENO
#define WRITE_FD STDOUT_FILENO
/* Set enviroment variable if value is not empty. */
#define set_env(name, value) \
if (strcmp((value), "") != 0) \
setenv((name), (value), 1)
/* negotiation fields */
#define SOCKSv5 0x05
#define RSV 0x00
/* authentication methods */
#define NO_AUTH 0x00
#define GSSAPI 0x01 /* not supported */
#define USRPASS 0x02 /* not supported */
#define NOT_ACC 0xFF
/* address types */
#define IPv4 0x01
#define BIND 0x03
#define IPv6 0x04
/* commands */
#define CMD_CONNECT 0x01
#define CMD_BIND 0x02 /* not supported */
#define CMD_UDP_ASS 0x03 /* not supported */
#define WRITE(fd, ptr, len) do { \
if (write((fd), (ptr), (len)) < (len)) \
goto err; \
} while(0);
struct nego {
uint8_t ver;
uint8_t nmethods;
uint8_t method;
};
struct nego_ans {
uint8_t ver;
uint8_t method;
};
struct request {
uint8_t ver;
uint8_t cmd;
uint8_t rsv;
uint8_t atyp;
union {
uint8_t ip6[16];
uint8_t ip4[4];
struct {
uint8_t len;
char str[255];
} name;
} addr;
uint16_t port;
};
char *
rep_mesg(uint8_t rep)
{
switch (rep) {
case 0x00: return "succeeded";
case 0x01: return "general SOCKS server failure";
case 0x02: return "connection not allowed by ruleset";
case 0x03: return "Network unreachable";
case 0x04: return "Host unreachable";
case 0x05: return "Connection refused";
case 0x06: return "TTL expired";
case 0x07: return "Command not supported";
case 0x08: return "Address type not supported";
case 0x09:
default: break;
}
return "unassigned";
}
void
usage(void)
{
fprintf(stderr, "tcpclient PROXY-HOST PROXY-PORT "
"socks HOST PORT PROGRAM [ARGS...]\n");
exit(EXIT_FAILURE);
}
#if 0
int
read_request(struct request *request)
{
read(READ_FD, request, 4);
if (request->atyp == IPv6) {
read(READ_FD, &request->addr.ip6, 16);
} else if (request->atyp == IPv4) {
read(READ_FD, &request->addr.ip4, 4);
} else if (request->atyp == BIND) {
read(READ_FD, &request->addr.name.len, \
sizeof request->addr.name.len);
read(READ_FD, request->addr.name.str, request->addr.name.len);
} else {
/* XXX: send err */
perror("this should not happen");
}
/* socks: send requested port */
if (read(READ_FD, &request->port, sizeof request->port) == -1) goto err;
return 0;
err:
return -1;
}
#endif
int
main(int argc, char *argv[], char *envp[])
{
struct nego nego = {SOCKSv5, 1, NO_AUTH};
struct nego_ans nego_ans = {0, 0};
struct request request = {SOCKSv5, CMD_CONNECT, RSV, 0, {{0}}, 0};
struct request reply = {SOCKSv5, 0, RSV, 0, {{0}}, 0};
int ch, af = AF_INET6;
environ = envp;
while ((ch = getopt(argc, argv, "p:")) != -1) {
switch (ch) {
case 'h':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc < 3) usage();
char *host = *argv; argv++; argc--;
char *port = *argv; argv++; argc--;
char *prog = *argv; /* argv[0] == program name */
if (strlen(host) > 255)
perror("socks: hostname is too long");
/* parsing address argument */
if (inet_pton(AF_INET6, host, &request.addr.ip6) == 1) {
af = AF_INET6;
request.atyp = IPv6;
} else if (inet_pton(AF_INET, host, &request.addr.ip4) == 1) {
af = AF_INET;
request.atyp = IPv4;
} else if ((memcpy(request.addr.name.str, host, strlen(host))) != NULL){
af = AF_INET;
request.atyp = BIND;
} else {
perror("could not handle address");
}
/* parsing port number */
if ((request.port = htons((uint16_t)strtol(port, NULL, 0))) == 0) goto err;
/* socks: start negotiation */
if (write(WRITE_FD, &nego, sizeof nego) < 0) goto err;
if (read(READ_FD, &nego_ans, sizeof nego_ans) < 0) goto err;
if (nego_ans.method == NOT_ACC)
perror("No acceptable authentication methods");
/* socks: request for connection */
if (write(WRITE_FD, &request, 4) < 0) goto err;
if (request.atyp == IPv6) {
write(WRITE_FD, &request.addr.ip6, 16);
} else if (request.atyp == IPv4) {
write(WRITE_FD, &request.addr.ip4, 4);
} else if (request.atyp == BIND) {
request.addr.name.len = strlen(host);
write(WRITE_FD, &request.addr.name.len, \
sizeof request.addr.name.len);
write(WRITE_FD, request.addr.name.str, request.addr.name.len);
} else {
perror("this should not happen");
}
/* socks: send requested port */
if (write(WRITE_FD, &request.port, sizeof request.port) < 0) goto err;
/* socks: start analysing reply */
if (read(READ_FD, &reply, 4) < 0) goto err;
if (reply.cmd != 0)
perror(rep_mesg(reply.cmd));
/* read the bind address of the reply */
if (reply.atyp == IPv6) {
af = AF_INET6;
read(READ_FD, &reply.addr.ip6, sizeof reply.addr.ip6);
} else if (reply.atyp == IPv4) {
read(READ_FD, &reply.addr.ip4, sizeof reply.addr.ip4);
} else if (reply.atyp == BIND) {
read(READ_FD, &reply.addr.name.len, sizeof reply.addr.name.len);
read(READ_FD, &reply.addr.name.str, reply.addr.name.len);
} else {
perror("socks: unknown address type in reply");
}
/* read the port of the replay */
read(READ_FD, &reply.port, sizeof reply.port);
/* set ucspi enviroment variables */
char *tcp_remote_ip = getenv("TCPREMOTEIP");
char *tcp_remote_port = getenv("TCPREMOTEPORT");
char *tcp_remote_host = getenv("TCPREMOTEHOST");
char *tcp_local_ip = getenv("TCPLOCALIP");
char *tcp_local_port = getenv("TCPLOCALPORT");
char *tcp_local_host = getenv("TCPLOCALHOST");
set_env("SOCKSREMOTEIP", strdup(tcp_remote_ip));
set_env("SOCKSREMOTEPORT", strdup(tcp_remote_port));
set_env("SOCKSREMOTEHOST", strdup(tcp_remote_host));
set_env("SOCKSLOCALIP", strdup(tcp_local_ip));
set_env("SOCKSLOCALPORT", strdup(tcp_local_port));
set_env("SOCKSLOCALHOST", strdup(tcp_local_host));
char tmp[BUFSIZ];
if (request.atyp == IPv6 || request.atyp == IPv4) {
inet_ntop(af, &request.addr, tmp, sizeof tmp);
setenv("TCPREMOTEIP", tmp, 1);
unsetenv("TCPREMOTEHOST");
} else {
setenv("TCPREMOTEHOST", host, 1);
unsetenv("TCPREMOTEIP");
}
snprintf(tmp, sizeof tmp, "%d", ntohs(request.port));
setenv("TCPREMOTEPORT", tmp, 1);
if (reply.atyp == IPv6 || reply.atyp == IPv4) {
inet_ntop(af, &reply.addr, tmp, sizeof tmp);
setenv("TCPLOCALIP", tmp, 1);
unsetenv("TCPLOCALHOST");
} else {
strlcpy(tmp, reply.addr.name.str, reply.addr.name.len);
setenv("TCPLOCALHOST", tmp, 1);
unsetenv("TCPLOCALIP");
}
snprintf(tmp, sizeof tmp, "%d", ntohs(reply.port));
setenv("TCPLOCALPORT", tmp, 1);
/* start client program */
execve(prog, argv, environ);
err:
perror("socks");
return EXIT_FAILURE;
}