/
module_tcp_synscan.c
278 lines (259 loc) · 9.53 KB
/
module_tcp_synscan.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
/*
* ZMap Copyright 2013 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
// probe module for performing TCP SYN scans
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "../../lib/includes.h"
#include "../../lib/logger.h"
#include "../fieldset.h"
#include "module_tcp_synscan.h"
#include "probe_modules.h"
#include "packet.h"
#include "validate.h"
// defaults
static uint8_t zmap_tcp_synscan_tcp_header_len = 20;
static uint8_t zmap_tcp_synscan_packet_len = 54;
probe_module_t module_tcp_synscan;
static uint16_t num_source_ports;
static uint8_t os_for_tcp_options;
static int synscan_global_initialize(struct state_conf *state)
{
num_source_ports =
state->source_port_last - state->source_port_first + 1;
module_tcp_synscan.max_packet_length = zmap_tcp_synscan_packet_len;
// Based on the OS, we'll set the TCP options differently
if (!state->probe_args) {
// user didn't provide any probe args, defaulting to linux
log_debug("tcp_synscan", "no probe-args, "
"defaulting to linux TCP options");
state->probe_args = (char *)"linux";
}
if (strcmp(state->probe_args, "none") == 0) {
os_for_tcp_options = NO_OPTIONS;
zmap_tcp_synscan_tcp_header_len = 20;
zmap_tcp_synscan_packet_len = 54;
} else if (strcmp(state->probe_args, "bsd") == 0) {
os_for_tcp_options = BSD_OS_OPTIONS;
zmap_tcp_synscan_tcp_header_len = 44;
zmap_tcp_synscan_packet_len = 78;
} else if (strcmp(state->probe_args, "windows") == 0) {
os_for_tcp_options = WINDOWS_OS_OPTIONS;
zmap_tcp_synscan_tcp_header_len = 32;
zmap_tcp_synscan_packet_len = 66;
} else if (strcmp(state->probe_args, "linux") == 0) {
os_for_tcp_options = LINUX_OS_OPTIONS;
zmap_tcp_synscan_tcp_header_len = 40;
zmap_tcp_synscan_packet_len = 74;
} else {
log_fatal("tcp_synscan", "unknown "
"probe-args value: %s, probe-args "
"should have format: \"--probe-args=os\" "
"where os can be \"none\", \"bsd\", "
"\"windows\", and \"linux\"",
state->probe_args);
}
// double-check arithmetic
assert(zmap_tcp_synscan_packet_len - zmap_tcp_synscan_tcp_header_len == 34);
return EXIT_SUCCESS;
}
static int synscan_prepare_packet(void *buf, macaddr_t *src, macaddr_t *gw,
UNUSED void *arg_ptr)
{
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) + zmap_tcp_synscan_tcp_header_len);
make_ip_header(ip_header, IPPROTO_TCP, len);
struct tcphdr *tcp_header = (struct tcphdr *)(&ip_header[1]);
make_tcp_header(tcp_header, TH_SYN);
set_tcp_options(tcp_header, os_for_tcp_options);
return EXIT_SUCCESS;
}
static int synscan_make_packet(void *buf, size_t *buf_len, ipaddr_n_t src_ip,
ipaddr_n_t dst_ip, port_n_t dport, 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 tcphdr *tcp_header = (struct tcphdr *)(&ip_header[1]);
uint32_t tcp_seq = validation[0];
ip_header->ip_src.s_addr = src_ip;
ip_header->ip_dst.s_addr = dst_ip;
ip_header->ip_ttl = ttl;
port_h_t sport = get_src_port(num_source_ports, probe_num, validation);
tcp_header->th_sport = htons(sport);
tcp_header->th_dport = dport;
tcp_header->th_seq = tcp_seq;
// checksum value must be zero when calculating packet's checksum
tcp_header->th_sum = 0;
tcp_header->th_sum = tcp_checksum(zmap_tcp_synscan_tcp_header_len,
ip_header->ip_src.s_addr,
ip_header->ip_dst.s_addr, tcp_header);
ip_header->ip_id = ip_id;
// checksum value must be zero when calculating packet's checksum
ip_header->ip_sum = 0;
ip_header->ip_sum = zmap_ip_checksum((unsigned short *)ip_header);
*buf_len = zmap_tcp_synscan_packet_len;
return EXIT_SUCCESS;
}
// not static because used by synack scan
void synscan_print_packet(FILE *fp, void *packet)
{
struct ether_header *ethh = (struct ether_header *)packet;
struct ip *iph = (struct ip *)ðh[1];
struct tcphdr *tcph = (struct tcphdr *)&iph[1];
fprintf(fp,
"tcp { source: %u | dest: %u | seq: %u | checksum: %#04X }\n",
ntohs(tcph->th_sport), ntohs(tcph->th_dport),
ntohl(tcph->th_seq), ntohs(tcph->th_sum));
fprintf_ip_header(fp, iph);
fprintf_eth_header(fp, ethh);
fprintf(fp, PRINT_PACKET_SEP);
}
static int synscan_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_TCP) {
struct tcphdr *tcp = get_tcp_header(ip_hdr, len);
if (!tcp) {
return PACKET_INVALID;
}
port_h_t sport = ntohs(tcp->th_sport);
port_h_t dport = ntohs(tcp->th_dport);
// validate source port
if (!check_src_port(sport, ports)) {
return PACKET_INVALID;
}
// validate destination port
if (!check_dst_port(dport, num_source_ports, validation)) {
return PACKET_INVALID;
}
// check whether we'll ever send to this IP during the scan
if (!blocklist_is_allowed(*src_ip)) {
return PACKET_INVALID;
}
// We treat RST packets different from non RST packets
if (tcp->th_flags & TH_RST) {
// For RST packets, recv(ack) == sent(seq) + 0 or + 1
if (htonl(tcp->th_ack) != htonl(validation[0]) &&
htonl(tcp->th_ack) != htonl(validation[0]) + 1) {
return PACKET_INVALID;
}
} else {
// For non RST packets, recv(ack) == sent(seq) + 1
if (htonl(tcp->th_ack) != htonl(validation[0]) + 1) {
return PACKET_INVALID;
}
}
} else if (ip_hdr->ip_p == IPPROTO_ICMP) {
struct ip *ip_inner;
size_t ip_inner_len;
if (icmp_helper_validate(ip_hdr, len, sizeof(struct tcphdr),
&ip_inner,
&ip_inner_len) == PACKET_INVALID) {
return PACKET_INVALID;
}
struct tcphdr *tcp = get_tcp_header(ip_inner, ip_inner_len);
if (!tcp) {
return PACKET_INVALID;
}
// 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. Note this is *different*
// than the logic above because we're validating the probe packet
// rather than the response packet
port_h_t sport = ntohs(tcp->th_sport);
port_h_t dport = ntohs(tcp->th_dport);
if (!check_src_port(dport, ports)) {
return PACKET_INVALID;
}
validate_gen(ip_hdr->ip_dst.s_addr, ip_inner->ip_dst.s_addr,
tcp->th_dport, (uint8_t *)validation);
if (!check_dst_port(sport, num_source_ports, validation)) {
return PACKET_INVALID;
}
} else {
return PACKET_INVALID;
}
return PACKET_VALID;
}
static void synscan_process_packet(const u_char *packet, UNUSED uint32_t len,
fieldset_t *fs, UNUSED uint32_t *validation,
UNUSED struct timespec ts)
{
struct ip *ip_hdr = get_ip_header(packet, len);
assert(ip_hdr);
if (ip_hdr->ip_p == IPPROTO_TCP) {
struct tcphdr *tcp = get_tcp_header(ip_hdr, len);
assert(tcp);
fs_add_uint64(fs, "sport", (uint64_t)ntohs(tcp->th_sport));
fs_add_uint64(fs, "dport", (uint64_t)ntohs(tcp->th_dport));
fs_add_uint64(fs, "seqnum", (uint64_t)ntohl(tcp->th_seq));
fs_add_uint64(fs, "acknum", (uint64_t)ntohl(tcp->th_ack));
fs_add_uint64(fs, "window", (uint64_t)ntohs(tcp->th_win));
if (tcp->th_flags & TH_RST) { // RST packet
fs_add_constchar(fs, "classification", "rst");
fs_add_bool(fs, "success", 0);
} else { // SYNACK packet
fs_add_constchar(fs, "classification", "synack");
fs_add_bool(fs, "success", 1);
}
fs_add_null_icmp(fs);
} else if (ip_hdr->ip_p == IPPROTO_ICMP) {
// tcp
fs_add_null(fs, "sport");
fs_add_null(fs, "dport");
fs_add_null(fs, "seqnum");
fs_add_null(fs, "acknum");
fs_add_null(fs, "window");
// global
fs_add_constchar(fs, "classification", "icmp");
fs_add_bool(fs, "success", 0);
// icmp
fs_populate_icmp_from_iphdr(ip_hdr, len, fs);
}
}
static fielddef_t fields[] = {
{.name = "sport", .type = "int", .desc = "TCP source port"},
{.name = "dport", .type = "int", .desc = "TCP destination port"},
{.name = "seqnum", .type = "int", .desc = "TCP sequence number"},
{.name = "acknum", .type = "int", .desc = "TCP acknowledgement number"},
{.name = "window", .type = "int", .desc = "TCP window"},
CLASSIFICATION_SUCCESS_FIELDSET_FIELDS,
ICMP_FIELDSET_FIELDS,
};
probe_module_t module_tcp_synscan = {
.name = "tcp_synscan",
.pcap_filter = "(tcp && tcp[13] & 4 != 0 || tcp[13] == 18) || icmp",
.pcap_snaplen = 96,
.port_args = 1,
.global_initialize = &synscan_global_initialize,
.prepare_packet = &synscan_prepare_packet,
.make_packet = &synscan_make_packet,
.print_packet = &synscan_print_packet,
.process_packet = &synscan_process_packet,
.validate_packet = &synscan_validate_packet,
.close = NULL,
.helptext =
"Probe module that sends a TCP SYN packet to a specific port. Possible "
"classifications are: synack and rst. A SYN-ACK packet is considered a "
"success and a reset packet is considered a failed response. "
"By default, TCP header options are set identically to the values for "
"linux (Ubuntu 23.10) (MSS, SACK permitted, Timestamp, and WindowScale "
"= 7). Use \"--probe-args=n\" to set the options, valid options are "
"\"none\", \"bsd\", \"windows\", \"linux\" (default).",
.output_type = OUTPUT_TYPE_STATIC,
.fields = fields,
.numfields = sizeof(fields) / sizeof(fields[0])};