forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-conf.c
2159 lines (1954 loc) · 76.1 KB
/
main-conf.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
/*
Read in the configuration for MASSCAN.
Configuration parameters can be read either from the command-line
or a configuration file. Long parameters of the --xxxx variety have
the same name in both.
Most of the code in this module is for 'nmap' options we don't support.
That's because we support some 'nmap' options, and I wanted to give
more feedback for some of them why they don't work as expected, such
as reminding people that this is an asynchronous scanner.
*/
#include "masscan.h"
#include "masscan-version.h"
#include "ranges.h"
#include "string_s.h"
#include "logger.h"
#include "proto-banner1.h"
#include "templ-payloads.h"
#include "templ-port.h"
#include "crypto-base64.h"
#include "script.h"
#include "masscan-app.h"
#include <ctype.h>
#include <limits.h>
/***************************************************************************
***************************************************************************/
/*static struct Range top_ports_tcp[] = {
{80, 80},{23, 23}, {443,443},{21,22},{25,25},{3389,3389},{110,110},
{445,445},
};
static struct Range top_ports_udp[] = {
{161, 161}, {631, 631}, {137,138},{123,123},{1434},{445,445},{135,135},
{67,67},
};
static struct Range top_ports_sctp[] = {
{7, 7},{9, 9},{20,22},{80,80},{179,179},{443,443},{1167,1167},
};*/
/***************************************************************************
***************************************************************************/
void
masscan_usage(void)
{
printf("usage:\n");
printf("masscan -p80,8000-8100 10.0.0.0/8 --rate=10000\n");
printf(" scan some web ports on 10.x.x.x at 10kpps\n");
printf("masscan --nmap\n");
printf(" list those options that are compatible with nmap\n");
printf("masscan -p80 10.0.0.0/8 --banners -oB <filename>\n");
printf(" save results of scan in binary format to <filename>\n");
printf("masscan --open --banners --readscan <filename> -oX <savefile>\n");
printf(" read binary scan results in <filename> and save them as xml in <savefile>\n");
exit(1);
}
/***************************************************************************
***************************************************************************/
static void
print_version()
{
const char *cpu = "unknown";
const char *compiler = "unknown";
const char *compiler_version = "unknown";
const char *os = "unknown";
printf("\n");
printf("Masscan version %s ( %s )\n",
MASSCAN_VERSION,
"https://github.com/robertdavidgraham/masscan"
);
printf("Compiled on: %s %s\n", __DATE__, __TIME__);
#if defined(_MSC_VER)
#if defined(_M_AMD64) || defined(_M_X64)
cpu = "x86";
#elif defined(_M_IX86)
cpu = "x86";
#elif defined (_M_ARM_FP)
cpu = "arm";
#endif
{
int msc_ver = _MSC_VER;
compiler = "VisualStudio";
if (msc_ver < 1500)
compiler_version = "pre2008";
else if (msc_ver == 1500)
compiler_version = "2008";
else if (msc_ver == 1600)
compiler_version = "2010";
else if (msc_ver == 1700)
compiler_version = "2012";
else if (msc_ver == 1800)
compiler_version = "2013";
else
compiler_version = "post-2013";
}
#elif defined(__GNUC__)
compiler = "gcc";
compiler_version = __VERSION__;
#if defined(i386) || defined(__i386) || defined(__i386__)
cpu = "x86";
#endif
#if defined(__corei7) || defined(__corei7__)
cpu = "x86-Corei7";
#endif
#endif
#if defined(WIN32)
os = "Windows";
#elif defined(__linux__)
os = "Linux";
#elif defined(__APPLE__)
os = "Apple";
#elif defined(__MACH__)
os = "MACH";
#elif defined(__FreeBSD__)
os = "FreeBSD";
#elif defined(unix) || defined(__unix) || defined(__unix__)
os = "Unix";
#endif
printf("Compiler: %s %s\n", compiler, compiler_version);
printf("OS: %s\n", os);
printf("CPU: %s (%u bits)\n", cpu, (unsigned)(sizeof(void*))*8);
#if defined(GIT)
printf("GIT version: %s\n", GIT);
#endif
}
/***************************************************************************
***************************************************************************/
static void
print_nmap_help(void)
{
printf("Masscan (https://github.com/robertdavidgraham/masscan)\n"
"Usage: masscan [Options] -p{Target-Ports} {Target-IP-Ranges}\n"
"TARGET SPECIFICATION:\n"
" Can pass only IPv4 address, CIDR networks, or ranges (non-nmap style)\n"
" Ex: 10.0.0.0/8, 192.168.0.1, 10.0.0.1-10.0.0.254\n"
" -iL <inputfilename>: Input from list of hosts/networks\n"
" --exclude <host1[,host2][,host3],...>: Exclude hosts/networks\n"
" --excludefile <exclude_file>: Exclude list from file\n"
" --randomize-hosts: Randomize order of hosts (default)\n"
"HOST DISCOVERY:\n"
" -Pn: Treat all hosts as online (default)\n"
" -n: Never do DNS resolution (default)\n"
"SCAN TECHNIQUES:\n"
" -sS: TCP SYN (always on, default)\n"
"SERVICE/VERSION DETECTION:\n"
" --banners: get the banners of the listening service if available. The\n"
" default timeout for waiting to recieve data is 30 seconds.\n"
"PORT SPECIFICATION AND SCAN ORDER:\n"
" -p <port ranges>: Only scan specified ports\n"
" Ex: -p22; -p1-65535; -p 111,137,80,139,8080\n"
"TIMING AND PERFORMANCE:\n"
" --max-rate <number>: Send packets no faster than <number> per second\n"
" --connection-timeout <number>: time in seconds a TCP connection will\n"
" timeout while waiting for banner data from a port.\n"
"FIREWALL/IDS EVASION AND SPOOFING:\n"
" -S/--source-ip <IP_Address>: Spoof source address\n"
" -e <iface>: Use specified interface\n"
" -g/--source-port <portnum>: Use given port number\n"
" --ttl <val>: Set IP time-to-live field\n"
" --spoof-mac <mac address/prefix/vendor name>: Spoof your MAC address\n"
"OUTPUT:\n"
" --output-format <format>: Sets output to binary/list/unicornscan/json/grepable/xml\n"
" --output-file <file>: Write scan results to file. If --output-format is\n"
" not given default is xml\n"
" -oL/-oJ/-oG/-oB/-oX/-oU <file>: Output scan in List/JSON/Grepable/Binary/XML/Unicornscan format,\n"
" respectively, to the given filename. Shortcut for\n"
" --output-format <format> --output-file <file>\n"
" -v: Increase verbosity level (use -vv or more for greater effect)\n"
" -d: Increase debugging level (use -dd or more for greater effect)\n"
" --open: Only show open (or possibly open) ports\n"
" --packet-trace: Show all packets sent and received\n"
" --iflist: Print host interfaces and routes (for debugging)\n"
" --append-output: Append to rather than clobber specified output files\n"
" --resume <filename>: Resume an aborted scan\n"
"MISC:\n"
" --send-eth: Send using raw ethernet frames (default)\n"
" -V: Print version number\n"
" -h: Print this help summary page.\n"
"EXAMPLES:\n"
" masscan -v -sS 192.168.0.0/16 10.0.0.0/8 -p 80\n"
" masscan 23.0.0.0/0 -p80 --banners -output-format binary --output-filename internet.scan\n"
" masscan --open --banners --readscan internet.scan -oG internet_scan.grepable\n"
"SEE (https://github.com/robertdavidgraham/masscan) FOR MORE HELP\n"
"\n");
}
/***************************************************************************
***************************************************************************/
static unsigned
count_cidr_bits(struct Range range)
{
unsigned i;
for (i=0; i<32; i++) {
unsigned mask = 0xFFFFFFFF >> i;
if ((range.begin & ~mask) == (range.end & ~mask)) {
if ((range.begin & mask) == 0 && (range.end & mask) == mask)
return i;
}
}
return 0;
}
/***************************************************************************
* Echoes the configuration for one nic
***************************************************************************/
static void
masscan_echo_nic(struct Masscan *masscan, FILE *fp, unsigned i)
{
char zzz[64];
/* If we have only one adapter, then don't print the array indexes.
* Otherwise, we need to print the array indexes to distinguish
* the NICs from each other */
if (masscan->nic_count <= 1)
zzz[0] = '\0';
else
sprintf_s(zzz, sizeof(zzz), "[%u]", i);
fprintf(fp, "adapter%s = %s\n", zzz, masscan->nic[i].ifname);
if (masscan->nic[i].src.ip.first == masscan->nic[i].src.ip.last)
fprintf(fp, "adapter-ip%s = %u.%u.%u.%u\n", zzz,
(masscan->nic[i].src.ip.first>>24)&0xFF,
(masscan->nic[i].src.ip.first>>16)&0xFF,
(masscan->nic[i].src.ip.first>> 8)&0xFF,
(masscan->nic[i].src.ip.first>> 0)&0xFF
);
else
fprintf(fp, "adapter-ip%s = %u.%u.%u.%u-%u.%u.%u.%u\n", zzz,
(masscan->nic[i].src.ip.first>>24)&0xFF,
(masscan->nic[i].src.ip.first>>16)&0xFF,
(masscan->nic[i].src.ip.first>> 8)&0xFF,
(masscan->nic[i].src.ip.first>> 0)&0xFF,
(masscan->nic[i].src.ip.last>>24)&0xFF,
(masscan->nic[i].src.ip.last>>16)&0xFF,
(masscan->nic[i].src.ip.last>> 8)&0xFF,
(masscan->nic[i].src.ip.last>> 0)&0xFF
);
fprintf(fp, "adapter-mac%s = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
masscan->nic[i].my_mac[0],
masscan->nic[i].my_mac[1],
masscan->nic[i].my_mac[2],
masscan->nic[i].my_mac[3],
masscan->nic[i].my_mac[4],
masscan->nic[i].my_mac[5]);
if (masscan->nic[i].router_ip) {
fprintf(fp, "router-ip%s = %u.%u.%u.%u\n", zzz,
(masscan->nic[i].router_ip>>24)&0xFF,
(masscan->nic[i].router_ip>>16)&0xFF,
(masscan->nic[i].router_ip>> 8)&0xFF,
(masscan->nic[i].router_ip>> 0)&0xFF
);
} else
fprintf(fp, "router-mac%s = %02x:%02x:%02x:%02x:%02x:%02x\n", zzz,
masscan->nic[i].router_mac[0],
masscan->nic[i].router_mac[1],
masscan->nic[i].router_mac[2],
masscan->nic[i].router_mac[3],
masscan->nic[i].router_mac[4],
masscan->nic[i].router_mac[5]);
}
/***************************************************************************
* Prints the current configuration to the command-line then exits.
* Use#1: create a template file of all setable parameters.
* Use#2: make sure your configuration was interpreted correctly.
***************************************************************************/
static void
masscan_echo(struct Masscan *masscan, FILE *fp)
{
unsigned i;
fprintf(fp, "rate = %10.2f\n", masscan->max_rate);
fprintf(fp, "randomize-hosts = true\n");
fprintf(fp, "seed = %" PRIu64 "\n", masscan->seed);
fprintf(fp, "shard = %u/%u\n", masscan->shard.one, masscan->shard.of);
if (masscan->is_banners)
fprintf(fp, "banners = true\n");
fprintf(fp, "# ADAPTER SETTINGS\n");
if (masscan->nic_count == 0)
masscan_echo_nic(masscan, fp, 0);
else {
for (i=0; i<masscan->nic_count; i++)
masscan_echo_nic(masscan, fp, i);
}
/*
* Output information
*/
fprintf(fp, "# OUTPUT/REPORTING SETTINGS\n");
switch (masscan->output.format) {
case Output_Interactive:fprintf(fp, "output-format = interactive\n"); break;
case Output_List: fprintf(fp, "output-format = list\n"); break;
case Output_Unicornscan:fprintf(fp, "output-format = unicornscan\n"); break;
case Output_XML: fprintf(fp, "output-format = xml\n"); break;
case Output_Binary: fprintf(fp, "output-format = binary\n"); break;
case Output_Grepable: fprintf(fp, "output-format = grepable\n"); break;
case Output_JSON: fprintf(fp, "output-format = json\n"); break;
case Output_Certs: fprintf(fp, "output-format = certs\n"); break;
case Output_None: fprintf(fp, "output-format = none\n"); break;
case Output_Redis:
fprintf(fp, "output-format = redis\n");
fprintf(fp, "redis = %u.%u.%u.%u:%u\n",
(unsigned char)(masscan->redis.ip>>24),
(unsigned char)(masscan->redis.ip>>16),
(unsigned char)(masscan->redis.ip>> 8),
(unsigned char)(masscan->redis.ip>> 0),
masscan->redis.port);
break;
default:
fprintf(fp, "output-format = unknown(%u)\n", masscan->output.format);
break;
}
fprintf(fp, "show = %s,%s,%s\n",
masscan->output.is_show_open?"open":"",
masscan->output.is_show_closed?"closed":"",
masscan->output.is_show_host?"host":""
);
if (!masscan->output.is_show_open)
fprintf(fp, "noshow = open\n");
fprintf(fp, "output-filename = %s\n", masscan->output.filename);
if (masscan->output.is_append)
fprintf(fp, "output-append = true\n");
fprintf(fp, "rotate = %u\n", masscan->output.rotate.timeout);
fprintf(fp, "rotate-dir = %s\n", masscan->output.rotate.directory);
fprintf(fp, "rotate-offset = %u\n", masscan->output.rotate.offset);
fprintf(fp, "rotate-filesize = %" PRIu64 "\n", masscan->output.rotate.filesize);
fprintf(fp, "pcap = %s\n", masscan->pcap_filename);
/*
* Targets
*/
for (i=0; i<masscan->ports.count; i++) {
struct Range range = masscan->ports.list[i];
if ( (range.begin == range.end) && (range.begin == Templ_ICMP_echo) )
{
fprintf(fp,"ping = true\n");
break;
}
}
fprintf(fp, "# TARGET SELECTION (IP, PORTS, EXCLUDES)\n");
fprintf(fp, "ports = ");
for (i=0; i<masscan->ports.count; i++) {
struct Range range = masscan->ports.list[i];
if (range.begin == range.end)
fprintf(fp, "%u", range.begin);
else
fprintf(fp, "%u-%u", range.begin, range.end);
if (i+1 < masscan->ports.count)
fprintf(fp, ",");
}
fprintf(fp, "\n");
for (i=0; i<masscan->targets.count; i++) {
struct Range range = masscan->targets.list[i];
fprintf(fp, "range = ");
fprintf(fp, "%u.%u.%u.%u",
(range.begin>>24)&0xFF,
(range.begin>>16)&0xFF,
(range.begin>> 8)&0xFF,
(range.begin>> 0)&0xFF
);
if (range.begin != range.end) {
unsigned cidr_bits = count_cidr_bits(range);
if (cidr_bits) {
fprintf(fp, "/%u", cidr_bits);
} else
fprintf(fp, "-%u.%u.%u.%u",
(range.end>>24)&0xFF,
(range.end>>16)&0xFF,
(range.end>> 8)&0xFF,
(range.end>> 0)&0xFF
);
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
if (masscan->http_user_agent)
fprintf( fp,
"http-user-agent = %.*s\n",
masscan->http_user_agent_length,
masscan->http_user_agent);
for (i=0; i<sizeof(masscan->http_headers)/sizeof(masscan->http_headers[0]); i++) {
if (masscan->http_headers[i].header_name == 0)
continue;
fprintf( fp,
"http-header[%s] = %.*s\n",
masscan->http_headers[i].header_name,
masscan->http_headers[i].header_value_length,
masscan->http_headers[i].header_value);
}
fprintf(fp, "%scapture = cert\n", masscan->is_capture_cert?"":"no");
fprintf(fp, "%scapture = html\n", masscan->is_capture_html?"":"no");
fprintf(fp, "%scapture = heartbleed\n", masscan->is_capture_heartbleed?"":"no");
/*
* TCP payloads
*/
fprintf(fp, "\n");
fprintf(fp, "min-packet = %u\n", masscan->min_packet_size);
{
struct TcpCfgPayloads *pay;
for (pay = masscan->tcp_payloads; pay; pay = pay->next) {
fprintf(fp, "hello-string[%u] = %s\n",
pay->port, pay->payload_base64);
}
}
}
/***************************************************************************
***************************************************************************/
void
masscan_save_state(struct Masscan *masscan)
{
char filename[512];
FILE *fp;
int err;
strcpy_s(filename, sizeof(filename), "paused.conf");
fprintf(stderr, " "
" \r");
fprintf(stderr, "saving resume file to: %s\n", filename);
err = fopen_s(&fp, filename, "wt");
if (err) {
perror(filename);
return;
}
fprintf(fp, "\n# resume information\n");
fprintf(fp, "resume-index = %" PRIu64 "\n", masscan->resume.index);
masscan_echo(masscan, fp);
fclose(fp);
}
/*****************************************************************************
* Read in ranges from a file
*
* There can be multiple ranges on a line, delimited by spaces. In fact,
* millions of ranges can be on a line: there is limit to the line length.
* That makes reading the file a little bit squirrelly. From one perspective
* this parser doesn't treat the new-line '\n' any different than other
* space. But, from another perspective, it has to, because things like
* comments are terminated by a newline. Also, it has to count the number
* of lines correctly to print error messages.
*****************************************************************************/
static void
ranges_from_file(struct RangeList *ranges, const char *filename)
{
FILE *fp;
errno_t err;
unsigned line_number = 0;
err = fopen_s(&fp, filename, "rt");
if (err) {
perror(filename);
exit(1); /* HARD EXIT: because if it's an exclusion file, we don't
* want to continue. We don't want ANY chance of
* accidentally scanning somebody */
}
while (!feof(fp)) {
int c = '\n';
/* remove leading whitespace */
while (!feof(fp)) {
c = getc(fp);
line_number += (c == '\n');
if (!isspace(c&0xFF))
break;
}
/* If this is a punctuation, like '#', then it's a comment */
if (ispunct(c&0xFF)) {
while (!feof(fp)) {
c = getc(fp);
line_number += (c == '\n');
if (c == '\n') {
break;
}
}
/* Loop back to the begining state at the start of a line */
continue;
}
if (c == '\n') {
continue;
}
/*
* Read in a single entry
*/
if (!feof(fp)) {
char address[64];
size_t i;
struct Range range;
unsigned offset = 0;
/* Grab all bytes until the next space or comma */
address[0] = (char)c;
i = 1;
while (!feof(fp)) {
c = getc(fp);
line_number += (c == '\n');
if (isspace(c&0xFF) || c == ',') {
break;
}
if (i+1 >= sizeof(address)) {
LOG(0, "%s:%u:%u: bad address spec: \"%.*s\"\n",
filename, line_number, offset, i, address);
exit(1);
} else
address[i] = (char)c;
i++;
}
address[i] = '\0';
/* parse the address range */
range = range_parse_ipv4(address, &offset, (unsigned)i);
if (range.begin == 0xFFFFFFFF && range.end == 0) {
LOG(0, "%s:%u:%u: bad range spec: \"%.*s\"\n",
filename, line_number, offset, i, address);
exit(1);
} else {
rangelist_add_range(ranges, range.begin, range.end);
}
}
}
fclose(fp);
}
/***************************************************************************
***************************************************************************/
static unsigned
hexval(char c)
{
if ('0' <= c && c <= '9')
return (unsigned)(c - '0');
if ('a' <= c && c <= 'f')
return (unsigned)(c - 'a' + 10);
if ('A' <= c && c <= 'F')
return (unsigned)(c - 'A' + 10);
return 0xFF;
}
/***************************************************************************
***************************************************************************/
static int
parse_mac_address(const char *text, unsigned char *mac)
{
unsigned i;
for (i=0; i<6; i++) {
unsigned x;
char c;
while (isspace(*text & 0xFF) && ispunct(*text & 0xFF))
text++;
c = *text;
if (!isxdigit(c&0xFF))
return -1;
x = hexval(c)<<4;
text++;
c = *text;
if (!isxdigit(c&0xFF))
return -1;
x |= hexval(c);
text++;
mac[i] = (unsigned char)x;
if (ispunct(*text & 0xFF))
text++;
}
return 0;
}
/***************************************************************************
***************************************************************************/
static uint64_t
parseInt(const char *str)
{
uint64_t result = 0;
while (*str && isdigit(*str & 0xFF)) {
result = result * 10 + (*str - '0');
str++;
}
return result;
}
/***************************************************************************
* Parses the number of seconds (for rotating files mostly). We do a little
* more than just parse an integer. We support strings like:
*
* hourly
* daily
* Week
* 5days
* 10-months
* 3600
***************************************************************************/
static uint64_t
parseTime(const char *value)
{
uint64_t num = 0;
unsigned is_negative = 0;
while (*value == '-') {
is_negative = 1;
value++;
}
while (isdigit(value[0]&0xFF)) {
num = num*10 + (value[0] - '0');
value++;
}
while (ispunct(value[0]) || isspace(value[0]))
value++;
if (isalpha(value[0]) && num == 0)
num = 1;
if (value[0] == '\0')
return num;
switch (tolower(value[0])) {
case 's':
num *= 1;
break;
case 'm':
num *= 60;
break;
case 'h':
num *= 60*60;
break;
case 'd':
num *= 24*60*60;
break;
case 'w':
num *= 24*60*60*7;
break;
default:
fprintf(stderr, "--rotate-offset: unknown character\n");
exit(1);
}
if (num >= 24*60*60) {
fprintf(stderr, "--rotate-offset: value is greater than 1 day\n");
exit(1);
}
if (is_negative)
num = 24*60*60 - num;
return num;
}
/***************************************************************************
* Parses a size integer, which can be suffixed with "tera", "giga",
* "mega", and "kilo". These numbers are in units of 1024 so suck it.
***************************************************************************/
static uint64_t
parseSize(const char *value)
{
uint64_t num = 0;
while (isdigit(value[0]&0xFF)) {
num = num*10 + (value[0] - '0');
value++;
}
while (ispunct(value[0]) || isspace(value[0]))
value++;
if (isalpha(value[0]) && num == 0)
num = 1;
if (value[0] == '\0')
return num;
switch (tolower(value[0])) {
case 'k': /* kilobyte */
num *= 1024ULL;
break;
case 'm': /* megabyte */
num *= 1024ULL * 1024ULL;
break;
case 'g': /* gigabyte */
num *= 1024ULL * 1024ULL * 1024ULL;
break;
case 't': /* terabyte, 'cause we roll that way */
num *= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
break;
case 'p': /* petabyte, 'cause we are awesome */
num *= 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL;
break;
case 'e': /* exabyte, now that's just silly */
num *= 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL;
break;
default:
fprintf(stderr, "--rotate-size: unknown character\n");
exit(1);
}
return num;
}
/***************************************************************************
***************************************************************************/
static int
is_power_of_two(uint64_t x)
{
while ((x&1) == 0)
x >>= 1;
return x == 1;
}
/***************************************************************************
* Tests if the named parameter on the command-line. We do a little
* more than a straight string compare, because I get confused
* whether parameter have punctuation. Is it "--excludefile" or
* "--exclude-file"? I don't know if it's got that dash. Screw it,
* I'll just make the code so it don't care.
***************************************************************************/
static int
EQUALS(const char *lhs, const char *rhs)
{
for (;;) {
while (*lhs == '-' || *lhs == '.' || *lhs == '_')
lhs++;
while (*rhs == '-' || *rhs == '.' || *rhs == '_')
rhs++;
if (*lhs == '\0' && *rhs == '[')
return 1; /*arrays*/
if (tolower(*lhs & 0xFF) != tolower(*rhs & 0xFF))
return 0;
if (*lhs == '\0')
return 1;
lhs++;
rhs++;
}
}
static int
EQUALSx(const char *lhs, const char *rhs, size_t rhs_length)
{
for (;;) {
while (*lhs == '-' || *lhs == '.' || *lhs == '_')
lhs++;
while (*rhs == '-' || *rhs == '.' || *rhs == '_')
rhs++;
if (*lhs == '\0' && *rhs == '[')
return 1; /*arrays*/
if (tolower(*lhs & 0xFF) != tolower(*rhs & 0xFF))
return 0;
if (*lhs == '\0')
return 1;
lhs++;
rhs++;
if (--rhs_length == 0)
return 1;
}
}
static unsigned
INDEX_OF(const char *str, char c)
{
unsigned i;
for (i=0; str[i] && str[i] != c; i++)
;
return i;
}
static unsigned
ARRAY(const char *rhs)
{
const char *p = strchr(rhs, '[');
if (p == NULL)
return 0;
else
p++;
return (unsigned)parseInt(p);
}
/***************************************************************************
* Called either from the "command-line" parser when it sees a --parm,
* or from the "config-file" parser for normal options.
***************************************************************************/
static void
masscan_set_parameter(struct Masscan *masscan,
const char *name, const char *value)
{
unsigned index = ARRAY(name);
if (index >= 65536) {
fprintf(stderr, "%s: bad index\n", name);
exit(1);
}
if (EQUALS("conf", name) || EQUALS("config", name)) {
masscan_read_config_file(masscan, value);
} else if (EQUALS("adapter", name) || EQUALS("if", name) || EQUALS("interface", name)) {
if (masscan->nic[index].ifname[0]) {
fprintf(stderr, "CONF: overwriting \"adapter=%s\"\n", masscan->nic[index].ifname);
}
if (masscan->nic_count < index + 1)
masscan->nic_count = index + 1;
sprintf_s( masscan->nic[index].ifname,
sizeof(masscan->nic[index].ifname),
"%s",
value);
}
else if (EQUALS("adapter-ip", name) || EQUALS("source-ip", name)
|| EQUALS("source-address", name) || EQUALS("spoof-ip", name)
|| EQUALS("spoof-address", name) || EQUALS("src-ip", name)) {
/* Send packets FROM this IP address */
struct Range range;
range = range_parse_ipv4(value, 0, 0);
/* Check for bad format */
if (range.begin > range.end) {
LOG(0, "FAIL: bad source IPv4 address: %s=%s\n",
name, value);
LOG(0, "hint addresses look like \"19.168.1.23\"\n");
exit(1);
}
/* If more than one IP address given, make the range is
* an even power of two (1, 2, 4, 8, 16, ...) */
if (!is_power_of_two((uint64_t)range.end - range.begin + 1)) {
LOG(0, "FAIL: range must be even power of two: %s=%s\n",
name, value);
exit(1);
}
masscan->nic[index].src.ip.first = range.begin;
masscan->nic[index].src.ip.last = range.end;
masscan->nic[index].src.ip.range = (uint64_t)range.end - range.begin + 1;
} else if (EQUALS("adapter-port", name) || EQUALS("source-port", name)
|| EQUALS("src-port", name)) {
/* Send packets FROM this port number */
unsigned is_error = 0;
struct RangeList ports;
memset(&ports, 0, sizeof(ports));
rangelist_parse_ports(&ports, value, &is_error);
/* Check if there was an error in parsing */
if (is_error) {
LOG(0, "FAIL: bad source port specification: %s\n",
name);
exit(1);
}
/* Only allow one range of ports */
if (ports.count != 1) {
LOG(0, "FAIL: only one source port range may be specified: %s\n",
name);
exit(1);
}
/* verify range is even power of 2 (1, 2, 4, 8, 16, ...) */
if (!is_power_of_two(ports.list[0].end - ports.list[0].begin + 1)) {
LOG(0, "FAIL: source port range must be even power of two: %s=%s\n",
name, value);
exit(1);
}
masscan->nic[index].src.port.first = ports.list[0].begin;
masscan->nic[index].src.port.last = ports.list[0].end;
masscan->nic[index].src.port.range = ports.list[0].end - ports.list[0].begin + 1;
} else if (EQUALS("adapter-mac", name) || EQUALS("spoof-mac", name)
|| EQUALS("source-mac", name) || EQUALS("src-mac", name)) {
/* Send packets FROM this MAC address */
unsigned char mac[6];
if (parse_mac_address(value, mac) != 0) {
fprintf(stderr, "CONF: bad MAC address: %s=%s\n", name, value);
return;
}
/* Check for duplicates */
if (memcmp(masscan->nic[index].my_mac, mac, 6) == 0)
return;
/* Warn if we are overwriting a Mac address */
if (masscan->nic[index].my_mac_count != 0) {
LOG(0, "WARNING: overwriting MAC address\n");
}
memcpy(masscan->nic[index].my_mac, mac, 6);
masscan->nic[index].my_mac_count = 1;
}
else if (EQUALS("router-mac", name) || EQUALS("router", name)
|| EQUALS("dest-mac", name) || EQUALS("destination-mac", name)
|| EQUALS("dst-mac", name) || EQUALS("target-mac", name)) {
unsigned char mac[6];
if (parse_mac_address(value, mac) != 0) {
fprintf(stderr, "CONF: bad MAC address: %s=%s\n", name, value);
return;
}
memcpy(masscan->nic[index].router_mac, mac, 6);
}
else if (EQUALS("router-ip", name)) {
/* Send packets FROM this IP address */
struct Range range;
range = range_parse_ipv4(value, 0, 0);
/* Check for bad format */
if (range.begin != range.end) {
LOG(0, "FAIL: bad source IPv4 address: %s=%s\n",
name, value);
LOG(0, "hint addresses look like \"19.168.1.23\"\n");
exit(1);
}
masscan->nic[index].router_ip = range.begin;
}
else if (EQUALS("rate", name) || EQUALS("max-rate", name) ) {
double rate = 0.0;
double point = 10.0;
unsigned i;
for (i=0; value[i] && value[i] != '.'; i++) {
char c = value[i];
if (c < '0' || '9' < c) {
fprintf(stderr, "CONF: non-digit in rate spec: %s=%s\n", name, value);
return;
}
rate = rate * 10.0 + (c - '0');
}
if (value[i] == '.') {
i++;
while (value[i]) {
char c = value[i];
if (c < '0' || '9' < c) {
fprintf(stderr, "CONF: non-digit in rate spec: %s=%s\n",
name, value);
return;
}
rate += (c - '0')/point;
point /= 10.0;
value++;
}
}
masscan->max_rate = rate;
}
else if (EQUALS("ports", name) || EQUALS("port", name)
|| EQUALS("dst-port", name) || EQUALS("dest-port", name)
|| EQUALS("destination-port", name)
|| EQUALS("target-port", name)) {
unsigned is_error = 0;