forked from sorbo/tcpcrypt
/
tcs.c
387 lines (296 loc) · 5.92 KB
/
tcs.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
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "src/tcpcryptd.h"
#include "src/tcpcrypt.h"
#include "src/tcpcrypt_ctl.h"
static char *_bind_ip = "0.0.0.0";
enum {
TYPE_CLIENT = 0,
TYPE_SERVER,
TYPE_RAW,
};
enum {
FLAG_HELLO = 1,
};
struct sock {
int s;
int type;
int dead;
time_t added;
struct sockaddr_in peer;
int port;
int flags;
struct sock *next;
} _socks;
struct client {
int sport;
int dport;
int flags;
struct in_addr ip;
time_t added;
struct client *next;
} _clients;
static struct sock *add_sock(int s)
{
struct sock *sock = malloc(sizeof(*sock));
if (!sock)
err(1, "malloc()");
memset(sock, 0, sizeof(*sock));
sock->s = s;
sock->added = time(NULL);
sock->next = _socks.next;
_socks.next = sock;
return sock;
}
static void add_server(int port)
{
int s;
struct sockaddr_in s_in;
struct sock *sock;
int one = 1;
if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
err(1, "socket()");
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1)
err(1, "setsockopt()");
memset(&s_in, 0, sizeof(s_in));
s_in.sin_addr.s_addr = inet_addr(_bind_ip);
s_in.sin_port = htons(port);
if (bind(s, (struct sockaddr*) &s_in, sizeof(s_in)) == -1)
err(1, "bind()");
if (listen(s, 5) == -1)
err(1, "listen()");
sock = add_sock(s);
sock->type = TYPE_SERVER;
sock->port = port;
}
static void add_sniffer(void)
{
int s;
struct sock *sock;
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
err(1, "socket()");
sock = add_sock(s);
sock->type = TYPE_RAW;
}
static void find_client(struct sock *s)
{
struct client *c = &_clients;
while (c->next) {
struct client *next = c->next;
struct client *del = NULL;
if (next->dport == s->port
&& next->sport == ntohs(s->peer.sin_port)) {
s->flags = next->flags;
del = next;
}
if ((time(NULL) - next->added) > 10)
del = next;
if (del) {
c->next = next->next;
free(del);
} else
c = next;
}
}
static void handle_server(struct sock *s)
{
struct sockaddr_in s_in;
socklen_t len = sizeof(s_in);
int dude;
struct sock *d;
dude = accept(s->s, (struct sockaddr*) &s_in, &len);
if (dude == -1) {
perror("accept()");
return;
}
d = add_sock(dude);
memcpy(&d->peer, &s_in, sizeof(d->peer));
d->port = s->port;
find_client(d);
}
static void handle_client(struct sock *s)
{
char buf[1024];
int rc;
int got = -1;
int i;
int crypt = 0;
unsigned int len;
struct tm *tm;
time_t t;
len = sizeof(buf);
rc = tcpcrypt_getsockopt(s->s, IPPROTO_TCP, TCP_CRYPT_SESSID, buf,
&len);
crypt = rc != -1;
rc = read(s->s, buf, sizeof(buf) - 1);
if (rc <= 0) {
s->dead = 1;
return;
}
buf[rc] = 0;
s->dead = 1;
for (i = 0; i < sizeof(REQS) / sizeof(*REQS); i++) {
if (strcmp(buf, REQS[i]) == 0) {
got = i;
break;
}
}
if (got == -1)
return;
snprintf(buf, sizeof(buf), "%s%d", TEST_REPLY, s->flags);
rc = strlen(buf);
if (write(s->s, buf, rc) != rc)
return;
t = time(NULL);
tm = localtime(&t);
strftime(buf, sizeof(buf), "%m/%d/%y %H:%M:%S", tm);
printf("[%s] GOT %s:%d - %d\t[MSG %d] crypt %d flags %d\n",
buf,
inet_ntoa(s->peer.sin_addr),
ntohs(s->peer.sin_port),
s->port,
got,
crypt,
s->flags);
s->dead = 1;
}
static void found_crypt(struct ip *ip, struct tcphdr *th)
{
struct client *c = malloc(sizeof(*c));
if (!c)
err(1, "malloc()");
memset(c, 0, sizeof(*c));
c->ip = ip->ip_src;
c->sport = ntohs(th->th_sport);
c->dport = ntohs(th->th_dport);
c->added = time(NULL);
c->flags = FLAG_HELLO;
c->next = _clients.next;
_clients.next = c;
}
static void handle_raw(struct sock *s)
{
unsigned char buf[2048];
int rc;
struct ip *ip = (struct ip*) buf;
struct tcphdr *th;
unsigned char *end, *p;
if ((rc = read(s->s, buf, sizeof(buf))) <= 0)
err(1, "read()");
if (ip->ip_v != 4)
return;
if (ip->ip_p != IPPROTO_TCP)
return;
th = (struct tcphdr*) (((unsigned long) ip) + (ip->ip_hl << 2));
if ((unsigned long) th >= (unsigned long) (&buf[rc] - sizeof(*th)))
return;
p = (unsigned char*) (th + 1);
end = (unsigned char*) (((unsigned long) th) + (th->th_off << 2));
if ((unsigned long) end > (unsigned long) &buf[rc])
return;
if (th->th_flags != TH_SYN)
return;
while (p < end) {
int opt = *p++;
int len;
switch (opt) {
case TCPOPT_EOL:
case TCPOPT_NOP:
continue;
}
if (p >= end)
break;
len = *p++ - 2;
if ((p + len) >= end)
break;
switch (opt) {
case TCPOPT_CRYPT:
found_crypt(ip, th);
break;
}
p += len;
}
}
static void process_socket(struct sock *s)
{
switch (s->type) {
case TYPE_SERVER:
handle_server(s);
break;
case TYPE_RAW:
handle_raw(s);
break;
case TYPE_CLIENT:
handle_client(s);
break;
default:
printf("WTF %d\n", s->type);
abort();
break;
}
}
static void check_sockets(void)
{
struct sock *s = _socks.next;
fd_set fds;
int max = 0;
struct timeval tv;
FD_ZERO(&fds);
while (s) {
FD_SET(s->s, &fds);
if (s->s > max)
max = s->s;
s = s->next;
}
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(max + 1, &fds, NULL, NULL, &tv) == -1)
err(1, "select()");
s = &_socks;
while ((s = s->next)) {
if (FD_ISSET(s->s, &fds))
process_socket(s);
}
s = &_socks;
while (s->next) {
struct sock *next = s->next;
if (next->type == TYPE_CLIENT
&& (time(NULL) - next->added > 10))
next->dead = 1;
if (next->dead) {
close(next->s);
s->next = next->next;
free(next);
} else
s = next;
}
}
static void pwn(void)
{
add_sniffer();
add_server(80);
add_server(7777);
tzset();
chroot("/tmp");
setgid(666);
setuid(666);
while (1)
check_sockets();
}
int main(int argc, char *argv[])
{
if (argc > 1) {
_bind_ip = argv[1];
printf("Binding to %s\n", _bind_ip);
}
pwn();
exit(0);
}