-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodule_ja4tscan.c
507 lines (440 loc) · 16.4 KB
/
module_ja4tscan.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* Copyright (c) 2024, FoxIO, All rights reserved.
* Portions Copyright 2013 Regents of the University of Michigan
* Licensed under the FoxIO License 1.1, see repo root
* JA4TScan by John Althouse
* Module by Timothy Noel and Thatcher Thornberry
*/
// 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 <math.h>
#include <pthread.h>
#include <time.h>
#include "cachehash.h"
#include "../../lib/includes.h"
#include "../../lib/logger.h"
#include "../../lib/blocklist.h"
#include "../fieldset.h"
#include "probe_modules.h"
#include "packet.h"
#include "validate.h"
#include "module_tcp_synscan.h"
#define ZMAP_JA4TS_TCP_HEADER_LEN 40
#define ZMAP_JA4TS_PACKET_LEN 74
// TCP Option Kind Values
#define TCP_OPTION_KIND_MSS 2
#define TCP_OPTION_KIND_WINDOW_SCALE 3
#define TCP_OPTION_KIND_NO_OP 1
#define TCP_OPTION_END 0
#define RST_TIMEOUT 120
static uint16_t num_source_ports;
static cachehash *ch = NULL;
fielddefset_t *ja4tscan_fds;
typedef struct ja4t_tuple {
int saddr;
int daddr;
} ja4t_tuple_t;
typedef struct ja4t_timedata {
char retransmits[256];
char ja4tscan_str[512];
char *options;
uint8_t scaling_factor;
uint16_t mss_value;
uint16_t window_size;
uint64_t sport;
uint64_t dport;
uint64_t seq;
uint64_t ack;
uint64_t ip_src_num;
int rst;
struct timespec ts;
fieldset_t *fs;
struct ip *ip;
} ja4t_timedata_t;
static int num_of_digits(int n)
{
return ( n==0 ) ? 1 : (int) log10(n)+1;
}
static int timediff(struct timespec *current, struct timespec *prev)
{
struct timespec result;
result.tv_sec = current->tv_sec - prev->tv_sec;
result.tv_nsec = current->tv_nsec - prev->tv_nsec;
if (result.tv_nsec < 0) {
--result.tv_sec;
result.tv_nsec += 1000000000L;
}
float nsecs = ((float)result.tv_nsec / 1000000000);
int diff = result.tv_sec + (((nsecs - floor(nsecs))> 0.5) ? 1 : 0);
return diff;
}
size_t set_additional_options(struct tcphdr *tcp_header)
{
size_t header_size = tcp_header->th_off * 4;
uint8_t *base = (uint8_t *)tcp_header;
uint8_t *last_opt = (uint8_t *)base + header_size;
// Add the Window Scale (Kind 3) option with scaling factor of 2
last_opt[0] = 3; // Option Kind for Window Scale
last_opt[1] = 3; // Option Length (typically 3 bytes)
last_opt[2] = 7; // Scaling Factor (0 to 14)
// Add the Sack Permitted (Kind 4) option
last_opt[3] = 4; // Option Kind for Sack Permitted
last_opt[4] = 2; // Option Length (typically 2 bytes)
// Add the Timestamp (Kind 8) option
last_opt[5] = 8; // Option Kind for Timestamp
last_opt[6] = 10; // Option Length (typically 10 bytes)
// Timestamp Value (4 bytes)
uint32_t timestamp_value = htonl((uint32_t)time(NULL));
memcpy(&last_opt[7], ×tamp_value, sizeof(uint32_t));
// Timestamp Echo Reply (4 bytes)
uint32_t timestamp_echo_reply = 0;
memcpy(&last_opt[11], ×tamp_echo_reply, sizeof(uint32_t));
// Update the header length
tcp_header->th_off += 4;
return tcp_header->th_off * 4;
}
static void compute_ja4tscan( fieldset_t *fs, ja4t_timedata_t *timedata ) {
// Calculate the required size for ja4ts_str
int required_size =
snprintf(NULL, 0, "%u", timedata->window_size) +
strlen(timedata->options) + // For b: TCP Parameters
strlen("_") + // Separator between b and c
num_of_digits(timedata->mss_value) + // For c: MSS mss_value
strlen("_") + // Separator between c and d
num_of_digits(timedata->scaling_factor) + 2; // For d: Window Scale
int extra_room = strlen(timedata->retransmits);
// Allocate memory for ja4ts_str
//char *ja4ts_str = (char *)malloc(required_size + extra_room);
// Construct ja4ts_str
snprintf(timedata->ja4tscan_str, required_size, "%u_%s_%02u_%d",
timedata->window_size, timedata->options, timedata->mss_value,
timedata->scaling_factor);
// Allocate extra space to include retransmits
if (extra_room > 0) {
snprintf(timedata->ja4tscan_str + strlen(timedata->ja4tscan_str), extra_room+1, "_%s", timedata->retransmits);
}
fs_add_uint64(fs, "sport", timedata->sport);
fs_add_uint64(fs, "dport", timedata->dport);
fs_add_uint64(fs, "seqnum", timedata->seq);
fs_add_uint64(fs, "acknum", timedata->ack);
fs_add_uint64(fs, "window", timedata->window_size);
fs_add_string(fs, "ja4tscan", (char *)timedata->ja4tscan_str, 0);
fs_add_uint64(fs, "timestamp", (uint64_t) timedata->ts.tv_sec);
fs_add_uint64(fs, "ip_src_num", timedata->ip_src_num);
if (timedata->rst == 1) {
fs_add_constchar(fs, "classification", "rst");
fs_add_bool(fs, "success", 1);
} else {
fs_add_constchar(fs, "classification", "synack");
fs_add_bool(fs, "success", 0);
}
fs_add_null_icmp(fs);
}
static int ja4tscan_global_initialize(struct state_conf *state)
{
num_source_ports =
state->source_port_last - state->source_port_first + 1;
ch = cachehash_init(10000, NULL);
ja4tscan_fds = &(state->fsconf.defs);
return EXIT_SUCCESS;
}
static int ja4tscan_prepare_packet(void *buf, macaddr_t *src, macaddr_t *gw,
//port_h_t dest_port,
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_JA4TS_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_mss_option(tcp_header);
set_additional_options(tcp_header);
return EXIT_SUCCESS;
}
static int ja4tscan_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,
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];
//uint8_t *tcp_options =
// (uint8_t *)(&tcp_header[1]); // Points to the start of TCP options
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_JA4TS_TCP_HEADER_LEN, ip_header->ip_src.s_addr,
ip_header->ip_dst.s_addr, tcp_header);
// 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_JA4TS_PACKET_LEN;
return EXIT_SUCCESS;
}
static int ja4tscan_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);
//(uint8_t *)validation);
if (!check_dst_port(sport, num_source_ports, validation)) {
return PACKET_INVALID;
}
} else {
return PACKET_INVALID;
}
return PACKET_VALID;
}
static void ja4tscan_process_packet(const u_char *packet, UNUSED uint32_t len,
fieldset_t *fs, UNUSED uint32_t *validation,
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);
ja4t_tuple_t t = {.saddr = ip_hdr->ip_src.s_addr, .daddr = ip_hdr->ip_dst.s_addr}; //, .sport = ntohs(tcp->th_sport), .dport = ntohs(tcp->th_dport) };
ja4t_timedata_t *timedata = cachehash_get(ch, &t, sizeof(ja4t_tuple_t));
// JA4TS IMPLEMENTATION
if (timedata == NULL) {
timedata = malloc(sizeof(ja4t_timedata_t));
timedata->ts.tv_sec = ts.tv_sec;
timedata->ts.tv_nsec = ts.tv_nsec;
timedata->retransmits[0] = '\0';
timedata->ja4tscan_str[0] = '\0';
timedata->options = malloc(sizeof(char) * 100);
timedata->options[0] = '\0';
timedata->window_size = 0;
timedata->scaling_factor = 0;
timedata->mss_value = 0;
timedata->rst = 0;
timedata->sport = ntohs(tcp->th_sport);
timedata->dport = ntohs(tcp->th_dport);
timedata->seq = ntohl(tcp->th_seq);
timedata->ack = ntohl(tcp->th_ack);
timedata->ip_src_num = (uint64_t)ntohl(ip_hdr->ip_src.s_addr);
timedata->fs = fs;
timedata->ip = malloc(sizeof(struct ip));
memcpy(timedata->ip, ip_hdr, sizeof(struct ip));
cachehash_put(ch, &t, sizeof(ja4t_tuple_t), (void *)timedata);
// Pointer to the start of TCP options
// +1 because the 'tcp' variable points to the TCP header
uint8_t *tcp_options = (uint8_t *)(tcp + 1);
// Length of TCP options field in 32-bit words (DWORDs)
// th_off is the offset in 32-bit words
int options_length = (tcp->th_off - 5) * 4;
int buffer_size = options_length;
// PARSE OPTIONS
uint8_t remaining_length = options_length;
int offset = 0;
// initialize variables
uint16_t mss_value = 0;
uint8_t scaling_factor = 0;
uint8_t option_kinds[100];
int num_option_kinds = 0;
while (remaining_length > 0) {
if (remaining_length < sizeof(uint8_t)) {
// Not enough bytes left for any more options
break;
}
uint8_t option_kind = tcp_options[offset];
uint8_t option_length =
(option_kind == TCP_OPTION_KIND_NO_OP ||
option_kind == TCP_OPTION_END)
? 1
: tcp_options[offset + 1];
// Check for malformed packet (option_length too small or beyond remaining_length)
if (option_length < 1 ||
option_length > remaining_length) {
break;
}
option_kinds[num_option_kinds] = option_kind;
num_option_kinds++;
switch (option_kind) {
case TCP_OPTION_KIND_MSS:
// The MSS value is 2 bytes following the Kind and Length fields
mss_value = ntohs(
*(uint16_t *)(tcp_options + offset + 2));
break;
case TCP_OPTION_KIND_WINDOW_SCALE:
// The scaling factor is 1 byte following the Kind and Length fields
scaling_factor = tcp_options[offset + 2];
break;
case TCP_OPTION_KIND_NO_OP:
// has no option length, decrement remaining_length by 1
//remaining_length--; (Bug fix: this decrements by an additional 1)
break;
case TCP_OPTION_END:
// end of options, set remaining_length to 0
remaining_length = 1; //(Buf fix: was setting this to 0 making remaining length -ve)
break;
default:
break;
}
// Update the remaining length and offset for the next option
remaining_length -= option_length;
offset += option_length;
// Check if offset has exceeded buffer bounds (consider buffer_size as the size of tcp_options)
if (offset > buffer_size) {
break;
}
}
// b: TCP Parameters
// Options are processed only for the first SYN-ACK
// For retransmissions and RST, we use the cached value
for (int i = 0; i < num_option_kinds; i++) {
// Convert the current option kind to a string
char option_kind_str[5];
snprintf(option_kind_str, sizeof(option_kind_str), "%d",
option_kinds[i]);
// If the option_kinds_str is not empty, append a hyphen separator
if (timedata->options[0] != '\0') {
strncat(timedata->options, "-", 1);
}
// Append the current sorted option kind
strncat(timedata->options, option_kind_str, strlen(option_kind_str));
}
if (strlen(timedata->options) == 0)
snprintf(timedata->options, 3, "%s", "00");
// a: window size
timedata->window_size = ntohs(tcp->th_win);
timedata->mss_value = mss_value;
timedata->scaling_factor = scaling_factor;
} else {
int diff = timediff(&ts, &timedata->ts);
if (tcp->th_flags & TH_RST) {
snprintf(timedata->retransmits + strlen(timedata->retransmits), num_of_digits(diff)+3, "R%d-", diff);
} else {
snprintf(timedata->retransmits + strlen(timedata->retransmits), num_of_digits(diff)+2, "%d-", diff);
}
timedata->ts.tv_sec = ts.tv_sec;
timedata->ts.tv_nsec = ts.tv_nsec;
timedata->ja4tscan_str[0] = '\0';
}
if (tcp->th_flags & TH_RST) {
timedata->rst = 1;
}
compute_ja4tscan (fs, timedata);
log_debug("ja4tscan", "JA4TScan: %s", timedata->ja4tscan_str);
log_debug("ja4tscan", "IP: %s", make_ip_str(ip_hdr->ip_src.s_addr));
} else if (ip_hdr->ip_p == IPPROTO_ICMP) {
log_debug("ja4tscan", "ICMP packet");
// 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");
fs_add_null(fs, "ja4tscan");
// 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"},
{.name = "ja4tscan", .type = "string", .desc = "TCP JA3 hash"},
{.name = "timestamp", .type = "int", .desc = "Unix Epoch timestamp"},
{.name = "ip_src_num", .type = "int", .desc = "Source IP address"},
CLASSIFICATION_SUCCESS_FIELDSET_FIELDS,
ICMP_FIELDSET_FIELDS,
};
probe_module_t module_ja4tscan = {
.name = "ja4tscan",
.max_packet_length = ZMAP_JA4TS_PACKET_LEN,
.pcap_filter = "tcp",
.pcap_snaplen = 256,
.port_args = 1,
.global_initialize = &ja4tscan_global_initialize,
//.thread_initialize = &ja4tscan_prepare_packet,
.prepare_packet = &ja4tscan_prepare_packet,
.make_packet = &ja4tscan_make_packet,
.print_packet = NULL,
.process_packet = &ja4tscan_process_packet,
.validate_packet = &ja4tscan_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.",
.output_type = OUTPUT_TYPE_STATIC,
.fields = fields,
.numfields = sizeof(fields) / sizeof(fields[0])};