-
Notifications
You must be signed in to change notification settings - Fork 4
/
getopts.c
363 lines (286 loc) · 6.39 KB
/
getopts.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
#include "settings.h"
#include "ext/xmalloc.h"
#include "getopts.h"
#define GETOPT "4" "6" "a:" "b:" "c:" "d" "e" "D" "f:" "F" "h" "H:" "r" "s" "S" "v" "V"
static void display_version(void) __attribute__((noreturn));
int get_opts_argv(int argc, char **argv) {
extern char *optarg;
extern int optind;
char bob[128];
int ch=0, j=0;
unsigned int idx=0;
while ((ch=getopt(argc, argv, GETOPT)) != -1) {
switch (ch) {
case '4': /* XXX */
if (set_addrfam("ipv4") != 1) {
usage();
}
break;
case '6': /* XXX */
if (set_addrfam("ipv6") != 1) {
usage();
}
break;
case 'a':
#if HAVE_C_ARES == 1
if (set_async(optarg) != 1) {
usage();
}
#else
ERR("No async dns support compiled in");
usage();
#endif
break;
case 'b':
memset(bob, 0, sizeof(bob));
strcat(bob, "brute-");
strncat(bob, optarg, sizeof(bob) - 7 /* brute- + STR + \0 */);
if (set_mode(bob) != 1) {
usage();
}
break;
case 'c':
if (set_allowed(optarg) != 1) {
usage();
}
break;
case 'd':
if (set_discover(1) != 1) {
usage();
}
break;
case 'D':
if (set_mode("dict") != 1) {
usage();
}
break;
case 'e':
if (set_exact(1) != 1) {
usage();
}
break;
case 'f':
if (set_dictfile(optarg) != 1) {
usage();
}
break;
case 'F':
if (set_filter(0) != 1) {
usage();
}
break;
case 'H':
if (set_sillyhostname(optarg) != 1) {
usage();
}
break;
case 'r':
if (set_mode("rev") != 1) {
usage();
}
break;
case 's':
if (set_outsql(1) != 1) {
usage();
}
break;
case 'S':
if (set_async("no") != 1) {
usage();
}
break;
case 'v':
if (set_verbose(s->verbose + 1) != 1) {
usage();
}
break;
case 'V':
display_version();
break;
case 'h':
/* fall though */
default:
usage();
} /* switch */
}
assert(argc >= optind);
if (argc - optind == 0) {
ERR("nothing to lookup! (-h for help)");
exit(1);
}
memset(s->targets, 0, sizeof(char *) * MAX_TARGETS);
assert(MAX_TARGETS > (argc - optind));
for ( j=optind; j < argc; j++) {
s->targets[idx++]=xstrdup(argv[j]);
}
s->targets[idx]=NULL;
return 1;
}
static void display_version(void) {
OUT("%s %s", PROGNAME, PACKAGE_VERSION);
exit(0);
}
void usage(void) {
OUT("Usage: %s (%s %s)\n"
"\t-4\t only scan ipv4 forward lookups\n"
"\t-6\t only scan ipv6 lookups\n"
"\t-a\t* async mode (if compiled in) with a numeric argument of concurrency (%u default)\n"
"\t-b\t* brute force hostnames from X to Y chars (x-y)\n"
"\t-c\t* characters allowed in brute force mode\n"
"\t-d\t discover more information based upon found information\n"
"\t-D\t dictionary mode (default)\n"
"\t-e\t exact information returned is displayed, not asked for information\n"
"\t-f\t* wordlist to lookup hosts from\n"
"\t-F\t do not filter output for unique results\n"
"\t-h\t help (you are here)\n"
"\t-H\t* silly hostname to use to detect wildcards (otherwise itll be random)\n"
"\t-r\t reverse dns scan mode\n"
"\t-s\t SQL output mode\n"
"\t-S\t force SYNC scanning mode (if async mode is available, it is default)\n"
"\t-v\t verbose (more for more)\n"
"\t-V\t display program version\n"
, GETOPT, PROGNAME, PACKAGE_VERSION, DEF_CONCURRENCY);
exit(0);
}
int set_allowed(const char *str) {
if (str == NULL || strlen(str) < 1) {
return -1;
}
if (s->allowed != NULL) {
xfree(s->allowed);
}
s->allowed=xstrdup(str);
return 1;
}
int set_sillyhostname(const char *name) {
if (name == NULL || strlen(name) < 1) {
return -1;
}
if (s->sillyhostname != NULL) {
xfree(s->sillyhostname);
}
s->sillyhostname=xstrdup(name);
return 1;
}
int set_addrfam(const char *addrfams) {
s->ipv4_lookup=0;
s->ipv6_lookup=0;
if (addrfams == NULL || strlen(addrfams) < 1) {
ERR("no address family to set, use ipv4|ipv6|ipv46");
return -1;
}
if (strcasecmp(addrfams, "ipv4") == 0) {
s->ipv4_lookup=1;
}
else if (strcasecmp(addrfams, "ipv6") == 0) {
s->ipv6_lookup=1;
}
else if (strcasecmp(addrfams, "ipv46") == 0 || strcasecmp(addrfams, "all") == 0) {
s->ipv4_lookup=s->ipv6_lookup=1;
}
else {
ERR("unknown address family `%s', try ipv4|ipv6|ipv46", addrfams);
return -1;
}
return 1;
}
int set_mode(const char *bstr) {
assert(bstr != NULL);
if (sscanf(bstr, "brute-%d-%d", &s->brute_lenmin, &s->brute_lenmax) == 2) {
if (s->brute_lenmin > s->brute_lenmax) {
int tmp=0;
tmp=s->brute_lenmax;
s->brute_lenmax=s->brute_lenmin;
s->brute_lenmin=tmp;
}
if (s->brute_lenmax < 1 || s->brute_lenmin < 1) {
ERR("bad brute length parameters");
return -1;
}
if (s->brute_lenmax > MAX_BRUTELEN) {
ERR("who has that much time?");
return -1;
}
s->mode=MODE_BRUTE;
}
else if (sscanf(bstr, "brute-%d", &s->brute_lenmax) == 1) {
s->brute_lenmin=1;
if (s->brute_lenmax < 1) {
ERR("brute length is less than one");
return -1;
}
/* else could be assumed ;] */
if (s->brute_lenmax > MAX_BRUTELEN) {
ERR("who has that much time?");
return -1;
}
s->mode=MODE_BRUTE;
}
else if (strcmp(bstr, "dict") == 0 || strcmp(bstr, "dictionary") == 0) {
s->mode=MODE_DICT;
}
else if (strcmp(bstr, "rev") == 0 || strcmp(bstr, "reverse") == 0) {
s->mode=MODE_REVERSE;
}
else {
ERR("unknown mode `%s'", bstr);
return -1;
}
return 1;
}
int set_dictfile(const char *in) {
if (in == NULL || strlen(in) < 1) {
return -1;
}
if (s->dictfile != NULL) {
xfree(s->dictfile);
}
s->dictfile=xstrdup(in);
return 1;
}
int set_async(const char *str) {
if (str == NULL || strlen(str) < 1) {
ERR("bad sync|async input, try no|number");
return -1;
}
if (strcasecmp(str, "no") == 0) {
s->async=0;
}
else {
if (sscanf(str, "%u", &s->concurrency) != 1) {
ERR("bad number for async concurrency");
return -1;
}
if (s->concurrency < 2) {
ERR("concurrency should be > 1, otherwise dont use it");
return -1;
}
if (s->concurrency > 256) {
ERR("concurrency too large!");
s->concurrency=DEF_CONCURRENCY;
return -1;
}
s->async=1;
}
return 1;
}
int set_exact(int yesno) {
s->exact=yesno;
return 1;
}
int set_filter(int yesno) {
s->filter_unique=yesno;
return 1;
}
int set_discover(int yesno) {
s->discover=1;
return 1;
}
int set_outsql(int yesno) {
s->output_sql=yesno;
return 1;
}
int set_verbose(int level) {
s->verbose=level;
return 1;
}