Created
November 28, 2025 01:01
-
-
Save claustra01/32377589575f34600cb886169c5ece6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from bcc import BPF | |
| bpf_text = r""" | |
| #include <linux/bpf.h> | |
| #include <linux/if_ether.h> | |
| #include <linux/ip.h> | |
| #include <linux/icmp.h> | |
| #define IPPROTO_ICMP 1 | |
| int icmp_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 != bpf_htons(ETH_P_IP)) | |
| return XDP_PASS; | |
| struct iphdr *iph = (void*)(eth + 1); | |
| if ((void*)(iph + 1) > data_end) | |
| return XDP_PASS; | |
| if (iph->protocol != IPPROTO_ICMP) | |
| return XDP_PASS; | |
| struct icmphdr *icmp = (void*)iph + sizeof(*iph); | |
| if ((void*)(icmp + 1) > data_end) | |
| return XDP_PASS; | |
| bpf_trace_printk("ICMP type=%d code=%d id=%d\n", | |
| icmp->type, icmp->code, icmp->un.echo.id); | |
| return XDP_PASS; | |
| } | |
| """ | |
| b = BPF(text=bpf_text) | |
| fn = b.load_func("icmp_xdp", BPF.XDP) | |
| DEV = "eno12399np0" # ip route get 1.1.1.1 | |
| b.attach_xdp(DEV, fn, 0) | |
| print("ICMP hooked") | |
| try: | |
| while True: | |
| print(b.trace_readline().decode("utf-8", errors="ignore").strip()) | |
| except KeyboardInterrupt: | |
| pass | |
| finally: | |
| b.remove_xdp(DEV, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment