TGTGInsighttelegram intelligenceLIVE / telegram public index
← Welcome to the Black Parade
Welcome to the Black Parade avatar

TGINSIGHT POST

Post #977

@TheB1ackParade

Welcome to the Black Parade

Views2,090Post view count
PostedDec 1412/14/2025, 04:31 AM
Post content

Post content

去年被 ibug 感谢了今天才发现(这种阴暗爬行的风格有点像我,相反面就是伊洪那种大开大合的性格“我在这里感谢了你请查收谢谢”) https://ibug.io/blog/2024/08/first-touch-bpftrace/ 其他的我看不懂,但他说这个 bpftrace 跑不起来我就来劲了 #include <linux/socket.h> #include <net/netfilter/nf_conntrack.h> kprobe:nf_ct_delete { // The first argument is the struct nf_conn $ct = (struct nf_conn *)arg0; // Check if the connection is for IPv4 if ($ct->tuplehash[0].tuple.src.l3num == AF_INET) { $src_ip = $ct->tuplehash[0].tuple.src.u3.ip; $dst_ip = $ct->tuplehash[0].tuple.dst.u3.ip; printf("Conntrack destroyed (IPv4): src=%s dst=%s\n", ntop($src_ip), ntop($dst_ip)); } } 我试了一下最新版的 0.24 虽然报错不同但依然解不出 $dst_ip。其实这种依赖 BTF 自动计算偏移解引用如果有问题的话就改成手动解引用,零难度,就是可读性差一点。 比如我们要读 $ct->tuplehash[0].tuple.dst.u3.ip ,那就依次检查一下这些结构体字段的偏移: ~ $ pahole -C nf_conn | grep tuplehash struct nf_conntrack_tuple_hash tuplehash[2]; /* 16 112 */ ~ $ pahole -C nf_conntrack_tuple_hash | grep tuple struct nf_conntrack_tuple tuple; /* 16 40 */ ~ $ pahole -C nf_conntrack_tuple | grep u3 union nf_inet_addr u3; /* 20 16 */ ~ $ pahole -C nf_inet_addr | grep ip __be32 ip; /* 0 4 */ 然后从 $ct 指针手动偏移再 deref 就可以了 $dst_ip = *(uint32*)((uint8*)$ct + 16 + 16 + 20 + 0); 然后就可以运行了 $ sudo bpftrace ct_del.bt Conntrack destroyed (IPv4): src=127.0.0.1 dst=127.0.0.53 ^C