mirror of
https://github.com/bol-van/zapret.git
synced 2025-08-26 13:25:40 +00:00
239 lines
5.7 KiB
C
239 lines
5.7 KiB
C
/*
|
|
* TLS Fingerprint Randomization eBPF Program
|
|
* JA3/JA3S spoofing and browser fingerprint randomization
|
|
* Simplified stub implementation for compatibility
|
|
*/
|
|
|
|
#ifndef __KERNEL__
|
|
#define __KERNEL__
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
/* Basic type definitions for eBPF compatibility */
|
|
typedef uint8_t __u8;
|
|
typedef uint16_t __u16;
|
|
typedef uint32_t __u32;
|
|
typedef uint64_t __u64;
|
|
|
|
/* eBPF helper function stubs */
|
|
#ifdef __APPLE__
|
|
#define SEC(name) __attribute__((section("__TEXT," name), used))
|
|
#else
|
|
#define SEC(name) __attribute__((section(name), used))
|
|
#endif
|
|
#define __always_inline inline __attribute__((always_inline))
|
|
|
|
/* License required for eBPF programs */
|
|
#ifdef __APPLE__
|
|
char LICENSE[] __attribute__((section("__TEXT,license"), used)) = "GPL";
|
|
#else
|
|
char LICENSE[] SEC("license") = "GPL";
|
|
#endif
|
|
|
|
#define MAX_FINGERPRINTS 256
|
|
#define MAX_JA3_ENTRIES 1024
|
|
#define MAX_CIPHER_SUITES 64
|
|
#define MAX_EXTENSIONS 32
|
|
|
|
/* Basic eBPF map types */
|
|
#define BPF_MAP_TYPE_ARRAY 2
|
|
#define BPF_MAP_TYPE_HASH 1
|
|
|
|
/* TC action codes */
|
|
#define TC_ACT_OK 0
|
|
|
|
/* XDP action codes */
|
|
#define XDP_PASS 2
|
|
|
|
/* Network protocol constants */
|
|
#define ETH_P_IP 0x0800
|
|
#define IPPROTO_TCP 6
|
|
|
|
/* TLS fingerprint structure */
|
|
struct tls_fingerprint {
|
|
__u16 cipher_suites[MAX_CIPHER_SUITES];
|
|
__u16 cipher_count;
|
|
__u16 extensions[MAX_EXTENSIONS];
|
|
__u16 extension_count;
|
|
__u16 tls_version;
|
|
__u8 compression_methods;
|
|
};
|
|
|
|
/* Simplified network headers */
|
|
struct ethhdr {
|
|
__u8 h_dest[6];
|
|
__u8 h_source[6];
|
|
__u16 h_proto;
|
|
};
|
|
|
|
struct iphdr {
|
|
__u8 ihl:4, version:4;
|
|
__u8 tos;
|
|
__u16 tot_len;
|
|
__u16 id;
|
|
__u16 frag_off;
|
|
__u8 ttl;
|
|
__u8 protocol;
|
|
__u16 check;
|
|
__u32 saddr;
|
|
__u32 daddr;
|
|
};
|
|
|
|
struct tcphdr {
|
|
__u16 source;
|
|
__u16 dest;
|
|
__u32 seq;
|
|
__u32 ack_seq;
|
|
__u16 res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1;
|
|
__u16 window;
|
|
__u16 check;
|
|
__u16 urg_ptr;
|
|
};
|
|
|
|
/* eBPF context structures */
|
|
struct __sk_buff {
|
|
__u32 len;
|
|
__u32 pkt_type;
|
|
__u32 mark;
|
|
__u32 queue_mapping;
|
|
__u32 protocol;
|
|
__u32 vlan_present;
|
|
__u32 vlan_tci;
|
|
__u32 vlan_proto;
|
|
__u32 priority;
|
|
__u32 ingress_ifindex;
|
|
__u32 ifindex;
|
|
__u32 tc_index;
|
|
__u32 cb[5];
|
|
__u32 hash;
|
|
__u32 tc_classid;
|
|
__u32 data;
|
|
__u32 data_end;
|
|
__u32 napi_id;
|
|
__u32 family;
|
|
__u32 remote_ip4;
|
|
__u32 local_ip4;
|
|
__u32 remote_ip6[4];
|
|
__u32 local_ip6[4];
|
|
__u32 remote_port;
|
|
__u32 local_port;
|
|
};
|
|
|
|
struct xdp_md {
|
|
__u32 data;
|
|
__u32 data_end;
|
|
__u32 data_meta;
|
|
__u32 ingress_ifindex;
|
|
__u32 rx_queue_index;
|
|
};
|
|
|
|
/* Stub helper functions */
|
|
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
|
|
static __u16 (*bpf_htons)(__u16 hostshort) = (void *) 9;
|
|
static __u16 (*bpf_ntohs)(__u16 netshort) = (void *) 10;
|
|
|
|
/* Get random fingerprint stub */
|
|
static __always_inline struct tls_fingerprint *get_random_fingerprint(void) {
|
|
return (struct tls_fingerprint *)0;
|
|
}
|
|
|
|
/* Calculate JA3 hash stub */
|
|
static __always_inline __u32 calculate_ja3_hash(const __u8 *data, __u32 len) {
|
|
__u32 hash = 5381;
|
|
__u32 i;
|
|
for (i = 0; i < len && i < 256; i++) {
|
|
hash = ((hash << 5) + hash) + data[i];
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
/* Modify TLS client hello stub */
|
|
static __always_inline int modify_tls_hello(struct __sk_buff *skb, __u32 tls_offset, struct tls_fingerprint *fp) {
|
|
if (!fp) return -1;
|
|
return 0;
|
|
}
|
|
|
|
/* TC program for TLS fingerprint randomization */
|
|
#ifdef __APPLE__
|
|
__attribute__((section("__TEXT,tc"), used))
|
|
#else
|
|
SEC("tc")
|
|
#endif
|
|
int tls_fingerprint_randomizer(struct __sk_buff *skb) {
|
|
void *data_end = (void *)(long)skb->data_end;
|
|
void *data = (void *)(long)skb->data;
|
|
|
|
struct ethhdr *eth = data;
|
|
if ((void *)(eth + 1) > data_end)
|
|
return TC_ACT_OK;
|
|
|
|
if (eth->h_proto != 0x0008) /* htons(ETH_P_IP) */
|
|
return TC_ACT_OK;
|
|
|
|
struct iphdr *ip = (void *)(eth + 1);
|
|
if ((void *)(ip + 1) > data_end)
|
|
return TC_ACT_OK;
|
|
|
|
if (ip->protocol != IPPROTO_TCP)
|
|
return TC_ACT_OK;
|
|
|
|
struct tcphdr *tcp = (void *)ip + (ip->ihl * 4);
|
|
if ((void *)(tcp + 1) > data_end)
|
|
return TC_ACT_OK;
|
|
|
|
/* Check for TLS handshake on common ports */
|
|
__u16 dport = tcp->dest;
|
|
if (dport != 443 && dport != 8443)
|
|
return TC_ACT_OK;
|
|
|
|
/* Calculate TLS payload offset */
|
|
__u32 tls_offset = sizeof(struct ethhdr) + (ip->ihl * 4) + (tcp->doff * 4);
|
|
|
|
/* Get new fingerprint and apply it */
|
|
struct tls_fingerprint *new_fp = get_random_fingerprint();
|
|
if (new_fp) {
|
|
modify_tls_hello(skb, tls_offset, new_fp);
|
|
}
|
|
|
|
return TC_ACT_OK;
|
|
}
|
|
|
|
/* XDP program for TLS traffic identification */
|
|
#ifdef __APPLE__
|
|
__attribute__((section("__TEXT,xdp"), used))
|
|
#else
|
|
SEC("xdp")
|
|
#endif
|
|
int tls_fingerprint_xdp(struct xdp_md *ctx) {
|
|
void *data_end = (void *)(long)ctx->data_end;
|
|
void *data = (void *)(long)ctx->data;
|
|
|
|
struct ethhdr *eth = data;
|
|
if ((void *)(eth + 1) > data_end)
|
|
return XDP_PASS;
|
|
|
|
if (eth->h_proto != 0x0008) /* htons(ETH_P_IP) */
|
|
return XDP_PASS;
|
|
|
|
struct iphdr *ip = (void *)(eth + 1);
|
|
if ((void *)(ip + 1) > data_end)
|
|
return XDP_PASS;
|
|
|
|
if (ip->protocol != IPPROTO_TCP)
|
|
return XDP_PASS;
|
|
|
|
struct tcphdr *tcp = (void *)ip + (ip->ihl * 4);
|
|
if ((void *)(tcp + 1) > data_end)
|
|
return XDP_PASS;
|
|
|
|
/* Check for TLS traffic on ports 443 or 8443 */
|
|
__u16 dport = tcp->dest;
|
|
if (dport == 443 || dport == 8443) {
|
|
/* Mark for TC processing - XDP cannot easily modify packets */
|
|
/* Pass to TC layer for actual fingerprint modification */
|
|
}
|
|
|
|
return XDP_PASS;
|
|
} |