/
module_ipip.c
418 lines (377 loc) · 13.3 KB
/
module_ipip.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
/* heavily copied from module_udp.c */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "../../lib/blocklist.h"
#include "../../lib/includes.h"
#include "../../lib/xalloc.h"
#include "../../lib/lockfd.h"
#include "../../lib/logger.h"
#include "../../lib/xalloc.h"
#include "../state.h"
#include "../validate.h"
#include "probe_modules.h"
#include "packet.h"
#define MAX_UDP_PAYLOAD_LEN 200
#define ICMP_UNREACH_HEADER_SIZE 8
#define UNUSED __attribute__((unused))
static char *udp_send_msg = NULL;
static int udp_send_msg_len = 0;
static const char *udp_send_msg_default = "GET / HTTP/1.1\r\nHost: www\r\n\r\n";
#define DEFUALT_PAYLOAD_LEN (30)
const char *ipip_usage_error =
"unknown UDP probe specification (expected file:/path or text:STRING or hex:01020304)";
static int num_ports;
probe_module_t module_ipip;
int ipip_global_initialize(struct state_conf *conf)
{
char *args, *c;
int i;
unsigned int n;
FILE *inp;
num_ports = conf->source_port_last - conf->source_port_first + 1;
udp_send_msg = strdup(udp_send_msg_default);
udp_send_msg_len = strlen(udp_send_msg);
if (!(conf->probe_args && strlen(conf->probe_args) > 0))
return (0);
args = strdup(conf->probe_args);
assert(args);
c = strchr(args, ':');
if (!c) {
free(args);
free(udp_send_msg);
log_fatal("ipip", "%s", ipip_usage_error);
}
*c++ = 0;
if (strcmp(args, "text") == 0) {
free(udp_send_msg);
udp_send_msg = strdup(c);
udp_send_msg_len = strlen(udp_send_msg);
} else if (strcmp(args, "file") == 0) {
inp = fopen(c, "rb");
if (!inp) {
free(udp_send_msg);
// c points to memory in args, let exit free args
log_fatal("ipip", "could not open UDP data file '%s'",
c);
}
free(udp_send_msg);
udp_send_msg = xmalloc(MAX_UDP_PAYLOAD_LEN);
udp_send_msg_len =
fread(udp_send_msg, 1, MAX_UDP_PAYLOAD_LEN, inp);
fclose(inp);
} else if (strcmp(args, "hex") == 0) {
udp_send_msg_len = strlen(c) / 2;
free(udp_send_msg);
udp_send_msg = xmalloc(udp_send_msg_len);
for (i = 0; i < udp_send_msg_len; i++) {
if (sscanf(c + (i * 2), "%2x", &n) != 1) {
char nonhexchr = c[i * 2];
free(args);
free(udp_send_msg);
log_fatal("ipip", "non-hex character: '%c'",
nonhexchr);
}
udp_send_msg[i] = (n & 0xff);
}
} else {
free(udp_send_msg);
free(args);
log_fatal("ipip", "%s", ipip_usage_error);
}
if (udp_send_msg_len > MAX_UDP_PAYLOAD_LEN) {
log_warn("ipip",
"warning: reducing UDP payload to %d "
"bytes (from %d) to fit on the wire",
MAX_UDP_PAYLOAD_LEN, udp_send_msg_len);
udp_send_msg_len = MAX_UDP_PAYLOAD_LEN;
}
module_ipip.max_packet_length = sizeof(struct ether_header) +
sizeof(struct ip) * 2 +
sizeof(struct udphdr) + udp_send_msg_len;
assert(module_ipip.max_packet_length <= MAX_PACKET_SIZE);
free(args);
return EXIT_SUCCESS;
}
int ipip_global_cleanup(UNUSED struct state_conf *zconf,
UNUSED struct state_send *zsend,
UNUSED struct state_recv *zrecv)
{
if (udp_send_msg) {
free(udp_send_msg);
udp_send_msg = NULL;
}
return EXIT_SUCCESS;
}
int ipip_prepare_packet(void *buf, macaddr_t *src, macaddr_t *gw,
UNUSED void *arg_ptr)
{
memset(buf, 0, MAX_PACKET_SIZE);
struct ether_header *eth_header = (struct ether_header *)buf;
make_eth_header(eth_header, src, gw);
struct ip *ip_header = (struct ip *)(ð_header[1]);
uint16_t len = htons(sizeof(struct ip) * 2 + sizeof(struct udphdr) +
udp_send_msg_len);
make_ip_header(ip_header, IPPROTO_IPIP, len);
struct ip *ip_header2 = &ip_header[1];
len =
htons(sizeof(struct ip) + sizeof(struct udphdr) + udp_send_msg_len);
make_ip_header(ip_header2, IPPROTO_UDP, len);
struct udphdr *udp_header = (struct udphdr *)(&ip_header2[1]);
len = sizeof(struct udphdr) + udp_send_msg_len;
make_udp_header(udp_header, len);
char *payload = (char *)(&udp_header[1]);
memcpy(payload, udp_send_msg, udp_send_msg_len);
return EXIT_SUCCESS;
}
int ipip_make_packet(void *buf, size_t *buf_len, ipaddr_n_t src_ip,
ipaddr_n_t dst_ip, port_n_t dport, UNUSED uint8_t ttl,
uint32_t *validation, int probe_num, uint16_t ip_id,
UNUSED void *arg)
{
struct ether_header *eth_header = (struct ether_header *)buf;
struct ip *ip_header = (struct ip *)(ð_header[1]);
struct ip *ip_header2 = (struct ip *)(&ip_header[1]);
struct udphdr *udp_header = (struct udphdr *)&ip_header2[1];
ip_header->ip_src.s_addr = src_ip;
ip_header->ip_dst.s_addr = dst_ip;
ip_header->ip_id = ip_id;
ip_header2->ip_src.s_addr = dst_ip;
ip_header2->ip_dst.s_addr = src_ip; // TODO put "external_ip"
ip_header2->ip_id = ip_id;
udp_header->uh_sport =
htons(get_src_port(num_ports, probe_num, validation));
udp_header->uh_dport = dport;
ip_header2->ip_sum = 0;
ip_header2->ip_sum = zmap_ip_checksum((unsigned short *)ip_header2);
ip_header->ip_sum = 0;
ip_header->ip_sum = zmap_ip_checksum((unsigned short *)ip_header);
// Output the total length of the packet
const size_t header_len = sizeof(struct ether_header) + sizeof(struct ip) +
sizeof(struct ip) + sizeof(struct udphdr);
*buf_len = header_len + udp_send_msg_len;
return EXIT_SUCCESS;
}
void ipip_print_packet(FILE *fp, void *packet)
{
struct ether_header *ethh = (struct ether_header *)packet;
struct ip *iph = (struct ip *)ðh[1];
struct ip *iph2 = (struct ip *)&iph[1];
struct udphdr *udph = (struct udphdr *)(&iph2[1]);
fprintf(fp, "udp { source: %u | dest: %u | checksum: %#04X }\n",
ntohs(udph->uh_sport), ntohs(udph->uh_dport),
ntohs(udph->uh_sum));
fprintf_ip_header(fp, iph2);
fprintf_ip_header(fp, iph);
fprintf_eth_header(fp, ethh);
fprintf(fp, "------------------------------------------------------\n");
}
void ipip_process_packet(const u_char *packet, UNUSED uint32_t len,
fieldset_t *fs, UNUSED uint32_t *validation,
UNUSED const struct timespec ts)
{
struct ip *ip_hdr = (struct ip *)&packet[sizeof(struct ether_header)];
if (ip_hdr->ip_p == IPPROTO_UDP) {
struct udphdr *udp =
(struct udphdr *)((char *)ip_hdr + ip_hdr->ip_hl * 4);
fs_add_constchar(fs, "classification", "udp");
fs_add_bool(fs, "success", 1);
fs_add_uint64(fs, "sport", ntohs(udp->uh_sport));
fs_add_uint64(fs, "dport", ntohs(udp->uh_dport));
fs_add_null(fs, "icmp_responder");
fs_add_null(fs, "icmp_type");
fs_add_null(fs, "icmp_code");
fs_add_null(fs, "icmp_unreach_str");
fs_add_uint64(fs, "udp_pkt_size", ntohs(udp->uh_ulen));
// Verify that the UDP length is big enough for the header and
// at least one byte
uint16_t data_len = ntohs(udp->uh_ulen);
if (data_len > sizeof(struct udphdr)) {
uint32_t overhead =
(sizeof(struct udphdr) + (ip_hdr->ip_hl * 4));
uint32_t max_rlen = len - overhead;
uint32_t max_ilen = ntohs(ip_hdr->ip_len) - overhead;
// Verify that the UDP length is inside of our received
// buffer
if (data_len > max_rlen) {
data_len = max_rlen;
}
// Verify that the UDP length is inside of our IP packet
if (data_len > max_ilen) {
data_len = max_ilen;
}
fs_add_binary(fs, "data", data_len, (void *)&udp[1], 0);
// Some devices reply with a zero UDP length but still
// return data, ignore the data
} else {
fs_add_null(fs, "data");
}
} else if (ip_hdr->ip_p == IPPROTO_ICMP) {
struct icmp *icmp =
(struct icmp *)((char *)ip_hdr + ip_hdr->ip_hl * 4);
struct ip *ip_inner =
(struct ip *)((char *)icmp + ICMP_UNREACH_HEADER_SIZE);
// ICMP unreach comes from another server (not the one we sent a
// probe to); But we will fix up saddr to be who we sent the
// probe to, in case you care.
fs_modify_string(fs, "saddr",
make_ip_str(ip_inner->ip_dst.s_addr), 1);
fs_add_string(fs, "classification", (char *)"icmp-unreach", 0);
fs_add_bool(fs, "success", 0);
fs_add_null(fs, "sport");
fs_add_null(fs, "dport");
fs_add_string(fs, "icmp_responder",
make_ip_str(ip_hdr->ip_src.s_addr), 1);
fs_add_uint64(fs, "icmp_type", icmp->icmp_type);
fs_add_uint64(fs, "icmp_code", icmp->icmp_code);
if (icmp->icmp_code <= ICMP_UNREACH_PRECEDENCE_CUTOFF) {
fs_add_string(
fs, "icmp_unreach_str",
(char *)icmp_unreach_strings[icmp->icmp_code], 0);
} else {
fs_add_string(fs, "icmp_unreach_str", (char *)"unknown",
0);
}
fs_add_null(fs, "udp_pkt_size");
fs_add_null(fs, "data");
} else {
fs_add_string(fs, "classification", (char *)"other", 0);
fs_add_bool(fs, "success", 0);
fs_add_null(fs, "sport");
fs_add_null(fs, "dport");
fs_add_null(fs, "icmp_responder");
fs_add_null(fs, "icmp_type");
fs_add_null(fs, "icmp_code");
fs_add_null(fs, "icmp_unreach_str");
fs_add_null(fs, "udp_pkt_size");
fs_add_null(fs, "data");
}
}
int ipip_validate_packet(const struct ip *ip_hdr, uint32_t len,
uint32_t *src_ip, uint32_t *validation,
const struct port_conf *ports)
{
if (ip_hdr->ip_p == IPPROTO_UDP) {
if ((4 * ip_hdr->ip_hl + sizeof(struct udphdr)) > len) {
// buffer not large enough to contain expected udp
// header
return PACKET_INVALID;
}
struct udphdr *udp =
(struct udphdr *)((char *)ip_hdr + 4 * ip_hdr->ip_hl);
uint16_t dport = ntohs(udp->uh_dport);
if (check_src_port(dport, ports) == 0) {
return PACKET_INVALID;
}
if (!blocklist_is_allowed(*src_ip)) {
return PACKET_INVALID;
}
uint16_t sport = ntohs(udp->uh_sport);
if (check_dst_port(sport, num_ports, validation)) {
return PACKET_VALID;
}
for (unsigned i = 0; i < zconf.number_source_ips; i++) {
validate_gen(
zconf.source_ip_addresses[i],
ip_hdr->ip_src.s_addr, udp->uh_dport, (uint8_t *)validation);
if (check_dst_port(sport, num_ports, validation)) {
return PACKET_VALID;
}
}
} else if (ip_hdr->ip_p == IPPROTO_ICMP) {
// IPIP can return ICMP Destination unreach
// IP( ICMP( IP ( IP( UDP ) ) ) ) for a destination unreach
uint32_t min_len =
4 * ip_hdr->ip_hl + ICMP_UNREACH_HEADER_SIZE +
sizeof(struct ip) + sizeof(struct ip) + sizeof(struct udphdr);
if (len < min_len) {
return PACKET_INVALID;
}
struct icmp *icmp =
(struct icmp *)((char *)ip_hdr + 4 * ip_hdr->ip_hl);
if (icmp->icmp_type != ICMP_UNREACH) {
return PACKET_INVALID;
}
struct ip *ip_inner1 =
(struct ip *)((char *)icmp + ICMP_UNREACH_HEADER_SIZE);
// update min_len according to first internal IP packet
min_len = min_len - sizeof(struct ip) + 4 * ip_inner1->ip_hl;
if (len < min_len) {
return PACKET_INVALID;
}
uint32_t dest = ip_inner1->ip_dst.s_addr;
if (!blocklist_is_allowed(dest)) {
return PACKET_INVALID;
}
struct ip *ip_inner2 =
(struct ip *)((char *)ip_inner1 + 4 * ip_inner1->ip_hl);
// update min_len according to second internal IP packet
min_len = min_len - sizeof(struct ip) + 4 * ip_inner2->ip_hl;
if (len < min_len) {
return PACKET_INVALID;
}
// ensure the internal dst addr is the outer src addr and vice versa
if (ip_inner1->ip_dst.s_addr != ip_inner2->ip_src.s_addr ||
ip_inner1->ip_src.s_addr != ip_inner2->ip_dst.s_addr) {
return PACKET_INVALID;
}
struct udphdr *udp =
(struct udphdr *)((char *)ip_inner2 + 4 * ip_inner2->ip_hl);
// we can always check the destination port because this is the
// original packet and wouldn't have been altered by something
// responding on a different port
uint16_t dport = ntohs(udp->uh_dport);
if (check_src_port(dport, ports) == 0) {
return PACKET_INVALID;
}
uint16_t sport = ntohs(udp->uh_sport);
if (!check_dst_port(sport, num_ports, validation)) {
return PACKET_INVALID;
}
return PACKET_VALID;
}
return PACKET_INVALID;
}
static fielddef_t fields[] = {
{.name = "classification",
.type = "string",
.desc = "packet classification"},
{.name = "success",
.type = "bool",
.desc = "is response considered success"},
{.name = "sport", .type = "int", .desc = "UDP source port"},
{.name = "dport", .type = "int", .desc = "UDP destination port"},
{.name = "icmp_responder",
.type = "string",
.desc = "Source IP of ICMP_UNREACH message"},
{.name = "icmp_type", .type = "int", .desc = "icmp message type"},
{.name = "icmp_code", .type = "int", .desc = "icmp message sub type code"},
{.name = "icmp_unreach_str",
.type = "string",
.desc =
"for icmp_unreach responses, the string version of icmp_code (e.g. network-unreach)"},
{.name = "udp_pkt_size", .type = "int", .desc = "UDP packet length"},
{.name = "data", .type = "binary", .desc = "UDP payload"}};
probe_module_t module_ipip = {
.name = "ipip",
.max_packet_length = sizeof(struct ether_header) + sizeof(struct ip) * 2 +
sizeof(struct udphdr) + DEFUALT_PAYLOAD_LEN,
.pcap_filter = "udp || icmp",
.pcap_snaplen = 1500,
.port_args = 1,
.global_initialize = &ipip_global_initialize,
.prepare_packet = &ipip_prepare_packet,
.make_packet = &ipip_make_packet,
.print_packet = &ipip_print_packet,
.validate_packet = &ipip_validate_packet,
.process_packet = &ipip_process_packet,
.close = &ipip_global_cleanup,
.helptext = "Probe module that sends UDP packets to hosts. Packets can "
"optionally be templated based on destination host. Specify"
" packet file with --probe-args=file:/path_to_packet_file "
"and templates with template:/path_to_template_file.",
.fields = fields,
.numfields = sizeof(fields) / sizeof(fields[0])};