-
Notifications
You must be signed in to change notification settings - Fork 59
/
ike-scan.c
3625 lines (3537 loc) · 140 KB
/
ike-scan.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
/*
* The IKE Scanner (ike-scan) is Copyright (C) 2003-2013 Roy Hills,
* NTA Monitor Ltd.
*
* This file is part of ike-scan.
*
* ike-scan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ike-scan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ike-scan. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library, and distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version.
*
* If this license is unacceptable to you, I may be willing to negotiate
* alternative licenses (contact ike-scan@nta-monitor.com).
*
* You are encouraged to submit comments, improvements or suggestions
* at the github repository https://github.com/royhills/ike-scan
*
* ike-scan -- The IKE Scanner
*
* Author: Roy Hills
* Date: 11 September 2002
*
* Usage:
* ike-scan [options] [host...]
*
* Description:
*
* ike-scan - The IKE Scanner
*
* ike-scan sends IKE Phase 1 requests to the specified hosts and displays
* any responses that are received. It handles retry and retransmission with
* backoff to cope with packet loss.
*
* Use ike-scan --help to display information on the usage and options.
* See the README file for full details.
*
*/
#include "ike-scan.h"
#include "hash_functions.h"
/* Global variables */
host_entry *helist = NULL; /* Dynamic array of host entries */
host_entry **helistptr; /* Array of pointers to host entries */
host_entry **cursor; /* Pointer to current list entry */
pattern_list *patlist = NULL; /* Backoff pattern list */
vid_pattern_list *vidlist = NULL; /* Vendor ID pattern list */
char **idlist = NULL; /* Array of pointers to ID strings */
static int verbose=0; /* Verbose level */
unsigned experimental_value=0; /* Experimental value */
int tcp_flag=0; /* TCP flag */
int psk_crack_flag=0; /* Pre-shared key cracking flag */
psk_crack psk_values = { /* Pre-shared key values */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
0, 0, 0, 0, 0, 0, 0, 0, 0
};
int no_dns_flag=0; /* No DNS flag */
int mbz_value=0; /* Value for MBZ fields */
uint32_t lifetime_be; /* Default lifetime in big endian format */
uint32_t lifesize_be; /* Default lifesize in big endian format */
int write_pkt_to_file=0; /* Write packet to file for debugging */
int read_pkt_from_file=0; /* Read packet from file for debugging */
int timestamp_flag=0; /* Timestamp flag */
int randsrc_flag=0; /* Randomise source IP address flag */
int sourceip_flag=0; /* Set source IP address flag */
uint32_t src_ip_val; /* Specified source IP */
int shownum_flag=0; /* Display packet number */
int nat_t_flag=0; /* RFC 3947 NAT Traversal */
int bindip_flag=0; /* Set bind IP address flag */
uint32_t bind_ip_val; /* IP address to bind to */
extern const id_name_map notification_map[];
extern const id_name_map attr_map[];
extern const id_name_map enc_map[];
extern const id_name_map hash_map[];
extern const id_name_map auth_map[];
extern const id_name_map dh_map[];
extern const id_name_map life_map[];
extern const id_name_map payload_map[];
extern const id_name_map doi_map[];
extern const id_name_map protocol_map[];
extern const id_name_map id_map[];
extern const id_name_map cert_map[];
int
main(int argc, char *argv[]) {
/*
* long_options can be const because the flag is always set to zero (NULL)
* and is never changed.
*/
const struct option long_options[] = {
{"file", required_argument, 0, 'f'},
{"help", no_argument, 0, 'h'},
{"sport", required_argument, 0, 's'},
{"dport", required_argument, 0, 'd'},
{"retry", required_argument, 0, 'r'},
{"timeout", required_argument, 0, 't'},
{"interval", required_argument, 0, 'i'},
{"backoff", required_argument, 0, 'b'},
{"selectwait", required_argument, 0, 'w'},
{"verbose", no_argument, 0, 'v'},
{"lifetime", required_argument, 0, 'l'},
{"lifesize", required_argument, 0, 'z'},
{"auth", required_argument, 0, 'm'},
{"version", no_argument, 0, 'V'},
{"vendor", required_argument, 0, 'e'},
{"trans", required_argument, 0, 'a'},
{"showbackoff", optional_argument, 0, 'o'},
{"fuzz", required_argument, 0, 'u'},
{"id", required_argument, 0, 'n'},
{"idtype", required_argument, 0, 'y'},
{"dhgroup", required_argument, 0, 'g'},
{"patterns", required_argument, 0, 'p'},
{"aggressive", no_argument, 0, 'A'},
{"gssid", required_argument, 0, 'G'},
{"vidpatterns", required_argument, 0, 'I'},
{"quiet", no_argument, 0, 'q'},
{"multiline", no_argument, 0, 'M'},
{"random", no_argument, 0, 'R'},
{"tcp", optional_argument, 0, 'T'},
{"pskcrack", optional_argument, 0, 'P'},
{"tcptimeout", required_argument, 0, 'O'},
{"nodns", no_argument, 0, 'N'},
{"noncelen", required_argument, 0, 'c'},
{"bandwidth", required_argument, 0, 'B'},
{"headerlen", required_argument, 0, 'L'},
{"mbz", required_argument, 0, 'Z'},
{"headerver", required_argument, 0, 'E'},
{"certreq", required_argument, 0, 'C'},
{"doi", required_argument, 0, 'D'},
{"situation", required_argument, 0, 'S'},
{"protocol", required_argument, 0, 'j'},
{"transid", required_argument, 0, 'k'},
{"spisize", required_argument, 0, OPT_SPISIZE},
{"hdrflags", required_argument, 0, OPT_HDRFLAGS},
{"hdrmsgid", required_argument, 0, OPT_HDRMSGID},
{"cookie", required_argument, 0, OPT_COOKIE},
{"exchange", required_argument, 0, OPT_EXCHANGE},
{"nextpayload", required_argument, 0, OPT_NEXTPAYLOAD},
{"writepkttofile", required_argument, 0, OPT_WRITEPKTTOFILE},
{"randomseed", required_argument, 0, OPT_RANDOMSEED},
{"timestamp", no_argument, 0, OPT_TIMESTAMP},
{"sourceip", required_argument, 0, OPT_SOURCEIP},
{"bindip", required_argument, 0, OPT_BINDIP},
{"shownum", no_argument, 0, OPT_SHOWNUM},
{"ikev2", no_argument, 0, '2'},
{"nat-t", no_argument, 0, OPT_NAT_T},
{"rcookie", required_argument, 0, OPT_RCOOKIE},
{"readpktfromfile", required_argument, 0, OPT_READPKTFROMFILE},
{"experimental", required_argument, 0, 'X'},
{0, 0, 0, 0}
};
/*
* available short option characters:
*
* lower: -----------------------x--
* UPPER: -----F-H-JK-----Q---U-W-Y-
* Digits: 01-3456789
*/
const char *short_options =
"f:hs:d:r:t:i:b:w:vl:z:m:Ve:a:o::u:n:y:g:p:AG:I:qMRT::P::O:Nc:B:"
"L:Z:E:C:D:S:j:k:2X:";
int arg;
int options_index=0;
char filename[MAXLINE];
int filename_flag=0;
char pkt_filename[MAXLINE]; /* for --writepkttofile option */
int pkt_filename_flag=0;
int pkt_read_filename_flag=0;
int random_flag=0; /* Should we randomise the list? */
int sockfd; /* UDP socket file descriptor */
unsigned source_port = DEFAULT_SOURCE_PORT; /* UDP source port */
unsigned dest_port = DEFAULT_DEST_PORT; /* UDP destination port */
unsigned retry = DEFAULT_RETRY; /* Number of retries */
unsigned interval = 0; /* Interval between packets */
double backoff_factor = DEFAULT_BACKOFF_FACTOR; /* Backoff factor */
unsigned end_wait = 1000 * DEFAULT_END_WAIT; /* Time to wait after all done in ms */
unsigned timeout = DEFAULT_TIMEOUT; /* Per-host timeout in ms */
ike_packet_params ike_params = {
NULL, /* Lifetime in seconds */
0, /* Lifetime data length */
NULL, /* Lifesize in KB */
0, /* Lifesize data length */
DEFAULT_AUTH_METHOD, /* Authentication method */
DEFAULT_DH_GROUP, /* Diffie Hellman Group */
DEFAULT_IDTYPE, /* IKE Identification type */
NULL, /* Identity data */
0, /* Identity data length */
0, /* Indicates if VID to be used */
0, /* Indicates custom transform */
DEFAULT_EXCHANGE_TYPE, /* Main or Aggressive mode */
0, /* Indicates if GSSID to be used */
NULL, /* Binary GSSID data */
0, /* GSSID data length */
DEFAULT_NONCE_LEN, /* Nonce data length */
NULL, /* ISAKMP header length modifier */
NULL, /* Cert req. data */
0, /* cd_data_len */
DEFAULT_HEADER_VERSION, /* header_version */
DEFAULT_DOI, /* SA DOI */
DEFAULT_SITUATION, /* SA Situation */
DEFAULT_PROTOCOL, /* Proposal Protocol ID */
DEFAULT_TRANS_ID, /* Transform ID */
0, /* Proposal SPI Size */
0, /* ISAKMP Header Flags */
0, /* ISAKMP Header Message ID */
0, /* ISAKMP Header Next Payload */
0, /* advanced_trans_flag */
DEFAULT_IKE_VERSION, /* IKE Version */
NULL, /* rcookie data */
0 /* rcookie data length */
};
unsigned pattern_fuzz = DEFAULT_PATTERN_FUZZ; /* Pattern matching fuzz in ms */
unsigned tcp_connect_timeout = DEFAULT_TCP_CONNECT_TIMEOUT;
struct sockaddr_in sa_local;
struct sockaddr_in sa_peer;
struct timeval now;
unsigned char packet_in[MAXUDP]; /* Received packet */
int n;
host_entry *temp_cursor;
struct timeval diff; /* Difference between two timevals */
IKE_UINT64 loop_timediff; /* Time since last packet sent in us */
IKE_UINT64 host_timediff; /* Time since last packet sent to this host */
unsigned long end_timediff=0; /* Time since last packet received in ms */
int req_interval; /* Requested per-packet interval */
int select_timeout; /* Select timeout */
int cum_err=0; /* Cumulative timing error */
static int reset_cum_err;
struct timeval start_time; /* Program start time */
struct timeval end_time; /* Program end time */
struct timeval last_packet_time; /* Time last packet was sent */
struct timeval elapsed_time; /* Elapsed time as timeval */
double elapsed_seconds; /* Elapsed time in seconds */
char patfile[MAXLINE]; /* IKE Backoff pattern file name */
char vidfile[MAXLINE]; /* IKE Vendor ID pattern file name */
char psk_crack_file[MAXLINE];/* PSK crack data output file name */
unsigned pass_no=0;
int first_timeout=1;
unsigned char *vid_data; /* Binary Vendor ID data */
size_t vid_data_len; /* Vendor ID data length */
int showbackoff_flag = 0; /* Display backoff table? */
struct timeval last_recv_time; /* Time last packet was received */
unsigned char *packet_out; /* IKE packet to send */
size_t packet_out_len; /* Length of IKE packet to send */
unsigned sa_responders = 0; /* Number of hosts giving handshake */
unsigned notify_responders = 0; /* Number of hosts giving notify msg */
unsigned num_hosts = 0; /* Number of entries in the list */
unsigned live_count; /* Number of entries awaiting reply */
int quiet=0; /* Only print the basic info if nonzero */
int multiline=0; /* Split decodes across lines if nonzero */
unsigned hostno;
unsigned bandwidth=DEFAULT_BANDWIDTH; /* Bandwidth in bits per sec */
unsigned char *cookie_data=NULL;
size_t cookie_data_len;
unsigned int random_seed=0;
/*
* Get program start time for statistics displayed on completion.
*/
Gettimeofday(&start_time);
/*
* Initialise file names to the empty string.
*/
patfile[0] = '\0';
vidfile[0] = '\0';
/*
* Set lifetime and lifesize parameters to the default.
*/
if (DEFAULT_LIFETIME) {
lifetime_be = htonl(DEFAULT_LIFETIME);
ike_params.lifetime_data = (unsigned char *) &lifetime_be;
ike_params.lifetime_data_len = 4;
}
if (DEFAULT_LIFESIZE) {
lifesize_be = htonl(DEFAULT_LIFETIME);
ike_params.lifesize_data = (unsigned char *) &lifesize_be;
ike_params.lifesize_data_len = 4;
}
/*
* Process options and arguments.
*/
while ((arg=getopt_long_only(argc, argv, short_options, long_options, &options_index)) != -1) {
switch (arg) {
unsigned trans_enc; /* Custom transform cipher */
unsigned trans_keylen; /* Custom transform cipher key length */
unsigned trans_hash; /* Custom transform hash */
unsigned trans_auth; /* Custom transform auth */
unsigned trans_group; /* Custom transform DH group */
char trans_str[MAXLINE]; /* Custom transform string */
struct in_addr src_ip_struct;
struct in_addr bind_ip_struct;
case 'f': /* --file */
strlcpy(filename, optarg, sizeof(filename));
filename_flag=1;
break;
case 'h': /* --help */
usage(EXIT_SUCCESS, 1); /* Doesn't return */
break; /* Not required but prevents fall through warning */
case 's': /* --sport */
source_port=Strtoul(optarg, 10);
break;
case 'd': /* --dport */
dest_port=Strtoul(optarg, 10);
break;
case 'r': /* --retry */
retry=Strtoul(optarg, 10);
break;
case 't': /* --timeout */
timeout=Strtoul(optarg, 10);
break;
case 'i': /* --interval */
interval=str_to_interval(optarg);
break;
case 'b': /* --backoff */
backoff_factor=atof(optarg);
break;
case 'w': /* --selectwait */
fprintf(stderr, "--selectwait option ignored - no longer needed\n");
break;
case 'v': /* --verbose */
verbose++;
break;
case 'l': /* --lifetime */
if ((strcmp(optarg, "none")) == 0) {
ike_params.lifetime_data = NULL;
ike_params.lifetime_data_len = 0;
} else {
ike_params.lifetime_data=
hex_or_num(optarg, &(ike_params.lifetime_data_len));
}
break;
case 'z': /* --lifesize */
if ((strcmp(optarg, "none")) == 0) {
ike_params.lifesize_data = NULL;
ike_params.lifesize_data_len = 0;
} else {
ike_params.lifesize_data=
hex_or_num(optarg, &(ike_params.lifesize_data_len));
}
break;
case 'm': /* --auth */
ike_params.auth_method=name_or_number(optarg, auth_map);
break;
case 'V': /* --version */
fprintf(stderr, "%s\n\n", PACKAGE_STRING);
fprintf(stderr, "Copyright (C) 2003-2013 Roy Hills, NTA Monitor Ltd.\n");
fprintf(stderr, "ike-scan comes with NO WARRANTY to the extent permitted by law.\n");
fprintf(stderr, "You may redistribute copies of ike-scan under the terms of the GNU\n");
fprintf(stderr, "General Public License.\n");
fprintf(stderr, "For more information about these matters, see the file named COPYING.\n");
fprintf(stderr, "\n");
exit(EXIT_SUCCESS); /* Doesn't return */
case 'e': /* --vendor */
if (strlen(optarg) % 2) /* Length is odd */
err_msg("ERROR: Length of --vendor argument must be even (multiple of 2).");
ike_params.vendor_id_flag=1;
vid_data=hex2data(optarg, &vid_data_len);
add_vid(0, NULL, vid_data, vid_data_len, ike_params.ike_version, 0);
free(vid_data);
break;
case 'a': /* --trans */
strlcpy(trans_str, optarg, sizeof(trans_str));
ike_params.trans_flag++;
if (trans_str[0] == '(') { /* Advanced transform specification */
unsigned char *attr=NULL;
size_t attr_len;
attr = decode_transform(trans_str, &attr_len);
add_transform(0, NULL, ike_params.trans_id, attr, attr_len);
ike_params.advanced_trans_flag = 1;
} else { /* Simple transform specification */
decode_trans_simple(trans_str, &trans_enc, &trans_keylen,
&trans_hash, &trans_auth, &trans_group);
add_trans_simple(0, NULL, trans_enc, trans_keylen, trans_hash,
trans_auth, trans_group,
ike_params.lifetime_data,
ike_params.lifetime_data_len,
ike_params.lifesize_data,
ike_params.lifesize_data_len,
ike_params.gss_id_flag,
ike_params.gss_data, ike_params.gss_data_len,
ike_params.trans_id);
}
break;
case 'o': /* --showbackoff */
showbackoff_flag=1;
if (optarg == NULL || *optarg == '\0') {
end_wait=1000 * DEFAULT_END_WAIT;
} else {
end_wait=1000 * Strtoul(optarg, 10);
}
break;
case 'u': /* --fuzz */
pattern_fuzz=Strtoul(optarg, 10);
break;
case 'n': /* --id */
if (ike_params.id_data)
err_msg("ERROR: You may only specify one identity payload with --id");
ike_params.id_data=hex_or_str(optarg, &(ike_params.id_data_len));
break;
case 'y': /* --idtype */
ike_params.idtype = Strtoul(optarg, 10);
break;
case 'g': /* --dhgroup */
ike_params.dhgroup = Strtoul(optarg, 10);
break;
case 'p': /* --patterns */
strlcpy(patfile, optarg, sizeof(patfile));
break;
case 'A': /* --aggressive */
ike_params.exchange_type = ISAKMP_XCHG_AGGR;
break;
case 'G': /* --gssid */
if (strlen(optarg) % 2) { /* Length is odd */
err_msg("ERROR: Length of --gssid argument must be even (multiple of 2).");
}
ike_params.gss_id_flag=1;
ike_params.gss_data=hex2data(optarg, &(ike_params.gss_data_len));
break;
case 'I': /* --vidpatterns */
strlcpy(vidfile, optarg, sizeof(vidfile));
break;
case 'q': /* --quiet */
quiet=1;
break;
case 'M': /* --multiline */
multiline=1;
break;
case 'R': /* --random */
random_flag=1;
break;
case 'T': /* --tcp */
if (optarg == NULL || *optarg == '\0') {
tcp_flag = TCP_PROTO_RAW;
} else {
tcp_flag = Strtoul(optarg, 10);
}
break;
case 'P': /* --pskcrack */
psk_crack_flag=1;
if (optarg == NULL || *optarg == '\0') {
psk_crack_file[0] = '\0'; /* use stdout */
} else {
strlcpy(psk_crack_file, optarg, sizeof(psk_crack_file));
}
break;
case 'O': /* --tcptimeout */
tcp_connect_timeout = Strtoul(optarg, 10);
break;
case 'N': /* --nodns */
no_dns_flag=1;
break;
case 'c': /* --noncelen */
ike_params.nonce_data_len = Strtoul(optarg, 10);
break;
case 'B': /* --bandwidth */
bandwidth=str_to_bandwidth(optarg);
break;
case 'L': /* --headerlen */
ike_params.header_length = dupstr(optarg);
break;
case 'Z': /* --mbz */
mbz_value = Strtoul(optarg, 0);
break;
case 'E': /* --headerver */
ike_params.header_version = Strtoul(optarg, 0);
break;
case 'C': /* --certreq */
if (strlen(optarg) % 2) /* Length is odd */
err_msg("ERROR: Length of --certreq argument must be even (multiple of 2).");
ike_params.cr_data=hex2data(optarg, &(ike_params.cr_data_len));
break;
case 'D': /* --doi */
ike_params.doi = Strtoul(optarg, 0);
break;
case 'S': /* --situation */
ike_params.situation = Strtoul(optarg, 0);
break;
case 'j': /* --protocol */
ike_params.protocol = Strtoul(optarg, 0);
break;
case 'k': /* --transid */
ike_params.trans_id = Strtoul(optarg, 0);
break;
case OPT_SPISIZE: /* --spisize */
ike_params.spi_size=Strtoul(optarg, 0);
break;
case OPT_HDRFLAGS: /* --hdrflags */
ike_params.hdr_flags=Strtoul(optarg, 0);
break;
case OPT_HDRMSGID: /* --hdrmsgid */
ike_params.hdr_msgid=Strtoul(optarg, 0);
break;
case OPT_COOKIE: /* --cookie */
if (strlen(optarg) % 2) /* Length is odd */
err_msg("ERROR: Length of --cookie argument must be even (multiple of 2).");
cookie_data=hex2data(optarg, &cookie_data_len);
if (cookie_data_len > 8)
cookie_data_len = 8;
break;
case OPT_EXCHANGE: /* --exchange */
ike_params.exchange_type=Strtoul(optarg, 0);
break;
case OPT_NEXTPAYLOAD: /* --nextpayload */
ike_params.hdr_next_payload=Strtoul(optarg, 0);
break;
case OPT_WRITEPKTTOFILE: /* --writepkttofile */
strlcpy(pkt_filename, optarg, sizeof(pkt_filename));
pkt_filename_flag=1;
break;
case OPT_RANDOMSEED: /* --randomseed */
random_seed=Strtoul(optarg, 0);
break;
case OPT_TIMESTAMP: /* --timestamp */
timestamp_flag = 1;
break;
case OPT_SOURCEIP: /* --sourceip */
sourceip_flag = 1;
if ((strcmp(optarg, "random")) == 0) {
randsrc_flag = 1;
} else {
if (!(inet_aton(optarg, &src_ip_struct)))
err_msg("ERROR: %s is not a valid IP address", optarg);
src_ip_val=src_ip_struct.s_addr;
}
break;
case OPT_BINDIP: /* --bindip */
bindip_flag = 1;
if (!(inet_aton(optarg, &bind_ip_struct)))
err_msg("ERROR: %s is not a valid IP address", optarg);
bind_ip_val=bind_ip_struct.s_addr;
break;
case OPT_SHOWNUM: /* --shownum */
shownum_flag = 1;
break;
case '2': /* --ikev2 */
ike_params.ike_version = 2;
ike_params.header_version = 0x20; /* v2.0 */
ike_params.hdr_flags=0x08; /* Set Initiator bit */
ike_params.exchange_type = ISAKMP_XCHG_IKE_SA_INIT;
break;
case OPT_NAT_T: /* --nat-t */
nat_t_flag = 1;
source_port = DEFAULT_NAT_T_SOURCE_PORT;
dest_port = DEFAULT_NAT_T_DEST_PORT;
break;
case OPT_RCOOKIE: /* --rcookie */
if (strlen(optarg) % 2) /* Length is odd */
err_msg("ERROR: Length of --rcookie argument must be even (multiple of 2).");
ike_params.rcookie_data=hex2data(optarg,
&(ike_params.rcookie_data_len));
if (ike_params.rcookie_data_len > 8)
ike_params.rcookie_data_len = 8;
break;
case OPT_READPKTFROMFILE: /* --readpktfromfile */
strlcpy(pkt_filename, optarg, sizeof(pkt_filename));
pkt_read_filename_flag=1;
break;
case 'X': /* --experimental */
experimental_value = Strtoul(optarg, 0);
break;
default: /* Unknown option */
usage(EXIT_FAILURE, 0); /* Doesn't return */
}
}
/*
* Seed random number generator.
* If the random seed has been specified (is non-zero), then use that.
* Otherwise, seed the RNG with an unpredictable value.
*/
if (!random_seed) {
struct timeval tv;
Gettimeofday(&tv);
random_seed = ((unsigned) tv.tv_usec ^ (unsigned) getpid());
}
init_genrand(random_seed);
/*
* Create network socket and bind to local source port.
*/
if (tcp_flag) {
const int on = 1; /* for setsockopt() */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("ERROR: socket");
if ((setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on))) < 0)
err_sys("ERROR: setsockopt() failed");
if ((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0)
err_sys("ERROR: setsockopt() failed");
} else if (sourceip_flag) { /* Raw IP socket */
const int on = 1; /* for setsockopt() */
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
err_sys("socket");
if ((setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))) != 0)
err_sys("setsockopt");
if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) != 0)
err_sys("setsockopt");
} else {
const int on = 1; /* for setsockopt() */
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
err_sys("ERROR: socket");
if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) != 0)
err_sys("setsockopt");
}
memset(&sa_local, '\0', sizeof(sa_local));
sa_local.sin_family = AF_INET;
if (bindip_flag)
sa_local.sin_addr.s_addr = bind_ip_val;
else
sa_local.sin_addr.s_addr = htonl(INADDR_ANY);
sa_local.sin_port = htons(source_port);
if ((bind(sockfd, (struct sockaddr *)&sa_local, sizeof(sa_local))) < 0) {
warn_msg("ERROR: Could not bind network socket to local port %u", source_port);
if (errno == EACCES)
warn_msg("You need to be root, or ike-scan must be suid root to bind to ports below 1024.");
if (errno == EADDRINUSE)
warn_msg("Only one process may bind to the source port at any one time.");
err_sys("ERROR: bind");
}
/*
* Drop privileges if we are SUID.
*/
if ((setuid(getuid())) < 0) {
err_sys("setuid");
}
/*
* If we're not reading from a file, then we must have some hosts
* given as command line arguments.
*/
if (!filename_flag)
if ((argc - optind) < 1)
usage(EXIT_FAILURE, 0);
/*
* Populate the list from the specified file if --file was specified, or
* otherwise from the remaining command line arguments.
*/
if (filename_flag) { /* Populate list from file */
FILE *fp;
char line[MAXLINE];
char *cp;
if ((strcmp(filename, "-")) == 0) { /* Filename "-" means stdin */
fp = stdin;
} else {
if ((fp = fopen(filename, "r")) == NULL) {
err_sys("ERROR: fopen");
}
}
while (fgets(line, MAXLINE, fp)) {
for (cp = line; !isspace((unsigned char)*cp) && *cp != '\0'; cp++)
;
*cp = '\0';
add_host_pattern(line, timeout, &num_hosts,
cookie_data, cookie_data_len);
}
if (fp != stdin)
fclose(fp);
} else { /* Populate list from command line arguments */
argv = &argv[optind];
while (*argv) {
add_host_pattern(*argv, timeout, &num_hosts,
cookie_data, cookie_data_len);
argv++;
}
}
/*
* If we are using TCP transport, then connect the socket to the peer.
* We know that there is only one entry in the host list if we're using
* TCP.
*/
if (tcp_flag) {
struct sockaddr_in sa_tcp;
NET_SIZE_T sa_tcp_len;
struct sigaction act, oact; /* For sigaction */
/*
* Set signal handler for alarm.
* Must use sigaction() rather than signal() to prevent SA_RESTART
*/
act.sa_handler=sig_alarm;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGALRM,&act,&oact);
/*
* Set alarm
*/
alarm(tcp_connect_timeout);
/*
* Connect to peer
*/
memset(&sa_tcp, '\0', sizeof(sa_tcp));
sa_tcp.sin_family = AF_INET;
sa_tcp.sin_addr.s_addr = helist->addr.s_addr;
sa_tcp.sin_port = htons(dest_port);
sa_tcp_len = sizeof(sa_tcp);
if ((connect(sockfd, (struct sockaddr *) &sa_tcp, sa_tcp_len)) != 0) {
if (errno == EINTR)
errno = ETIMEDOUT;
err_sys("ERROR: TCP connect");
}
/*
* Cancel alarm
*/
alarm(0);
}
/*
* If we are displaying the backoff table, load known backoff
* patterns from the backoff patterns file.
*/
if (showbackoff_flag) {
load_backoff_patterns(patfile, pattern_fuzz);
}
/*
* Load known Vendor ID patterns from the Vendor ID file.
*/
load_vid_patterns(vidfile);
/*
* Check that we have at least one entry in the list.
*/
if (!num_hosts)
err_msg("ERROR: No hosts to process.");
/*
* If --writepkttofile was specified, open the specified output file.
*/
if (pkt_filename_flag) {
write_pkt_to_file = open(pkt_filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (write_pkt_to_file == -1)
err_sys("open %s", pkt_filename);
}
/*
* If --readpktfromfile was specified, open the specified input file.
*/
if (pkt_read_filename_flag) {
read_pkt_from_file = open(pkt_filename, O_RDONLY);
if (read_pkt_from_file == -1)
err_sys("open %s", pkt_filename);
}
/*
* Check that the combination of specified options and arguments is
* valid.
*/
if (cookie_data && num_hosts > 1)
err_msg("ERROR: You can only specify one target host with the --cookie option.");
if (tcp_flag && num_hosts > 1)
err_msg("ERROR: You can only specify one target host with the --tcp option.");
if (*patfile != '\0' && !showbackoff_flag)
warn_msg("WARNING: Specifying a backoff pattern file with --patterns or -p does not\n"
" have any effect unless you also specify --showbackoff or -o\n");
if (ike_params.id_data && ike_params.exchange_type != ISAKMP_XCHG_AGGR)
warn_msg("WARNING: Specifying an identification payload with --id or -n does not have\n"
" any effect unless you also specify aggressive mode with --aggressive\n"
" or -A\n");
if (ike_params.idtype != DEFAULT_IDTYPE &&
ike_params.exchange_type != ISAKMP_XCHG_AGGR)
warn_msg("WARNING: Specifying an idtype payload with --idtype or -y does not have any\n"
" effect unless you also specify aggressive mode with --aggressive or -A\n");
if (ike_params.nonce_data_len != DEFAULT_NONCE_LEN &&
ike_params.exchange_type != ISAKMP_XCHG_AGGR &&
ike_params.ike_version == 1)
warn_msg("WARNING: Specifying the nonce payload length with --noncelen or -c does not\n"
" have any effect unless you also specify aggressive mode with\n"
" --aggressive or -A, or IKEv2 with --ikev2 or -2\n");
if (ike_params.dhgroup != DEFAULT_DH_GROUP &&
ike_params.exchange_type != ISAKMP_XCHG_AGGR &&
ike_params.ike_version == 1)
warn_msg("WARNING: Specifying the DH Group with --dhgroup or -g does not have any effect\n"
" unless you also specify aggressive mode with --aggressive or -A, or\n"
" IKEv2 with --ikev2 or -2\n");
if (psk_crack_flag && ike_params.exchange_type != ISAKMP_XCHG_AGGR) {
warn_msg("WARNING: The --pskcrack (-P) option is only relevant for aggressive mode.\n");
psk_crack_flag=0;
}
if (psk_crack_flag && num_hosts > 1)
err_msg("ERROR: You can only specify one target host with the --pskcrack (-P) option.");
if (interval && bandwidth != DEFAULT_BANDWIDTH)
err_msg("ERROR: You cannot specify both --bandwidth and --interval.");
if (ike_params.trans_flag != 0 && ike_params.ike_version == 2)
warn_msg("WARNING: IKEv2 does not support custom proposals.");
if (ike_params.ike_version == 2 &&
ike_params.exchange_type == ISAKMP_XCHG_AGGR)
err_msg("ERROR: You can not specify both aggressive mode and IKEv2.\n"
" Aggressive mode is only applicable to IKEv1.");
/*
* Create and initialise array of pointers to host entries.
*/
helistptr = Malloc(num_hosts * sizeof(host_entry *));
for (hostno=0; hostno<num_hosts; hostno++)
helistptr[hostno] = &helist[hostno];
/*
* Randomise the list if required.
* Uses Knuth's shuffle algorithm.
*/
if (random_flag) {
int i;
int r;
host_entry *temp;
for (i=num_hosts-1; i>0; i--) {
r = (int)(genrand_real2() * i); /* 0<=r<i */
temp = helistptr[i];
helistptr[i] = helistptr[r];
helistptr[r] = temp;
}
}
/*
* Set current host pointer (cursor) to start of list, zero
* last packet sent time, set last receive time to now and
* initialise static IKE header fields.
*/
live_count = num_hosts;
cursor = helistptr;
last_packet_time.tv_sec=0;
last_packet_time.tv_usec=0;
Gettimeofday(&last_recv_time);
packet_out=initialise_ike_packet(&packet_out_len, &ike_params);
/*
* Calculate the appropriate interval to achieve the required outgoing
* bandwidth unless an interval was specified.
*/
if (!interval) {
interval = ((IKE_UINT64)(packet_out_len+PACKET_OVERHEAD) * 8 * 1000000) /
bandwidth;
if (verbose) {
warn_msg("DEBUG: pkt len=%zu bytes, bandwidth=%u bps, int=%u us",
packet_out_len, bandwidth, interval);
}
}
/*
* Display initial message.
*/
printf("Starting %s with %u hosts (http://www.nta-monitor.com/tools/ike-scan/)\n", PACKAGE_STRING, num_hosts);
/*
* Display the lists if verbose setting is 3 or more.
*/
if (verbose > 2) {
dump_list(num_hosts);
if (showbackoff_flag)
dump_backoff(pattern_fuzz);
dump_vid();
}
/*
* Main loop: send packets to all hosts in order until a response
* has been received or the host has exhausted its retry limit.
*
* The loop exits when all hosts have either responded or timed out
* and, if showbackoff_flag is set, at least end_wait ms have elapsed
* since the last packet was received and we have received at least one
* transform response.
*/
reset_cum_err = 1;
req_interval = interval;
while (live_count ||
(showbackoff_flag && sa_responders && (end_timediff < end_wait))) {
/*
* Obtain current time and calculate deltas since last packet and
* last packet to this host.
*/
Gettimeofday(&now);
timeval_diff(&now, &last_recv_time, &diff);
end_timediff = 1000*diff.tv_sec + diff.tv_usec/1000;
/*
* If the last packet was sent more than interval us ago, then we can
* potentially send a packet to the current host.
*/
timeval_diff(&now, &last_packet_time, &diff);
loop_timediff = (IKE_UINT64)1000000*diff.tv_sec + diff.tv_usec;
if (loop_timediff >= (unsigned)req_interval) {
/*
* If the last packet to this host was sent more than the current
* timeout for this host us ago, then we can potentially send a packet
* to it.
*/
timeval_diff(&now, &((*cursor)->last_send_time), &diff);
host_timediff = (IKE_UINT64)1000000*diff.tv_sec + diff.tv_usec;
if (host_timediff >= (*cursor)->timeout && (*cursor)->live) {
if (reset_cum_err) {
cum_err = 0;
req_interval = interval;
reset_cum_err = 0;
} else {
cum_err += loop_timediff - interval;
if (req_interval > cum_err) {
req_interval = req_interval - cum_err;
} else {
req_interval = 0;
}
}
select_timeout = req_interval;
/*
* If we've exceeded our retry limit, then this host has timed out so
* remove it from the list. Otherwise, increase the timeout by the
* backoff factor if this is not the first packet sent to this host
* and send a packet.
*/
/* This message only works if the list is not empty */
if (verbose && (*cursor)->num_sent > pass_no)
warn_msg("---\tPass %d of %u completed", ++pass_no, retry);
if ((*cursor)->num_sent >= retry) {
if (verbose > 1)
warn_msg("---\tRemoving host entry %u (%s) - Timeout", (*cursor)->n, inet_ntoa((*cursor)->addr));
remove_host(cursor, &live_count, num_hosts); /* Automatically calls advance_cursor() */
if (first_timeout) {
timeval_diff(&now, &((*cursor)->last_send_time), &diff);
host_timediff = (IKE_UINT64)1000000*diff.tv_sec +
diff.tv_usec;
while (host_timediff >= (*cursor)->timeout && live_count) {
if ((*cursor)->live) {
if (verbose > 1)
warn_msg("---\tRemoving host %u (%s) - Timeout",
(*cursor)->n, inet_ntoa((*cursor)->addr));
remove_host(cursor, &live_count, num_hosts);
} else {
advance_cursor(live_count, num_hosts);
}
timeval_diff(&now, &((*cursor)->last_send_time), &diff);
host_timediff = (IKE_UINT64)1000000*diff.tv_sec +
diff.tv_usec;
}
first_timeout=0;
}
Gettimeofday(&last_packet_time);
} else { /* Retry limit not reached for this host */
if ((*cursor)->num_sent)
(*cursor)->timeout *= backoff_factor;
send_packet(sockfd, packet_out, packet_out_len, *cursor,
source_port, dest_port, &last_packet_time);
advance_cursor(live_count, num_hosts);
}
} else { /* We can't send a packet to this host yet */
/*
* Note that there is no point calling advance_cursor() here because if
* host n is not ready to send, then host n+1 will not be ready either.
*/
if (live_count)
select_timeout = (*cursor)->timeout - host_timediff;
else
select_timeout = interval;
reset_cum_err = 1; /* Zero cumulative error */
} /* End If */
} else { /* We can't send a packet yet */
select_timeout = req_interval - loop_timediff;
} /* End If */
#ifdef DEBUG_TIMINGS
printf("int=%d, loop_t=%llu, req_int=%d, sel=%d, cum_err=%d\n",
interval, loop_timediff, req_interval, select_timeout, cum_err);
#endif
n=recvfrom_wto(sockfd, packet_in, MAXUDP, (struct sockaddr *)&sa_peer,
select_timeout);
if (n != -1) {
/*
* We've received a response try to match up the packet by cookie
*
* Note: We start at cursor->prev because we call advance_cursor() after
* each send_packet().
*/
temp_cursor=find_host_by_cookie(cursor, packet_in, n, num_hosts);
if (temp_cursor) {
/*
* We found a cookie match for the returned packet.
*/
add_recv_time(temp_cursor, &last_recv_time);
if (verbose > 1)
warn_msg("---\tReceived packet #%u from %s",temp_cursor->num_recv ,inet_ntoa(sa_peer.sin_addr));
if (temp_cursor->live) {
display_packet(n, packet_in, temp_cursor, &(sa_peer.sin_addr),
&sa_responders, ¬ify_responders, quiet,
multiline);
if (verbose > 1)
warn_msg("---\tRemoving host entry %u (%s) - Received %d bytes", temp_cursor->n, inet_ntoa(sa_peer.sin_addr), n);
remove_host(&temp_cursor, &live_count, num_hosts);
}
} else {
struct isakmp_hdr hdr_in;
/*
* The received cookie doesn't match any entry in the list.
* Issue a message to that effect if verbose is on and ignore the packet.
*/