2024-08-04 21:07:34 +00:00
|
|
|
#define _GNU_SOURCE
|
2024-08-12 22:59:04 +00:00
|
|
|
#include "types.h" // IWYU pragma: keep
|
2024-07-30 20:10:00 +00:00
|
|
|
#include "mangle.h"
|
2024-08-04 12:55:07 +00:00
|
|
|
#include "config.h"
|
2024-08-13 17:48:35 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "quic.h"
|
|
|
|
#include "logging.h"
|
2024-07-30 20:10:00 +00:00
|
|
|
|
2024-08-13 17:48:35 +00:00
|
|
|
#ifndef KERNEL_SCOPE
|
2024-08-12 22:59:04 +00:00
|
|
|
#include <stdlib.h>
|
2024-07-30 20:10:00 +00:00
|
|
|
#endif
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
int process_packet(const uint8_t *raw_payload, uint32_t raw_payload_len) {
|
|
|
|
if (raw_payload_len > MAX_PACKET_SIZE) {
|
|
|
|
return PKT_ACCEPT;
|
|
|
|
}
|
|
|
|
|
2024-08-13 17:48:35 +00:00
|
|
|
const struct iphdr *iph;
|
2024-08-27 16:27:27 +00:00
|
|
|
const struct ip6_hdr *ip6h;
|
2024-08-13 17:48:35 +00:00
|
|
|
uint32_t iph_len;
|
|
|
|
const uint8_t *ip_payload;
|
|
|
|
uint32_t ip_payload_len;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int transport_proto = -1;
|
|
|
|
int ipver = netproto_version(raw_payload, raw_payload_len);
|
2024-08-13 17:48:35 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
if (ipver == IP4VERSION) {
|
|
|
|
ret = ip4_payload_split((uint8_t *)raw_payload, raw_payload_len,
|
2024-08-13 17:48:35 +00:00
|
|
|
(struct iphdr **)&iph, &iph_len,
|
|
|
|
(uint8_t **)&ip_payload, &ip_payload_len);
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto accept;
|
|
|
|
|
|
|
|
transport_proto = iph ->protocol;
|
|
|
|
|
2024-08-27 17:01:34 +00:00
|
|
|
} else if (ipver == IP6VERSION && config.use_ipv6) {
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = ip6_payload_split((uint8_t *)raw_payload, raw_payload_len,
|
|
|
|
(struct ip6_hdr **)&ip6h, &iph_len,
|
|
|
|
(uint8_t **)&ip_payload, &ip_payload_len);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto accept;
|
|
|
|
|
|
|
|
transport_proto = ip6h->ip6_ctlun.ip6_un1.ip6_un1_nxt;
|
|
|
|
|
2024-08-27 17:01:34 +00:00
|
|
|
} else {
|
|
|
|
lgtracemsg("Unknown layer 3 protocol version: %d", ipver);
|
|
|
|
goto accept;
|
2024-08-27 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 17:48:35 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
switch (transport_proto) {
|
2024-08-13 17:48:35 +00:00
|
|
|
case IPPROTO_TCP:
|
2024-08-27 16:27:27 +00:00
|
|
|
return process_tcp_packet(raw_payload, raw_payload_len);
|
2024-08-13 17:48:35 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
return process_udp4_packet(raw_payload, raw_payload_len);
|
|
|
|
default:
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
accept:
|
|
|
|
return PKT_ACCEPT;
|
|
|
|
drop:
|
|
|
|
return PKT_DROP;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int process_tcp_packet(const uint8_t *raw_payload, uint32_t raw_payload_len) {
|
2024-08-29 11:45:27 +00:00
|
|
|
const void *ipxh;
|
|
|
|
uint32_t iph_len;
|
2024-08-11 21:23:08 +00:00
|
|
|
const struct tcphdr *tcph;
|
|
|
|
uint32_t tcph_len;
|
|
|
|
const uint8_t *data;
|
|
|
|
uint32_t dlen;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int ipxv = netproto_version(raw_payload, raw_payload_len);
|
|
|
|
|
|
|
|
lgtrace_start("TCP");
|
|
|
|
lgtrace_addp("IPv%d", ipxv);
|
|
|
|
|
|
|
|
int ret = tcp_payload_split((uint8_t *)raw_payload, raw_payload_len,
|
2024-08-29 11:45:27 +00:00
|
|
|
(void *)&ipxh, &iph_len,
|
2024-08-27 16:27:27 +00:00
|
|
|
(struct tcphdr **)&tcph, &tcph_len,
|
2024-08-11 21:23:08 +00:00
|
|
|
(uint8_t **)&data, &dlen);
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
goto accept;
|
2024-08-10 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-29 16:28:26 +00:00
|
|
|
if (tcph->syn && config.synfake) {
|
2024-08-29 11:45:27 +00:00
|
|
|
lgtrace_addp("TCP syn alter");
|
|
|
|
uint8_t payload[MAX_PACKET_SIZE];
|
|
|
|
memcpy(payload, ipxh, iph_len);
|
|
|
|
memcpy(payload + iph_len, tcph, tcph_len);
|
2024-08-29 12:49:01 +00:00
|
|
|
uint32_t fake_len = config.fake_sni_pkt_sz;
|
|
|
|
|
|
|
|
if (config.synfake_len)
|
|
|
|
fake_len = min(config.synfake_len, fake_len);
|
|
|
|
|
|
|
|
memcpy(payload + iph_len + tcph_len, config.fake_sni_pkt, fake_len);
|
2024-08-29 11:45:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct tcphdr *tcph = (struct tcphdr *)(payload + iph_len);
|
|
|
|
if (ipxv == IP4VERSION) {
|
|
|
|
struct iphdr *iph = (struct iphdr *)payload;
|
2024-08-29 12:49:01 +00:00
|
|
|
iph->tot_len = htons(iph_len + tcph_len + fake_len);
|
2024-08-29 11:45:27 +00:00
|
|
|
set_ip_checksum(payload, iph_len);
|
|
|
|
set_tcp_checksum(tcph, iph, iph_len);
|
|
|
|
} else if (ipxv == IP6VERSION) {
|
|
|
|
struct ip6_hdr *ip6h = (struct ip6_hdr *)payload;
|
2024-08-29 12:49:01 +00:00
|
|
|
ip6h->ip6_ctlun.ip6_un1.ip6_un1_plen =
|
|
|
|
ntohs(tcph_len + fake_len);
|
2024-08-29 11:45:27 +00:00
|
|
|
set_ip_checksum(ip6h, iph_len);
|
|
|
|
set_tcp_checksum(tcph, ip6h, iph_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 12:49:01 +00:00
|
|
|
ret = instance_config.send_raw_packet(payload, iph_len + tcph_len + fake_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("send_syn_altered", ret);
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
lgtrace_addp("rawsocket sent %d", ret);
|
2024-08-29 11:45:27 +00:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2024-08-29 12:55:05 +00:00
|
|
|
if (tcph->syn) goto accept;
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
struct tls_verdict vrd = analyze_tls_data(data, dlen);
|
|
|
|
|
|
|
|
if (vrd.target_sni) {
|
2024-08-13 17:48:35 +00:00
|
|
|
lgdebugmsg("Target SNI detected: %.*s", vrd.sni_len, data + vrd.sni_offset);
|
2024-08-11 21:23:08 +00:00
|
|
|
|
|
|
|
uint8_t payload[MAX_PACKET_SIZE];
|
|
|
|
uint32_t payload_len = raw_payload_len;
|
|
|
|
memcpy(payload, raw_payload, raw_payload_len);
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
void *iph;
|
2024-08-11 21:23:08 +00:00
|
|
|
uint32_t iph_len;
|
|
|
|
struct tcphdr *tcph;
|
|
|
|
uint32_t tcph_len;
|
|
|
|
uint8_t *data;
|
|
|
|
uint32_t dlen;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int ret = tcp_payload_split(payload, payload_len,
|
2024-08-11 21:23:08 +00:00
|
|
|
&iph, &iph_len, &tcph, &tcph_len,
|
|
|
|
&data, &dlen);
|
2024-08-11 21:45:30 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("tcp_payload_split in targ_sni", ret);
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
2024-08-11 21:45:30 +00:00
|
|
|
if (config.fk_winsize) {
|
|
|
|
tcph->window = htons(config.fk_winsize);
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
set_ip_checksum(iph, iph_len);
|
|
|
|
set_tcp_checksum(tcph, iph, iph_len);
|
2024-08-11 21:23:08 +00:00
|
|
|
|
|
|
|
if (dlen > 1480 && config.verbose) {
|
2024-08-13 17:48:35 +00:00
|
|
|
lgdebugmsg("WARNING! Client Hello packet is too big and may cause issues!");
|
2024-08-11 21:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config.fake_sni) {
|
|
|
|
post_fake_sni(iph, iph_len, tcph, tcph_len,
|
|
|
|
config.fake_sni_seq_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ipd_offset;
|
|
|
|
size_t mid_offset;
|
|
|
|
|
|
|
|
switch (config.fragmentation_strategy) {
|
|
|
|
case FRAG_STRAT_TCP: {
|
|
|
|
ipd_offset = vrd.sni_offset;
|
|
|
|
mid_offset = ipd_offset + vrd.sni_len / 2;
|
|
|
|
|
2024-08-17 09:51:53 +00:00
|
|
|
uint32_t poses[2];
|
|
|
|
int cnt = 0;
|
2024-08-11 21:23:08 +00:00
|
|
|
|
2024-08-17 09:51:53 +00:00
|
|
|
if (config.frag_sni_pos && dlen > config.frag_sni_pos) {
|
|
|
|
poses[cnt++] = config.frag_sni_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.frag_middle_sni) {
|
|
|
|
poses[cnt++] = mid_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt > 1 && poses[0] > poses[1]) {
|
|
|
|
uint32_t tmp = poses[0];
|
|
|
|
poses[0] = poses[1];
|
|
|
|
poses[1] = tmp;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = send_tcp_frags(payload, payload_len, poses, cnt, 0);
|
2024-08-11 21:23:08 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("tcp4 send frags", ret);
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
break;
|
2024-08-27 16:27:27 +00:00
|
|
|
case FRAG_STRAT_IP:
|
|
|
|
if (ipxv != IP4VERSION) {
|
2024-08-11 21:23:08 +00:00
|
|
|
ipd_offset = ((char *)data - (char *)tcph) + vrd.sni_offset;
|
|
|
|
mid_offset = ipd_offset + vrd.sni_len / 2;
|
|
|
|
mid_offset += 8 - mid_offset % 8;
|
|
|
|
|
2024-08-17 09:51:53 +00:00
|
|
|
uint32_t poses[2];
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
if (config.frag_sni_pos && dlen > config.frag_sni_pos) {
|
|
|
|
poses[cnt] = config.frag_sni_pos + ((char *)data - (char *)tcph);
|
|
|
|
poses[cnt] += 8 - poses[cnt] % 8;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.frag_middle_sni) {
|
|
|
|
poses[cnt++] = mid_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt > 1 && poses[0] > poses[1]) {
|
|
|
|
uint32_t tmp = poses[0];
|
|
|
|
poses[0] = poses[1];
|
|
|
|
poses[1] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = send_ip4_frags(payload, payload_len, poses, cnt, 0);
|
2024-08-11 21:23:08 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("ip4 send frags", ret);
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto drop;
|
2024-08-27 16:27:27 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
printf("WARNING: IP fragmentation is supported only for IPv4\n");
|
2024-08-11 21:23:08 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
ret = instance_config.send_raw_packet(payload, payload_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("raw pack send", ret);
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
accept:
|
2024-08-27 16:27:27 +00:00
|
|
|
lgtrace_addp("accept");
|
|
|
|
lgtrace_end();
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
return PKT_ACCEPT;
|
|
|
|
drop:
|
2024-08-27 16:27:27 +00:00
|
|
|
lgtrace_addp("drop");
|
|
|
|
lgtrace_end();
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
return PKT_DROP;
|
|
|
|
}
|
|
|
|
|
2024-08-13 17:48:35 +00:00
|
|
|
int process_udp4_packet(const uint8_t *pkt, uint32_t pktlen) {
|
|
|
|
const struct iphdr *iph;
|
|
|
|
uint32_t iph_len;
|
|
|
|
const struct udphdr *udph;
|
|
|
|
const uint8_t *data;
|
|
|
|
uint32_t dlen;
|
|
|
|
|
|
|
|
int ret = udp4_payload_split((uint8_t *)pkt, pktlen,
|
|
|
|
(struct iphdr **)&iph, &iph_len,
|
|
|
|
(struct udphdr **)&udph,
|
|
|
|
(uint8_t **)&data, &dlen);
|
|
|
|
|
|
|
|
lgtrace_start("Got udp packet");
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
lgtrace_addp("undefined");
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dlen > 10 && config.verbose >= VERBOSE_TRACE) {
|
|
|
|
printf("UDP payload start: [ ");
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
printf("%02x ", data[i]);
|
|
|
|
}
|
|
|
|
printf("], ");
|
|
|
|
}
|
|
|
|
|
|
|
|
lgtrace_addp("QUIC probe");
|
|
|
|
const struct quic_lhdr *qch;
|
|
|
|
uint32_t qch_len;
|
|
|
|
struct quic_cids qci;
|
|
|
|
uint8_t *quic_raw_payload;
|
|
|
|
uint32_t quic_raw_plen;
|
|
|
|
ret = quic_parse_data((uint8_t *)data, dlen,
|
|
|
|
(struct quic_lhdr **)&qch, &qch_len, &qci,
|
|
|
|
&quic_raw_payload, &quic_raw_plen);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
lgtrace_addp("undefined type");
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
lgtrace_addp("QUIC detected");
|
|
|
|
uint8_t qtype = qch->type;
|
|
|
|
|
|
|
|
if (config.quic_drop) {
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qch->version == QUIC_V1)
|
|
|
|
qtype = quic_convtype_v1(qtype);
|
|
|
|
else if (qch->version == QUIC_V2)
|
|
|
|
qtype = quic_convtype_v2(qtype);
|
|
|
|
|
|
|
|
if (qtype != QUIC_INITIAL_TYPE) {
|
|
|
|
lgtrace_addp("quic message type: %d", qtype);
|
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
lgtrace_addp("quic initial message");
|
|
|
|
|
|
|
|
accept:
|
|
|
|
lgtrace_addp("accepted");
|
|
|
|
lgtrace_end();
|
|
|
|
|
|
|
|
return PKT_ACCEPT;
|
|
|
|
drop:
|
|
|
|
lgtrace_addp("dropped");
|
|
|
|
lgtrace_end();
|
|
|
|
|
|
|
|
return PKT_DROP;
|
|
|
|
}
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
int send_ip4_frags(const uint8_t *packet, uint32_t pktlen, const uint32_t *poses, uint32_t poses_sz, uint32_t dvs) {
|
|
|
|
if (poses_sz == 0) {
|
|
|
|
if (config.seg2_delay && ((dvs > 0) ^ config.frag_sni_reverse)) {
|
|
|
|
if (!instance_config.send_delayed_packet) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance_config.send_delayed_packet(
|
|
|
|
packet, pktlen, config.seg2_delay);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return instance_config.send_raw_packet(
|
|
|
|
packet, pktlen);
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-10 23:10:18 +00:00
|
|
|
uint8_t frag1[MAX_PACKET_SIZE];
|
|
|
|
uint8_t frag2[MAX_PACKET_SIZE];
|
|
|
|
uint32_t f1len = MAX_PACKET_SIZE;
|
|
|
|
uint32_t f2len = MAX_PACKET_SIZE;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dvs > poses[0]) {
|
2024-08-13 17:48:35 +00:00
|
|
|
lgerror("send_frags: Recursive dvs(%d) is more than poses0(%d)", -EINVAL, dvs, poses[0]);
|
2024-08-10 23:10:18 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ip4_frag(packet, pktlen, poses[0] - dvs,
|
|
|
|
frag1, &f1len, frag2, &f2len);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2024-08-13 17:48:35 +00:00
|
|
|
lgerror("send_frags: frag: with context packet with size %d, position: %d, recursive dvs: %d", ret, pktlen, poses[0], dvs);
|
2024-08-10 23:10:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
if (config.frag_sni_reverse)
|
|
|
|
goto send_frag2;
|
|
|
|
send_frag1:
|
2024-08-10 23:10:18 +00:00
|
|
|
ret = send_ip4_frags(frag1, f1len, NULL, 0, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
if (config.frag_sni_reverse)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
send_frag2:
|
|
|
|
dvs += poses[0];
|
2024-08-10 23:10:18 +00:00
|
|
|
ret = send_ip4_frags(frag2, f2len, poses + 1, poses_sz - 1, dvs);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2024-08-11 21:23:08 +00:00
|
|
|
|
|
|
|
if (config.frag_sni_reverse)
|
|
|
|
goto send_frag1;
|
2024-08-10 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
out:
|
2024-08-10 23:10:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int send_tcp_frags(const uint8_t *packet, uint32_t pktlen, const uint32_t *poses, uint32_t poses_sz, uint32_t dvs) {
|
2024-08-10 23:10:18 +00:00
|
|
|
if (poses_sz == 0) {
|
2024-08-11 12:34:58 +00:00
|
|
|
if (config.seg2_delay && ((dvs > 0) ^ config.frag_sni_reverse)) {
|
2024-08-10 23:10:18 +00:00
|
|
|
if (!instance_config.send_delayed_packet) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance_config.send_delayed_packet(
|
|
|
|
packet, pktlen, config.seg2_delay);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
2024-08-27 16:27:27 +00:00
|
|
|
lgtrace_addp("raw send packet of %d bytes with %d dvs", pktlen, dvs);
|
2024-08-10 23:10:18 +00:00
|
|
|
return instance_config.send_raw_packet(
|
|
|
|
packet, pktlen);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint8_t frag1[MAX_PACKET_SIZE];
|
|
|
|
uint8_t frag2[MAX_PACKET_SIZE];
|
|
|
|
uint8_t fake_pad[MAX_PACKET_SIZE];
|
|
|
|
uint32_t f1len = MAX_PACKET_SIZE;
|
|
|
|
uint32_t f2len = MAX_PACKET_SIZE;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dvs > poses[0]) {
|
2024-08-13 17:48:35 +00:00
|
|
|
lgerror("send_frags: Recursive dvs(%d) is more than poses0(%d)", -EINVAL, dvs, poses[0]);
|
2024-08-10 23:10:18 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
|
|
|
|
ret = tcp_frag(packet, pktlen, poses[0] - dvs,
|
2024-08-10 23:10:18 +00:00
|
|
|
frag1, &f1len, frag2, &f2len);
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
lgtrace_addp("Packet split in %d bytes position of payload start, dvs: %d to two packets of %d and %d lengths", poses[0], dvs, f1len, f2len);
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
if (ret < 0) {
|
2024-08-27 16:27:27 +00:00
|
|
|
lgerror("send_frags: tcp_frag: with context packet with size %d, position: %d, recursive dvs: %d", ret, pktlen, poses[0], dvs);
|
2024-08-10 23:10:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
|
2024-08-11 12:34:58 +00:00
|
|
|
if (config.frag_sni_reverse)
|
2024-08-10 23:10:18 +00:00
|
|
|
goto send_frag2;
|
|
|
|
|
|
|
|
send_frag1:
|
|
|
|
{
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = send_tcp_frags(frag1, f1len, NULL, 0, 0);
|
2024-08-10 23:10:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-11 12:34:58 +00:00
|
|
|
if (config.frag_sni_reverse)
|
2024-08-10 23:10:18 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_fake:
|
2024-08-27 16:27:27 +00:00
|
|
|
// TODO
|
2024-08-10 23:10:18 +00:00
|
|
|
if (config.frag_sni_faked) {
|
|
|
|
uint32_t iphfl, tcphfl;
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = tcp_payload_split(frag2, f2len, NULL, &iphfl, NULL, &tcphfl, NULL, NULL);
|
2024-08-10 23:10:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("Invalid frag2", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
memcpy(fake_pad, frag2, iphfl + tcphfl);
|
|
|
|
memset(fake_pad + iphfl + tcphfl, 0, f2len - iphfl - tcphfl);
|
2024-08-21 09:25:13 +00:00
|
|
|
struct tcphdr *fakethdr = (void *)(fake_pad + iphfl);
|
|
|
|
if (config.faking_strategy == FAKE_STRAT_PAST_SEQ) {
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace("frag fake sent with %u -> ", ntohl(fakethdr->seq));
|
2024-08-21 09:25:13 +00:00
|
|
|
fakethdr->seq = htonl(ntohl(fakethdr->seq) - dvs);
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace_addp("%u, ", ntohl(fakethdr->seq));
|
2024-08-21 09:25:13 +00:00
|
|
|
}
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = fail_packet(fake_pad, f2len);
|
2024-08-10 23:10:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("Failed to fail packet", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = send_tcp_frags(fake_pad, f2len, NULL, 0, 0);
|
2024-08-10 23:10:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-11 12:34:58 +00:00
|
|
|
if (config.frag_sni_reverse)
|
|
|
|
goto send_frag1;
|
2024-08-10 23:10:18 +00:00
|
|
|
|
|
|
|
send_frag2:
|
|
|
|
{
|
|
|
|
dvs += poses[0];
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = send_tcp_frags(frag2, f2len, poses + 1, poses_sz - 1, dvs);
|
2024-08-10 23:10:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-11 12:34:58 +00:00
|
|
|
if (config.frag_sni_reverse)
|
2024-08-10 23:10:18 +00:00
|
|
|
goto send_fake;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int post_fake_sni(const void *iph, unsigned int iph_len,
|
2024-08-11 21:23:08 +00:00
|
|
|
const struct tcphdr *tcph, unsigned int tcph_len,
|
|
|
|
unsigned char sequence_len) {
|
2024-08-27 16:27:27 +00:00
|
|
|
uint8_t rfsiph[128];
|
2024-08-11 21:23:08 +00:00
|
|
|
uint8_t rfstcph[60];
|
|
|
|
int ret;
|
2024-08-10 23:10:18 +00:00
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
memcpy(rfsiph, iph, iph_len);
|
|
|
|
memcpy(rfstcph, tcph, tcph_len);
|
2024-08-10 23:10:18 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
void *fsiph = (void *)rfsiph;
|
2024-08-11 21:23:08 +00:00
|
|
|
struct tcphdr *fstcph = (void *)rfstcph;
|
2024-08-10 23:10:18 +00:00
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
for (int i = 0; i < sequence_len; i++) {
|
|
|
|
uint8_t fake_sni[MAX_PACKET_SIZE];
|
|
|
|
uint32_t fsn_len = MAX_PACKET_SIZE;
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = gen_fake_sni(fsiph, iph_len, fstcph, tcph_len,
|
|
|
|
fake_sni, &fsn_len);
|
2024-08-11 21:23:08 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("gen_fake_sni", ret);
|
|
|
|
return ret;
|
2024-08-10 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
lgtrace_addp("post fake sni #%d", i + 1);
|
2024-08-11 21:23:08 +00:00
|
|
|
ret = instance_config.send_raw_packet(fake_sni, fsn_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
lgerror("send fake sni", ret);
|
|
|
|
return ret;
|
2024-08-10 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
uint32_t iph_len;
|
|
|
|
uint32_t tcph_len;
|
|
|
|
uint32_t plen;
|
2024-08-27 16:27:27 +00:00
|
|
|
tcp_payload_split(
|
2024-08-11 21:23:08 +00:00
|
|
|
fake_sni, fsn_len,
|
2024-08-27 16:27:27 +00:00
|
|
|
&fsiph, &iph_len,
|
|
|
|
&fstcph, &tcph_len,
|
2024-08-11 21:23:08 +00:00
|
|
|
NULL, &plen);
|
2024-08-10 23:10:18 +00:00
|
|
|
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
fstcph->seq = htonl(ntohl(fstcph->seq) + plen);
|
|
|
|
memcpy(rfsiph, fsiph, iph_len);
|
|
|
|
memcpy(rfstcph, fstcph, tcph_len);
|
|
|
|
fsiph = (void *)rfsiph;
|
|
|
|
fstcph = (void *)rfstcph;
|
2024-08-10 23:10:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-11 21:23:08 +00:00
|
|
|
return 0;
|
2024-08-10 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 23:31:48 +00:00
|
|
|
void z_function(const char *str, int *zbuf, size_t len) {
|
|
|
|
zbuf[0] = len;
|
|
|
|
|
|
|
|
ssize_t lh = 0, rh = 1;
|
|
|
|
for (ssize_t i = 1; i < len; i++) {
|
|
|
|
zbuf[i] = 0;
|
|
|
|
if (i < rh) {
|
|
|
|
zbuf[i] = min(zbuf[i - lh], rh - i);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i + zbuf[i] < len && str[zbuf[i]] == str[i + zbuf[i]])
|
|
|
|
zbuf[i]++;
|
2024-08-04 09:57:16 +00:00
|
|
|
|
2024-08-14 23:31:48 +00:00
|
|
|
if (i + zbuf[i] > rh) {
|
|
|
|
lh = i;
|
|
|
|
rh = i + zbuf[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-04 09:57:16 +00:00
|
|
|
|
2024-07-30 20:10:00 +00:00
|
|
|
#define TLS_CONTENT_TYPE_HANDSHAKE 0x16
|
|
|
|
#define TLS_HANDSHAKE_TYPE_CLIENT_HELLO 0x01
|
|
|
|
#define TLS_EXTENSION_SNI 0x0000
|
|
|
|
#define TLS_EXTENSION_CLIENT_HELLO_ENCRYPTED 0xfe0d
|
|
|
|
|
2024-08-12 22:59:04 +00:00
|
|
|
typedef uint8_t uint8_t;
|
|
|
|
typedef uint32_t uint32_t;
|
|
|
|
typedef uint16_t uint16_t;
|
2024-07-30 20:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes tls payload of the tcp request.
|
|
|
|
*
|
|
|
|
* data Payload data of TCP.
|
|
|
|
* dlen Length of `data`.
|
|
|
|
*/
|
2024-08-11 08:40:13 +00:00
|
|
|
struct tls_verdict analyze_tls_data(
|
2024-07-30 20:10:00 +00:00
|
|
|
const uint8_t *data,
|
|
|
|
uint32_t dlen)
|
|
|
|
{
|
2024-08-11 08:40:13 +00:00
|
|
|
struct tls_verdict vrd = {0};
|
2024-07-30 20:10:00 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
const uint8_t *data_end = data + dlen;
|
|
|
|
|
|
|
|
while (i + 4 < dlen) {
|
|
|
|
const uint8_t *msgData = data + i;
|
|
|
|
|
|
|
|
uint8_t tls_content_type = *msgData;
|
|
|
|
uint8_t tls_vmajor = *(msgData + 1);
|
|
|
|
uint8_t tls_vminor = *(msgData + 2);
|
|
|
|
uint16_t message_length = ntohs(*(uint16_t *)(msgData + 3));
|
|
|
|
const uint8_t *message_length_ptr = msgData + 3;
|
|
|
|
|
2024-08-14 23:31:48 +00:00
|
|
|
if (tls_vmajor != 0x03) goto nextMessage;
|
2024-07-30 20:10:00 +00:00
|
|
|
|
2024-08-14 23:31:48 +00:00
|
|
|
if (i + 5 > dlen) break;
|
2024-07-30 20:10:00 +00:00
|
|
|
|
|
|
|
if (tls_content_type != TLS_CONTENT_TYPE_HANDSHAKE)
|
|
|
|
goto nextMessage;
|
|
|
|
|
2024-08-14 23:31:48 +00:00
|
|
|
if (config.sni_detection == SNI_DETECTION_BRUTE) {
|
|
|
|
goto brute;
|
|
|
|
}
|
2024-07-30 20:10:00 +00:00
|
|
|
|
|
|
|
const uint8_t *handshakeProto = msgData + 5;
|
|
|
|
|
|
|
|
if (handshakeProto + 1 >= data_end) break;
|
|
|
|
|
|
|
|
uint8_t handshakeType = *handshakeProto;
|
|
|
|
|
|
|
|
if (handshakeType != TLS_HANDSHAKE_TYPE_CLIENT_HELLO)
|
|
|
|
goto nextMessage;
|
|
|
|
|
|
|
|
const uint8_t *msgPtr = handshakeProto;
|
|
|
|
msgPtr += 1;
|
|
|
|
const uint8_t *handshakeProto_length_ptr = msgPtr + 1;
|
|
|
|
msgPtr += 3 + 2 + 32;
|
|
|
|
|
|
|
|
if (msgPtr + 1 >= data_end) break;
|
|
|
|
uint8_t sessionIdLength = *msgPtr;
|
|
|
|
msgPtr++;
|
|
|
|
msgPtr += sessionIdLength;
|
|
|
|
|
|
|
|
if (msgPtr + 2 >= data_end) break;
|
|
|
|
uint16_t ciphersLength = ntohs(*(uint16_t *)msgPtr);
|
|
|
|
msgPtr += 2;
|
|
|
|
msgPtr += ciphersLength;
|
|
|
|
|
|
|
|
if (msgPtr + 1 >= data_end) break;
|
|
|
|
uint8_t compMethodsLen = *msgPtr;
|
|
|
|
msgPtr++;
|
|
|
|
msgPtr += compMethodsLen;
|
|
|
|
|
|
|
|
if (msgPtr + 2 >= data_end) break;
|
|
|
|
uint16_t extensionsLen = ntohs(*(uint16_t *)msgPtr);
|
|
|
|
const uint8_t *extensionsLen_ptr = msgPtr;
|
|
|
|
msgPtr += 2;
|
|
|
|
|
|
|
|
const uint8_t *extensionsPtr = msgPtr;
|
|
|
|
const uint8_t *extensions_end = extensionsPtr + extensionsLen;
|
2024-08-14 23:31:48 +00:00
|
|
|
if (extensions_end > data_end) extensions_end = data_end;
|
2024-07-30 20:10:00 +00:00
|
|
|
|
|
|
|
while (extensionsPtr < extensions_end) {
|
|
|
|
const uint8_t *extensionPtr = extensionsPtr;
|
|
|
|
if (extensionPtr + 4 >= extensions_end) break;
|
|
|
|
|
|
|
|
uint16_t extensionType =
|
|
|
|
ntohs(*(uint16_t *)extensionPtr);
|
|
|
|
extensionPtr += 2;
|
|
|
|
|
|
|
|
uint16_t extensionLen =
|
|
|
|
ntohs(*(uint16_t *)extensionPtr);
|
|
|
|
const uint8_t *extensionLen_ptr = extensionPtr;
|
|
|
|
extensionPtr += 2;
|
|
|
|
|
|
|
|
|
|
|
|
if (extensionPtr + extensionLen > extensions_end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (extensionType != TLS_EXTENSION_SNI)
|
|
|
|
goto nextExtension;
|
|
|
|
|
|
|
|
const uint8_t *sni_ext_ptr = extensionPtr;
|
|
|
|
|
|
|
|
if (sni_ext_ptr + 2 >= extensions_end) break;
|
|
|
|
uint16_t sni_ext_dlen = ntohs(*(uint16_t *)sni_ext_ptr);
|
|
|
|
|
|
|
|
const uint8_t *sni_ext_dlen_ptr = sni_ext_ptr;
|
|
|
|
sni_ext_ptr += 2;
|
|
|
|
|
|
|
|
const uint8_t *sni_ext_end = sni_ext_ptr + sni_ext_dlen;
|
|
|
|
if (sni_ext_end >= extensions_end) break;
|
|
|
|
|
|
|
|
if (sni_ext_ptr + 3 >= sni_ext_end) break;
|
|
|
|
uint8_t sni_type = *sni_ext_ptr++;
|
|
|
|
uint16_t sni_len = ntohs(*(uint16_t *)sni_ext_ptr);
|
|
|
|
sni_ext_ptr += 2;
|
|
|
|
|
|
|
|
if (sni_ext_ptr + sni_len > sni_ext_end) break;
|
|
|
|
|
|
|
|
char *sni_name = (char *)sni_ext_ptr;
|
|
|
|
|
|
|
|
vrd.sni_offset = (uint8_t *)sni_name - data;
|
|
|
|
vrd.sni_len = sni_len;
|
|
|
|
|
2024-08-08 12:29:04 +00:00
|
|
|
if (config.all_domains) {
|
|
|
|
vrd.target_sni = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int j = 0;
|
2024-08-08 15:19:25 +00:00
|
|
|
for (unsigned int i = 0; i <= config.domains_strlen; i++) {
|
2024-08-08 17:26:35 +00:00
|
|
|
if ( i > j &&
|
|
|
|
(i == config.domains_strlen ||
|
2024-08-08 15:19:25 +00:00
|
|
|
config.domains_str[i] == '\0' ||
|
|
|
|
config.domains_str[i] == ',' ||
|
2024-08-08 17:26:35 +00:00
|
|
|
config.domains_str[i] == '\n' )) {
|
2024-08-08 15:19:25 +00:00
|
|
|
|
2024-08-08 12:29:04 +00:00
|
|
|
unsigned int domain_len = (i - j);
|
|
|
|
const char *sni_startp = sni_name + sni_len - domain_len;
|
|
|
|
const char *domain_startp = config.domains_str + j;
|
|
|
|
|
|
|
|
if (sni_len >= domain_len &&
|
|
|
|
sni_len < 128 &&
|
|
|
|
!strncmp(sni_startp,
|
|
|
|
domain_startp,
|
|
|
|
domain_len)) {
|
|
|
|
vrd.target_sni = 1;
|
|
|
|
}
|
2024-07-30 20:10:00 +00:00
|
|
|
|
2024-08-08 12:29:04 +00:00
|
|
|
j = i + 1;
|
|
|
|
}
|
2024-07-30 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextExtension:
|
|
|
|
extensionsPtr += 2 + 2 + extensionLen;
|
|
|
|
}
|
|
|
|
nextMessage:
|
|
|
|
i += 5 + message_length;
|
|
|
|
}
|
|
|
|
|
2024-08-08 12:29:04 +00:00
|
|
|
out:
|
2024-07-30 20:10:00 +00:00
|
|
|
return vrd;
|
2024-08-14 23:31:48 +00:00
|
|
|
|
|
|
|
brute:
|
|
|
|
if (config.all_domains) {
|
|
|
|
vrd.target_sni = 1;
|
|
|
|
vrd.sni_len = 0;
|
|
|
|
vrd.sni_offset = dlen / 2;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int j = 0;
|
|
|
|
for (unsigned int i = 0; i <= config.domains_strlen; i++) {
|
|
|
|
if ( i > j &&
|
|
|
|
(i == config.domains_strlen ||
|
|
|
|
config.domains_str[i] == '\0' ||
|
|
|
|
config.domains_str[i] == ',' ||
|
|
|
|
config.domains_str[i] == '\n' )) {
|
|
|
|
|
|
|
|
uint8_t buf[MAX_PACKET_SIZE];
|
|
|
|
int zbuf[MAX_PACKET_SIZE];
|
|
|
|
unsigned int domain_len = (i - j);
|
|
|
|
const char *domain_startp = config.domains_str + j;
|
|
|
|
|
|
|
|
if (domain_len + dlen + 1> MAX_PACKET_SIZE) continue;
|
|
|
|
|
|
|
|
memcpy(buf, domain_startp, domain_len);
|
|
|
|
memcpy(buf + domain_len, "#", 1);
|
|
|
|
memcpy(buf + domain_len + 1, data, dlen);
|
|
|
|
|
|
|
|
z_function((char *)buf, zbuf, domain_len + 1 + dlen);
|
|
|
|
|
|
|
|
for (unsigned int k = 0; k < dlen; k++) {
|
|
|
|
if (zbuf[k] == domain_len) {
|
|
|
|
vrd.target_sni = 1;
|
|
|
|
vrd.sni_len = domain_len;
|
|
|
|
vrd.sni_offset = (k - domain_len - 1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
j = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
2024-07-30 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int gen_fake_sni(const void *ipxh, uint32_t iph_len,
|
|
|
|
const struct tcphdr *tcph, uint32_t tcph_len,
|
2024-08-03 23:20:09 +00:00
|
|
|
uint8_t *buf, uint32_t *buflen) {
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
if (!ipxh || !tcph || !buf || !buflen)
|
2024-08-03 23:20:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int ipxv = netproto_version(ipxh, iph_len);
|
|
|
|
|
|
|
|
if (ipxv == IP4VERSION) {
|
|
|
|
const struct iphdr *iph = ipxh;
|
|
|
|
|
|
|
|
memcpy(buf, iph, iph_len);
|
|
|
|
struct iphdr *niph = (struct iphdr *)buf;
|
|
|
|
|
|
|
|
niph->protocol = IPPROTO_TCP;
|
|
|
|
} else if (ipxv == IP6VERSION) {
|
|
|
|
const struct ip6_hdr *iph = ipxh;
|
|
|
|
|
|
|
|
iph_len = sizeof(struct ip6_hdr);
|
|
|
|
memcpy(buf, iph, iph_len);
|
|
|
|
struct ip6_hdr *niph = (struct ip6_hdr *)buf;
|
|
|
|
|
|
|
|
niph->ip6_ctlun.ip6_un1.ip6_un1_nxt = IPPROTO_TCP;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2024-08-03 23:20:09 +00:00
|
|
|
|
2024-08-11 12:34:58 +00:00
|
|
|
const char *data = config.fake_sni_pkt;
|
|
|
|
size_t data_len = config.fake_sni_pkt_sz;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
size_t dlen = iph_len + tcph_len + data_len;
|
2024-08-03 23:20:09 +00:00
|
|
|
|
|
|
|
if (*buflen < dlen)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
memcpy(buf + iph_len, tcph, tcph_len);
|
|
|
|
memcpy(buf + iph_len + tcph_len, data, data_len);
|
2024-08-11 12:34:58 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
struct tcphdr *ntcph = (struct tcphdr *)(buf + iph_len);
|
2024-08-03 23:20:09 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
if (ipxv == IP4VERSION) {
|
|
|
|
struct iphdr *niph = (struct iphdr *)buf;
|
|
|
|
niph->tot_len = htons(dlen);
|
|
|
|
} else if (ipxv == IP6VERSION) {
|
|
|
|
struct ip6_hdr *niph = (struct ip6_hdr *)buf;
|
|
|
|
niph->ip6_ctlun.ip6_un1.ip6_un1_plen = htons(dlen - iph_len);
|
|
|
|
}
|
2024-08-03 23:20:09 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
fail_packet(buf, *buflen);
|
2024-08-03 23:20:09 +00:00
|
|
|
|
2024-08-11 12:34:58 +00:00
|
|
|
*buflen = dlen;
|
2024-08-27 16:27:27 +00:00
|
|
|
|
2024-08-03 23:20:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-08-10 23:10:18 +00:00
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
int fail_packet(uint8_t *payload, uint32_t plen) {
|
|
|
|
void *iph;
|
2024-08-10 23:10:18 +00:00
|
|
|
uint32_t iph_len;
|
|
|
|
struct tcphdr *tcph;
|
|
|
|
uint32_t tcph_len;
|
|
|
|
uint8_t *data;
|
|
|
|
uint32_t dlen;
|
|
|
|
int ret;
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
ret = tcp_payload_split(payload, plen,
|
2024-08-10 23:10:18 +00:00
|
|
|
&iph, &iph_len, &tcph, &tcph_len,
|
|
|
|
&data, &dlen);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
|
2024-08-16 19:47:55 +00:00
|
|
|
if (config.faking_strategy == FAKE_STRAT_RAND_SEQ) {
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace("fake seq: %u -> ", ntohl(tcph->seq));
|
2024-08-27 20:23:54 +00:00
|
|
|
|
|
|
|
if (config.fakeseq_offset) {
|
|
|
|
tcph->seq = htonl(ntohl(tcph->seq) - config.fakeseq_offset);
|
|
|
|
} else {
|
2024-08-13 17:48:35 +00:00
|
|
|
#ifdef KERNEL_SCOPE
|
2024-08-27 20:23:54 +00:00
|
|
|
tcph->seq = 124;
|
2024-08-13 17:48:35 +00:00
|
|
|
#else
|
2024-08-27 20:23:54 +00:00
|
|
|
tcph->seq = random();
|
2024-08-13 17:48:35 +00:00
|
|
|
#endif
|
2024-08-27 20:23:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace_addp("%u", ntohl(tcph->seq));
|
2024-08-16 19:47:55 +00:00
|
|
|
} else if (config.faking_strategy == FAKE_STRAT_PAST_SEQ) {
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace("fake seq: %u -> ", ntohl(tcph->seq));
|
2024-08-16 19:47:55 +00:00
|
|
|
tcph->seq = htonl(ntohl(tcph->seq) - dlen);
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace_addp("%u", ntohl(tcph->seq));
|
2024-08-21 09:25:13 +00:00
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
} else if (config.faking_strategy == FAKE_STRAT_TTL) {
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace_addp("set fake ttl to %d", config.faking_ttl);
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
uint32_t ipxv = netproto_version(payload, plen);
|
|
|
|
if (ipxv == IP4VERSION) {
|
|
|
|
((struct iphdr *)iph)->ttl = config.faking_ttl;
|
|
|
|
} else if (ipxv == IP6VERSION) {
|
|
|
|
((struct ip6_hdr *)iph)->ip6_ctlun.ip6_un1.ip6_un1_hlim = config.faking_ttl;
|
|
|
|
} else {
|
|
|
|
lgerror("fail_packet: IP version is unsupported", -EINVAL);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2024-08-10 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 16:27:27 +00:00
|
|
|
set_ip_checksum(iph, iph_len);
|
|
|
|
set_tcp_checksum(tcph, iph, iph_len);
|
2024-08-10 23:10:18 +00:00
|
|
|
|
2024-08-16 19:47:55 +00:00
|
|
|
if (config.faking_strategy == FAKE_STRAT_TCP_CHECK) {
|
2024-08-27 18:21:33 +00:00
|
|
|
lgtrace_addp("break fake tcp checksum");
|
2024-08-16 19:47:55 +00:00
|
|
|
tcph->check += 1;
|
|
|
|
}
|
|
|
|
|
2024-08-10 23:10:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|