This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
thc-ipv6-lib.c
3436 lines (3010 loc) · 98.9 KB
/
thc-ipv6-lib.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
/*
* (c) 2014 by van Hauser / THC <[email protected]>
*
* THC IPv6 Attack Library
*
* Functions: see README
*
* The AGPL v3 license applies to this code, see the LICENSE file
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
/* network */
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
//#include <linux/if.h>
/* files */
#include <fcntl.h>
#include <sys/ioctl.h>
/* misc */
#include <time.h>
#include <errno.h>
/* libpcap */
#include <pcap.h>
#ifdef _HAVE_SSL
/* libssl */
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#endif
/* OS specifics */
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define bswap_16 OSSwapInt16
#define bswap_32 OSSwapInt32
#define bswap_64 OSSwapInt64
#else
#include <byteswap.h>
#endif
#if !defined (SIOCGIFHWADDR)
#include <ifaddrs.h>
#include <net/if_dl.h>
#include <netinet/if_ether.h>
#else
#include <linux/if_ether.h>
#include <linux/netlink.h>
#endif
#include "thc-ipv6.h"
/***********************************************************/
// exported to external via thc-ipv6.h
int debug = 0;
int _thc_ipv6_showerrors = SHOW_LIBRARY_ERRORS;
int do_hdr_size = 0, do_hdr_vlan = 0;
// injection variables
#define _PPPOE_HDR_SIZE 22
#define _6IN4_HDR_SIZE 34
int do_6in4 = 0, do_pppoe = 0, do_hdr_off = 0;
char *do_hdr = NULL, *do_capture = NULL;
// other internal global vars
char default_interface[16] = "eth0";
int thc_socket = -1;
int _thc_ipv6_rawmode = 0;
void thc_ipv6_rawmode(int mode) {
_thc_ipv6_rawmode = mode;
fprintf(stderr, "Error: raw mode is not working, use THC_IPV6_... injection!\n");
exit(-1);
}
void thc_ipv6_show_errors(int mode) {
_thc_ipv6_showerrors = mode;
}
unsigned char *thc_ipv6_dummymac() {
char *ptr = malloc(7);
if (ptr == NULL)
return NULL;
memset(ptr, 0xff, 6);
ptr[6] = 0;
return ptr;
}
int thc_pcap_function(char *interface, char *capture, char *function, int promisc, char *opt) {
pcap_t *pcap_link = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fcode;
if (thc_socket < 0)
thc_socket = thc_open_ipv6();
if (do_pppoe || do_6in4 || do_hdr_vlan)
promisc = 1;
if (interface == NULL)
interface = default_interface;
if ((pcap_link = pcap_open_live(interface, 65535, promisc, -1, errbuf)) == NULL)
return -1;
if (do_pppoe || do_6in4 || do_hdr_vlan)
pcap_compile(pcap_link, &fcode, do_capture, 1, 0);
else
if (pcap_compile(pcap_link, &fcode, capture, 1, 0) < 0)
return -2;
pcap_setfilter(pcap_link, &fcode);
while(1) {
if (pcap_dispatch(pcap_link, 1, (pcap_handler) function, opt) < 0)
return -3;
usleep(10);
}
return -4; // never reached
}
pcap_t *thc_pcap_init(char *interface, char *capture) {
pcap_t *pcap_link = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fcode;
int promisc = 0;
if (thc_socket < 0)
thc_socket = thc_open_ipv6();
if (do_pppoe || do_6in4 || do_hdr_vlan)
promisc = 1;
if (interface == NULL)
interface = default_interface;
if ((pcap_link = pcap_open_live(interface, 65535, promisc, -1, errbuf)) == NULL)
return NULL;
if (do_pppoe || do_6in4 || do_hdr_vlan)
pcap_compile(pcap_link, &fcode, do_capture, 1, 0);
else
if (pcap_compile(pcap_link, &fcode, capture, 1, 0) < 0)
return NULL;
pcap_setfilter(pcap_link, &fcode);
pcap_setnonblock(pcap_link, 1, errbuf);
return pcap_link;
}
pcap_t *thc_pcap_init_promisc(char *interface, unsigned char *capture) {
pcap_t *pcap_link = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fcode;
if (thc_socket < 0)
thc_socket = thc_open_ipv6();
if (interface == NULL)
interface = default_interface;
if ((pcap_link = pcap_open_live(interface, 65535, 1, -1, errbuf)) == NULL)
return NULL;
if (do_pppoe || do_6in4 || do_hdr_vlan)
pcap_compile(pcap_link, &fcode, do_capture, 1, 0);
else
if (pcap_compile(pcap_link, &fcode, capture, 1, 0) < 0)
return NULL;
pcap_setfilter(pcap_link, &fcode);
pcap_setnonblock(pcap_link, 1, errbuf);
return pcap_link;
}
int thc_pcap_check(pcap_t * pcap_link, char *function, char *opt) {
if (pcap_link == NULL)
return -1;
return pcap_dispatch(pcap_link, 1, (pcap_handler) function, opt);
}
char *thc_pcap_close(pcap_t * pcap_link) {
if (pcap_link != NULL)
pcap_close(pcap_link);
return NULL;
}
/* wow, ugly, complicated work for something a standard linux library could do as well :-) */
void thc_notation2beauty(unsigned char *ipv6) {
char buf[40], buf2[40] = ":0:0:", *ptr, *ptr2 = NULL;
int i, j, k = 0, l = 0;
if (ipv6[39] != 0 || strlen(ipv6) != 39)
return;
memset(buf, 0, sizeof(buf));
// remove leading zeros from ipv6-input to buf, :0023: = :23:, :0000: = :0:
for (i = 0; i < 8; i++) {
ptr = ipv6 + i * 4 + i;
j = 0;
while (*ptr == '0' && j < 3) {
ptr++;
j++;
}
memcpy(&buf[k], ptr, 4 - j);
k += 4 - j;
buf[k++] = ':';
}
buf[k - 1] = 0;
// find the longest :0: chain
while ((ptr = strstr(buf, buf2)) != NULL) {
ptr2 = ptr;
strcat(buf2, "0:");
}
// if at least :0:0: is found, on the longest replace with ::, ptr2 shows where
if (ptr2 != NULL) {
buf2[strlen(buf2) - 2] = 0;
memset(ipv6, 0, 40);
// special case: 0000::....
if (buf + 1 == ptr2 && buf[0] == '0') {
ipv6[0] = ':';
l = -1;
} else
memcpy(ipv6, buf, ptr2 - buf + 1);
memcpy(ipv6 + (ptr2 - buf + 1 + l), ptr2 + strlen(buf2) - 1, strlen(buf) - (ptr2 - buf) - strlen(buf2) + 1);
// special case ....::0000
if (ipv6[strlen(ipv6) - 1] == '0' && ipv6[strlen(ipv6) - 2] == ':' && ptr2 - buf + 1 + strlen(buf2) == strlen(buf))
ipv6[strlen(ipv6) - 1] = 0;
} else
strcpy(ipv6, buf);
// if (strncmp(ipv6, "::ffff:", 7) == 0 && strlen(ipv6) <= 16) {
// printf("XXX beauty for ::ffff:123.123.132.123\n");
// }
}
unsigned char *thc_ipv62string(unsigned char *ipv6) {
char *string = malloc(33);
int a;
if (ipv6 != NULL && string != NULL) {
for (a = 0; a < 16; a++) {
if (ipv6[a] / 16 >= 10)
string[a * 2] = 'a' + ipv6[a] / 16 - 10;
else
string[a * 2] = '0' + ipv6[a] / 16;
if (ipv6[a] % 16 >= 10)
string[a * 2 + 1] = 'a' + ipv6[a] % 16 - 10;
else
string[a * 2 + 1] = '0' + ipv6[a] % 16;
}
string[32] = 0;
} else {
free(string);
return NULL;
}
return string;
}
unsigned char *thc_string2ipv6(unsigned char *string) {
unsigned char *ipv6 = malloc(16);
int a;
if (string != NULL && ipv6 != NULL) {
for (a = 0; a < 16; a++) {
ipv6[a] = (string[2 * a] >= 'a' ? 10 + string[2 * a] - 'a' : string[2 * a] - '0') * 16;
ipv6[a] += string[2 * a + 1] >= 'a' ? 10 + string[2 * a + 1] - 'a' : string[2 * a + 1] - '0';
}
} else {
free(ipv6);
return NULL;
}
return ipv6;
}
unsigned char *thc_string2notation(unsigned char *string) {
unsigned char *notation = malloc(40);
int a;
if (notation != NULL && string != NULL) {
for (a = 0; a < 8; a++) {
memcpy(notation + a * 5, string + a * 4, 4);
notation[4 + a * 5] = ':';
}
notation[39] = 0;
} else {
return NULL;
free(notation);
}
thc_notation2beauty(notation);
return notation;
}
unsigned char *thc_ipv62notation(unsigned char *ipv6) {
char *res, *ptr;
if (ipv6 == NULL)
return NULL;
if ((res = thc_ipv62string(ipv6)) == NULL)
return NULL;
ptr = thc_string2notation(res);
free(res);
return ptr;
}
int calculate_checksum(unsigned char *data, int data_len) {
int i = 0, checksum = 0;
if (debug)
thc_dump_data(data, data_len, "Checksum Packet Data");
while (i < data_len) {
if (i++ % 2 == 0)
checksum += *data++;
else
checksum += *data++ << 8;
}
checksum = (checksum & 0xffff) + (checksum >> 16);
checksum = htons(~checksum);
return checksum;
}
int checksum_pseudo_header(unsigned char *src, unsigned char *dst, unsigned char type, unsigned char *data, int length) {
unsigned char ptr[40 + length + 48];
int checksum;
if (((type != NXT_IP4 && type != NXT_ICMP4) && (src == NULL || dst == NULL)) || data == NULL || length < 0)
return -1;
if (length + 40 > 65535)
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: checksums for packets > 65535 are unreliable due implementation differences on target platforms\n");
memset(&ptr, 0, 40 + length);
if (type == NXT_IP4 || type == NXT_IP4_RUDIMENTARY || type == NXT_ICMP4) {
memcpy(ptr, data, length);
checksum = calculate_checksum(ptr, length);
} else {
memcpy(&ptr[0], src, 16);
memcpy(&ptr[16], dst, 16);
ptr[34] = length / 256;
ptr[35] = length % 256;
ptr[39] = type;
if (data != NULL && length > 0)
memcpy(&ptr[40], data, length);
checksum = calculate_checksum(ptr, 40 + length);
}
/*if (length > 65495) {
printf("DEBUG length: %d, high: %d, low: %d, sum: %x\n", length, ptr[34], ptr[35], checksum);
printf("65535: %x\n", calculate_checksum(ptr, 65535));
printf("65536: %x\n", calculate_checksum(ptr, 65536));
printf("65535+40: %x\n", calculate_checksum(ptr, 65535 + 40));
printf("65535+40: %x\n", calculate_checksum(ptr, 65536 + 40));
}*/
if (type == NXT_UDP && checksum == 0)
checksum = 65535;
if (debug)
printf("Checksum: %d = %p, %p, %d, %p, %d\n", checksum, src, dst, type, data, length);
return checksum;
}
unsigned char *thc_resolve6(char *target) {
char *ret_addr, *ptr2, *ptr = target, tmp[264];
struct in6_addr glob_in6;
char *glob_addr = (char *) &glob_in6;
struct addrinfo glob_hints, *glob_result;
unsigned char out[64];
if (target == NULL)
return NULL;
if (index(target, '/') != NULL || *target == '[' || index(target, '%') != NULL) {
ptr = strncpy(tmp, target, sizeof(tmp) - 1);
tmp[sizeof(tmp) - 1] = 0;
if ((ptr2 = index(tmp, '/')) != NULL)
*ptr2 = 0;
if ((ptr2 = index(tmp, '%')) != NULL)
*ptr2 = 0;
if (*ptr == '[') {
ptr++;
if ((ptr2 = index(tmp, ']')) != NULL)
*ptr2 = 0;
}
}
memset(&glob_hints, 0, sizeof(glob_hints));
glob_hints.ai_family = AF_INET6;
if (getaddrinfo(ptr, NULL, &glob_hints, &glob_result) != 0)
return NULL;
if (getnameinfo(glob_result->ai_addr, glob_result->ai_addrlen, out, sizeof(out), NULL, 0, NI_NUMERICHOST) != 0)
return NULL;
if (inet_pton(AF_INET6, out, glob_addr) < 0)
return NULL;
if ((ret_addr = malloc(16)) == NULL)
return NULL;
memcpy(ret_addr, glob_in6.s6_addr, 16);
if (debug)
thc_dump_data(ret_addr, 16, "Target Resolve IPv6");
freeaddrinfo(glob_result);
return ret_addr;
}
int thc_get_mtu(char *interface) {
int s;
struct ifreq ifr;
if (interface == NULL)
interface = default_interface;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", interface);
if (ioctl(s, SIOCGIFMTU, (int8_t *) & ifr) < 0) {
close(s);
return -1;
}
close(s);
if (debug)
printf("DEBUG: MTU %d\n", ifr.ifr_mtu);
return ifr.ifr_mtu;
}
unsigned char *thc_get_own_mac(char *interface) {
int s;
struct ifreq ifr;
char *mac;
if (interface == NULL)
interface = default_interface;
if (_thc_ipv6_rawmode)
return thc_ipv6_dummymac();
#if !defined (SIOCGIFHWADDR)
struct ifaddrs *ifa, *ifx = NULL;
struct sockaddr_dl *dl;
getifaddrs(&ifa);
ifx = ifa;
mac = malloc(6);
while (ifa != NULL) {
dl = (struct sockaddr_dl *) ifa->ifa_addr;
if (debug)
thc_dump_data(dl->sdl_data, dl->sdl_nlen, "Interface loop");
if (dl->sdl_nlen > 0 && strncmp(interface, dl->sdl_data, dl->sdl_nlen) == 0) {
memcpy(mac, LLADDR(dl), 6);
break;
} else {
ifa = ifa->ifa_next;
}
}
if (ifa == NULL) {
freeifaddrs(ifx);
return NULL; // error: could not find requested interface.
} else {
freeifaddrs(ifx);
}
#else /* SIOCGIFHWADDR */
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return NULL;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", interface);
if (ioctl(s, SIOCGIFHWADDR, (int8_t *) & ifr) < 0) {
close(s);
return NULL;
}
mac = malloc(6);
memcpy(mac, &ifr.ifr_hwaddr.sa_data, 6);
close(s);
#endif
if (debug)
thc_dump_data(mac, 6, "Own MAC address");
return mac;
}
unsigned char *thc_get_own_ipv6(char *interface, unsigned char *dst, int prefer) {
char *myipv6;
FILE *f;
unsigned char ipv6[36] = "", save[34] = "", tmpbuf[34], buf[1024], *tmpdst = NULL;
int a, b, c, done = 0, picky = 0, orig_prefer = prefer;
unsigned char tmpd, tmpb;
char bla[16];
if (interface == NULL)
interface = default_interface;
if (dst != NULL && dst[0] == 0xff)
dst = NULL;
if (dst != NULL && dst[0] == 0xfe)
prefer = PREFER_LINK;
if (dst != NULL && dst[0] != 0xfe)
prefer = PREFER_GLOBAL;
if (dst != NULL)
tmpdst = thc_ipv62string(dst);
memset(save, 0, sizeof(save));
while (done < 2 && picky < 2) {
if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
fprintf(stderr, "Error: /proc/net/if_inet6 does not exist, no IPv6 support on your Linux box!\n");
if (tmpdst != NULL)
free(tmpdst);
return NULL;
}
if (picky == 1) {
if (prefer == PREFER_GLOBAL)
prefer = PREFER_LINK;
else
prefer = PREFER_GLOBAL;
}
while (done < 2 && fgets(buf, sizeof(buf), f) != NULL) {
if (strncmp(interface, &buf[strlen(buf) - strlen(interface) - 1], strlen(interface)) == 0) {
sscanf(buf, "%s %x %x %x %s", tmpbuf, &a, &b, &c, bla);
if (c == prefer && done != 2) {
ipv6[0] = c; // scope type
ipv6[1] = b; // netmask
memcpy(&ipv6[2], tmpbuf, 32);
ipv6[34] = 0;
//printf("(c scope/prefer is %d) dst %p == NULL && ( prefer %d == %d PREFER_LINK || %c != f )\n", c, dst, prefer, PREFER_LINK, tmpbuf[0]);
if (dst == NULL && (prefer == PREFER_LINK || tmpbuf[0] != 'f'))
done = 2;
else
done = 1;
}
// if a destination was given, we always prefer the local ip which is in the same subnet of the target
if (dst != NULL) {
if (strncmp(tmpbuf, tmpdst, b / 4) == 0) {
if (b % 4 > 0) {
tmpb = tmpbuf[b / 4 + 1] >> (b % 4);
tmpd = tmpdst[b / 4 + 1] >> (b % 4);
if (tmpb == tmpd) {
done = 2;
}
} else
done = 2;
if (done == 2) {
if (debug)
printf("DEBUG: Found local IPv6 address to destination\n");
ipv6[0] = c; // scope type
ipv6[1] = b; // netmask
memcpy(&ipv6[2], tmpbuf, 32);
ipv6[34] = 0;
}
}
}
//printf("done is %d - %s - %s\n", done, tmpbuf, buf);
// ensure that 2000::/3 and fc00::/7 is selected correctly
if (done != 2 && dst != NULL) {
if (
((strncmp(tmpbuf, "fc", 2) == 0 || strncmp(tmpbuf, "fd", 2) == 0) && (strncmp(tmpdst, "fc", 2) == 0 || strncmp(tmpdst, "fd", 2) == 0))
||
((tmpdst[0] == '2' || tmpdst[0] == '3') && (tmpbuf[0] == '2' || tmpbuf[0] == '3')) ) {
/*
printf("SAVE! %s -> %s\n", tmpbuf, tmpdst);
memcpy(save + 2, tmpbuf, 32);
memset(ipv6, 0, sizeof(ipv6));
*/
done = 2;
}
//printf("here! %d => %c%c != %c%c \n", save[2], tmpbuf[0], tmpbuf[1], tmpdst[0], tmpdst[1]);
if ( save[2] == 0
&&
( ((strncmp(tmpbuf, "fc", 2) == 0 || strncmp(tmpbuf, "fd", 2) == 0) && (tmpdst[0] == '2' || tmpdst[0] == '3'))
||
((strncmp(tmpdst, "fc", 2) == 0 || strncmp(tmpdst, "fd", 2) == 0) && (tmpbuf[0] == '2' || tmpbuf[0] == '3')) ) ) {
//printf("RESORT! %c -> %c\n", tmpbuf[1], tmpdst[0]);
memcpy(save + 2, tmpbuf, 32);
memset(ipv6, 0, sizeof(ipv6));
done = 0;
}
}
//printf("final done is %d - %s - %s\n", done, tmpbuf, buf);
}
}
fclose(f);
picky++;
//printf("x %d, %s == 0, %s > 0\n", done, ipv6 + 2, save + 2 );
if (done < 2 && strlen(&ipv6[2]) == 0 && strlen(&save[2]) > 0) {
//printf("RESORT IS TAKEN => %s\n", save + 2);
memcpy(ipv6, save, sizeof(ipv6));
done = 2;
}
}
//printf("%s > 0, %s== fe80\n", save +2, ipv6 +2);
if (strlen(&save[2]) > 0 && prefer == PREFER_GLOBAL && strncmp(ipv6 + 2, "fe80", 2) == 0) {
//printf("z\n");
memcpy(ipv6, save, sizeof(ipv6));
done = 2;
}
if (strlen(&ipv6[2]) == 0) {
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: no IPv6 address on interface defined\n");
if (tmpdst != NULL)
free(tmpdst);
return NULL;
}
if (picky == 2 && orig_prefer != ipv6[0])
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: unprefered IPv6 address had to be selected\n");
if (tmpdst != NULL)
free(tmpdst);
tmpdst = thc_string2notation(&ipv6[2]);
myipv6 = thc_resolve6(tmpdst);
free(tmpdst);
if (debug)
thc_dump_data(myipv6, 16, "Own IPv6 address");
return myipv6;
}
unsigned char *thc_get_multicast_mac(unsigned char *dst) {
unsigned char *mac;
if (_thc_ipv6_rawmode)
return thc_ipv6_dummymac();
if (dst == NULL || (mac = malloc(6)) == NULL)
return NULL;
mac[0] = 0x33;
mac[1] = 0x33;
memcpy(&mac[2], dst + 12, 4);
return mac;
}
void thc_get_mac_from_sniff(u_char *foo, const struct pcap_pkthdr *header, const unsigned char *data) {
int off = 0, len = header->caplen - 14;
unsigned char *ptr = (unsigned char*)data + 14;
if (do_hdr_size) {
ptr += (do_hdr_size - 14);
len -= (do_hdr_size - 14);
if ((ptr[0] & 240) != 0x60)
return;
}
if (ptr[6] == NXT_FRAG) {
if (ptr[40] == NXT_ICMP6)
off = 8;
else
return;
} else
if (ptr[6] != NXT_ICMP6)
return;
if (ptr[40 + off] != ICMP6_NEIGHBORADV)
return;
if (len < 64 + off)
return;
if (memcmp(ptr + 48 + off, foo + 7, 16) != 0)
return;
foo[0] = 32;
if (len >= 72 && ptr[64 + off] == 2 && ptr[65 + off] == 1)
memcpy(foo + 1, ptr + 66 + off, 6);
else
memcpy(foo + 1, data + 6, 6);
}
unsigned char *thc_lookup_ipv6_mac(char *interface, unsigned char *dst) {
unsigned char *mac = NULL;
time_t curr;
int count = 0, found = 0;
char string[64] = "ip6 and dst ", resolved_mac[23] = "", *p1, *p2, *mysrc;
pcap_t *p;
if (thc_socket < 0)
thc_socket = thc_open_ipv6();
if (_thc_ipv6_rawmode || do_pppoe || do_6in4 || do_hdr_vlan)
return thc_ipv6_dummymac();
if (dst == NULL)
return NULL;
if (interface == NULL)
interface = default_interface;
if ((p1 = thc_get_own_ipv6(interface, dst, PREFER_LINK)) == NULL)
return NULL;
mysrc = p1;
if ((p2 = thc_ipv62notation(p1)) == NULL) {
free(p1);
return NULL;
}
strcat(string, p2);
free(p2);
memcpy(resolved_mac + 7, dst, 16);
if ((p = thc_pcap_init(interface, string)) == NULL) {
free(mysrc);
return NULL;
}
while (found == 0 && count < 3) {
//printf("X %d %p %02x%02x %p %02x%02x\n", count, mysrc, mysrc[14], mysrc[15], dst, dst[14], dst[15]);
thc_neighborsol6(interface, mysrc, NULL, dst, NULL, NULL);
curr = time(NULL);
while (found == 0 && time(NULL) < curr + 2) {
thc_pcap_check(p, (char *) thc_get_mac_from_sniff, resolved_mac);
if (resolved_mac[0] != 0) {
found = 1;
if ((mac = malloc(6)) == NULL) {
free(mysrc);
return NULL;
}
memcpy(mac, resolved_mac + 1, 6);
}
}
count++;
}
thc_pcap_close(p);
free(mysrc);
if (debug)
thc_dump_data(mac, 6, "MAC address for packet target");
return mac;
}
/* If the following looks like shit to you:
This is code submitted by Dan Kaminksy with whom I bet that he is not
able to code a 1 page function which extracts the mac address from the
neighbor cache on linux - which is such a complex and horrible
implementation. Well you get what you ask for - a function which will
break once the interface even slightly changes ... but its 1 page.
*/
unsigned char *thc_look_neighborcache(unsigned char *dst) {
int fd, fromlen, gotsize, rcvbuf = 65535;
struct sockaddr_nl nladdr;
unsigned char buf[32768], *ptr, *found;
// char magic[] = { 0x80, 0x00, 0x00, 0x01, 0x14, 0x00, 0x01, 0x00 };
char blob[] = { 0x14, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x01, 0x03, 0xda,
0x0f, 0xb8, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
memset(&nladdr, 0, sizeof(struct sockaddr_nl));
nladdr.nl_family = AF_NETLINK;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
bind(fd, (struct sockaddr *) &nladdr, sizeof(nladdr));
sendto(fd, blob, sizeof(blob), 0, (struct sockaddr *) &nladdr, sizeof(nladdr));
fromlen = sizeof(nladdr);
gotsize = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) &nladdr, &fromlen);
shutdown(fd, SHUT_RDWR);
close(fd);
if (debug)
thc_dump_data(buf, gotsize, "Neighbor cache lookup result");
// if ((ptr = thc_memstr(buf, magic, gotsize, sizeof(magic))) == NULL)
// return NULL;
if ((ptr = thc_memstr(buf, dst, gotsize /* - (ptr - buf) */ , 16)) == NULL)
return NULL;
if ((found = malloc(7)) == NULL)
return NULL;
memcpy(found, ptr + 16 + 4, 6);
found[6] = 0;
return found;
}
int thc_is_dst_local(char *interface, unsigned char *dst) {
int local = 0;
FILE *f;
unsigned char tmpbuf[34], buf[1024], *tmpdst = NULL;
int a, b, c /*, found = 0, fd = -1 */ ;
unsigned char tmpd, tmpb;
char bla[16];
if (thc_socket < 0)
thc_socket = thc_open_ipv6();
if (_thc_ipv6_rawmode || dst == NULL || do_pppoe || do_6in4 || do_hdr_vlan)
return 0;
if (interface == NULL)
interface = default_interface;
if (dst[0] == 0xff) // multicast address ?
return 1;
if (dst[0] == 0xfe && dst[1] == 0x80) // link local
return 1;
tmpdst = thc_ipv62string(dst);
if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
fprintf(stderr, "Error: /proc/net/if_inet6 does not exist, no IPv6 support on your Linux box!\n");
exit(-1);
}
while (local == 0 && fgets(buf, sizeof(buf), f) != NULL) {
if (strncmp(interface, &buf[strlen(buf) - strlen(interface) - 1], strlen(interface)) == 0) {
sscanf(buf, "%s %x %x %x %s", tmpbuf, &a, &b, &c, bla);
if (strncmp(tmpbuf, tmpdst, b / 4) == 0) {
if (b % 4 > 0) {
tmpb = tmpbuf[b / 4 + 1] >> (b % 4);
tmpd = tmpdst[b / 4 + 1] >> (b % 4);
if (tmpb == tmpd) {
local = 1;
}
} else
local = 1;
}
}
}
fclose(f);
if (debug)
printf("DEBUG: is dst local: %d\n", local);
free(tmpdst);
return local;
}
unsigned char *thc_get_mac(char *interface, unsigned char *src, unsigned char *dst) {
int local = 0;
FILE *f;
unsigned char tmpbuf[34], router1[34], router2[34], defaultgw[34] = "", buf[1024], *tmpdst = NULL;
int a, b, c /*, found = 0, fd = -1 */ ;
unsigned char tmpd, tmpb;
char bla[16], *ret, *p1;
if (thc_socket < 0)
thc_socket = thc_open_ipv6();
if (_thc_ipv6_rawmode || do_pppoe || do_6in4 || do_hdr_vlan)
return thc_ipv6_dummymac();
if (dst == NULL)
return NULL;
if (interface == NULL)
interface = default_interface;
if (dst[0] == 0xff) // then its a multicast target
return thc_get_multicast_mac(dst);
tmpdst = thc_ipv62string(dst);
if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
fprintf(stderr, "Error: /proc/net/if_inet6 does not exist, no IPv6 support on your Linux box!\n");
exit(-1);
}
while (local == 0 && fgets(buf, sizeof(buf), f) != NULL) {
if (strncmp(interface, &buf[strlen(buf) - strlen(interface) - 1], strlen(interface)) == 0) {
sscanf(buf, "%s %x %x %x %s", tmpbuf, &a, &b, &c, bla);
if (strncmp(tmpbuf, tmpdst, b / 4) == 0) {
if (b % 4 > 0) {
tmpb = tmpbuf[b / 4 + 1] >> (b % 4);
tmpd = tmpdst[b / 4 + 1] >> (b % 4);
if (tmpb == tmpd) {
local = 1;
}
} else
local = 1;
}
}
}
fclose(f);
if (debug)
printf("DEBUG: is mac local: %d\n", local);
if (!local) {
if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
fprintf(stderr, "Error: /proc/net/ipv6_route does not exist, no IPv6 support on your Linux box!\n");
exit(-1);
}
while (local == 0 && fgets(buf, sizeof(buf), f) != NULL) {
if (strncmp(interface, &buf[strlen(buf) - strlen(interface) - 1], strlen(interface)) == 0) {
sscanf(buf, "%s %x %s %x %s %s", tmpbuf, &b, router1, &a, router2, bla);
if (b > 0) {
if (strncmp(tmpbuf, tmpdst, b / 4) == 0) {
if (b % 4 > 0) {
tmpb = tmpbuf[b / 4 + 1] >> (b % 4);
tmpd = tmpdst[b / 4 + 1] >> (b % 4);
if (tmpb == tmpd)
local = 1;
} else
local = 1;
}
} else
strcpy(defaultgw, router2);
if (local == 1) {
if (debug)
printf("DEBUG: router found for %s: %s\n", tmpdst, router2);
strcpy(tmpdst, router2);
}
}
}
if (local == 0 && strlen(defaultgw) > 0) {
if (debug)
printf("DEBUG: using default router for %s: %s\n", tmpdst, defaultgw);
strcpy(tmpdst, defaultgw);
local = 1;
}
if (local == 0) {
if (_thc_ipv6_showerrors)
fprintf(stderr, "Error: No idea where to route the packet to %s!\n", tmpdst);
fclose(f);
free(tmpdst);
return NULL;
}
fclose(f);
}
p1 = thc_string2ipv6(tmpdst);
if ((ret = thc_look_neighborcache(p1)) != NULL) {
free(p1);
free(tmpdst);
return ret;
}
ret = thc_lookup_ipv6_mac(interface, p1);
free(tmpdst);
free(p1);
return ret;
}
unsigned char *thc_inverse_packet(unsigned char *pkt, int pkt_len) {
unsigned char tmp[16];
int type = -1, iptr = 0, checksum;
char *src = &pkt[8], *dst = &pkt[24];
if (pkt == NULL)
return NULL;
pkt[7] = 255; // ttl
memcpy(tmp, pkt + 8, 16); // reverse IP6 src and dst
memcpy(pkt + 8, pkt + 24, 16);
memcpy(pkt + 24, tmp, 16);
if (pkt_len > 44) {
type = pkt[6];
iptr = 40;
}
while (type == NXT_HDR || type == NXT_ROUTE || type == NXT_FRAG || type == NXT_OPTS || type == NXT_PIM || type == NXT_ICMP6 || type == NXT_TCP || type == NXT_UDP || type == NXT_IP4 || type == NXT_IP4_RUDIMENTARY) {
switch (type) {
case NXT_ICMP6:
if (pkt[iptr] == ICMP6_PINGREQUEST || pkt[iptr] == ICMP6_PINGREPLY)
pkt[iptr] = (pkt[iptr] == ICMP6_PINGREQUEST ? ICMP6_PINGREPLY : ICMP6_PINGREQUEST);
else if (pkt[iptr] == ICMP6_NEIGHBORSOL || pkt[iptr] == ICMP6_NEIGHBORADV)
pkt[iptr] = (pkt[iptr] == ICMP6_NEIGHBORSOL ? ICMP6_NEIGHBORADV : ICMP6_NEIGHBORSOL);
else if (pkt[iptr] == ICMP6_ROUTERSOL || pkt[iptr] == ICMP6_ROUTERADV)
pkt[iptr] = (pkt[iptr] == ICMP6_ROUTERSOL ? ICMP6_ROUTERADV : ICMP6_ROUTERSOL);
else if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: ICMP6 type %d can not be inversed\n", type);
pkt[iptr + 2] = 0;
pkt[iptr + 3] = 0;
checksum = checksum_pseudo_header(src, dst, NXT_ICMP6, &pkt[iptr], pkt_len - iptr);
pkt[iptr + 2] = checksum / 256;
pkt[iptr + 3] = checksum % 256;
type = -1;
break;
case NXT_MIPV6:
case NXT_PIM:
case NXT_UDP:
case NXT_TCP:
case NXT_IP4:
case NXT_IP4_RUDIMENTARY:
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: inverse_packet has not implement type %d yet!\n", type);
// fall through
case NXT_NONXT:
case NXT_DATA:
case NXT_AH:
case NXT_ESP:
type = -1; // no processing of other headers
break;
case NXT_ROUTE:
case NXT_FRAG:
case NXT_HDR:
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: inverse_packet has not implement type %d yet!\n", type);
type = pkt[iptr];
iptr += (pkt[iptr + 1] + 1) * 8;
if (iptr + 4 > pkt_len) {
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: packet to inverse is shorter than header tells me\n");
type = -1;
}
break;
default:
if (_thc_ipv6_showerrors)
fprintf(stderr, "Warning: Unsupported header type %d!\n", type);
// XXX TODO FIXME : other packet types