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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <regex.h>
#include <arpa/inet.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include "recon.h"
#include "util_ct.h"
#include "util_rt.h"
#include "inet.h"
static regex_t re_entry, re_comment, re_empty;
static void print_help (FILE *o, const char *prog) {
fprintf(
o,
"Usage: %s <conf> <port 1> [port 2] ... [port n]\n"
"Options:\n"
"\t<conf>: path to config file. \"-\" to read stdin\n"
"Config Format: <ENTRY SPEC> <NET SPEC>\n"
"\t<ENTRY SPEC>: \"T\" for target or \"BL\" for blacklist\n"
"\t<NET SPEC>: <IPv4 or IPv6 Address>/<CIDR>\n"
"\tNote:\n"
"\t\t- Parsed case-insensitively\n"
"\t\t- Lines start with \"#\" are ignored\n"
"Config Example:\n"
"\t# Test pool A\n"
"\tT\t192.18.0.0/24\n"
"\t# Test pool B\n"
"\tT\t192.18.1.0/24\n"
"\t# Test pool C\n"
"\tT\tfc00:A::/96\n"
"\t# Test pool D\n"
"\tT\tfc00:B::/96\n"
"\t# My Private Net (IPv4)\n"
"\tBL\t192.168.0.1/24\n"
"\t# My Private Net (IPv6)\n"
"\tBL\tfd00:ABBA::/64\n",
prog);
}
static int do_parse_conf (FILE *file, prne_recon_param_t *param) {
static const size_t RM_SIZE = 8;
regmatch_t rm[RM_SIZE];
char line[2][1024];
size_t nr_line = 0;
char *ent_spec, *ent_addr, *ent_cidr;
uint8_t cidr;
prne_recon_network_t net;
while (true) {
if (fgets(line[0], sizeof(line[0]), file) == NULL) {
break;
}
nr_line += 1;
if (regexec(&re_empty, line[0], RM_SIZE, rm, 0) == 0 ||
regexec(&re_comment, line[0], RM_SIZE, rm, 0) == 0)
{
continue;
}
if (regexec(&re_entry, line[0], RM_SIZE, rm, 0) != 0) {
goto INV_LINE;
}
strcpy(line[1], line[0]);
prne_memzero(&net, sizeof(prne_recon_network_t));
line[1][rm[2].rm_eo] = 0; // terminate ENTRY SPEC
line[1][rm[4].rm_eo] = 0; // terminate address
line[1][rm[5].rm_eo] = 0; // terminate CIDR
ent_spec = line[1] + rm[2].rm_so;
ent_addr = line[1] + rm[4].rm_so;
ent_cidr = line[1] + rm[5].rm_so;
prne_transcstr(ent_spec, prne_ctoupper);
prne_transcstr(ent_addr, prne_ctolower);
if (inet_pton(AF_INET6, ent_addr, net.addr.addr)) {
net.addr.ver = PRNE_IPV_6;
}
else if (inet_pton(AF_INET, ent_addr, net.addr.addr)) {
net.addr.ver = PRNE_IPV_4;
}
else {
goto INV_LINE;
}
if (sscanf(ent_cidr, "%"SCNu8, &cidr) != 1 ||
(net.addr.ver == PRNE_IPV_6 && cidr > 128) ||
(net.addr.ver == PRNE_IPV_4 && cidr > 32))
{
goto INV_LINE;
}
prne_netmask_from_cidr(net.mask, cidr);
if (strcmp(ent_spec, "T") == 0) {
prne_assert(prne_alloc_recon_param(
param,
param->blist.cnt,
param->target.cnt + 1,
param->ports.cnt));
param->target.arr[param->target.cnt - 1] = net;
}
else if (strcmp(ent_spec, "BL") == 0) {
prne_assert(prne_alloc_recon_param(
param,
param->blist.cnt + 1,
param->target.cnt,
param->ports.cnt));
param->blist.arr[param->blist.cnt - 1] = net;
}
else {
abort();
}
}
return 0;
INV_LINE:
fprintf(
stderr,
"*** Invalid entry at line %zu: %s\n",
nr_line,
line[0]);
return 2;
}
static void evt_cb (void *ctx, const prne_net_endpoint_t *ep) {
char addr_str[prne_op_max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
switch (ep->addr.ver) {
case PRNE_IPV_4:
inet_ntop(AF_INET, ep->addr.addr, addr_str, sizeof(addr_str));
printf("%s:%"PRIu16"\n", addr_str, ep->port);
break;
case PRNE_IPV_6:
inet_ntop(AF_INET6, ep->addr.addr, addr_str, sizeof(addr_str));
printf(
"[%s%%%"PRIu32"]:%"PRIu16"\n",
addr_str,
ep->addr.scope_id,
ep->port);
break;
default: abort();
}
}
int main (const int argc, const char **args) {
int ret = 0;
prne_recon_param_t param;
FILE *conf_f = NULL;
bool own_conf_f = false;
prne_worker_t wkr;
prne_recon_t *recon;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
sigset_t ss_term;
int caught_sig;
prne_assert(regcomp(
&re_entry,
// ^(\s+)?(T|BL)(\s+)([0-9a-f:.]+)\/([0-9]{1,3})(\s+)?(#.*)?$
// number of captures: 7
// significant groups: 2, 4, 5
"^(\\s+)?(T|BL)(\\s+)([0-9a-f:.]+)\\/([0-9]{1,3})(\\s+)?(#.*)?$",
REG_EXTENDED | REG_ICASE) == 0);
prne_assert(regcomp(
&re_comment,
// ^(\s+)?#.*$
"^(\\s+)?#.*$",
REG_EXTENDED | REG_ICASE) == 0);
prne_assert(regcomp(
&re_empty,
// ^(\s+)?$
"^(\\s+)?$",
REG_EXTENDED | REG_ICASE) == 0);
prne_init_recon_param(¶m);
param.evt_cb = evt_cb;
prne_init_worker(&wkr);
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);
sigemptyset(&ss_term);
sigaddset(&ss_term, SIGTERM);
sigaddset(&ss_term, SIGINT);
// parse args
if (argc < 3) {
print_help(stderr, args[0]);
ret = 2;
goto END;
}
if (prne_nstreq(args[1], "-")) {
conf_f = stdin;
}
else {
own_conf_f = true;
conf_f = fopen(args[1], "r");
if (conf_f == NULL) {
perror(args[1]);
ret = 1;
goto END;
}
}
for (int i = 2; i < argc; i += 1) {
uint16_t port;
if (sscanf(args[i], "%"SCNu16, &port) != 1 || port == 0) {
fprintf(stderr, "*** %s: invalid port value\n", args[i]);
ret = 2;
goto END;
}
prne_assert(prne_alloc_recon_param(
¶m,
param.blist.cnt,
param.target.cnt,
param.ports.cnt + 1));
param.ports.arr[param.ports.cnt - 1] = port;
}
prne_assert(pth_init());
// try-catch init
prne_assert(mbedtls_ctr_drbg_seed(
&ctr_drbg,
mbedtls_entropy_func,
&entropy,
NULL,
0) == 0);
// parse conf
ret = do_parse_conf(conf_f, ¶m);
if (ret != 0) {
goto END;
}
if (param.target.cnt == 0) {
fprintf(stderr, "*** No target network configured\n");
ret = 2;
goto END;
}
// alloc recon
param.ownership = false;
recon = prne_alloc_recon(
&wkr,
&ctr_drbg,
¶m);
param.ownership = true;
if (recon == NULL) {
perror("prne_alloc_recon()");
ret = 2;
goto END;
}
wkr.pth = pth_spawn(PTH_ATTR_DEFAULT, wkr.entry, wkr.ctx);
prne_assert(wkr.pth != NULL);
// wait for termination
prne_assert(sigprocmask(SIG_BLOCK, &ss_term, NULL) == 0);
pth_sigwait(&ss_term, &caught_sig);
sigprocmask(SIG_UNBLOCK, &ss_term, NULL);
// fin worker
wkr.fin(wkr.ctx);
pth_join(wkr.pth, NULL);
wkr.pth = NULL;
prne_free_worker(&wkr);
END:
// clean up
regfree(&re_entry);
regfree(&re_comment);
regfree(&re_empty);
prne_free_recon_param(¶m);
if (own_conf_f && conf_f != NULL) {
fclose(conf_f);
}
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
pth_kill();
return ret;
}
|