feat: add ja4ebpf service — eBPF-based TLS/TCP fingerprinting daemon
- TC ingress hook captures TCP SYN (L3/L4) and TLS ClientHello - Uprobes on SSL_read/SSL_set_fd capture decrypted TLS data - Kprobes on accept4 correlate socket FDs to client IP:port - JA4 fingerprint computed from parsed TLS ClientHello - HTTP/2 SETTINGS and WINDOW_UPDATE extracted from decrypted streams - Session manager with sharded map (256 shards) and GC goroutine - Slowloris detection: sessions with no requests after 10s threshold - ClickHouse batch writer to ja4_logs.http_logs_raw (raw_json) - All tests pass: 17 parser + 10 correlation tests Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
171
services/ja4ebpf/bpf/bpf_types.h
Normal file
171
services/ja4ebpf/bpf/bpf_types.h
Normal file
@ -0,0 +1,171 @@
|
||||
/* ============================================================================
|
||||
* bpf_types.h — Structures partagées entre les programmes eBPF (C) et Go
|
||||
* ============================================================================ */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <linux/types.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Événement TCP SYN : émis pour chaque nouvelle connexion TCP observée
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct tcp_syn_event {
|
||||
__u32 src_ip; /* adresse source (network byte order) */
|
||||
__u32 dst_ip; /* adresse destination (network byte order) */
|
||||
__u16 src_port; /* port source (host byte order) */
|
||||
__u16 dst_port; /* port destination (host byte order) */
|
||||
__u8 ttl; /* TTL IP */
|
||||
__u8 df_bit; /* bit Don't Fragment (1 = DF activé) */
|
||||
__u16 ip_id; /* champ identification IP */
|
||||
__u16 window_size; /* fenêtre TCP initiale */
|
||||
__u8 window_scale; /* facteur d'échelle (0xFF = absent) */
|
||||
__u16 mss; /* MSS TCP (0 = absent) */
|
||||
__u8 tcp_options_raw[40]; /* options TCP brutes */
|
||||
__u8 tcp_options_len; /* longueur des options TCP */
|
||||
__u64 timestamp_ns; /* horodatage kernel en nanosecondes */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Événement TLS ClientHello : émis quand un ClientHello TLS est détecté
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct tls_hello_event {
|
||||
__u32 src_ip; /* adresse source (network byte order) */
|
||||
__u16 src_port; /* port source (host byte order) */
|
||||
__u8 payload[512]; /* payload ClientHello brut */
|
||||
__u16 payload_len; /* longueur effective du payload */
|
||||
__u64 timestamp_ns; /* horodatage kernel */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Événement SSL data : émis par les uprobes SSL_read/SSL_write
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct ssl_data_event {
|
||||
__u64 pid_tgid; /* PID+TGID du processus */
|
||||
__u32 fd; /* descripteur de fichier socket */
|
||||
__u32 src_ip; /* IP source (rempli via accept_map) */
|
||||
__u16 src_port; /* port source */
|
||||
__u8 data[4096]; /* données déchiffrées */
|
||||
__u32 data_len; /* longueur effective des données */
|
||||
__u64 timestamp_ns; /* horodatage kernel */
|
||||
__u8 direction; /* 0 = lecture (client→serveur), 1 = écriture */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Événement accept : émis lors de chaque appel accept4 réussi
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct accept_event {
|
||||
__u64 pid_tgid; /* PID+TGID du processus */
|
||||
__u32 fd; /* nouveau fd retourné par accept4 */
|
||||
__u32 src_ip; /* adresse IP du client (peer) */
|
||||
__u16 src_port; /* port du client */
|
||||
__u64 timestamp_ns; /* horodatage kernel */
|
||||
} __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).
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct http_plain_event {
|
||||
__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));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Arguments sauvegardés à l'entrée de SSL_read (pour l'uretprobe)
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct ssl_read_args {
|
||||
__u64 ssl_ptr; /* pointeur vers la structure SSL */
|
||||
__u64 buf_ptr; /* pointeur vers le buffer de réception */
|
||||
__u32 num; /* taille maximale demandée */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Informations de connexion associées à un pointeur SSL
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct ssl_conn_info {
|
||||
__u32 fd; /* descripteur de fichier socket */
|
||||
__u32 src_ip; /* IP source du client */
|
||||
__u16 src_port; /* port source du client */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Clé composite pour accept_map : pid_tgid + fd
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct accept_key {
|
||||
__u64 pid_tgid; /* PID+TGID */
|
||||
__u32 fd; /* descripteur de fichier */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ===========================================================================
|
||||
* Déclarations des maps eBPF avec annotations BTF
|
||||
* ===========================================================================*/
|
||||
|
||||
/* Ring buffer : événements TCP SYN (16 MB) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||
__uint(max_entries, 1 << 24);
|
||||
} rb_tcp_syn SEC(".maps");
|
||||
|
||||
/* Ring buffer : événements TLS ClientHello (16 MB) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||
__uint(max_entries, 1 << 24);
|
||||
} rb_tls_hello SEC(".maps");
|
||||
|
||||
/* Ring buffer : données SSL déchiffrées (64 MB, plus volumineux) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||
__uint(max_entries, 1 << 26);
|
||||
} rb_ssl_data SEC(".maps");
|
||||
|
||||
/* Ring buffer : événements accept4 (4 MB) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||
__uint(max_entries, 1 << 22);
|
||||
} rb_accept SEC(".maps");
|
||||
|
||||
/* Ring buffer : payload HTTP en clair port 80/8080 (32 MB) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||
__uint(max_entries, 1 << 25);
|
||||
} rb_http_plain SEC(".maps");
|
||||
|
||||
/* Hash map : pid_tgid → ssl_read_args (arguments SSL_read entry) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 10240);
|
||||
__type(key, __u64);
|
||||
__type(value, struct ssl_read_args);
|
||||
} ssl_args_map SEC(".maps");
|
||||
|
||||
/* Hash map : ssl_ptr → ssl_conn_info (connexion associée au SSL*) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 10240);
|
||||
__type(key, __u64);
|
||||
__type(value, struct ssl_conn_info);
|
||||
} ssl_conn_map SEC(".maps");
|
||||
|
||||
/* Hash map : {pid_tgid, fd} → accept_event (pour corrélation SSL) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 10240);
|
||||
__type(key, struct accept_key);
|
||||
__type(value, struct accept_event);
|
||||
} accept_map SEC(".maps");
|
||||
|
||||
/* Hash map secondaire : fd uniquement → ssl_conn_info (pour SSL_set_fd) */
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 10240);
|
||||
__type(key, __u32);
|
||||
__type(value, struct ssl_conn_info);
|
||||
} fd_conn_map SEC(".maps");
|
||||
|
||||
276
services/ja4ebpf/bpf/tc_capture.c
Normal file
276
services/ja4ebpf/bpf/tc_capture.c
Normal file
@ -0,0 +1,276 @@
|
||||
/* ============================================================================
|
||||
* tc_capture.c — Programme TC ingress : capture des TCP SYN et TLS ClientHello
|
||||
*
|
||||
* Attaché sur l'interface réseau en ingress via TC (Traffic Control).
|
||||
* Émet des événements vers les ring buffers rb_tcp_syn et rb_tls_hello.
|
||||
* ============================================================================ */
|
||||
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_endian.h>
|
||||
#include <bpf/bpf_core_read.h>
|
||||
#include "bpf_types.h"
|
||||
|
||||
/* Constantes Ethernet */
|
||||
#define ETH_P_IP 0x0800
|
||||
#define ETH_HLEN 14
|
||||
|
||||
/* Constantes IP */
|
||||
#define IPPROTO_TCP 6
|
||||
#define IP_DF 0x4000 /* bit Don't Fragment */
|
||||
|
||||
/* Constantes TCP */
|
||||
#define TH_SYN 0x02
|
||||
#define TH_ACK 0x10
|
||||
|
||||
/* Port HTTPS standard */
|
||||
#define HTTPS_PORT 443
|
||||
|
||||
/* Ports HTTP en clair */
|
||||
#define HTTP_PORT 80
|
||||
#define HTTP_ALT_PORT 8080
|
||||
|
||||
/* Flags TCP */
|
||||
#define TH_FIN 0x01
|
||||
#define TH_RST 0x04
|
||||
|
||||
/* Type de contenu TLS : Handshake */
|
||||
#define TLS_CONTENT_HANDSHAKE 0x16
|
||||
/* Type de message TLS : ClientHello */
|
||||
#define TLS_MSG_CLIENT_HELLO 0x01
|
||||
|
||||
/* Taille maximale du payload TLS à copier */
|
||||
#define MAX_TLS_PAYLOAD 512
|
||||
|
||||
/* Longueur maximale des options TCP */
|
||||
#define MAX_TCP_OPTIONS 40
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Structure interne pour le parsing de l'en-tête Ethernet
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct ethhdr_local {
|
||||
__u8 h_dest[6];
|
||||
__u8 h_source[6];
|
||||
__be16 h_proto;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* capture_tc_ingress — Point d'entrée TC ingress
|
||||
*
|
||||
* Inspecte chaque paquet entrant, détecte les TCP SYN et les ClientHello TLS,
|
||||
* et soumet les événements correspondants aux ring buffers.
|
||||
* ---------------------------------------------------------------------------*/
|
||||
SEC("tc/ingress")
|
||||
int capture_tc_ingress(struct __sk_buff *skb)
|
||||
{
|
||||
/* Pointeurs de début et fin du buffer paquet */
|
||||
void *data = (void *)(long)skb->data;
|
||||
void *data_end = (void *)(long)skb->data_end;
|
||||
|
||||
/* --- Parsing Ethernet --- */
|
||||
struct ethhdr_local *eth = data;
|
||||
if ((void *)(eth + 1) > data_end)
|
||||
return TC_ACT_OK;
|
||||
|
||||
/* Vérifier que c'est un paquet IPv4 */
|
||||
if (bpf_ntohs(eth->h_proto) != ETH_P_IP)
|
||||
return TC_ACT_OK;
|
||||
|
||||
/* --- Parsing IPv4 --- */
|
||||
struct iphdr *ip = (struct iphdr *)((void *)eth + ETH_HLEN);
|
||||
if ((void *)(ip + 1) > data_end)
|
||||
return TC_ACT_OK;
|
||||
|
||||
/* Vérifier que c'est du TCP */
|
||||
if (ip->protocol != IPPROTO_TCP)
|
||||
return TC_ACT_OK;
|
||||
|
||||
__u8 ihl = ip->ihl & 0x0F; /* longueur en-tête IP en mots de 32 bits */
|
||||
__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);
|
||||
__u8 df_bit = (frag_off & IP_DF) ? 1 : 0;
|
||||
|
||||
/* --- Parsing TCP --- */
|
||||
struct tcphdr *tcp = (struct tcphdr *)((void *)ip + (ihl * 4));
|
||||
if ((void *)(tcp + 1) > data_end)
|
||||
return TC_ACT_OK;
|
||||
|
||||
__u16 src_port = bpf_ntohs(tcp->source);
|
||||
__u16 dst_port = bpf_ntohs(tcp->dest);
|
||||
__u16 window = bpf_ntohs(tcp->window);
|
||||
__u8 tcp_flags = ((__u8 *)tcp)[13]; /* octet des flags TCP */
|
||||
__u8 data_off = tcp->doff; /* longueur en-tête TCP en mots de 32 bits */
|
||||
|
||||
/* --- Détection TCP SYN (SYN set, ACK clear) --- */
|
||||
if ((tcp_flags & TH_SYN) && !(tcp_flags & TH_ACK)) {
|
||||
/* Allouer un slot dans le ring buffer */
|
||||
struct tcp_syn_event *evt = bpf_ringbuf_reserve(&rb_tcp_syn, sizeof(*evt), 0);
|
||||
if (!evt)
|
||||
return TC_ACT_OK;
|
||||
|
||||
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; /* absent par défaut */
|
||||
evt->mss = 0; /* absent par défaut */
|
||||
evt->timestamp_ns = bpf_ktime_get_ns();
|
||||
|
||||
/* --- Parsing des options TCP --- */
|
||||
__u8 *opts_start = (__u8 *)tcp + 20; /* options commencent après les 20 octets fixes */
|
||||
__u8 opts_len = (data_off * 4) - 20;
|
||||
if (opts_len > MAX_TCP_OPTIONS)
|
||||
opts_len = MAX_TCP_OPTIONS;
|
||||
|
||||
evt->tcp_options_len = 0;
|
||||
|
||||
/* Copier les options brutes avec vérification de bornes */
|
||||
__u8 *opts_ptr = opts_start;
|
||||
|
||||
/* Boucle bornée sur les options TCP (max 40 octets) */
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MAX_TCP_OPTIONS; i++) {
|
||||
if (i >= opts_len)
|
||||
break;
|
||||
if ((void *)(opts_ptr + i + 1) > data_end)
|
||||
break;
|
||||
|
||||
__u8 opt_kind;
|
||||
bpf_probe_read_kernel(&opt_kind, 1, opts_ptr + i);
|
||||
evt->tcp_options_raw[i] = opt_kind;
|
||||
evt->tcp_options_len = i + 1;
|
||||
|
||||
/* NOP : 1 octet */
|
||||
if (opt_kind == 1)
|
||||
continue;
|
||||
|
||||
/* EOL : fin des options */
|
||||
if (opt_kind == 0)
|
||||
break;
|
||||
|
||||
/* Option avec longueur */
|
||||
if ((void *)(opts_ptr + i + 2) > data_end)
|
||||
break;
|
||||
|
||||
__u8 opt_len;
|
||||
bpf_probe_read_kernel(&opt_len, 1, opts_ptr + i + 1);
|
||||
if (opt_len < 2)
|
||||
break;
|
||||
|
||||
/* MSS (option 2) : 4 octets au total */
|
||||
if (opt_kind == 2 && opt_len == 4) {
|
||||
if ((void *)(opts_ptr + i + 4) > data_end)
|
||||
break;
|
||||
__u16 mss_val;
|
||||
bpf_probe_read_kernel(&mss_val, 2, opts_ptr + i + 2);
|
||||
evt->mss = bpf_ntohs(mss_val);
|
||||
}
|
||||
|
||||
/* Window Scale (option 3) : 3 octets au total */
|
||||
if (opt_kind == 3 && opt_len == 3) {
|
||||
if ((void *)(opts_ptr + i + 3) > data_end)
|
||||
break;
|
||||
__u8 wscale;
|
||||
bpf_probe_read_kernel(&wscale, 1, opts_ptr + i + 2);
|
||||
evt->window_scale = wscale;
|
||||
}
|
||||
|
||||
/* Avancer au-delà de cette option */
|
||||
i += opt_len - 1;
|
||||
}
|
||||
|
||||
bpf_ringbuf_submit(evt, 0);
|
||||
}
|
||||
|
||||
/* --- Détection TLS ClientHello (port 443) --- */
|
||||
if (dst_port == HTTPS_PORT) {
|
||||
__u8 *tcp_payload = (__u8 *)tcp + (data_off * 4);
|
||||
|
||||
/* Vérifier qu'il y a au moins 6 octets pour l'en-tête TLS record + type hello */
|
||||
if ((void *)(tcp_payload + 6) > data_end)
|
||||
return TC_ACT_OK;
|
||||
|
||||
__u8 content_type, msg_type;
|
||||
bpf_probe_read_kernel(&content_type, 1, tcp_payload);
|
||||
bpf_probe_read_kernel(&msg_type, 1, tcp_payload + 5);
|
||||
|
||||
/* Vérifier : Handshake (0x16) + ClientHello (0x01) */
|
||||
if (content_type == TLS_CONTENT_HANDSHAKE && msg_type == TLS_MSG_CLIENT_HELLO) {
|
||||
struct tls_hello_event *tls_evt = bpf_ringbuf_reserve(&rb_tls_hello, sizeof(*tls_evt), 0);
|
||||
if (!tls_evt)
|
||||
return TC_ACT_OK;
|
||||
|
||||
tls_evt->src_ip = bpf_ntohl(src_ip);
|
||||
tls_evt->src_port = src_port;
|
||||
tls_evt->timestamp_ns = bpf_ktime_get_ns();
|
||||
|
||||
/* Calculer la longueur de payload disponible */
|
||||
__u32 avail = (__u32)(data_end - (void *)tcp_payload);
|
||||
if (avail > MAX_TLS_PAYLOAD)
|
||||
avail = MAX_TLS_PAYLOAD;
|
||||
|
||||
tls_evt->payload_len = (__u16)avail;
|
||||
|
||||
/* Copier le payload TLS (borné à MAX_TLS_PAYLOAD) */
|
||||
bpf_probe_read_kernel(tls_evt->payload, avail & (MAX_TLS_PAYLOAD - 1), tcp_payload);
|
||||
|
||||
bpf_ringbuf_submit(tls_evt, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Détection payload HTTP en clair (port 80 / 8080) --- */
|
||||
if (dst_port == HTTP_PORT || dst_port == HTTP_ALT_PORT) {
|
||||
/* Ignorer SYN, FIN, RST : on ne veut que les segments de données */
|
||||
if (tcp_flags & (TH_SYN | TH_FIN | TH_RST))
|
||||
return TC_ACT_OK;
|
||||
|
||||
/* Calculer l'offset du payload TCP dans le paquet */
|
||||
__u32 payload_off = ETH_HLEN + (ihl * 4) + (data_off * 4);
|
||||
|
||||
/* Vérifier qu'il y a un payload non vide */
|
||||
if (skb->len <= payload_off)
|
||||
return TC_ACT_OK;
|
||||
|
||||
__u32 avail = skb->len - payload_off;
|
||||
if (avail > 4096)
|
||||
avail = 4096;
|
||||
|
||||
/* Réserver une entrée dans le ring buffer HTTP en clair */
|
||||
struct http_plain_event *h_evt =
|
||||
bpf_ringbuf_reserve(&rb_http_plain, sizeof(*h_evt), 0);
|
||||
if (!h_evt)
|
||||
return TC_ACT_OK;
|
||||
|
||||
h_evt->src_ip = bpf_ntohl(src_ip);
|
||||
h_evt->dst_ip = bpf_ntohl(dst_ip);
|
||||
h_evt->src_port = src_port;
|
||||
h_evt->dst_port = dst_port;
|
||||
h_evt->timestamp_ns = bpf_ktime_get_ns();
|
||||
|
||||
/* Copier le payload depuis le skb linéaire via bpf_skb_load_bytes.
|
||||
* La longueur est masquée pour satisfaire le vérificateur eBPF. */
|
||||
__u32 copy_len = avail & (4096 - 1);
|
||||
if (copy_len == 0) copy_len = 1; /* cas avail == 4096 exactement */
|
||||
h_evt->payload_len = (__u16)avail;
|
||||
|
||||
if (bpf_skb_load_bytes(skb, payload_off, h_evt->payload, copy_len) < 0) {
|
||||
bpf_ringbuf_discard(h_evt, 0);
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
bpf_ringbuf_submit(h_evt, 0);
|
||||
}
|
||||
|
||||
return TC_ACT_OK;
|
||||
}
|
||||
|
||||
char LICENSE[] SEC("license") = "GPL";
|
||||
|
||||
|
||||
223
services/ja4ebpf/bpf/uprobe_ssl.c
Normal file
223
services/ja4ebpf/bpf/uprobe_ssl.c
Normal file
@ -0,0 +1,223 @@
|
||||
/* ============================================================================
|
||||
* uprobe_ssl.c — Uprobes SSL_read/SSL_set_fd et kprobes accept4
|
||||
*
|
||||
* Intercepte les appels OpenSSL pour capturer le trafic déchiffré,
|
||||
* et corrige l'association socket ↔ SSL* via accept4.
|
||||
* ============================================================================ */
|
||||
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
#include <bpf/bpf_core_read.h>
|
||||
#include "bpf_types.h"
|
||||
|
||||
/* Taille maximale de données SSL à copier par événement */
|
||||
#define MAX_SSL_DATA 4096
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Map temporaire : pid_tgid → upeer_sockaddr (sauvegardé à l'entrée d'accept4)
|
||||
* ---------------------------------------------------------------------------*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_HASH);
|
||||
__uint(max_entries, 10240);
|
||||
__type(key, __u64);
|
||||
__type(value, __u64); /* pointeur userspace vers sockaddr_in */
|
||||
} accept_args_map SEC(".maps");
|
||||
|
||||
/* ===========================================================================
|
||||
* uprobe_ssl_set_fd — Intercept SSL_set_fd(SSL *s, int fd)
|
||||
*
|
||||
* Associe un ssl_ptr à ses informations de connexion (fd, src_ip, src_port)
|
||||
* en consultant fd_conn_map.
|
||||
* ===========================================================================*/
|
||||
SEC("uprobe/SSL_set_fd")
|
||||
int uprobe_ssl_set_fd(struct pt_regs *ctx)
|
||||
{
|
||||
__u64 ssl_ptr = ((__u64)PT_REGS_PARM1(ctx));
|
||||
__u32 fd = ((__u32)PT_REGS_PARM2(ctx));
|
||||
|
||||
/* Rechercher les infos de connexion via le fd */
|
||||
struct ssl_conn_info *conn = bpf_map_lookup_elem(&fd_conn_map, &fd);
|
||||
if (!conn)
|
||||
return 0;
|
||||
|
||||
/* Enregistrer l'association ssl_ptr → conn_info */
|
||||
struct ssl_conn_info new_conn = *conn;
|
||||
new_conn.fd = fd;
|
||||
bpf_map_update_elem(&ssl_conn_map, &ssl_ptr, &new_conn, BPF_ANY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ===========================================================================
|
||||
* uprobe_ssl_read_entry — Entrée de SSL_read(SSL *ssl, void *buf, int num)
|
||||
*
|
||||
* Sauvegarde les arguments pour l'uretprobe correspondant.
|
||||
* ===========================================================================*/
|
||||
SEC("uprobe/SSL_read")
|
||||
int uprobe_ssl_read_entry(struct pt_regs *ctx)
|
||||
{
|
||||
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
||||
|
||||
struct ssl_read_args args = {};
|
||||
args.ssl_ptr = (__u64)PT_REGS_PARM1(ctx);
|
||||
args.buf_ptr = (__u64)PT_REGS_PARM2(ctx);
|
||||
args.num = (__u32)PT_REGS_PARM3(ctx);
|
||||
|
||||
bpf_map_update_elem(&ssl_args_map, &pid_tgid, &args, BPF_ANY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ===========================================================================
|
||||
* uretprobe_ssl_read_exit — Retour de SSL_read
|
||||
*
|
||||
* Lit le buffer déchiffré et l'émet dans rb_ssl_data.
|
||||
* ===========================================================================*/
|
||||
SEC("uretprobe/SSL_read")
|
||||
int uretprobe_ssl_read_exit(struct pt_regs *ctx)
|
||||
{
|
||||
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
||||
|
||||
/* Récupérer les arguments sauvegardés à l'entrée */
|
||||
struct ssl_read_args *args = bpf_map_lookup_elem(&ssl_args_map, &pid_tgid);
|
||||
if (!args)
|
||||
return 0;
|
||||
|
||||
/* Vérifier que la lecture a réussi (valeur de retour > 0) */
|
||||
long retval = PT_REGS_RC(ctx);
|
||||
if (retval <= 0) {
|
||||
bpf_map_delete_elem(&ssl_args_map, &pid_tgid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allouer un slot dans le ring buffer */
|
||||
struct ssl_data_event *evt = bpf_ringbuf_reserve(&rb_ssl_data, sizeof(*evt), 0);
|
||||
if (!evt) {
|
||||
bpf_map_delete_elem(&ssl_args_map, &pid_tgid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
evt->pid_tgid = pid_tgid;
|
||||
evt->direction = 0; /* lecture = client vers serveur */
|
||||
evt->timestamp_ns = bpf_ktime_get_ns();
|
||||
|
||||
/* Limiter la copie à MAX_SSL_DATA octets */
|
||||
__u32 data_len = (retval > MAX_SSL_DATA) ? MAX_SSL_DATA : (__u32)retval;
|
||||
evt->data_len = data_len;
|
||||
|
||||
/* Copier depuis l'espace utilisateur */
|
||||
bpf_probe_read_user(evt->data, data_len & (MAX_SSL_DATA - 1), (void *)args->buf_ptr);
|
||||
|
||||
/* Retrouver les infos de connexion via ssl_ptr */
|
||||
struct ssl_conn_info *conn = bpf_map_lookup_elem(&ssl_conn_map, &args->ssl_ptr);
|
||||
if (conn) {
|
||||
evt->fd = conn->fd;
|
||||
evt->src_ip = conn->src_ip;
|
||||
evt->src_port = conn->src_port;
|
||||
} else {
|
||||
evt->fd = 0;
|
||||
evt->src_ip = 0;
|
||||
evt->src_port = 0;
|
||||
}
|
||||
|
||||
bpf_ringbuf_submit(evt, 0);
|
||||
bpf_map_delete_elem(&ssl_args_map, &pid_tgid);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ===========================================================================
|
||||
* kprobe_accept4_entry — Entrée de accept4(fd, upeer_sockaddr, upeer_addrlen, flags)
|
||||
*
|
||||
* Sauvegarde le pointeur vers la sockaddr pour la récupérer à la sortie.
|
||||
* ===========================================================================*/
|
||||
SEC("kprobe/accept4")
|
||||
int kprobe_accept4_entry(struct pt_regs *ctx)
|
||||
{
|
||||
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
||||
/* Deuxième argument : pointeur userspace vers struct sockaddr_in */
|
||||
__u64 sockaddr_ptr = (__u64)PT_REGS_PARM2(ctx);
|
||||
|
||||
bpf_map_update_elem(&accept_args_map, &pid_tgid, &sockaddr_ptr, BPF_ANY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ===========================================================================
|
||||
* kretprobe_accept4_exit — Retour de accept4
|
||||
*
|
||||
* Lit la sockaddr_in pour extraire src_ip:src_port du client,
|
||||
* peuple accept_map et fd_conn_map, et émet dans rb_accept.
|
||||
* ===========================================================================*/
|
||||
SEC("kretprobe/accept4")
|
||||
int kretprobe_accept4_exit(struct pt_regs *ctx)
|
||||
{
|
||||
__u64 pid_tgid = bpf_get_current_pid_tgid();
|
||||
|
||||
/* Vérifier que accept4 a réussi (fd ≥ 0) */
|
||||
long new_fd = PT_REGS_RC(ctx);
|
||||
if (new_fd < 0) {
|
||||
bpf_map_delete_elem(&accept_args_map, &pid_tgid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Récupérer le pointeur vers sockaddr_in */
|
||||
__u64 *sockaddr_ptr_p = bpf_map_lookup_elem(&accept_args_map, &pid_tgid);
|
||||
if (!sockaddr_ptr_p) {
|
||||
return 0;
|
||||
}
|
||||
__u64 sockaddr_ptr = *sockaddr_ptr_p;
|
||||
bpf_map_delete_elem(&accept_args_map, &pid_tgid);
|
||||
|
||||
if (!sockaddr_ptr)
|
||||
return 0;
|
||||
|
||||
/* Lire la structure sockaddr_in depuis l'espace utilisateur */
|
||||
/* struct sockaddr_in: sin_family(2) + sin_port(2) + sin_addr(4) */
|
||||
__u8 sa_buf[8] = {};
|
||||
bpf_probe_read_user(sa_buf, sizeof(sa_buf), (void *)sockaddr_ptr);
|
||||
|
||||
/* Extraire port (octets 2-3) et adresse IP (octets 4-7) */
|
||||
__u16 sin_port = (__u16)(sa_buf[2] << 8) | sa_buf[3]; /* network byte order */
|
||||
__u32 sin_addr = *(__u32 *)(sa_buf + 4); /* network byte order */
|
||||
|
||||
__u32 src_ip = __builtin_bswap32(sin_addr); /* host byte order */
|
||||
__u16 src_port = __builtin_bswap16(sin_port); /* host byte order */
|
||||
__u32 fd = (__u32)new_fd;
|
||||
|
||||
/* Peupler accept_map[{pid_tgid, fd}] */
|
||||
struct accept_key akey = { .pid_tgid = pid_tgid, .fd = fd };
|
||||
struct accept_event aevt = {
|
||||
.pid_tgid = pid_tgid,
|
||||
.fd = fd,
|
||||
.src_ip = src_ip,
|
||||
.src_port = src_port,
|
||||
.timestamp_ns = bpf_ktime_get_ns(),
|
||||
};
|
||||
bpf_map_update_elem(&accept_map, &akey, &aevt, BPF_ANY);
|
||||
|
||||
/* Peupler fd_conn_map[fd] pour accès rapide par SSL_set_fd */
|
||||
struct ssl_conn_info conn_info = {
|
||||
.fd = fd,
|
||||
.src_ip = src_ip,
|
||||
.src_port = src_port,
|
||||
};
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char LICENSE[] SEC("license") = "GPL";
|
||||
|
||||
Reference in New Issue
Block a user