-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmdline.c
197 lines (175 loc) · 5.82 KB
/
cmdline.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
#include <cmdline.h>
#include <dns.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static void activate_debug(int argc, char* argv[])
{
unsigned i;
set_debug(false);
for (i = 0; i < argc; i++) {
if (strncmp(argv[i], "-v", 2) == 0) {
set_debug(true);
LOG_DEBUG("Verbose mode on\n");
}
}
}
static void usage(char* name)
{
printf("Usage: %s [-i <iface>] [-v] [-s <source>] [-d <delay>] ", name);
printf("[-t <timeout>] [-o <outfile>] [-n <domain name>] [-q <type>] ");
printf("[-c <class>] [-r] [-l <level>] [-p <name>] [-e] <addresses to scan>");
printf("\n\n");
}
static void parse_addresses(char* optarg, scanner_params_t* sp)
{
char* cidr;
char* addr;
char* mask;
uint32_t addr32;
uint32_t mask32;
char* saveptr1;
char* saveptr2;
uint32_t ip_start;
uint32_t ip_end;
char buf1[INET_ADDRSTRLEN];
char buf2[INET_ADDRSTRLEN];
cidr = strtok_r(optarg, ",", &saveptr1);
while (cidr != NULL) {
addr = strtok_r(cidr, "/", &saveptr2);
mask = strtok_r(saveptr2, "/", &saveptr2);
addr32 = ntohl(inet_addr(addr));
if (mask)
mask32 = atoi(mask);
else
mask32 = 32;
ip_start = htonl(addr32 & (0xFFFFFFFF << (32 - mask32)));
ip_end = htonl(addr32 | ~(0xFFFFFFFF << (32 - mask32)));
LOG_DEBUG("Loading range: %s - %s\n", inet_ntop(AF_INET, &ip_start, buf1,
INET_ADDRSTRLEN),
inet_ntop(AF_INET, &ip_end, buf2, INET_ADDRSTRLEN));
sp->ranges_count++;
if (sp->ranges == NULL || sizeof(sp->ranges) < sizeof(ip_range_t) * sp->ranges_count) {
sp->ranges = realloc(sp->ranges, sizeof(ip_range_t) * sp->ranges_count);
}
sp->ranges[sp->ranges_count - 1].ip_from = ip_start;
sp->ranges[sp->ranges_count - 1].ip_to = ip_end;
cidr = strtok_r(saveptr1, ",", &saveptr1);
}
}
int parse_cmdline(int argc, char* argv[], radar_params_t* rp, scanner_params_t* sp)
{
int opt;
int val;
if (argc == 1) {
usage(argv[0]);
return 1;
}
activate_debug(argc, argv);
while ((opt = getopt(argc, argv, "i:vs:d:t:o:n:p:q:c:hrl:e")) != -1) {
switch (opt) {
case 'i':
rp->dev = strdup(optarg);
LOG_INFO("Running on %s\n", rp->dev);
break;
case 'v':
break;
case 's':
LOG_INFO("Source: %s\n", optarg);
sp->saddr = inet_addr(optarg);
break;
case 'd':
val = strtol(optarg, (char**)NULL, 10);
if (val == LONG_MIN || val == LONG_MAX || val <= 0) {
LOG_ERROR("Can't convert %s to a valid int\n", optarg);
return 1;
}
sp->delay = val;
LOG_INFO("Delay: %u\n", sp->delay);
break;
case 't':
val = strtol(optarg, (char**)NULL, 10);
if (val == LONG_MIN || val == LONG_MAX || val <= 0) {
LOG_ERROR("Can't convert %s to a valid int\n", optarg);
return 1;
}
sp->timeout = val;
LOG_INFO("Timeout: %u\n", sp->timeout);
break;
case 'o':
rp->outfile = fopen(optarg, "w");
if (rp->outfile == NULL) {
LOG_ERROR("Can't open outfile: %s\n", optarg);
return 1;
}
LOG_INFO("Writing output to %s\n", optarg);
break;
case 'n':
free(sp->qname);
sp->qname = strdup(optarg);
break;
case 'p':
rp->pcap_dumper_name = strdup(optarg);
break;
case 'q':
val = strtol(optarg, (char**)NULL, 10);
if (val == LONG_MIN || val == LONG_MAX || val <= 0) {
val = 0;
val = string_to_qtype(optarg);
if (val == 0) {
LOG_ERROR("Can't convert %s to a valid qtype\n", optarg);
return 1;
}
}
sp->qtype = val;
LOG_INFO("Qtype: %s (%u)\n", optarg, sp->qtype);
break;
case 'c':
val = strtol(optarg, (char**)NULL, 10);
if (val == LONG_MIN || val == LONG_MAX || val <= 0) {
val = 0;
val = string_to_qclass(optarg);
if (val == 0) {
LOG_ERROR("Can't convert %s to a valid int\n", optarg);
return 1;
}
}
sp->qclass = val;
LOG_INFO("Qclass: %s (%u)\n", optarg, sp->qclass);
break;
case 'h':
usage(argv[0]);
return 1;
break;
case 'r':
sp->randomize = false;
break;
case 'l':
val = strtol(optarg, (char**)NULL, 10);
if (val == LONG_MIN || val == LONG_MAX || val <= 0) {
LOG_ERROR("Can't convert %s to a valid int\n", optarg);
return 1;
}
rp->level = val;
LOG_INFO("Minimum amplification ratio: %u\n", rp->level);
break;
case 'e':
sp->edns0 = false;
LOG_INFO("EDNS0 record disabled\n");
break;
default:
LOG_ERROR("Error parsing command line\n");
return 1;
}
}
// The rest of the cmdline contains the addresses to scan
if (argc == optind) {
usage(argv[0]);
return 1;
}
parse_addresses(argv[optind], sp);
return 0;
}