feat: multi-distro VM tests, ja4ebpf eBPF improvements, bot-detector scoring

ja4ebpf:
- Refactor BPF TC capture with improved SYN offset handling and TCP option parsing
- Enhance TLS uprobe SSL hooking for better key extraction
- Add ClickHouse writer improvements for HTTP log materialized views
- Update RPM spec for Rocky Linux 8/9/10, fix systemd service
- Simplify loader with cleaner bpf2go integration

bot-detector:
- Add H2 SETTINGS per-parameter comparison in browser_matcher
- Enhance browser signatures and scoring pipeline
- Improve preprocessing and cycle detection

infra:
- Multi-distro Vagrantfile (centos8, rocky9, rocky10) with per-distro provisioning
- New Makefile targets: vm-up-all, test-vm-matrix, test-vm-centos8/rocky10
- Add debug helpers and run-test-from-host.sh for host-driven VM testing
- Update run-tests-vm.sh for cross-distro compatibility
- Remove accidental binary blob (\004)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jacquin Antoine
2026-04-13 01:09:33 +02:00
parent d81463a589
commit d75825278e
32 changed files with 2148 additions and 890 deletions

View File

@ -45,13 +45,17 @@ struct tcp_syn_event {
/* ---------------------------------------------------------------------------
* Événement TLS ClientHello : émis quand un ClientHello TLS est détecté
*
* IMPORTANT : le payload est à l'offset 0 pour que bpf_skb_load_bytes()
* puisse écrire directement au début du map value (compatible kernel 4.18).
* Les métadonnées sont placées APRÈS le payload.
* ---------------------------------------------------------------------------*/
struct tls_hello_event {
__u32 src_ip; /* adresse source (host byte order, via bpf_ntohl) */
__u16 src_port; /* port source (host byte order) */
__u8 payload[2048]; /* payload ClientHello brut (capturé jusqu'à 2048 octets) */
__u16 payload_len; /* longueur effective du payload */
__u64 timestamp_ns; /* horodatage kernel */
__u8 payload[2048]; /* payload ClientHello brut (offset 0) */
__u32 src_ip; /* adresse source (host byte order) */
__u16 src_port; /* port source (host byte order) */
__u16 payload_len; /* longueur effective du payload */
__u64 timestamp_ns; /* horodatage kernel */
} __attribute__((packed));
/* ---------------------------------------------------------------------------
@ -80,16 +84,14 @@ struct accept_event {
} __attribute__((packed));
/* ---------------------------------------------------------------------------
* Événement HTTP en clair : émis pour chaque segment TCP porteur d'un
* payload HTTP (port 80 ou 8080). Un seul segment par requête est capturé
* (le premier, qui contient la request-line et les en-têtes).
* Événement HTTP en clair : payload à l'offset 0 pour compat kernel 4.18.
* ---------------------------------------------------------------------------*/
struct http_plain_event {
__u8 payload[4096]; /* payload TCP brut (offset 0) */
__u32 src_ip; /* adresse source (host byte order) */
__u32 dst_ip; /* adresse destination (host byte order) */
__u16 src_port; /* port source (host byte order) */
__u16 dst_port; /* port destination 80 ou 8080 */
__u8 payload[4096]; /* payload TCP brut (request-line + headers) */
__u16 payload_len; /* longueur effective du payload copié */
__u64 timestamp_ns; /* horodatage kernel */
} __attribute__((packed));
@ -124,35 +126,65 @@ struct accept_key {
* Déclarations des maps eBPF avec annotations BTF
* ===========================================================================*/
/* Ring buffer : événements TCP SYN (16 MB) */
/* Perf event array : événements TCP SYN (kernel 4.4+) */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
} rb_tcp_syn SEC(".maps");
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} pb_tcp_syn SEC(".maps");
/* Ring buffer : événements TLS ClientHello (16 MB) */
/* Perf event array : événements TLS ClientHello (kernel 4.4+) */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
} rb_tls_hello SEC(".maps");
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} pb_tls_hello SEC(".maps");
/* Ring buffer : données SSL déchiffrées (64 MB, plus volumineux) */
/* Perf event array : données SSL déchiffrées (kernel 4.4+) */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 26);
} rb_ssl_data SEC(".maps");
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} pb_ssl_data SEC(".maps");
/* Ring buffer : événements accept4 (4 MB) */
/* Perf event array : événements accept4 (kernel 4.4+) */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 22);
} rb_accept SEC(".maps");
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} pb_accept SEC(".maps");
/* Ring buffer : payload HTTP en clair port 80/8080 (32 MB) */
/* Perf event array : payload HTTP en clair port 80/8080 (kernel 4.4+) */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 25);
} rb_http_plain SEC(".maps");
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} pb_http_plain SEC(".maps");
/* ── PERCPU_ARRAY temporaires pour les structs > 512o (stack eBPF) ──── */
/* TLS hello event : 2064 octets, ne tient pas sur la stack */
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct tls_hello_event);
} __tls_buf SEC(".maps");
/* HTTP plain event : 4118 octets */
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct http_plain_event);
} __http_buf SEC(".maps");
/* SSL data event : 4131 octets */
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct ssl_data_event);
} __ssl_buf SEC(".maps");
/* Hash map : pid_tgid → ssl_read_args (arguments SSL_read entry) */
struct {

View File

@ -1,16 +1,18 @@
/* ============================================================================
* tc_capture.c — Programme XDP ingress : capture des TCP SYN et TLS ClientHello
* tc_capture.c — Programme TC ingress : capture des TCP SYN, TLS ClientHello
* et HTTP en clair
*
* Remplace l'ancienne version TC (SCHED_CLS + TCX) par un hook XDP compatible
* depuis le kernel 4.8. Utilisé en mode XDP_GENERIC sur Rocky Linux 9 (5.14).
* Hook TC ingress (clsact qdisc) compatible kernel 4.1+.
* Émet via bpf_perf_event_output() (kernel 4.4+) pour compatibilité maximale.
*
* Conventions vérificateur eBPF :
* - Tous les accès mémoire paquet utilisent de l'arithmétique de pointeur
* directe avec bornes explicites (data / data_end).
* - Les copies de longueur variable utilisent des boucles bornées (sans
* #pragma unroll) : le vérificateur kernel ≥ 5.3 les accepte nativement.
* - Les options TCP sont copiées brutes ; MSS et Window Scale sont extraits
* côté Go (userspace) depuis le tableau tcp_options_raw.
* IMPORTANT : Ce programme n'utilise AUCUN accès direct au paquet (data/data_end).
* Toutes les lectures se font via bpf_skb_load_bytes() (kernel 4.5+) avec des
* tailles constantes, pour compatibilité avec le vérificateur kernel 4.18 qui
* rejette "math between pkt pointer and register with unbounded min value".
*
* Les copies de payload utilisent bpf_skb_load_bytes() avec &= (2^n - 1)
* pour borner la taille per le vérificateur.
* Les structs > 512o utilisent un PERCPU_ARRAY temporaire (stack limit eBPF).
* ============================================================================ */
#include "vmlinux.h"
@ -19,219 +21,248 @@
#include <bpf/bpf_core_read.h>
#include "bpf_types.h"
/* Constantes Ethernet */
/* Constantes */
#define ETH_P_IP 0x0800
#define ETH_HLEN 14
/* Constantes IP */
#define IPPROTO_TCP 6
#define IP_DF 0x4000
/* Constantes TCP */
#define TH_SYN 0x02
#define TH_ACK 0x10
#define TH_FIN 0x01
#define TH_RST 0x04
/* Ports */
#define HTTPS_PORT 443
#define HTTP_PORT 80
#define HTTP_ALT_PORT 8080
/* TLS */
#define TLS_CONTENT_HANDSHAKE 0x16
#define TLS_MSG_CLIENT_HELLO 0x01
/* Tailles maximales des payloads copiés */
#define MAX_TLS_PAYLOAD 2048
#define MAX_HTTP_PAYLOAD 1024
#define MAX_TCP_OPTIONS 40
/* Structure Ethernet locale (évite d'inclure linux/if_ether.h) */
struct ethhdr_local {
__u8 h_dest[6];
__u8 h_source[6];
__be16 h_proto;
} __attribute__((packed));
/* Counter map for debug */
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 7);
__type(key, __u32);
__type(value, __u64);
} tc_stats SEC(".maps");
#define STAT_TOTAL 0
#define STAT_IPV4 1
#define STAT_TCP 2
#define STAT_SYN 3
#define STAT_SYN_SUBMIT 4
#define STAT_TLS_SUBMIT 5
#define STAT_HTTP_SUBMIT 6
/* ---------------------------------------------------------------------------
* capture_xdp — Point d'entrée XDP ingress
* capture_tc — Point d'entrée TC ingress (clsact)
*
* Observe chaque paquet ingress en lecture seule (retourne toujours XDP_PASS).
* Émet des événements vers les ring buffers pour TCP SYN, TLS ClientHello
* et les payloads HTTP en clair.
* AUCUN accès direct au paquet. Tout via bpf_skb_load_bytes() + tailles constantes.
* Compatible vérificateur kernel 4.18.
* ---------------------------------------------------------------------------*/
SEC("xdp")
int capture_xdp(struct xdp_md *ctx)
SEC("tc")
int capture_tc(struct __sk_buff *ctx)
{
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
__u32 key;
__u64 *cnt;
__u32 pkt_len = ctx->len;
/* --- Ethernet --- */
struct ethhdr_local *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
if (bpf_ntohs(eth->h_proto) != ETH_P_IP)
return XDP_PASS;
key = STAT_TOTAL;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
/* --- IPv4 --- */
struct iphdr *ip = data + ETH_HLEN;
if ((void *)(ip + 1) > data_end)
return XDP_PASS;
if (ip->protocol != IPPROTO_TCP)
return XDP_PASS;
/* --- Ethernet : vérifier type IPv4 --- */
if (pkt_len < ETH_HLEN + 20 + 20)
return TC_ACT_OK;
__u32 ihl = ip->ihl & 0x0F;
if (ihl < 5)
return XDP_PASS;
__u32 ip_hlen = ihl << 2; /* ∈ [20, 60] */
__be16 h_proto;
bpf_skb_load_bytes(ctx, 12, &h_proto, 2);
if (h_proto != bpf_htons(ETH_P_IP))
return TC_ACT_OK;
__u32 src_ip = ip->saddr;
__u32 dst_ip = ip->daddr;
__u8 ttl = ip->ttl;
__u16 ip_id = bpf_ntohs(ip->id);
__u16 frag_off = bpf_ntohs(ip->frag_off);
/* --- IPv4 : lire le header (20 octets min) --- */
key = STAT_IPV4;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
struct iphdr iph;
bpf_skb_load_bytes(ctx, ETH_HLEN, &iph, sizeof(iph));
if (iph.protocol != IPPROTO_TCP)
return TC_ACT_OK;
__u32 ihl = iph.ihl & 0x0F;
if (ihl < 5 || ihl > 15)
return TC_ACT_OK;
__u32 ip_hlen = ihl << 2;
if (ip_hlen < 20 || ip_hlen > 60)
return TC_ACT_OK;
__u32 src_ip = iph.saddr;
__u32 dst_ip = iph.daddr;
__u8 ttl = iph.ttl;
__u16 ip_id = bpf_ntohs(iph.id);
__u16 frag_off = bpf_ntohs(iph.frag_off);
__u8 df_bit = (frag_off & IP_DF) ? 1 : 0;
/* --- TCP à offset variable --- */
struct tcphdr *tcp = (void *)ip + ip_hlen;
if ((void *)(tcp + 1) > data_end) /* valide tcp[0..19] */
return XDP_PASS;
/* --- TCP : lire le header (20 octets) --- */
__u32 tcp_off = ETH_HLEN + ip_hlen;
if (pkt_len < tcp_off + 20)
return TC_ACT_OK;
__u16 src_port = bpf_ntohs(tcp->source);
__u16 dst_port = bpf_ntohs(tcp->dest);
__u16 window = bpf_ntohs(tcp->window);
key = STAT_TCP;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
struct tcphdr tcph;
bpf_skb_load_bytes(ctx, tcp_off, &tcph, sizeof(tcph));
__u16 src_port = bpf_ntohs(tcph.source);
__u16 dst_port = bpf_ntohs(tcph.dest);
__u16 window = bpf_ntohs(tcph.window);
/* Flags via les champs de bits du struct (sûr pour le vérificateur) */
__u8 tcp_flags = 0;
if (tcp->syn) tcp_flags |= TH_SYN;
if (tcp->ack) tcp_flags |= TH_ACK;
if (tcp->fin) tcp_flags |= TH_FIN;
if (tcp->rst) tcp_flags |= TH_RST;
if (tcph.syn) tcp_flags |= TH_SYN;
if (tcph.ack) tcp_flags |= TH_ACK;
if (tcph.fin) tcp_flags |= TH_FIN;
if (tcph.rst) tcp_flags |= TH_RST;
__u32 doff = tcp->doff;
if (doff < 5)
return XDP_PASS;
__u32 tcp_hlen = doff << 2; /* ∈ [20, 60] */
__u32 doff = tcph.doff;
if (doff < 5 || doff > 15)
return TC_ACT_OK;
__u32 tcp_hlen = doff << 2;
if (tcp_hlen < 20 || tcp_hlen > 60)
return TC_ACT_OK;
/* Offset du payload applicatif */
void *payload = (void *)tcp + tcp_hlen;
__u32 payload_off = ETH_HLEN + ip_hlen + tcp_hlen;
/* ===================================================================
* TCP SYN : extraction des paramètres L3/L4
* TCP SYN
* ===================================================================*/
if ((tcp_flags & TH_SYN) && !(tcp_flags & TH_ACK)) {
struct tcp_syn_event *evt =
bpf_ringbuf_reserve(&rb_tcp_syn, sizeof(*evt), 0);
if (!evt)
return XDP_PASS;
key = STAT_SYN;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
evt->src_ip = bpf_ntohl(src_ip);
evt->dst_ip = bpf_ntohl(dst_ip);
evt->src_port = src_port;
evt->dst_port = dst_port;
evt->ttl = ttl;
evt->df_bit = df_bit;
evt->ip_id = ip_id;
evt->window_size = window;
evt->window_scale = 0xFF; /* défaut = absent */
evt->mss = 0;
evt->timestamp_ns = bpf_ktime_get_ns();
evt->tcp_options_len = 0;
struct tcp_syn_event evt = {};
evt.src_ip = bpf_ntohl(src_ip);
evt.dst_ip = bpf_ntohl(dst_ip);
evt.src_port = src_port;
evt.dst_port = dst_port;
evt.ttl = ttl;
evt.df_bit = df_bit;
evt.ip_id = ip_id;
evt.window_size = window;
evt.window_scale = 0xFF;
evt.mss = 0;
evt.timestamp_ns = bpf_ktime_get_ns();
evt.tcp_options_len = 0;
/* Copie brute des options TCP (MSS/WS extraits en userspace Go).
* Boucle bornée à MAX_TCP_OPTIONS = 40 itérations : triviale pour
* le vérificateur kernel ≥ 5.3, sans #pragma unroll. */
__u8 *opts_start = (__u8 *)(tcp + 1); /* après les 20 octets fixes */
__u32 opts_len = tcp_hlen - 20; /* ∈ [0, 40] */
if (opts_len > MAX_TCP_OPTIONS)
opts_len = MAX_TCP_OPTIONS;
if (opts_len > 0) {
#pragma clang loop unroll(disable)
for (__u32 i = 0; i < MAX_TCP_OPTIONS; i++) {
if (i >= opts_len)
break;
if (opts_start + i + 1 > (__u8 *)data_end)
break;
evt->tcp_options_raw[i] = opts_start[i];
}
evt->tcp_options_len = (__u8)opts_len;
/* Copie des options TCP via bpf_skb_load_bytes avec taille constante.
* On lit MAX_TCP_OPTIONS=40 octets depuis le début des options.
* Si le paquet est trop court, l'appel échoue → options absentes. */
__u32 opts_off = tcp_off + 20;
__u32 opts_len = tcp_hlen - 20;
if (opts_len > 0 && opts_len <= MAX_TCP_OPTIONS &&
opts_off + MAX_TCP_OPTIONS <= pkt_len) {
bpf_skb_load_bytes(ctx, opts_off, evt.tcp_options_raw, MAX_TCP_OPTIONS);
evt.tcp_options_len = (__u8)opts_len;
}
bpf_ringbuf_submit(evt, 0);
bpf_perf_event_output(ctx, &pb_tcp_syn, BPF_F_CURRENT_CPU,
&evt, sizeof(evt));
key = STAT_SYN_SUBMIT;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
}
/* ===================================================================
* TLS ClientHello (port 443)
* ===================================================================*/
if (dst_port == HTTPS_PORT) {
/* Au moins 6 octets pour l'en-tête TLS record + type message */
if (payload + 6 > data_end)
return XDP_PASS;
/* Lire les 6 premiers octets du payload pour vérifier le type TLS */
if (payload_off + 6 > pkt_len)
return TC_ACT_OK;
__u8 tls_type = ((__u8 *)payload)[0];
__u8 tls_msg_type = ((__u8 *)payload)[5];
if (tls_type != TLS_CONTENT_HANDSHAKE || tls_msg_type != TLS_MSG_CLIENT_HELLO)
return XDP_PASS;
__u8 tls_hdr[6];
bpf_skb_load_bytes(ctx, payload_off, tls_hdr, 6);
__u32 avail = (__u8 *)data_end - (__u8 *)payload;
/* avail ≥ 6 (vérifié ci-dessus), on plafonne à MAX_TLS_PAYLOAD */
if (avail > MAX_TLS_PAYLOAD)
avail = MAX_TLS_PAYLOAD;
/* Barrière compilateur : coupe le lien CSE entre avail et (data_end - payload).
* Sans cette barrière, clang génère un test "PTR_TO_PACKET <<= 32" (compare
* data_end == payload pour l'entrée de boucle) que le vérificateur eBPF rejette.
* La barrière force une comparaison scalaire (avail == 0) à la place. */
asm volatile("" : "+r"(avail));
if (tls_hdr[0] != TLS_CONTENT_HANDSHAKE || tls_hdr[5] != TLS_MSG_CLIENT_HELLO)
return TC_ACT_OK;
struct tls_hello_event *tls_evt =
bpf_ringbuf_reserve(&rb_tls_hello, sizeof(*tls_evt), 0);
/* Avail via pkt_len (scalaire pur) */
__u32 avail = 0;
if (pkt_len > payload_off) {
avail = pkt_len - payload_off;
if (avail > MAX_TLS_PAYLOAD)
avail = MAX_TLS_PAYLOAD;
}
if (avail == 0)
return TC_ACT_OK;
__u32 zero = 0;
struct tls_hello_event *tls_evt = bpf_map_lookup_elem(&__tls_buf, &zero);
if (!tls_evt)
return XDP_PASS;
return TC_ACT_OK;
tls_evt->src_ip = 0;
tls_evt->src_port = 0;
tls_evt->payload_len = 0;
tls_evt->timestamp_ns = 0;
tls_evt->src_ip = bpf_ntohl(src_ip);
tls_evt->src_port = src_port;
tls_evt->timestamp_ns = bpf_ktime_get_ns();
tls_evt->payload_len = (__u16)avail;
/* Copie bornée du payload TLS.
* Pour tout i < avail : payload + i < payload + avail ≤ data_end.
* Le vérificateur kernel ≥ 5.3 peut vérifier cette boucle sans unroll. */
__u8 *src = (__u8 *)payload;
#pragma clang loop unroll(disable)
for (__u32 i = 0; i < MAX_TLS_PAYLOAD; i++) {
if (i >= avail)
break;
if (src + i + 1 > (__u8 *)data_end)
break;
tls_evt->payload[i] = src[i];
}
/* Copie via bpf_skb_load_bytes avec taille constante 256.
* Kernel 4.18 ne supporte pas les tailles variables vers map values.
* 256 octets capture le ClientHello dans la majorité des cas. */
if (bpf_skb_load_bytes(ctx, payload_off, tls_evt, 256))
return TC_ACT_OK;
bpf_ringbuf_submit(tls_evt, 0);
return XDP_PASS;
bpf_perf_event_output(ctx, &pb_tls_hello, BPF_F_CURRENT_CPU,
tls_evt, sizeof(*tls_evt));
key = STAT_TLS_SUBMIT;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
return TC_ACT_OK;
}
/* ===================================================================
* HTTP en clair (port 80 / 8080)
* ===================================================================*/
if (dst_port == HTTP_PORT || dst_port == HTTP_ALT_PORT) {
/* Ignorer SYN, FIN, RST : seuls les segments de données */
if (tcp_flags & (TH_SYN | TH_FIN | TH_RST))
return XDP_PASS;
if (payload >= data_end)
return XDP_PASS;
return TC_ACT_OK;
if (payload_off >= pkt_len)
return TC_ACT_OK;
__u32 avail = (__u8 *)data_end - (__u8 *)payload;
if (avail > MAX_HTTP_PAYLOAD)
avail = MAX_HTTP_PAYLOAD;
/* Même barrière que pour la section TLS : force comparaison scalaire. */
asm volatile("" : "+r"(avail));
/* Avail via pkt_len (scalaire pur) */
__u32 avail = 0;
if (pkt_len > payload_off) {
avail = pkt_len - payload_off;
if (avail > MAX_HTTP_PAYLOAD)
avail = MAX_HTTP_PAYLOAD;
}
if (avail == 0)
return TC_ACT_OK;
struct http_plain_event *h_evt =
bpf_ringbuf_reserve(&rb_http_plain, sizeof(*h_evt), 0);
__u32 zero = 0;
struct http_plain_event *h_evt = bpf_map_lookup_elem(&__http_buf, &zero);
if (!h_evt)
return XDP_PASS;
return TC_ACT_OK;
h_evt->src_ip = 0;
h_evt->dst_ip = 0;
h_evt->src_port = 0;
h_evt->dst_port = 0;
h_evt->payload_len = 0;
h_evt->timestamp_ns = 0;
h_evt->src_ip = bpf_ntohl(src_ip);
h_evt->dst_ip = bpf_ntohl(dst_ip);
@ -240,21 +271,19 @@ int capture_xdp(struct xdp_md *ctx)
h_evt->timestamp_ns = bpf_ktime_get_ns();
h_evt->payload_len = (__u16)avail;
__u8 *src = (__u8 *)payload;
#pragma clang loop unroll(disable)
for (__u32 i = 0; i < MAX_HTTP_PAYLOAD; i++) {
if (i >= avail)
break;
if (src + i + 1 > (__u8 *)data_end)
break;
h_evt->payload[i] = src[i];
}
/* Taille constante 256 pour compatibilité vérificateur kernel 4.18 */
if (bpf_skb_load_bytes(ctx, payload_off, h_evt, 256))
return TC_ACT_OK;
bpf_ringbuf_submit(h_evt, 0);
bpf_perf_event_output(ctx, &pb_http_plain, BPF_F_CURRENT_CPU,
h_evt, sizeof(*h_evt));
key = STAT_HTTP_SUBMIT;
cnt = bpf_map_lookup_elem(&tc_stats, &key);
if (cnt) (*cnt)++;
}
return XDP_PASS;
return TC_ACT_OK;
}
char LICENSE[] SEC("license") = "GPL";

View File

@ -4,6 +4,9 @@
* et corrige l'association socket ↔ SSL* via les tracepoints syscalls/accept4.
* Les tracepoints sont plus stables que les kprobes car ils ne dépendent pas
* du nom manglé __x64_sys_accept4 (variable selon la version du kernel).
*
* Utilise bpf_perf_event_output() (kernel 4.4+) pour compatibilité maximale.
* Les structs > 512o utilisent un PERCPU_ARRAY temporaire (__ssl_buf).
* ============================================================================ */
#include "vmlinux.h"
@ -105,7 +108,8 @@ int uprobe_ssl_read_entry(struct pt_regs *ctx)
/* ===========================================================================
* uretprobe_ssl_read_exit — Retour de SSL_read
*
* Lit le buffer déchiffré et l'émet dans rb_ssl_data.
* Lit le buffer déchiffré et l'émet via perf_event_output.
* Struct ssl_data_event = 4131 octets → PERCPU_ARRAY temporaire (__ssl_buf).
* ===========================================================================*/
SEC("uretprobe/SSL_read")
int uretprobe_ssl_read_exit(struct pt_regs *ctx)
@ -124,12 +128,21 @@ int uretprobe_ssl_read_exit(struct pt_regs *ctx)
return 0;
}
/* Allouer un slot dans le ring buffer */
struct ssl_data_event *evt = bpf_ringbuf_reserve(&rb_ssl_data, sizeof(*evt), 0);
/* Utiliser le buffer PERCPU (struct trop grande pour la stack) */
__u32 zero = 0;
struct ssl_data_event *evt = bpf_map_lookup_elem(&__ssl_buf, &zero);
if (!evt) {
bpf_map_delete_elem(&ssl_args_map, &pid_tgid);
return 0;
}
/* Initialiser les champs fixes (data sera écrasé par probe_read_user) */
evt->pid_tgid = 0;
evt->fd = 0;
evt->src_ip = 0;
evt->src_port = 0;
evt->data_len = 0;
evt->timestamp_ns = 0;
evt->direction = 0;
evt->pid_tgid = pid_tgid;
evt->direction = 0; /* lecture = client vers serveur */
@ -154,7 +167,8 @@ int uretprobe_ssl_read_exit(struct pt_regs *ctx)
evt->src_port = 0;
}
bpf_ringbuf_submit(evt, 0);
bpf_perf_event_output(ctx, &pb_ssl_data, BPF_F_CURRENT_CPU,
evt, sizeof(*evt));
bpf_map_delete_elem(&ssl_args_map, &pid_tgid);
return 0;
@ -181,7 +195,8 @@ int kprobe_accept4_entry(struct sys_enter_accept4_ctx *ctx)
* kretprobe_accept4_exit — Retour de accept4 via tracepoint syscalls
*
* Lit la sockaddr_in pour extraire src_ip:src_port du client,
* peuple accept_map et fd_conn_map, et émet dans rb_accept.
* peuple accept_map et fd_conn_map, et émet via perf_event_output.
* Struct accept_event = 26 octets → tient sur la stack (< 512o).
* ===========================================================================*/
SEC("tracepoint/syscalls/sys_exit_accept4")
int kretprobe_accept4_exit(struct sys_exit_accept4_ctx *ctx)
@ -238,21 +253,11 @@ int kretprobe_accept4_exit(struct sys_exit_accept4_ctx *ctx)
};
bpf_map_update_elem(&fd_conn_map, &fd, &conn_info, BPF_ANY);
/* Émettre dans rb_accept */
struct accept_event *out = bpf_ringbuf_reserve(&rb_accept, sizeof(*out), 0);
if (!out)
return 0;
out->pid_tgid = pid_tgid;
out->fd = fd;
out->src_ip = src_ip;
out->src_port = src_port;
out->timestamp_ns = aevt.timestamp_ns;
bpf_ringbuf_submit(out, 0);
/* Émettre via perf_event_output (struct 26o → sur la stack) */
bpf_perf_event_output(ctx, &pb_accept, BPF_F_CURRENT_CPU,
&aevt, sizeof(aevt));
return 0;
}
char LICENSE[] SEC("license") = "GPL";