forked from vanhauser-thc/thc-ipv6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alive6.c
2072 lines (1992 loc) · 69.7 KB
/
alive6.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <time.h>
#include <netdb.h>
#include <pcap.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "thc-ipv6.h"
#include "fps.h"
#define MAX_ALIVE 65536
#define MAX_NETS 1024
#define MAX_VENDID 64
#define MAX_PORTS 256
#define MAX_FOUR 16
#define TCP_OPT_LEN 28 // 20
#define FPS_INVALID "invalid packet data"
#define FPS_UNKNOWN "unknown"
#define RESP_PONG "ICMP echo-reply"
#define RESP_UNREACH_PORT "ICMP port unreachable"
#define RESP_UNREACH_ROUTE "ICMP network unreachable"
#define RESP_UNREACH_FW "ICMP firewalled unreachable"
#define RESP_UNREACH_OOSCOPE "ICMP out of scope unreachable"
#define RESP_UNREACH_ADDR "ICMP host unreachable"
#define RESP_UNREACH_GRESS "ICMP ingress/egress filter unreachable"
#define RESP_UNREACH_REJECT "ICMP route reject unreachable"
#define RESP_TOOBIG "ICMP packet too big"
#define RESP_TTLEXCEED "ICMP TTL exceeded"
#define RESP_REDIR "ICMP local router traffic redirect"
#define RESP_PARAMPROB "ICMP parameter problem"
#define RESP_ERROR "ICMP error"
#define RESP_UDP "UDP"
#define RESP_SYNACK "TCP SYN-ACK"
#define RESP_RST "TCP RST"
#define RESP_ACK "TCP ACK"
#define RESP_OTHER "TCP misc-options"
#define RESP_UNKNOWN "unknown"
extern int _thc_ipv6_rawmode;
struct fingerprint {
char OS[1024];
char FP[200];
};
static char _fingerprint[4096]; // not thread safe!
unsigned char buf[8], *alive[MAX_ALIVE], *tagging = NULL;
int alive_no = 0, resolve = 0, waittime = 1, portscan = 0, curr = 0, list = 0,
slow = 0;
int synports[MAX_PORTS], ackports[MAX_PORTS], udpports[MAX_PORTS];
int ndp_only = 0, do_ping = 1, do_dst = 1, do_hop = 0, verbose = 0,
srcport = -1, do_help = 0, do_hopcount = 0, still_not_there = 0,
rst_means_alive = 1;
unsigned long int tcount = 0;
FILE * out = NULL;
struct hostent * he = NULL;
short int si, sp, sp2;
// all dict entries must start with a single from/to 0,0,0,0
// and end with a single from/to ffff,ffff,ffff,ffff
unsigned short int dict_small[] = {
0, 0, 0, 0, /*to */ 0, 0, 0, 0, 0, 0, 0, 1, /*to */ 0, 0, 0,
0x2ff, // 1975 tests
0, 0, 0, 0x300, /*to */ 0, 0, 0, 0x305, 0, 0, 0, 0x400, /*to */ 0, 0, 0,
0x405, 0, 0, 0, 0x443, /*to */ 0, 0, 0, 0x445, 0, 0, 0, 0x500, /*to */ 0, 0,
0, 0x505, 0, 0, 0, 0x530, /*to */ 0, 0, 0, 0x53f, 0, 0, 0, 0x555, /*to */ 0,
0, 0, 0x555, 0, 0, 0, 0x600, /*to */ 0, 0, 0, 0x605, 0, 0, 0, 0x666,
/*to */ 0, 0, 0, 0x667, 0, 0, 0, 0x700, /*to */ 0, 0, 0, 0x703, 0, 0, 0,
0x800, /*to */ 0, 0, 0, 0x803, 0, 0, 0, 0x900, /*to */ 0, 0, 0, 0x903, 0, 0,
0, 0xaaa, /*to */ 0, 0, 0, 0xaaa, 0, 0, 0, 0xc38, /*to */ 0, 0, 0, 0xc38, 0,
0, 0, 0x9dd, /*to */ 0, 0, 0, 0x9dd, 0, 0, 0, 0xff0, /*to */ 0, 0, 0, 0xfff,
0, 0, 0, 0x1000, /*to */ 0, 0, 0, 0x1111, 0, 0, 0, 0x1337, /*to */ 0, 0, 0,
0x1337, 0, 0, 0, 0x14e9, /*to */ 0, 0, 0, 0x14e9, 0, 0, 0, 0x1a0b,
/*to */ 0, 0, 0, 0x1a0b, 0, 0, 0, 0x1f40, /*to */ 0, 0, 0, 0x1f40, 0, 0, 0,
0x1f90, /*to */ 0, 0, 0, 0x1f90, 0, 0, 0, 0x2000, /*to */ 0, 0, 0, 0x2111,
0, 0, 0, 0x3000, /*to */ 0, 0, 0, 0x3011, 0, 0, 0, 0x3128, /*to */ 0, 0, 0,
0x3128, 0, 0, 0, 0x2525, /*to */ 0, 0, 0, 0x2525, 0, 0, 0, 0x5353,
/*to */ 0, 0, 0, 0x5353, 0, 0, 0, 0x6666, /*to */ 0, 0, 0, 0x6667, 0, 0, 0,
0x8000, /*to */ 0, 0, 0, 0x8000, 0, 0, 0, 0x8080, /*to */ 0, 0, 0, 0x8080,
0, 0, 0, 0xaaaa, /*to */ 0, 0, 0, 0xaaaa, 0, 0, 0, 0xabcd, /*to */ 0, 0, 0,
0xabcd, 0, 0, 0, 0xbabe, /*to */ 0, 0, 0, 0xbabe, 0, 0, 0, 0xbeef,
/*to */ 0, 0, 0, 0xbeef, 0, 0, 0, 0xcafe, /*to */ 0, 0, 0, 0xcafe, 0, 0, 0,
0xc0de, /*to */ 0, 0, 0, 0xc0de, 0, 0, 0, 0xdead, /*to */ 0, 0, 0, 0xdead,
0, 0, 0, 0xf500, /*to */ 0, 0, 0, 0xf500, 0, 0, 0, 0xfeed, /*to */ 0, 0, 0,
0xfeed, 0, 0, 0, 0xfff0, /*to */ 0, 0, 0, 0xffff, 0, 0, 1, 0, /*to */ 0, 0,
1, 0x1ff, 0, 0, 2, 0, /*to */ 0, 0, 0x1bb, 5, 0, 0, 2, 6, /*to */ 0, 0, 9,
9, 0, 0, 2, 0xa, /*to */ 0, 0, 2, 0x20, 0, 0, 2, 0x21, /*to */ 0, 0, 3,
0x21, 0, 0, 2, 0x22, /*to */ 0, 0, 3, 0x22, 0, 0, 2, 0x25, /*to */ 0, 0, 9,
0x25, 0, 0, 2, 0x50, /*to */ 0, 0, 9, 0x50, 0, 0, 2, 0x53, /*to */ 0, 0, 9,
0x53, 0, 0, 2, 0x80, /*to */ 0, 0, 9, 0x80, 0, 0, 2, 0x1bb, /*to */ 0, 0, 9,
0x1bb, 0, 0, 2, 0x500, /*to */ 0, 0, 9, 0x500,
// 0, 0, 0xa, 0, /*to */ 0, 0, 0xf, 2,
0, 0, 0x80, 6, /*to */ 0, 0, 0x80, 0x1f, 0, 0, 0x200, 0, /*to */ 0, 0,
0x200, 3, 0, 0, 0x389, 0, /*to */ 0, 0, 0x389, 3, 0, 0, 0x443, 0, /*to */ 0,
0, 0x443, 3, 0, 0, 0x500, 0, /*to */ 0, 0, 0x500, 2, 0, 0, 0x666, 0,
/*to */ 0, 0, 0x669, 2, 0, 0, 0x3128, 0, /*to */ 0, 0, 0x3128, 3, 0, 0,
0x6666, 0, /*to */ 0, 0, 0x6669, 2, 0, 0, 0x8080, 0, /*to */ 0, 0, 0x8080,
3, 0, 0, 0xdead, 0xbeef, /*to */ 0, 0, 0xdead, 0xbeef,
// 0, 1, 0, 0, /*to */ 0, 3, 3, 3,
0, 0, 0, 0, /*to */ 4, 4, 4, 4, // 24 doubles here == 0.42%
1, 0, 0, 5, /*to */ 1, 0, 0, 0xf,
// 2, 0, 1, 0, /*to */ 2, 0, 1, 3,
2, 0, 0, 5, /*to */ 2, 0, 0, 0xd,
// 1, 2, 3, 4, /*to */ 1, 2, 3, 4,
5, 0, 0, 1, /*to */ 0xff, 0, 0, 2, 0xffff, 0x00ff, 0xfe00, 0xfffe,
/*to */ 0xffff, 0x00ff, 0xfe00, 0xffff, 0xffff, 0xffff, 0xffff, 0xfffe,
/*to */ 0xffff, 0xffff, 0xffff, 0xfffe, 0xffff, 0xffff, 0xffff, 0xffff,
/*to */ 0xffff, 0xffff, 0xffff, 0xffff};
unsigned short int dict_large[] = {
0, 0, 0, 0, /*to */ 0, 0, 0, 0, 0, 0, 0, 1, /*to */ 0, 0, 1,
0x2fff, // 1975 tests
0, 0, 0, 0x3000, /*to */ 0, 0, 1, 0x3333, 0, 0, 0, 0x5353, /*to */ 0, 0, 2,
0x5353, 0, 0, 0, 0x6666, /*to */ 0, 0, 2, 0x6667, 0, 0, 0, 0x8000,
/*to */ 0, 0, 2, 0x8000, 0, 0, 0, 0x8080, /*to */ 0, 0, 2, 0x8080, 0, 0, 0,
0xaaaa, /*to */ 0, 0, 2, 0xaaaa, 0, 0, 0, 0xabcd, /*to */ 0, 0, 2, 0xabcd,
0, 0, 0, 0xbabe, /*to */ 0, 0, 2, 0xbabe, 0, 0, 0, 0xbeef, /*to */ 0, 0, 2,
0xbeef, 0, 0, 0, 0xcafe, /*to */ 0, 0, 2, 0xcafe, 0, 0, 0, 0xc0de,
/*to */ 0, 0, 2, 0xc0de, 0, 0, 0, 0xdead, /*to */ 0, 0, 2, 0xdead, 0, 0, 0,
0xf500, /*to */ 0, 0, 2, 0xf500, 0, 0, 0, 0xfeed, /*to */ 0, 0, 2, 0xfeed,
0, 0, 0, 0xfff0, /*to */ 0, 0, 2, 0xffff, 0, 0, 2, 0, /*to */ 0, 0, 0x1bb,
5, 0, 0, 2, 0x11, /*to */ 0, 0, 2, 0x100, 0, 0, 2, 0x1bb, /*to */ 0, 0, 9,
0x1bb, 0, 0, 2, 0x500, /*to */ 0, 0, 9, 0x500, 0, 0, 2, 6, /*to */ 0, 0, 10,
10, 0, 0, 0xa, 0, /*to */ 0, 0, 0xf, 5, 0, 0, 0x80, 6, /*to */ 0, 0, 0x80,
0x1f, 0, 0, 0x200, 0, /*to */ 0, 0, 0x200, 5, 0, 0, 0x389, 0, /*to */ 0, 0,
0x389, 5, 0, 0, 0x443, 0, /*to */ 0, 0, 0x443, 5, 0, 0, 0x500, 0, /*to */ 0,
0, 0x500, 5, 0, 0, 0x666, 0, /*to */ 0, 0, 0x669, 5, 0, 0, 0x3128, 0,
/*to */ 0, 0, 0x3128, 5, 0, 0, 0x6666, 0, /*to */ 0, 0, 0x6669, 5, 0, 0,
0x8080, 0, /*to */ 0, 0, 0x8080, 5, 0, 0, 0xdead, 0xbeef, /*to */ 0, 0,
0xdead, 0xbeef,
// 0, 1, 0, 0, /*to */ 0, 3, 3, 3,
0, 0, 0, 0, /*to */ 5, 5, 5, 5, // some doubles here
1, 0, 0, 6, /*to */ 1, 0, 0, 0x10,
// 2, 0, 1, 0, /*to */ 2, 0, 1, 3,
2, 0, 0, 5, /*to */ 2, 0, 0, 0x10,
// 1, 2, 3, 4, /*to */ 1, 2, 3, 4,
6, 0, 0, 0, /*to */ 0xff, 0, 0, 2, 0xffff, 0x00ff, 0xfe00, 0xfffe,
/*to */ 0xffff, 0x00ff, 0xfe00, 0xffff, 0xffff, 0xffff, 0xffff, 0xfffe,
/*to */ 0xffff, 0xffff, 0xffff, 0xfffe, 0xffff, 0xffff, 0xffff, 0xffff,
/*to */ 0xffff, 0xffff, 0xffff, 0xffff};
unsigned short int *dict = NULL;
unsigned char tcp_opt[TCP_OPT_LEN] = {0x02, 0x04, 0xff, 0xff, 0x04, 0x02, 0x08,
0x0a, 0x00, 0x09, 0x00, 0x09, 0x00, 0x00,
0x00, 0x00, 0x01, 0x03, 0x03, 0xff, 0x06,
0x06, 0xb0, 0x0b, 0xba, 0xbe, 0x01, 0x01};
// more keywords:
// cafe, dead, beef, affe, b00b, babe, f00, fefe, ffff, 1337, 666, 0, 1
void help(char *prg) {
printf("%s %s (c) 2022 by %s %s\n\n", prg, VERSION, AUTHOR, RESOURCE);
printf(
"Syntax: %s [-CFHLMPSdlpvV] [-I srcip6] [-i file] [-o file] [-e opt] [-s "
"port,..] [-a port,..] [-u port,..] [-T tag] [-W TIME] interface "
"[unicast-or-multicast-address [remote-router]]\n\n",
prg);
printf("Options:\n");
if (do_help) printf(" Output Options:\n");
printf(" -i file check systems from input file\n");
printf(" -o file write results to output file\n");
if (do_help)
printf(
" -v verbose information (twice: detailed, thrice: dumping "
"packets)\n");
if (do_help > 1) printf(" -V enable debug output\n");
printf(" -d DNS resolve alive IPv6 addresses\n");
if (do_help > 1) printf(" -H print hop count of received packets\n");
if (do_help) printf(" Enumerate Options:\n");
printf(
" -M enumerate hardware addresses (MAC) from input addresses "
"(slow!)\n");
printf(
" -C enumerate common addresses of input networks, -CC for "
"large scan\n");
printf(
" -4 ipv4/range test various IPv4 address encodings per network (eg "
"1.2.3.4/24)\n");
if (do_help > 1)
printf(
" -y step for range scans (2000::0-f), define the step range "
"(default: 1)\n");
if (do_help) printf(" Alive Technique Options:\n");
printf(" -p send a ping packet for alive check (default)\n");
printf(
" -e dst,hop send an errornous packets: destination (default), "
"hop-by-hop\n");
printf(
" -s port,port,.. TCP-SYN packet to ports for alive check or "
"\"portscan\"\n");
printf(" -a port,port,.. TCP-ACK packet to ports for alive check\n");
printf(" -u port,port,.. UDP packet to ports for alive check\n");
if (do_help)
printf(
" -F firewall mode: -p -e dst -u 53 -s 22,25,80,443,9511 -a "
"9511\n");
if (do_help > 1)
printf(
" -R do not consider TCP-RST as alive (good with firewalls, "
"e.g. -F)\n");
if (do_help) printf(" Sending Options\n");
if (do_help) {
printf(
" -n number how often to send each packet (default: local 1, remote "
"2)\n");
printf(
" -W time time in ms to wait after sending a packet (default: "
"%d)\n",
waittime);
printf(
" -S slow mode, get best router for each remote target or "
"when proxy-NA\n");
printf(
" -I src[/mask] use the specified IPv6 address as source. Use mask "
"for random.\n");
printf(
" -l use link-local address for multicast addresses instead "
"of global\n");
printf(
" -P only print addresses that would be scanned, no packets "
"are sent!\n");
if (do_help > 1) {
printf(
" -L local mode - perform only NDP and report local systems "
"alive\n");
printf(
" -r renew IPv6 src address for every new target (waits if "
"none there)\n");
printf(" -T tag put tag string in ICMP packets\n");
printf(" -x port TCP/UDP src port for -s, -a and -u\n");
printf(" -Z mac use given destination mac address\n");
}
printf(
" -m raw mode (for network adapters where you do not have "
"Ethernet)\n");
}
if (do_help == 1) {
printf(" Help Options:\n");
printf(" -hh show even more options\n");
} else
printf(
" -h to display more command line options and help (-hh: more "
"options)\n");
printf(
"\nTarget address on command line or in input file can include ranges in "
"the form\n");
printf("of 2001:db8::1-fff or 2001:db8::1-2:0-ffff:0:0-ffff, etc.\n");
if (do_help) {
printf("Do not use the ranges (from-to) option with -M, -C or -4.\n");
printf(
"If you use SYN packets (-s/-F option), automatic OS detection is "
"performed.\n");
// printf("If you specify a remote router, fragmentation+srcroute is
// performed.\n");
printf(
"Returns -1 on errors, 0 if a system was found alive or 1 if nothing "
"was found.\n");
}
exit(-1);
}
/*****************************************************************************************/
/*
Copyright (c) warlord @ nologin.org. All rights reserved.
For more information, please visit http://www.nologin.org
*/
char *get_OS(char *query_fp) {
int i = 0;
for (i = 0; i < sizeof(fingerprintsArray) / sizeof(fingerprintsArray[0]);
i++) {
if (strcmp(fingerprintsArray[i].fingerprint, query_fp) == 0)
return fingerprintsArray[i].OS;
}
return FPS_UNKNOWN;
}
char *warlord_checkFingerprint(char *buffer, int len) {
char *os, *end, *ptr, ip_mod, ip_ver = 0, ip_hdr_size = 0;
ip_ver = (((unsigned char)buffer[0] & 0xf0) >> 4);
if (ip_ver == 4) {
ip_mod = 0;
ip_hdr_size = ((buffer[0] & 0x0f) << 2);
} else if (ip_ver == 6) {
ip_mod = 20; // to align packet sizes in FPS with IPv4 for IPv6 (would be
// 40 bytes - 20 = 20 equals to IPv4)
ip_hdr_size = 40;
}
if (ip_ver == 0 || (len - ip_hdr_size) < 20 || ip_hdr_size < 20 ||
ip_hdr_size > 40) // invalid ip version or packet too short?
return FPS_INVALID;
snprintf(
_fingerprint, sizeof(_fingerprint) - 1, "%04x:%02x:%04x",
len + 20 -
ip_hdr_size, // total length (calculated with assumed ipv4 header
(unsigned char)buffer[ip_hdr_size + 12], // header length
ntohs(*(in_port_t *)&buffer[ip_hdr_size + 14])); // window size
// So what kind of tcp options did we receive?
// This is being used for OS fingerprinting
end = &buffer[len];
if (len > ip_hdr_size + (unsigned char)buffer[ip_hdr_size + 12])
end = &buffer[ip_hdr_size + (unsigned char)buffer[ip_hdr_size + 12]];
for (ptr = &buffer[ip_hdr_size + 20]; ptr < end;) {
switch (*ptr) {
case 0x0: // end of options
ptr = end;
break;
case 0x1: // some pad entire options portion with NOP to keep response
// option size the same
strncat(_fingerprint, ":NOP",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
ptr++;
break;
case 0x2: // segment size
snprintf(&_fingerprint[strlen(_fingerprint)],
sizeof(_fingerprint) - strlen(_fingerprint), ":SS%04x",
ntohs(*(in_port_t *)(ptr + 2)));
ptr += 4;
break;
case 0x3: // window scaling
strncat(_fingerprint, ":WSxx",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
ptr += 3;
break;
case 0x4: // Sack Permitted / Sack Denied
switch (ptr[1]) {
case 0x2:
strncat(_fingerprint, ":SP",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
break;
default:
strncat(_fingerprint, ":SD",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
break;
}
ptr += 2;
break;
case 0x6: // echo request
strncat(_fingerprint, ":PI",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
ptr += 6;
break;
case 0x7: // echo reply
strncat(_fingerprint, ":PO",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
ptr += 6;
break;
case 0x8: // Time stamp
strncat(_fingerprint, ":TS",
sizeof(_fingerprint) - strlen(_fingerprint) - 1);
ptr += 10;
break;
default: // unknown
snprintf(&_fingerprint[strlen(_fingerprint)],
sizeof(_fingerprint) - strlen(_fingerprint), ":UOP%02x",
(unsigned char)(*ptr));
ptr += (unsigned char)ptr[1];
break;
}
}
_fingerprint[sizeof(_fingerprint) - 1] = 0;
// printf ("%s\t\tport %5d\t", inet_ntoa (src), ntohs
// (recvtcp->source));
os = get_OS(_fingerprint);
if (strcmp(os, FPS_UNKNOWN) == 0) return _fingerprint;
return os;
}
/* end of warlords code */
/**************************************************************************/
void check_packets(u_char *foo, const struct pcap_pkthdr *header,
const unsigned char *data) {
int i, ok = 0, len = header->caplen, offset = 0, nxt;
unsigned char *ptr = (unsigned char *)data, *p1, *p2, *p3, sport[16] = "",
*orig_dst = NULL;
char *type = RESP_UNKNOWN, hopcount[20] = "", *os = NULL;
if (do_hdr_size) {
ptr += do_hdr_size;
len -= do_hdr_size;
if ((ptr[0] & 240) != 0x60) return;
} else if (_thc_ipv6_rawmode == 0) {
ptr += 14;
len -= 14;
}
if (debug) thc_dump_data(ptr, len, "Received Packet");
if (len < 48 + sizeof(buf)) return;
nxt = ptr[6];
// if the destination system sends source routed packets back, unlikely though
// if (ptr[6] == NXT_ROUTE)
// if ((offset = (ptr[41] + 1) * 8) + 48 + sizeof(buf) > len)
// return;
if (ptr[6 + offset] == NXT_FRAG) {
nxt = ptr[40 + offset];
offset += 8;
}
if (still_not_there == 1) still_not_there = 0;
if (nxt == NXT_ICMP6 && (do_ping || do_dst || do_hop || udpports[0] != -1)) {
if (ptr[40 + offset] == ICMP6_PINGREPLY && (do_ping || do_dst || do_hop)) {
if (tagging == NULL) {
if (memcmp(ptr + 50 + offset, (char *)&si + _TAKE2, 2) == 0) {
ok = 1;
type = RESP_PONG;
}
} else {
// printf("TAG: %s\n", ptr + 48 + offset);
if (memcmp(ptr + 48 + offset, (char *)tagging, strlen(tagging)) == 0) {
ok = 1;
type = RESP_PONG;
}
}
} else // if not a ping reply, its an error packet and the size is larger
if (len < 96 + sizeof(buf))
return;
if (ptr[40 + offset] == ICMP6_PARAMPROB && (do_dst || do_hop))
if (memcmp(ptr + len - 4, (char *)&si + _TAKE2, 2) == 0) {
if (list == 0 && do_hop)
ok = 2;
else
ok = 1;
type = RESP_PARAMPROB;
}
if (ptr[40 + offset] == ICMP6_UNREACH && ptr[41 + offset] == 4 &&
udpports[0] != -1)
if (memcmp(ptr + 88 + offset, (char *)&sp2 + _TAKE2, 2) == 0) {
ok = 1;
type = RESP_UNREACH_PORT;
i = (ptr[90 + offset] << 8) + ptr[91 + offset];
snprintf(sport, sizeof(sport), "%d/", i);
}
}
if (nxt == NXT_UDP && udpports[0] != -1)
if (memcmp(ptr + 42 + offset, (char *)&sp2 + _TAKE2, 2) == 0) {
ok = 1;
type = RESP_UDP;
}
if (nxt == NXT_TCP && (portscan || synports[0] != -1 || ackports[0] != -1))
if (memcmp(ptr + 42 + offset, (char *)&sp2 + _TAKE2, 2) == 0) {
ok = 1;
i = ptr[41 + offset] + (ptr[40 + offset] << 8);
snprintf(sport, sizeof(sport), "%d/", i);
switch (ptr[53 + offset]) {
case (TCP_SYN + TCP_ACK):
os = warlord_checkFingerprint(ptr, len);
type = RESP_SYNACK;
break;
case TCP_ACK:
type = RESP_ACK;
break;
case TCP_RST: /* fall through */
case (TCP_RST + TCP_ACK):
type = RESP_RST;
if (rst_means_alive == 0) ok = 0;
break;
default:
type = RESP_OTHER;
}
}
if (ok == 0 && nxt == NXT_ICMP6) {
ok = 2;
switch (ptr[40 + offset]) {
case 1:
switch (ptr[41 + offset]) {
case 0:
type = RESP_UNREACH_ROUTE;
break;
case 1:
type = RESP_UNREACH_FW;
break;
case 2:
type = RESP_UNREACH_OOSCOPE;
break;
case 3:
type = RESP_UNREACH_ADDR;
break;
case 4:
type = RESP_UNREACH_PORT;
break;
case 5:
type = RESP_UNREACH_GRESS;
break;
case 6:
type = RESP_UNREACH_REJECT;
break;
default:
ok = 0;
}
break;
case 2:
type = RESP_TOOBIG;
break;
case 3:
type = RESP_TTLEXCEED;
break;
case 4:
type = RESP_PARAMPROB;
break;
case 137:
type = RESP_REDIR;
break;
default:
ok = 0;
}
if (ok == 0) {
if (slow == 0 || ptr[40] != ICMP6_NEIGHBORADV) {
type = RESP_ERROR;
snprintf(sport, sizeof(sport), "%d:%d/", ptr[40], ptr[41]);
ok = 2;
}
} else
orig_dst = thc_ipv62notation(ptr + 64 + offset + 8);
}
i = 0;
if (verbose < 2)
while (ok && i < alive_no) {
if (memcmp(alive[i], ptr + 8 + offset, 16) == 0) ok = 0;
i++;
}
if (ok) {
if (do_hopcount) sprintf(hopcount, " {hop count: %d}", ptr[7]);
if (portscan == 0 ||
(portscan && (verbose > 2 || (strcmp(type, RESP_UNREACH_PORT) != 0 &&
strcmp(type, RESP_UNREACH_FW) != 0)))) {
if (resolve) he = gethostbyaddr(ptr + 8, 16, AF_INET6);
p2 = thc_ipv62notation(ptr + 8);
printf("Alive: %s%s%s%s [%s%s%s%s]%s%s%s%s\n", p2, resolve ? " (" : "",
resolve && he != NULL ? he->h_name : "", resolve ? ")" : "", sport,
type, orig_dst != NULL ? " for " : "",
orig_dst != NULL ? (char *)orig_dst : "", hopcount,
os == NULL ? "" : " (OS: ", os == NULL ? "" : os,
os == NULL ? "" : ")");
if (out != NULL)
fprintf(out, "%s%s%s%s\n", p2, resolve ? " (" : "",
(resolve && he != NULL) ? he->h_name : "", resolve ? ")" : "");
free(p2);
if (orig_dst != NULL) free(orig_dst);
if (alive_no < MAX_ALIVE && (alive[alive_no] = malloc(16)) != NULL) {
memcpy(alive[alive_no], ptr + 8, 16);
alive_no++;
if (alive_no == MAX_ALIVE)
fprintf(stderr,
"Warning: more than %d alive systems detected, disabling "
"double results check!\n",
MAX_ALIVE);
}
}
} else if (verbose && len >= 96 + sizeof(buf) && nxt == NXT_ICMP6 &&
ptr[41 + offset] != 4 && ptr[40 + offset] < 4 &&
ptr[40 + offset] > 0 && ptr[40 + 8 + offset + 6] == NXT_ICMP6) {
if (memcmp(ptr + len - 4, (char *)&si + _TAKE2, 2) == 0) {
if (resolve) he = gethostbyaddr(ptr + 8, 16, AF_INET6);
p2 = thc_ipv62notation(ptr + 8);
p3 = thc_ipv62notation(ptr + 24 + 40 + 8 + offset);
switch (ptr[40 + offset]) {
case 1:
p1 = "unreachable";
break;
case 2:
p1 = "toobig";
break;
case 3:
p1 = "time-to-live-exceeded";
break;
}
printf("Warning: %s%s%s%s sent an ICMP %s for %s\n", p2,
resolve ? " (" : "", resolve && he != NULL ? he->h_name : "",
resolve ? ")" : "", p1, p3);
free(p2);
free(p3);
}
}
if (still_not_there == 0) {
if (ok != 1)
still_not_there = 1;
else
still_not_there = -1;
}
}
void get_ports_from_cmdline(int ports[], char *plist, char param) {
int p, c = 0;
char mylist[strlen(plist) + 1], *ptr, *ptr2;
if (strtok(plist, "0123456789,") != NULL) {
fprintf(stderr,
"Error: ports must be defined by numbers and separated by a comma, "
"e.g. \"-%c 22,53,80\"\n",
param);
exit(-1);
}
strcpy(mylist, plist);
ptr = mylist;
do {
if (c >= MAX_PORTS) {
fprintf(stderr, "Error: a maximum number of %d ports can be specified\n",
MAX_PORTS);
exit(-1);
}
if ((ptr2 = index(ptr, ',')) != NULL) *ptr2++ = 0;
p = atoi(ptr);
if (p < 0 || p > 65535) { // allow port zero
fprintf(stderr, "Error: ports must be between 0 and 65535: %s\n", ptr);
exit(-1);
}
ports[c] = p % 65536;
c++;
ptr = ptr2;
} while (ptr2 != NULL);
}
int adress4to6(unsigned char *addr6, unsigned int addr4, char *state) {
unsigned char a, b, c, d;
a = (addr4 >> 24) % 256;
b = (addr4 >> 16) % 256;
c = (addr4 >> 8) % 256;
d = addr4 % 256;
memset(addr6 + 8, 0, 8);
switch (*state) {
case 0:
addr6[15] = d;
break;
case 1:
if (d > 9) { // is hex different to decimal?
addr6[14] = (d / 100);
d = d % 100;
if (d > 9) d = (d / 10) * 16 + (d % 10);
addr6[15] = d;
break;
} else
*state += 1; // otherwise fall through
case 2:
addr6[9] = a;
addr6[11] = b;
addr6[13] = c;
addr6[15] = d;
break;
case 3:
if (d > 9 || c > 9 || b > 9 || a > 9) { // is hex different to decimal?
addr6[8] = (a / 100);
a = a % 100;
if (a > 9) a = (a / 10) * 16 + (a % 10);
addr6[9] = a;
addr6[10] = (b / 100);
b = b % 100;
if (b > 9) b = (b / 10) * 16 + (b % 10);
addr6[11] = b;
addr6[12] = (c / 100);
c = c % 100;
if (c > 9) c = (c / 10) * 16 + (c % 10);
addr6[13] = c;
addr6[14] = (d / 100);
d = d % 100;
if (d > 9) d = (d / 10) * 16 + (d % 10);
addr6[15] = d;
break;
} else
*state += 1; // otherwise fall through
case 4:
addr6[12] = a;
addr6[13] = b;
addr6[14] = c;
addr6[15] = d;
*state += 1;
return 1; // end of state reached
break; // not reached
default:
fprintf(stderr, "Error: invalid address4to6 state %d!\n", *state);
exit(-1);
}
*state += 1;
return 0;
}
int main(int argc, char *argv[]) {
unsigned char string[128]; // = "ip6 and dst ";
unsigned char *pkt = NULL, *router6 = NULL, *cur_dst, *p2, *p3, *ptr3, *smac,
buf2[6];
unsigned char *multicast6 = NULL, *src6 = NULL, *mac = NULL, *rmac = NULL,
*routers[2];
int pkt_len = 0, prefer = PREFER_GLOBAL, fromto = 0, dictptr = 0, offset = 14,
step = 1;
int enumerate_mac = 0, enumerate_dhcp = 0, i, j, k, l, cur_enum = 0,
print_only = 0, rand_source = 0;
int no_vendid = 0, no_nets = 0, local = -1, no_send = 1, no_send_local = 1,
no_send_remote = 2, nos = 0, renew = 0, errcnt, sendrc;
char *interface = NULL, *input = NULL, *output = NULL, line[128], line2[128],
*ptr, *ptr2, do_router = 0, ok;
unsigned int four_from[MAX_FOUR], four_to[MAX_FOUR], addr_cur;
unsigned char fcnt = 0, bh, bm, bl, restart, use_dmac = 0, dump_all = 0,
inc_next = 0, inc_step = 1;
unsigned int ip1, ip2, ip3, ip4, cip1, cip2, cip3, cip4, cip5, cip6, cip7,
cip8;
unsigned int fip1, fip2, fip3, fip4, fip5, fip6, fip7, fip8, tip1, tip2, tip3,
tip4, tip5, tip6, tip7, tip8;
unsigned char vendid[MAX_VENDID][11], nets[MAX_NETS][8], orig_dst[16],
dmac[27] = {0, 0, 0, 0, 0, 0, 0};
in_addr_t addr4;
// unsigned char dns4buf[] = { 0xde, 0xad, 0x01, 0x00, 0x00, 0x01, 0x00,
// 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x09, 0x6c, 0x6f, 0x63, 0x61,
// 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x00, 0x00, 0x01, 0x00,
// 0x01 };
unsigned char dns6buf[] = {0xba, 0xbe, 0x01, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x6c,
0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
0x74, 0x00, 0x00, 0x1c, 0x00, 0x01};
thc_ipv6_hdr *hdr;
time_t passed;
pcap_t * p;
FILE * in = NULL;
time_t timeval;
for (i = 0; i < MAX_PORTS; i++)
udpports[i] = ackports[i] = synports[i] = -1;
if (argc == 1) help(argv[0]);
j = 0;
while ((i = getopt(argc, argv,
"CRhH4:W:w:PSLFdrlMDn:i:o:pvs:a:u:e:VZ:I:Xx:y:T:")) >= 0) {
switch (i) {
case 'h':
do_help++;
break; // not reached
case 'H':
do_hopcount = 1;
break;
case 'R':
rst_means_alive = 0;
break;
case 'P':
print_only = 1;
break;
case '4':
if (fcnt >= MAX_FOUR) {
fprintf(stderr,
"Error: maximum number of IPv4 addresses supported is %d!\n",
MAX_FOUR);
exit(-1);
}
if ((ptr = index(optarg, '/')) == NULL) {
if ((addr4 = inet_addr(optarg)) == 0xffffffff) {
fprintf(stderr, "Error: option is not a valid IPv4 address: %s\n",
optarg);
exit(-1);
}
four_from[fcnt] = htonl(addr4);
four_to[fcnt] = htonl(addr4);
fcnt++;
} else {
if ((ptr2 = malloc(strlen(optarg) + 1)) == NULL) {
fprintf(stderr, "Error: can not allocate memory\n");
exit(-1);
}
strcpy(ptr2, optarg);
k = 32;
if ((ptr = index(ptr2, '/')) != NULL) *ptr++ = 0;
if ((k = atoi(ptr)) < 8 || k > 31) {
fprintf(stderr,
"Error: network size may only be between /8 and /31: %s\n",
optarg);
exit(-1);
}
if ((addr4 = htonl(inet_addr(ptr2))) == 0xffffffff) {
fprintf(stderr, "Error: option is not a valid IPv4 address: %s\n",
ptr2);
exit(-1);
}
free(ptr2);
l = 1 << (32 - k);
l--;
four_to[fcnt] = (addr4 | l);
l = 0xffffffff - l;
four_from[fcnt] = (addr4 & l);
fcnt++;
}
break;
case 'T':
tagging = optarg;
break;
case 'Z':
use_dmac = 1;
sscanf(optarg, "%x:%x:%x:%x:%x:%x", (unsigned int *)&dmac[0],
(unsigned int *)&dmac[1], (unsigned int *)&dmac[2],
(unsigned int *)&dmac[3], (unsigned int *)&dmac[4],
(unsigned int *)&dmac[5]);
break;
case 'w':
case 'W':
waittime = atoi(optarg) * 1000;
break;
case 'S':
slow = 1;
break;
case 'L':
ndp_only = 1;
break;
case 'V':
debug = 1;
break;
case 'F':
do_ping = 1;
do_dst = 1;
udpports[0] = 53;
ackports[0] = 9511;
synports[0] = 22;
synports[1] = 25;
synports[2] = 80;
synports[3] = 443;
synports[4] = 9511;
break;
case 'd':
resolve = 1;
break;
case 'r':
renew = 1;
break;
case 'l':
prefer = PREFER_LINK;
break;
case 'm':
thc_ipv6_rawmode(1);
break;
case 'M':
enumerate_mac = 1;
break;
case 'C':
case 'D':
enumerate_dhcp = 1;
if (dict == NULL)
dict = dict_small;
else
dict = dict_large;
break;
case 'n':
no_send_local = no_send_remote = atoi(optarg);
break;
case 'I':
if ((src6 = thc_resolve6(optarg)) == NULL) {
fprintf(stderr, "Error: unable to resolve IPv6 source address %s\n",
optarg);
exit(-1);
}
if (index(optarg, '/') != NULL) {
if ((p2 = strdup(optarg)) == NULL) {
fprintf(stderr, "Error: malloc()\n");
exit(-1);
}
p3 = index(p2, '/');
*p3++ = 0;
if ((i = atoi(p3)) < 8 || i > 120 || i % 8 != 0) {
fprintf(stderr,
"Error: -I netmask parameter must be a multiple of 8\n");
exit(-1);
}
rand_source = 16 - (i / 8);
}
break;
case 'i':
input = optarg;
list++;
if (curr == 0) curr = 1;
break;
case 'o':
output = optarg;
break;
case 'p':
do_ping = 1;
j = (j | 1);
break;
case 'v':
verbose++;
break;
case 's':
j = (j | 8);
if (strcasecmp(optarg, "xxx") == 0 ||
strncasecmp(optarg, "port", 4) == 0 ||
strncasecmp(optarg, "scan", 4) == 0) {
portscan = 1;
if (verbose < 2) verbose = 2;
} else
get_ports_from_cmdline(synports, optarg, 's');
break;
case 'a':
j = (j | 8);
get_ports_from_cmdline(ackports, optarg, 'a');
break;
case 'u':
j = (j | 8);
get_ports_from_cmdline(udpports, optarg, 'u');
break;
case 'e':
if (index(optarg, ',') != 0) {
do_dst = 1;
do_hop = 1;
j = (j | 6);
} else {
if (strncasecmp(optarg, "dst", 3) == 0 ||
strncasecmp(optarg, "dest", 4) == 0) {
do_dst = 1;
j = (j | 4);
}
if (strncasecmp(optarg, "hop", 3) == 0) {
do_hop = 1;
j = (j | 2);
}
if (do_hop + do_dst == 0) {
fprintf(stderr,
"Error: unknown options to error packet option: %s\n",
optarg);
exit(-1);
}
}
break;
case 'X':
dump_all = 1;
break;
case 'x':
srcport = atoi(optarg);
if (srcport < 0 || srcport > 65535) {
fprintf(stderr, "Error: invalid port: %s\n", optarg);
exit(-1);
}
break;
case 'y':
step = atoi(optarg);
if (step < 1 || step > 256) {
fprintf(stderr, "Error: invalid step range (valid: 1-256): %s\n",
optarg);
exit(-1);
}
break;
default:
fprintf(stderr, "Error: unknown option -%c\n", i);
exit(-1);
}
}
if (do_help) help(argv[0]);
if (slow && ndp_only) {
fprintf(stderr, "Error: you can not use the -S and -L options together!\n");
exit(-1);
}
if (j) { // reset defaults if an alive check type was chosen
if ((j & 1) == 0) do_ping = 0;
if ((j & 2) == 0) do_hop = 0;
if ((j & 4) == 0) do_dst = 0;
}
if (verbose > 1)
fprintf(stderr,
"Warning: -vv disables duplicate checks, every packet will be "
"logged.\n");
if (no_send < 1 || no_send > 10) {
fprintf(stderr, "Error: -n option may only be set between 1 and 10\n");