From 4e45ade429ce0384b9a34026cdf52bb9ec2721d2 Mon Sep 17 00:00:00 2001 From: Vitaly Lavrov Date: Wed, 18 Nov 2026 15:35:13 +0300 Subject: [PATCH] Small changes. Changed implementation for risks 28 (Malicious Fingerprint) and 29 (Malicious SSL Cert/SHA1 Fingerprint). Use files /proc/net/xt_ndpi/hash_{ja4,sha1} Changed option ndpi_flow_opt. Read INSTALL. --- ndpi-netfilter/FLOW_INFO.txt | 3 +- ndpi-netfilter/INSTALL | 11 + ndpi-netfilter/ipt/libxt_ndpi.c | 40 ++-- ndpi-netfilter/src/Makefile | 2 +- ndpi-netfilter/src/main.c | 73 ++++-- ndpi-netfilter/src/ndpi_main_netfilter.h | 13 +- ndpi-netfilter/src/ndpi_proc_flow.c | 11 +- ndpi-netfilter/src/ndpi_proc_hash.c | 274 +++++++++++++++++++++++ ndpi-netfilter/src/ndpi_proc_hash.h | 8 + ndpi-netfilter/src/xt_ndpi.h | 2 +- src/include/ndpi_private.h | 3 + src/include/ndpi_typedefs.h | 1 + src/lib/ndpi_main.c | 7 +- src/lib/protocols/tls.c | 57 ++--- 14 files changed, 423 insertions(+), 82 deletions(-) create mode 100644 ndpi-netfilter/src/ndpi_proc_hash.c create mode 100644 ndpi-netfilter/src/ndpi_proc_hash.h diff --git a/ndpi-netfilter/FLOW_INFO.txt b/ndpi-netfilter/FLOW_INFO.txt index 69be59bf5..3f10778dc 100644 --- a/ndpi-netfilter/FLOW_INFO.txt +++ b/ndpi-netfilter/FLOW_INFO.txt @@ -110,7 +110,8 @@ the EOF is reached. Option ndpi_flow_opt (string) Allows you to specify which of the JA4 parameters are added to "flow". -c - client JA4 hash +4 - client JA4 hash +5 - client JA5 hash F - TLS fingerprint V - version of TLS L - confidence level diff --git a/ndpi-netfilter/INSTALL b/ndpi-netfilter/INSTALL index 8502d74c3..f639827a9 100644 --- a/ndpi-netfilter/INSTALL +++ b/ndpi-netfilter/INSTALL @@ -431,3 +431,14 @@ Bittorrents-specific files: Changing magic_ct can only be done before loading rules that use ndpi. The magic_ct value must be in the range 0-65565. +/proc/net/xt_ndpi/hash_ja4 - set or view list of malicious ja4 hash. +/proc/net/xt_ndpi/hash_sha1 - set or view list of malicious sha1 hash. + + "echo '+hash' >/proc/net/xt_ndpi/hash_ja4" - add hash to list. + "echo '-' >/proc/net/xt_ndpi/hash_ja4" - clean list. + + Warning: sha1 hash must be in upper case. + + hash_ja4 use for risk "Malicious Fingerprint" (28) + hash_sha1 use for risk "Malicious SSL Cert/SHA1 Fingerp" (29) + diff --git a/ndpi-netfilter/ipt/libxt_ndpi.c b/ndpi-netfilter/ipt/libxt_ndpi.c index d3dcb884b..cea506c33 100644 --- a/ndpi-netfilter/ipt/libxt_ndpi.c +++ b/ndpi-netfilter/ipt/libxt_ndpi.c @@ -66,6 +66,7 @@ enum ndpi_opt_index { NDPI_OPT_HOST, NDPI_OPT_INPROGRESS, NDPI_OPT_JA4C, + NDPI_OPT_JA5C, NDPI_OPT_TLSFP, NDPI_OPT_TLSV, NDPI_OPT_UNTRACKED, @@ -74,21 +75,22 @@ enum ndpi_opt_index { NDPI_OPT_LAST }; -#define FLAGS_ALL 0x1 -#define FLAGS_ERR 0x2 -#define FLAGS_HMASTER 0x4 -#define FLAGS_MPROTO 0x8 -#define FLAGS_APROTO 0x10 -#define FLAGS_HOST 0x20 +#define FLAGS_ALL 0x1 +#define FLAGS_ERR 0x2 +#define FLAGS_HMASTER 0x4 +#define FLAGS_MPROTO 0x8 +#define FLAGS_APROTO 0x10 +#define FLAGS_HOST 0x20 #define FLAGS_INPROGRESS 0x40 -#define FLAGS_PROTO 0x80 -#define FLAGS_JA4C 0x100 -#define FLAGS_TLSFP 0x400 -#define FLAGS_TLSV 0x800 -#define FLAGS_UNTRACKED 0x1000 -#define FLAGS_CLEVEL 0x2000 -#define FLAGS_HPROTO 0x4000 -#define FLAGS_RISK 0x8000 +#define FLAGS_PROTO 0x80 +#define FLAGS_JA4C 0x100 +#define FLAGS_JA5C 0x200 +#define FLAGS_TLSFP 0x400 +#define FLAGS_TLSV 0x800 +#define FLAGS_UNTRACKED 0x1000 +#define FLAGS_CLEVEL 0x2000 +#define FLAGS_HPROTO 0x4000 +#define FLAGS_RISK 0x8000 static void load_kernel_proto (void) { char buf[128],*c,pname[32],mark[32]; @@ -332,6 +334,8 @@ _ndpi_mt4_save(const void *entry, const struct xt_entry_match *match,int save) printf("inprogress"); } else if(info->ja4c) { printf("ja4c"); + } else if(info->ja5c) { + printf("ja5c"); } else if(info->tlsfp) { printf("tlsfp"); } else if(info->tlsv) { @@ -497,7 +501,7 @@ ndpi_mt4_parse(int c, char **argv, int invert, unsigned int *flags, return true; } if(c == NDPI_OPT_PROTO || c == NDPI_OPT_INPROGRESS || - c == NDPI_OPT_JA4C || c == NDPI_OPT_TLSFP || c == NDPI_OPT_TLSV) { + c == NDPI_OPT_JA4C || c == NDPI_OPT_JA5C || c == NDPI_OPT_TLSFP || c == NDPI_OPT_TLSV) { char *np = optarg,*n; int num; int op; @@ -542,6 +546,7 @@ ndpi_mt4_parse(int c, char **argv, int invert, unsigned int *flags, } if(c == NDPI_OPT_PROTO) { *flags |= FLAGS_PROTO; info->proto = 1; } if(c == NDPI_OPT_JA4C) { *flags |= FLAGS_JA4C; info->ja4c = 1; } + if(c == NDPI_OPT_JA5C) { *flags |= FLAGS_JA5C; info->ja5c = 1; } if(c == NDPI_OPT_TLSFP) { *flags |= FLAGS_TLSFP; info->tlsfp = 1; } if(c == NDPI_OPT_TLSV) { *flags |= FLAGS_TLSV; info->tlsv = 1; } if(c == NDPI_OPT_INPROGRESS ) { @@ -615,12 +620,13 @@ ndpi_mt_check (unsigned int flags) xtables_error(PARAMETER_PROBLEM, "xt_ndpi: You need to specify at least one protocol"); } - if (flags & (FLAGS_PROTO|FLAGS_JA4C|FLAGS_TLSFP|FLAGS_TLSV|FLAGS_INPROGRESS)) { + if (flags & (FLAGS_PROTO|FLAGS_JA4C|FLAGS_JA5C|FLAGS_TLSFP|FLAGS_TLSV|FLAGS_INPROGRESS)) { if(!(flags & FLAGS_HPROTO)) xtables_error(PARAMETER_PROBLEM, "xt_ndpi: You need to specify at least one protocol"); } if(flags & FLAGS_PROTO) nopt++; if(flags & FLAGS_JA4C) nopt++; + if(flags & FLAGS_JA5C) nopt++; if(flags & FLAGS_TLSFP) nopt++; if(flags & FLAGS_TLSV) nopt++; if(flags & FLAGS_INPROGRESS) nopt++; @@ -700,6 +706,7 @@ ndpi_mt_help(void) " (list of protocols comma separated)\n" " --inprogress protocols Match if protocols detection is not finished yet\n" " --ja4c protocols Match ja4 client hash (user defined protocols)\n" + " --ja5c protocols Match ja5 client hash (user defined protocols)\n" " --tlsfp protocols Match tls fingerprint (user defined protocols)\n" " --tlsv protocols Match tls version (user defined protocols)\n" "Special protocol names:\n" @@ -964,6 +971,7 @@ void _init(void) MT_OPT(NDPI_OPT_HOST,"host",1) MT_OPT(NDPI_OPT_INPROGRESS,"inprogress",1) MT_OPT(NDPI_OPT_JA4C,"ja4c",1) + MT_OPT(NDPI_OPT_JA5C,"ja5c",1) MT_OPT(NDPI_OPT_TLSFP,"tlsfp",1) MT_OPT(NDPI_OPT_TLSV,"tlsv",1) MT_OPT(NDPI_OPT_UNTRACKED,"untracked",0) diff --git a/ndpi-netfilter/src/Makefile b/ndpi-netfilter/src/Makefile index 48a0d1ed6..2e534c2e7 100644 --- a/ndpi-netfilter/src/Makefile +++ b/ndpi-netfilter/src/Makefile @@ -21,7 +21,7 @@ NDPI_PROTO_SRC := $(shell cd $(src) ; echo $(NDPI_PRO)/*.c) obj-m := xt_ndpi.o xt_ndpi-y := main.o ndpi_debug.o ndpi_long_list.o ndpi_strcol.o ndpi_proc_parsers.o ndpi_proc_generic.o \ ndpi_proc_info.o ndpi_proc_flow.o ndpi_proc_hostdef.o \ - ndpi_proc_ipdef.o ndpi_proc_magic_ct.o ndpi_static_bitmap.o \ + ndpi_proc_ipdef.o ndpi_proc_magic_ct.o ndpi_proc_hash.o ndpi_static_bitmap.o \ ../libre/regexp.o \ ${NDPI_SRC}/lib/third_party/src/ndpi_md5.o \ ${NDPI_SRC}/lib/third_party/src/ndpi_sha1.o \ diff --git a/ndpi-netfilter/src/main.c b/ndpi-netfilter/src/main.c index 5a765961a..6937fc80c 100644 --- a/ndpi-netfilter/src/main.c +++ b/ndpi-netfilter/src/main.c @@ -111,6 +111,8 @@ static char proto_name[]="proto"; static char debug_name[]="debug"; static char risk_name[]="risks"; static char cfg_name[]="cfg"; +static char hash_ja_name[]="hash_ja4"; +static char hash_sha1_name[]="hash_sha1"; static struct module *nfnl_mod = NULL; @@ -179,6 +181,7 @@ MODULE_ALIAS("ipt_NDPI"); #include "ndpi_proc_generic.h" #include "ndpi_proc_parsers.h" #include "ndpi_proc_info.h" +#include "ndpi_proc_hash.h" #include "ndpi_proc_flow.h" #include "ndpi_proc_hostdef.h" #include "ndpi_proc_ipdef.h" @@ -375,7 +378,7 @@ module_param_named(ndpi_enable_flow, ndpi_enable_flow, ulong, 0400); MODULE_PARM_DESC(ndpi_enable_flow,"Enable netflow info"); module_param_string(ndpi_flow_opt, ndpi_flow_opt, NDPI_FLOW_OPT_MAX , 0600); -MODULE_PARM_DESC(ndpi_enable_flow,"Enable flow info option. 'c' - ja4, 'F' - tls fingerprint sha1, 'L' - level, 'R' - risks"); +MODULE_PARM_DESC(ndpi_enable_flow,"Enable flow info option. '4' - ja4, '5' - ja5, 'F' - tls fingerprint sha1, 'L' - level, 'R' - risks"); module_param_named(ndpi_stun_cache,ndpi_stun_cache_opt,ulong,0600); MODULE_PARM_DESC(ndpi_stun_cache,"STUN cache control (0-1). Disabled by default."); @@ -1128,20 +1131,26 @@ static void ndpi_host_info(struct nf_ct_ext_ndpi *ct_ndpi) { (flow->core.tls_quic.certificate_processed || !flow->core.extra_packets_func)) set_tlsdone(ct_ndpi); - if(flow->metadata.protos.tls_quic.ja4_client[0]) { + if(!ct_ndpi->ja4c && flow->metadata.protos.tls_quic.ja4_client[0]) { ct_ndpi->ja4c = l+1; l += snprintf(&buf[l],sizeof(buf)-1-l,"%s", flow->metadata.protos.tls_quic.ja4_client); buf[l++] = 0; } - if(flow->metadata.protos.tls_quic.fingerprint_set) { + if(!ct_ndpi->ja5c && flow->metadata.protos.tls_quic.ja5_client[0]) { + ct_ndpi->ja5c = l+1; + l += snprintf(&buf[l],sizeof(buf)-1-l,"%s", + flow->metadata.protos.tls_quic.ja5_client); + buf[l++] = 0; + } + if(!ct_ndpi->tlsfp && flow->metadata.protos.tls_quic.fingerprint_set) { uint32_t * sha1 = (uint32_t *)flow->metadata.protos.tls_quic.sha1_certificate_fingerprint; ct_ndpi->tlsfp = l+1; - l += snprintf(&buf[l],sizeof(buf)-1-l,"%08x%08x%08x%08x%08x", + l += snprintf(&buf[l],sizeof(buf)-1-l,"%08X%08X%08X%08X%08X", htonl(sha1[0]),htonl(sha1[1]),htonl(sha1[2]),htonl(sha1[3]),htonl(sha1[4])); buf[l++] = 0; } - if(flow->metadata.protos.tls_quic.ssl_version) { + if(!ct_ndpi->tlsv && flow->metadata.protos.tls_quic.ssl_version) { char buf_ver[18]; u_int8_t known_tls = 0; char *r = ndpi_ssl_version2str(buf_ver, sizeof(buf_ver)-1, @@ -1153,9 +1162,10 @@ static void ndpi_host_info(struct nf_ct_ext_ndpi *ct_ndpi) { } if(_DBG_TRACE_JA4) - pr_info("%s: TLS ja4c %s, tlsfp %s, tlsv %s\n", + pr_info("%s: TLS ja4c %s, ja5c %s, tlsfp %s, tlsv %s\n", __func__, ct_ndpi->ja4c ? buf+ct_ndpi->ja4c-1 : "", + ct_ndpi->ja5c ? buf+ct_ndpi->ja5c-1 : "", ct_ndpi->tlsfp ? buf+ct_ndpi->tlsfp-1 : "", ct_ndpi->tlsv ? buf+ct_ndpi->tlsv-1 : ""); if(_DBG_TRACE_TLS) @@ -1217,7 +1227,7 @@ static inline int check_excluded_proto(struct ndpi_detection_module_struct *ndpi return 1; } -static bool ndpi_j4_match(struct ndpi_detection_module_struct *ndpi_struct, +static bool ndpi_opt_match(struct ndpi_detection_module_struct *ndpi_struct, const struct xt_ndpi_mtinfo *info, const char *prefix,const char *buf) { char key[64]; @@ -1238,21 +1248,25 @@ static bool ndpi_j4_match(struct ndpi_detection_module_struct *ndpi_struct, static void ndpi_check_opt(struct ndpi_detection_module_struct *ndpi_struct, const struct xt_ndpi_mtinfo *info, struct nf_ct_ext_ndpi *ct_ndpi, - bool *host_matched, bool *ja4c_matched, + bool *host_matched, bool *ja4c_matched, bool *ja5c_matched, bool *tlsfp_matched, bool *tlsv_matched) { if(info->host && info->hostname[0]) *host_matched = ndpi_host_match(info,ct_ndpi); if(ct_ndpi->flow_opt) { if(info->ja4c && ct_ndpi->ja4c) - *ja4c_matched = ndpi_j4_match(ndpi_struct,info,"JA4C_",ct_ndpi->flow_opt+ct_ndpi->ja4c-1); + *ja4c_matched = ndpi_opt_match(ndpi_struct,info,"JA4C_",ct_ndpi->flow_opt+ct_ndpi->ja4c-1); + if(info->ja5c && ct_ndpi->ja5c) + *ja5c_matched = ndpi_opt_match(ndpi_struct,info,"JA5C_",ct_ndpi->flow_opt+ct_ndpi->ja5c-1); if(info->tlsfp && ct_ndpi->tlsfp) - *tlsfp_matched = ndpi_j4_match(ndpi_struct,info,"TLSFP_",ct_ndpi->flow_opt+ct_ndpi->tlsfp-1); + *tlsfp_matched = ndpi_opt_match(ndpi_struct,info,"TLSFP_",ct_ndpi->flow_opt+ct_ndpi->tlsfp-1); if(info->tlsv && ct_ndpi->tlsv) - *tlsv_matched = ndpi_j4_match(ndpi_struct,info,"TLSV_",ct_ndpi->flow_opt+ct_ndpi->tlsv-1); - if(_DBG_TRACE_JA4) pr_info("%s: flow_opt %c%s, %c%s, %c%s\n",__func__, + *tlsv_matched = ndpi_opt_match(ndpi_struct,info,"TLSV_",ct_ndpi->flow_opt+ct_ndpi->tlsv-1); + if(_DBG_TRACE_JA4) pr_info("%s: flow_opt %c%s, %c%s, %c%s, %c%s\n",__func__, *ja4c_matched ? '+':'-', ct_ndpi->ja4c ? ct_ndpi->flow_opt+ct_ndpi->ja4c-1:"", + *ja5c_matched ? '+':'-', + ct_ndpi->ja5c ? ct_ndpi->flow_opt+ct_ndpi->ja5c-1:"", *tlsfp_matched ? '+':'-', ct_ndpi->tlsfp ? ct_ndpi->flow_opt+ct_ndpi->tlsfp-1:"", *tlsv_matched ? '+':'-', @@ -1358,7 +1372,7 @@ ndpi_mt(const struct sk_buff *skb, struct xt_action_param *par) struct ndpi_dissector_bitmask dissector_excluded_proto; uint8_t l4_proto=0,ct_dir=0,detect_complete=1,untracked=1,confidence=0,tls=0; bool result=false, host_matched = false, is_ipv6=false, - ja4c_matched = false, tlsfp_matched = false, tlsv_matched = false, + ja4c_matched = false, ja5c_matched = false, tlsfp_matched = false, tlsv_matched = false, ct_create = false, new_packet = false; struct ndpi_net *n; @@ -1715,17 +1729,17 @@ ndpi_mt(const struct sk_buff *skb, struct xt_action_param *par) if(ct_ndpi) { if(proto.proto.app_protocol != NDPI_PROCESS_ERROR) ndpi_check_opt(n->ndpi_struct,info,ct_ndpi, &host_matched, - &ja4c_matched, &tlsfp_matched, &tlsv_matched); + &ja4c_matched, &ja5c_matched, &tlsfp_matched, &tlsv_matched); spin_unlock_bh (&ct_ndpi->lock); } read_unlock(&n->ndpi_busy); if(_DBG_TRACE_MATCH2 && !info->error && !info->untracked) { - pr_info(" ndpi_match master %d, app %d, host %d, ja4c %d, tlsfp %d tlsv %d" + pr_info(" ndpi_match master %d, app %d, host %d, ja4c %d, ja5c %d, tlsfp %d tlsv %d" " excluded %d, master_map %d, app_map %d\n", proto.proto.master_protocol, proto.proto.app_protocol, - host_matched, ja4c_matched,tlsfp_matched,tlsv_matched, + host_matched, ja4c_matched,ja5c_matched,tlsfp_matched,tlsv_matched, check_excluded_proto(n->ndpi_struct,info,&dissector_excluded_proto,tls) != 0, NDPI_COMPARE_PROTOCOL_TO_BITMASK(&info->flags,proto.proto.master_protocol) != 0, NDPI_COMPARE_PROTOCOL_TO_BITMASK(&info->flags,proto.proto.app_protocol) != 0); @@ -1756,6 +1770,11 @@ ndpi_mt(const struct sk_buff *skb, struct xt_action_param *par) result &= ja4c_matched; if(_DBG_TRACE_MATCH) pr_info(" ndpi_match ja4c: %s\n",result ? "yes":"no"); + } else + if (info->ja5c) { + result &= ja5c_matched; + if(_DBG_TRACE_MATCH) + pr_info(" ndpi_match ja5c: %s\n",result ? "yes":"no"); } else if (info->tlsfp) { result &= tlsfp_matched; @@ -2846,6 +2865,8 @@ PROC_OPS(nproto_proc_fops, ninfo_proc_open,nproto_proc_read,nproto_proc_write,no PROC_OPS(ndebug_proc_fops, ninfo_proc_open,ndebug_proc_read,ndebug_proc_write,noop_llseek,ndebug_proc_close); PROC_OPS(nrisk_proc_fops, nrisk_proc_open,nrisk_proc_read,nrisk_proc_write,noop_llseek,nrisk_proc_close); PROC_OPS(ncfg_proc_fops, ncfg_proc_open, ncfg_proc_read, ncfg_proc_write, noop_llseek,ncfg_proc_close); +PROC_OPS(nhash_ja_proc_fops, nhash_ja4_proc_open, nhash_proc_read, nhash_proc_write, noop_llseek,nhash_proc_close); +PROC_OPS(nhash_sha1_proc_fops, nhash_sha1_proc_open, nhash_proc_read, nhash_proc_write, noop_llseek,nhash_proc_close); PROC_OPS(ninfo_proc_fops, ninfo_proc_open,ninfo_proc_read,ninfo_proc_write,noop_llseek,ninfo_proc_close); PROC_OPS(nflow_proc_fops, nflow_proc_open,nflow_proc_read,nflow_proc_write,nflow_proc_llseek,nflow_proc_close); PROC_OPS(nmagic_ct_proc_fops, nmagic_ct_proc_open,nmagic_ct_proc_read,nmagic_ct_proc_write,noop_llseek,nmagic_ct_proc_close); @@ -3033,6 +3054,10 @@ static void __net_exit ndpi_net_exit(struct net *net) remove_proc_entry(flow_name, n->pde); if(n->pe_magic_ct) remove_proc_entry(magic_ct_name, n->pde); + if(n->pe_hash_ja) + remove_proc_entry(hash_ja_name, n->pde); + if(n->pe_hash_sha1) + remove_proc_entry(hash_sha1_name, n->pde); #ifdef BT_ANNOUNCE if(n->pe_ann) remove_proc_entry(ann_name, n->pde); @@ -3232,6 +3257,20 @@ static int __net_init ndpi_net_init(struct net *net) pr_err("xt_ndpi: cant create net/%s/%s\n",dir_name,cfg_name); break; } + n->pe_hash_ja = proc_create_data(hash_ja_name, S_IRUGO, + n->pde, &nhash_ja_proc_fops, n); + if(!n->pe_hash_ja) { + pr_err("xt_ndpi: cant create net/%s/%s\n",dir_name,hash_ja_name); + break; + } + + n->pe_hash_sha1 = proc_create_data(hash_sha1_name, S_IRUGO, + n->pde, &nhash_sha1_proc_fops, n); + if(!n->pe_hash_sha1) { + pr_err("xt_ndpi: cant create net/%s/%s\n",dir_name,hash_sha1_name); + break; + } + #ifdef BT_ANNOUNCE n->pe_ann = proc_create_data(ann_name, S_IRUGO, n->pde, &nann_proc_fops, n); @@ -3301,6 +3340,8 @@ static int __net_init ndpi_net_init(struct net *net) nf_register_net_hooks(net, nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops))) break; /* All success! */ + ndpi_hash_init(&n->ndpi_struct->malicious_ja4_hashmap); + ndpi_hash_init(&n->ndpi_struct->malicious_sha1_hashmap); atomic_set(&n->ndpi_ready,1); net_ns_id++; if(_DBG_TRACE_NETNS) diff --git a/ndpi-netfilter/src/ndpi_main_netfilter.h b/ndpi-netfilter/src/ndpi_main_netfilter.h index aa0a4864a..bf9705bf6 100644 --- a/ndpi-netfilter/src/ndpi_main_netfilter.h +++ b/ndpi-netfilter/src/ndpi_main_netfilter.h @@ -30,6 +30,7 @@ typedef enum write_buf_id { W_BUF_DEBUG, W_BUF_RISK, W_BUF_CFG, + W_BUF_HASH, W_BUF_LAST } write_buf_id_t; @@ -69,6 +70,8 @@ struct ndpi_net { *pe_risk, *pe_cfg, *pe_hostdef, + *pe_hash_ja, + *pe_hash_sha1, *pe_ipdef; hosts_str_t *hosts; @@ -101,6 +104,8 @@ struct ndpi_net { struct list_head flow_new; // for created flows struct long_list flow_dump; // for flows with info + ndpi_str_hash *ndpi_hash; // current hash for read/write + int ndpi_hash_src; // source of ndpi_hash: 1=ja4,2=sha1 char *risk_names; size_t risk_names_len; ndpi_risk risk_mask; @@ -220,17 +225,17 @@ struct nf_ct_ext_ndpi { char *host; // 4/8 bytes char *flow_opt; // 4/8 bytes + spinlock_t lock; // 2/4 bytes + // ?/64 bytes with debug spinlock + ndpi_protocol_nf proto; // 4 bytes uint16_t flags, // 2 bytes ja4c, // 2 + ja5c, // 2 tlsv, // 2 tlsfp; // 2 - ndpi_protocol_nf proto; // 4 bytes uint8_t l4_proto, // 1 confidence, // 1 confirm:1,unconfirm:1; // 1 - // offset+1 in flow_opt - spinlock_t lock; // 2/4 bytes - // ?/64 bytes with debug spinlock } __attribute__((__aligned__(__SIZEOF_LONG__ * 2))); diff --git a/ndpi-netfilter/src/ndpi_proc_flow.c b/ndpi-netfilter/src/ndpi_proc_flow.c index 4a23fe564..026dea81c 100644 --- a/ndpi-netfilter/src/ndpi_proc_flow.c +++ b/ndpi-netfilter/src/ndpi_proc_flow.c @@ -72,6 +72,7 @@ static inline int add_opt_str(char *buf,size_t l,size_t size,char c,char *str) { if(l + nc >= size) return 0; buf += l; if(l) *buf++ = ' '; + if(c == '4' || c == '5') *buf++ = 'j'; *buf++ = c; *buf++ = '='; strcpy(buf,str); @@ -168,12 +169,18 @@ size_t ndpi_dump_opt(char *buf, size_t bufsize, flag |= 0x10; } break; - case 'c': + case '4': if(!(flag & 0x40) && ct->ja4c) { - l += add_opt_str(buf,l,bufsize,'c',&ct->flow_opt[ct->ja4c-1]); + l += add_opt_str(buf,l,bufsize,'4',&ct->flow_opt[ct->ja4c-1]); flag |= 0x40; } break; + case '5': + if(!(flag & 0x80) && ct->ja5c) { + l += add_opt_str(buf,l,bufsize,'5',&ct->flow_opt[ct->ja5c-1]); + flag |= 0x80; + } + break; case 'F': if(!(flag & 4) && ct->tlsfp) { l += add_opt_str(buf,l,bufsize,'F',&ct->flow_opt[ct->tlsfp-1]); diff --git a/ndpi-netfilter/src/ndpi_proc_hash.c b/ndpi-netfilter/src/ndpi_proc_hash.c new file mode 100644 index 000000000..c0af0d837 --- /dev/null +++ b/ndpi-netfilter/src/ndpi_proc_hash.c @@ -0,0 +1,274 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include "ndpi_config.h" +#undef HAVE_HYPERSCAN +#include "ndpi_main.h" +#include "ndpi_private.h" + +#define uthash_malloc(sz) ndpi_malloc(sz) +#define uthash_free(ptr,sz) ndpi_free(ptr) + +#include "third_party/include/uthash.h" + +#include "ndpi_strcol.h" +#include "ndpi_main_common.h" +#include "ndpi_main_netfilter.h" +#include "ndpi_proc_parsers.h" +#include "ndpi_proc_generic.h" +#include "ndpi_proc_hash.h" + +/* copy from src/lib/ndpi_utils.c */ + +typedef struct { + char *key; + u_int64_t value64; + ndpi_list value_list; + UT_hash_handle hh; +} ndpi_str_hash_priv; + +typedef int (*ndpi_hash_walk_iter2)(char *key, u_int64_t value64, void *data); + +int ndpi_hash_walk2(ndpi_str_hash **h, ndpi_hash_walk_iter2 cb, void *data) { + if(h && *h) { + ndpi_str_hash_priv *h_priv = (ndpi_str_hash_priv *)((*h)->priv); + ndpi_str_hash_priv *curr, *tmp; + + HASH_ITER(hh, h_priv, curr, tmp) { + if(cb(curr->key, curr->value64, data)) return 1; + } + } + return 0; +} + +/* copy from src/lib/ndpi_utils.c */ + +struct nhash_ws { + loff_t ppos; + char *u_buf; + size_t count; + + loff_t pos; + + size_t output; + int err,eob; +}; + +#define h_buf_size 128 + +static noinline int nhash_walker(char *key, u_int64_t value64, void *data) { + struct nhash_ws *ws = (struct nhash_ws *)data; + char *head; + size_t copy_len = 0, offs = 0, skip = 0, k_len; + loff_t ppos_end = ws->ppos + ws->count - 1; + loff_t pos_end; + char h_buf[h_buf_size+2]; + + k_len = strlen(key)+1; + pos_end = ws->pos + k_len - 1; + + if(_DBG_TRACE_GPROC) + pr_info("%s: buf %lld-%lld[%d] data %lld-%lld[%d]\n",__func__, + ws->ppos, ppos_end, (int)ws->count, + ws->pos, pos_end, (int)k_len); + + if(!ws->count || ws->pos > ppos_end) { // buffer is full + ws->eob = 1; + return 1; + } + if(pos_end < ws->ppos) { + ws->pos += k_len; + return 0; + } + if(k_len > h_buf_size-2) { // BUG! + pr_err("%s: error: key length %d > %d\n",__func__,(int)k_len, (int)h_buf_size-1); + ws->err = EINVAL; + return 1; + } + + memcpy(h_buf,key,k_len-1); + h_buf[k_len-1] = '\n'; + h_buf[k_len] = '\0'; + head = h_buf; + copy_len = k_len; + + if(ws->pos < ws->ppos) { + // if(ws->pos + k_len - 1 < ws->ppos) { } // imposible! + if(pos_end > ppos_end) { // k b b k + k_len = ws->ppos+ws->count - ws->pos; + ws->eob = 1; + } + copy_len = ws->pos + k_len - ws->ppos; // k b bk + skip = k_len - copy_len; + head += skip; + } else { + // if(ws->pos > ws->ppos + ws->count - 1) { } // inposible! + if(pos_end > ppos_end) { // b k b k + copy_len = ws->ppos + ws->count - ws->pos; + ws->eob = 1; + } + if(ws->pos > ws->ppos) { // b k bk + offs = ws->pos - ws->ppos; + } + } + if(copy_len != 0) { + if(_DBG_TRACE_GPROC) + pr_info("%s: copy key skip:%d size:%d offs:%d '%.*s'\n",__func__, + (int)skip, (int)copy_len, (int)offs,(int)copy_len,head); + memcpy(ws->u_buf+offs, head, copy_len); + } + copy_len += offs; + ws->ppos += copy_len; + ws->u_buf += copy_len; + ws->output += copy_len; + ws->pos += copy_len + skip; + ws->count -= copy_len; + if(!ws->count) { + ws->eob = 1; + return 1; + } + return 0; +} + +ssize_t nhash_proc_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct ndpi_net *n = pde_data(file_inode(file)); + size_t buf_len = count; + char *tmp_buf; + + struct nhash_ws ws = { .pos = 0, .ppos = *ppos, .eob = 0, .err = 0}; + + if(!n->ndpi_hash) { + if(!*ppos) { + static const char msg[] = "hash disabled\n"; + size_t l = sizeof(msg)-1; + if ( copy_to_user(buf, msg, l)) return -EFAULT; + (*ppos) += l; + return l; + } + return 0; + } + if(_DBG_TRACE_GPROC) + pr_info("%s: start: buf %lld-%lld[%d] total %u\n",__func__, + ws.ppos,ws.ppos+count-1,(int)count,(unsigned int)n->ndpi_hash->stats.n_keys_size); + + if(*ppos >= n->ndpi_hash->stats.n_keys_size) { + return 0; + } + buf_len = count; + if(buf_len > 16384) buf_len = 16384; + // < 32k + tmp_buf = kmalloc(buf_len,GFP_KERNEL); + if(!tmp_buf) return -ENOMEM; + + ws.u_buf = tmp_buf; + ws.count = buf_len; + + read_lock(&n->ndpi_struct->hash_map_lock); + ndpi_hash_walk2(&n->ndpi_hash, nhash_walker, (void *)&ws); + read_unlock(&n->ndpi_struct->hash_map_lock); + if(_DBG_TRACE_GPROC) + pr_info("%s: end: ppos %lld-%lld:%d, pos %lld-%lld:%d, %s err %d\n",__func__, + *ppos,ws.ppos-1,(int)ws.count, ws.pos-ws.output,ws.pos-1,(int)ws.output, + ws.eob ? " EOF, ":"",ws.err); + *ppos = ws.ppos; + if(ws.output && copy_to_user(buf,tmp_buf,ws.output)) + ws.err = EFAULT; + kfree(tmp_buf); + if(ws.err) + return -ws.err; + return ws.output; +} +static void ndpi_hash_sync(struct ndpi_net *n) { + + write_lock(&n->ndpi_struct->hash_map_lock); + switch(n->ndpi_hash_src) { + case 1: n->ndpi_struct->malicious_ja4_hashmap = n->ndpi_hash; + break; + case 2: n->ndpi_struct->malicious_sha1_hashmap = n->ndpi_hash; + break; + default: + pr_err("%s: BUG ndpi_hash_src %d\n",__func__,n->ndpi_hash_src); + } + write_unlock(&n->ndpi_struct->hash_map_lock); +} + +int parse_ndpi_hash(struct ndpi_net *n,char *cmd) { + size_t c_len = strlen(cmd); + if(_DBG_TRACE_SPROC) + pr_info("NDPI: hash %s\n",cmd); + + if(!n->ndpi_hash) return 1; + if(*cmd == '+') { + if(c_len > h_buf_size - 2) { + pr_info("NDPI: Error: hash too long\n"); + return 1; + } + write_lock(&n->ndpi_struct->hash_map_lock); + int r = ndpi_hash_add_entry(&n->ndpi_hash,cmd+1,c_len-1,1ULL,NULL); + write_unlock(&n->ndpi_struct->hash_map_lock); + if(r > 1) { + pr_info("NDPI: add hash %s error %d\n",cmd+1,r); + return 1; + } + n->ndpi_hash->stats.n_keys_size += c_len; + return 0; + } else if(*cmd == '-') { // clean; + write_lock(&n->ndpi_struct->hash_map_lock); + ndpi_hash_free(&n->ndpi_hash); + ndpi_hash_init(&n->ndpi_hash); + n->ndpi_hash->stats.n_keys_size = 0; + write_unlock(&n->ndpi_struct->hash_map_lock); + ndpi_hash_sync(n); + return 0; + } else + return 1; +} + +ssize_t nhash_proc_write(struct file *file, const char __user *buffer, + size_t length, loff_t *loff) +{ + return generic_proc_write(pde_data(file_inode(file)), buffer, length, loff, + parse_ndpi_hash, 256, W_BUF_HASH); +} + +int nhash_proc_close(struct inode *inode, struct file *file) +{ + struct ndpi_net *n = pde_data(file_inode(file)); + generic_proc_close(n,parse_ndpi_hash,W_BUF_HASH); + ndpi_hash_sync(n); + n->ndpi_hash = NULL; + return 0; +} + +int nhash_ja4_proc_open(struct inode *inode, struct file *file) { + struct ndpi_net *n = pde_data(file_inode(file)); + + if(cmpxchg(&n->ndpi_hash,NULL,n->ndpi_struct->malicious_ja4_hashmap) != NULL) + return -EBUSY; + n->ndpi_hash_src = 1; + return 0; +} + +int nhash_sha1_proc_open(struct inode *inode, struct file *file) { + struct ndpi_net *n = pde_data(file_inode(file)); + + if(cmpxchg(&n->ndpi_hash,NULL,n->ndpi_struct->malicious_sha1_hashmap) != NULL) + return -EBUSY; + n->ndpi_hash_src = 2; + return 0; +} + + diff --git a/ndpi-netfilter/src/ndpi_proc_hash.h b/ndpi-netfilter/src/ndpi_proc_hash.h new file mode 100644 index 000000000..e42d20638 --- /dev/null +++ b/ndpi-netfilter/src/ndpi_proc_hash.h @@ -0,0 +1,8 @@ + +int nhash_ja4_proc_open(struct inode *inode, struct file *file); +int nhash_sha1_proc_open(struct inode *inode, struct file *file); +ssize_t nhash_proc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos); +ssize_t nhash_proc_write(struct file *file, const char __user *buffer, size_t length, loff_t *loff); +int nhash_proc_close(struct inode *inode, struct file *file); +int parse_ndpi_hash(struct ndpi_net *n,char *cmd); + diff --git a/ndpi-netfilter/src/xt_ndpi.h b/ndpi-netfilter/src/xt_ndpi.h index b5080860a..339ad8bae 100644 --- a/ndpi-netfilter/src/xt_ndpi.h +++ b/ndpi-netfilter/src/xt_ndpi.h @@ -31,7 +31,7 @@ struct xt_ndpi_mtinfo { char hostname[128]; uint64_t risk; uint32_t invert:1,error:1,m_proto:1,p_proto:1,have_master:1,host:1,re:1,empty:1, - proto:1,inprogress:1,tlsfp:1,tlsv:1,ja4c:1,untracked:1,clevel_op:2, + proto:1,inprogress:1,tlsfp:1,tlsv:1,ja4c:1,ja5c:1,untracked:1,clevel_op:2, clevel:4; void *reg_data; // kernel only void *excluded_dissectors; // kernel only diff --git a/src/include/ndpi_private.h b/src/include/ndpi_private.h index 01d21aafb..7be68158a 100644 --- a/src/include/ndpi_private.h +++ b/src/include/ndpi_private.h @@ -442,6 +442,9 @@ struct ndpi_detection_module_struct { ndpi_str_hash *malicious_ja4_hashmap, *malicious_sha1_hashmap, *tcp_fingerprint_hashmap, *http_url_hashmap; spinlock_t host_automa_lock; +#ifdef __KERNEL__ + rwlock_t hash_map_lock; +#endif ndpi_list *trusted_issuer_dn; diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index be6411cf2..8672f945e 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -876,6 +876,7 @@ typedef enum { struct ndpi_str_hash_stats { u_int64_t n_search; u_int64_t n_found; + u_int64_t n_keys_size; }; typedef enum { diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 961739445..139654357 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -4449,6 +4449,9 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(struct ndpi_glob ndpi_str->tls_cert_subject_automa.ac_automa = ac_automata_init(NULL); ndpi_str->risky_domain_automa.ac_automa = NULL; /* Initialized on demand */ +#ifdef __KERNEL__ + rwlock_init(&ndpi_str->hash_map_lock); +#endif ndpi_str->malicious_ja4_hashmap = NULL; /* Initialized on demand */ ndpi_str->malicious_sha1_hashmap = NULL; /* Initialized on demand */ ndpi_str->ja4_custom_protos = NULL; /* Initialized on demand */ @@ -5477,17 +5480,15 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) { if(ndpi_str->tcp_fingerprint_hashmap != NULL) ndpi_hash_free(&ndpi_str->tcp_fingerprint_hashmap); -#ifndef __KERNEL__ if(ndpi_str->malicious_ja4_hashmap != NULL) ndpi_hash_free(&ndpi_str->malicious_ja4_hashmap); if(ndpi_str->malicious_sha1_hashmap != NULL) ndpi_hash_free(&ndpi_str->malicious_sha1_hashmap); - +#ifndef __KERNEL__ ndpi_domain_classify_free(ndpi_str->custom_categories.sc_hostnames_shadow); ndpi_domain_classify_free(ndpi_str->custom_categories.sc_hostnames); #endif - if(ndpi_str->custom_categories.ipAddresses != NULL) ndpi_patricia_destroy((ndpi_patricia_tree_t *) ndpi_str->custom_categories.ipAddresses, free_ptree_data); diff --git a/src/lib/protocols/tls.c b/src/lib/protocols/tls.c index 3fde605c3..fb27ff5a2 100644 --- a/src/lib/protocols/tls.c +++ b/src/lib/protocols/tls.c @@ -1194,35 +1194,27 @@ static void tls_match_ja4(struct ndpi_detection_module_struct *ndpi_struct, NDPI_CONFIDENCE_CUSTOM_RULE); } -#ifndef __KERNEL__ +#ifdef __KERNEL__ + read_lock(&ndpi_struct->hash_map_lock); +#endif if(ndpi_struct->malicious_ja4_hashmap != NULL) { - u_int16_t rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_ja4_hashmap, + u_int16_t rc1 = 1; + if(flow->metadata.protos.tls_quic.ja4_client[0]) + rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_ja4_hashmap, flow->metadata.protos.tls_quic.ja4_client, NDPI_ARRAY_LENGTH(flow->metadata.protos.tls_quic.ja4_client) - 1, NULL); - + if(rc1 && flow->metadata.protos.tls_quic.ja5_client[0]) + rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_ja4_hashmap, + flow->metadata.protos.tls_quic.ja5_client, + NDPI_ARRAY_LENGTH(flow->metadata.protos.tls_quic.ja5_client) - 1, + NULL); if(rc1 == 0) ndpi_set_risk(ndpi_struct, &flow->core, NDPI_MALICIOUS_FINGERPRINT, flow->metadata.protos.tls_quic.ja4_client); } -#else - { - static const char pref_str[]="RISK_JA4_"; - char risk_ja4_str[sizeof(pref_str) + sizeof(flow->metadata.protos.tls_quic.ja4_client) + 1]; - u_int32_t val; - u_int16_t rc1; - size_t len = sizeof(pref_str)-1,len2 = strlen(flow->metadata.protos.tls_quic.ja4_client); - - strcpy(risk_ja4_str,pref_str); - strncpy(&risk_ja4_str[len],flow->metadata.protos.tls_quic.ja4_client,len2); - len += len2; - risk_ja4_str[len] = '\0'; - - rc1 = ndpi_match_string_value(ndpi_struct->host_automa.ac_automa, - risk_ja4_str, len | AC_FEATURE_EXACT, &val) == -1; - if(rc1 == 0) - ndpi_set_risk(ndpi_struct, &flow->core, NDPI_MALICIOUS_FINGERPRINT, flow->metadata.protos.tls_quic.ja4_client); - } +#ifdef __KERNEL__ + read_unlock(&ndpi_struct->hash_map_lock); #endif } @@ -1337,27 +1329,16 @@ int processCertificate(struct ndpi_detection_module_struct *ndpi_struct, #ifdef DEBUG_TLS printf("[TLS] SHA-1: %s\n", sha1_str); #endif -#ifndef __KERNEL__ +#ifdef __KERNEL__ + read_lock(&ndpi_struct->hash_map_lock); +#endif if(ndpi_struct->malicious_sha1_hashmap != NULL) rc1 = ndpi_hash_find_entry(ndpi_struct->malicious_sha1_hashmap, sha1_str, sha1_siz * 2, NULL) != 0; -#else - { - static const char pref_str[]="RISK_SHA1CERT_"; - char risk_sha1_str[sizeof(pref_str) + 20 /* sha1_siz */ * 2 + 1]; - u_int32_t val; - size_t len = sizeof(pref_str)-1; - - strcpy(risk_sha1_str,pref_str); - strncpy(&risk_sha1_str[len],sha1_str,sha1_siz*2); - len += sha1_siz*2; - risk_sha1_str[len] = '\0'; - rc1 = ndpi_match_string_value(ndpi_struct->host_automa.ac_automa, - risk_sha1_str, len | AC_FEATURE_EXACT, &val) == -1; - } -#endif if(rc1 == 0) ndpi_set_risk(ndpi_struct, &flow->core, NDPI_MALICIOUS_SHA1_CERTIFICATE, sha1_str); - +#ifdef __KERNEL__ + read_unlock(&ndpi_struct->hash_map_lock); +#endif } if(num_certificates_found == 1) processCertificateElements(ndpi_struct, flow, certificates_offset, certificate_len);