forked from samyk/pwnat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpclient.c
460 lines (394 loc) · 13 KB
/
udpclient.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Project: udptunnel
* File: udpclient.c
*
* Copyright (C) 2009 Daniel Meekins
* Contact: dmeekins - gmail
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#else
#include "gettimeofday.h"
#endif
#include "common.h"
#include "message.h"
#include "socket.h"
#include "packet.h"
#include "client.h"
#include "list.h"
extern int debug_level;
extern int ipver;
static int running = 1;
static uint16_t next_req_id;
/* internal functions */
static int handle_message(client_t *c, uint16_t id, uint8_t msg_type,
char *data, int data_len);
static void disconnect_and_remove_client(uint16_t id, list_t *clients,
fd_set *fds);
static void signal_handler(int sig);
bool isnumber(const char* str) {
if (!str) {
return false;
}
char* end;
strtol(str, &end, 10);
return *end == '\0';
}
/*
* argv: [local ip] <local port> <proxy host> [proxy port] <remote host> <remote port>
*/
int udpclient(int argc, char* argv[])
{
char *lhost, *lport, *phost, *pport, *rhost, *rport;
list_t *clients;
list_t *conn_clients;
client_t *client;
client_t *client2;
socket_t *tcp_serv = NULL;
socket_t *tcp_sock = NULL;
socket_t *udp_sock = NULL;
char data[MSG_MAX_LEN];
char addrstr[ADDRSTRLEN];
char pport_s[6] = "2222";
struct timeval curr_time;
struct timeval check_time;
struct timeval check_interval;
struct timeval timeout;
fd_set client_fds;
fd_set read_fds;
uint16_t tmp_id;
uint8_t tmp_type;
uint16_t tmp_len;
uint16_t tmp_req_id;
int num_fds;
int ret;
int i;
int icmp_sock = 0;
int timeexc = 0;
struct sockaddr_in src, dest, rsrc;
struct hostent *hp;
uint32_t timeexc_ip;
signal(SIGINT, &signal_handler);
// Parse arguments
i = 0;
if (!isnumber(argv[i]))
lhost = argv[i++];
else
lhost = NULL;
lport = argv[i++];
phost = argv[i++];
if (isnumber(argv[i]))
pport = argv[i++];
else
pport = pport_s;
rhost = argv[i++];
rport = argv[i++];
/* Get address from the machine */
rsrc.sin_family = PF_INET;
rsrc.sin_addr.s_addr = INADDR_ANY;
if (lhost)
{
hp = gethostbyname(lhost);
memcpy(&rsrc.sin_addr, hp->h_addr, hp->h_length);
inet_pton(AF_INET, lhost, &(rsrc.sin_addr));
}
/* IP of destination */
memset(&src, 0, sizeof(struct sockaddr_in));
hp = gethostbyname(phost);
timeexc_ip = *(uint32_t*)hp->h_addr_list[0];
src.sin_family = AF_INET;
src.sin_port = 0;
src.sin_addr.s_addr = timeexc_ip;
/* IP of where the fake packet (echo request) was going */
hp = gethostbyname("3.3.3.3");
memcpy(&dest.sin_addr, hp->h_addr, hp->h_length);
inet_pton(AF_INET, "3.3.3.3", &(dest.sin_addr));
srand(time(NULL));
next_req_id = rand() % 0xffff;
/* Create an empty list for the clients */
clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
p_client_free);
ERROR_GOTO(clients == NULL, "Error creating clients list.", done);
/* Create and empty list for the connecting clients */
conn_clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
p_client_free);
ERROR_GOTO(conn_clients == NULL, "Error creating clients list.", done);
/* Create a TCP server socket to listen for incoming connections */
tcp_serv = sock_create(lhost, lport, ipver, SOCK_TYPE_TCP, 1, 1);
ERROR_GOTO(tcp_serv == NULL, "Error creating TCP socket.", done);
if(debug_level >= DEBUG_LEVEL1)
{
printf("Listening on TCP %s\n",
sock_get_str(tcp_serv, addrstr, sizeof(addrstr)));
}
FD_ZERO(&client_fds);
/* Initialize all the timers */
timerclear(&timeout);
check_interval.tv_sec = 0;
check_interval.tv_usec = 500000;
gettimeofday(&check_time, NULL);
/* open raw socket */
icmp_sock = create_icmp_socket();
if (icmp_sock == -1) {
printf("[main] can't open raw socket\n");
exit(1);
}
while(running)
{
if(!timerisset(&timeout))
timeout.tv_usec = 50000;
if (timeexc++ % 100 == 0)
{
/* Send ICMP TTL exceeded to penetrate remote NAT */
send_icmp(icmp_sock, &rsrc, &src, &dest, 0);
}
read_fds = client_fds;
FD_SET(SOCK_FD(tcp_serv), &read_fds);
ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);
PERROR_GOTO(ret < 0, "select", done);
num_fds = ret;
gettimeofday(&curr_time, NULL);
/* Go through all the clients and check if didn't get an ACK for sent
data during the timeout period */
if(timercmp(&curr_time, &check_time, >))
{
for(i = 0; i < LIST_LEN(clients); i++)
{
client = list_get_at(clients, i);
ret = client_check_and_resend(client, curr_time);
if(ret == -2)
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds);
i--;
continue;
}
ret = client_check_and_send_keepalive(client, curr_time);
if(ret == -2)
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds);
i--;
}
}
timeradd(&curr_time, &check_interval, &check_time);
}
if(num_fds == 0)
continue;
/* Check if pending TCP connection to accept and create a new client
and UDP connection if one is ready */
if(FD_ISSET(SOCK_FD(tcp_serv), &read_fds))
{
tcp_sock = sock_accept(tcp_serv);
udp_sock = sock_create(phost, pport, ipver,
SOCK_TYPE_UDP, 0, 1);
client = client_create(next_req_id++, tcp_sock, udp_sock, 1);
if(!client || !tcp_sock || !udp_sock)
{
if(tcp_sock)
sock_close(tcp_sock);
if(udp_sock)
sock_close(udp_sock);
}
else
{
client2 = list_add(conn_clients, client);
client_free(client);
client = NULL;
client_send_hello(client2, rhost, rport, CLIENT_ID(client2));
client_add_tcp_fd_to_set(client2, &client_fds);
client_add_udp_fd_to_set(client2, &client_fds);
}
sock_free(tcp_sock);
sock_free(udp_sock);
tcp_sock = NULL;
udp_sock = NULL;
num_fds--;
}
/* Check for pending handshakes from UDP connection */
for(i = 0; i < LIST_LEN(conn_clients) && num_fds > 0; i++)
{
client = list_get_at(conn_clients, i);
if(client_udp_fd_isset(client, &read_fds))
{
num_fds--;
tmp_req_id = CLIENT_ID(client);
ret = client_recv_udp_msg(client, data, sizeof(data),
&tmp_id, &tmp_type, &tmp_len);
if(ret == 0)
ret = handle_message(client, tmp_id, tmp_type,
data, tmp_len);
if(ret < 0)
{
disconnect_and_remove_client(tmp_req_id, conn_clients,
&client_fds);
i--;
}
else
{
client = list_add(clients, client);
list_delete_at(conn_clients, i);
client_remove_udp_fd_from_set(client, &read_fds);
i--;
}
}
}
/* Check if data is ready from any of the clients */
for(i = 0; i < LIST_LEN(clients) && num_fds > 0; i++)
{
client = list_get_at(clients, i);
/* Check for UDP data */
if(client_udp_fd_isset(client, &read_fds))
{
num_fds--;
ret = client_recv_udp_msg(client, data, sizeof(data),
&tmp_id, &tmp_type, &tmp_len);
if(ret == 0)
ret = handle_message(client, tmp_id, tmp_type,
data, tmp_len);
if(ret < 0)
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds);
i--;
continue; /* Don't go to check the TCP connection */
}
}
/* Check for TCP data */
if(client_tcp_fd_isset(client, &read_fds))
{
num_fds--;
ret = client_recv_tcp_data(client);
if(ret == 0)
ret = client_send_udp_data(client);
#if 0 /* if udptunnel is taking up 100% of cpu, try including this */
else if(ret == 1)
#ifdef WIN32
_sleep(1);
#else
usleep(1000); /* Quick hack so doesn't use 100% of CPU if
data wasn't ready yet (waiting for ack) */
#endif /*WIN32*/
#endif /*0*/
if(ret < 0)
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds);
i--;
}
}
}
}
done:
if(debug_level >= DEBUG_LEVEL1)
printf("Cleaning up...\n");
if(tcp_serv)
{
sock_close(tcp_serv);
sock_free(tcp_serv);
}
if(udp_sock)
{
sock_close(udp_sock);
sock_free(udp_sock);
}
if(clients)
list_free(clients);
if(debug_level >= DEBUG_LEVEL1)
printf("Goodbye.\n");
return 0;
}
/*
* Closes the TCP and UDP connections for the client and remove its stuff from
* the lists.
*/
void disconnect_and_remove_client(uint16_t id, list_t *clients, fd_set *fds)
{
client_t *c;
c = list_get(clients, &id);
if(!c)
return;
client_send_goodbye(c);
if(debug_level >= DEBUG_LEVEL1)
printf("Client %d disconnected.\n", CLIENT_ID(c));
client_remove_udp_fd_from_set(c, fds);
client_remove_tcp_fd_from_set(c, fds);
client_disconnect_tcp(c);
client_disconnect_udp(c);
list_delete(clients, &id);
}
/*
* Handles a message received from the UDP tunnel. Returns 0 if successful, -1
* on some error it handled, or -2 if the client is to disconnect.
*/
int handle_message(client_t *c, uint16_t id, uint8_t msg_type,
char *data, int data_len)
{
int ret = 0;
char addrstr[ADDRSTRLEN];
switch(msg_type)
{
case MSG_TYPE_GOODBYE:
ret = -2;
break;
case MSG_TYPE_HELLOACK:
client_got_helloack(c);
CLIENT_ID(c) = id;
ret = client_send_helloack(c, ntohs(*((uint16_t *)data)));
if(debug_level >= DEBUG_LEVEL1)
{
sock_get_str(c->tcp_sock, addrstr, sizeof(addrstr));
printf("New connection(%d): tcp://%s", CLIENT_ID(c), addrstr);
sock_get_str(c->udp_sock, addrstr, sizeof(addrstr));
printf(" -> udp://%s\n", addrstr);
}
break;
case MSG_TYPE_DATA0:
case MSG_TYPE_DATA1:
ret = client_got_udp_data(c, data, data_len, msg_type);
if(ret == 0)
ret = client_send_tcp_data(c);
break;
case MSG_TYPE_ACK0:
case MSG_TYPE_ACK1:
ret = client_got_ack(c, msg_type);
break;
default:
ret = -1;
break;
}
return ret;
}
void signal_handler(int sig)
{
switch(sig)
{
case SIGINT:
running = 0;
}
}