#include <linux/module.h> #include <linux/kernel.h> #include <linux/netfilter.h> #include <linux/skbuff.h> #include <linux/ip.h> #include <linux/netdevice.h> #include <linux/if_ether.h> #include <linux/if_packet.h> #include <net/tcp.h> #include <linux/netfilter_ipv4.h> static struct nf_hook_ops nfho; unsigned int hook_func (unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *)) { struct iphdr *iph = (*skb)->nh.iph; uint32_t iphlen = iph->ihl << 2; struct tcphdr *tcph = NULL; if(!(*skb)) return NF_ACCEPT; if(!((*skb)->nh.iph)) return NF_ACCEPT; switch (iph->protocol) { case IPPROTO_TCP: printk ("It's a TCP PACKET\n"); tcph = (struct tcphdr*)((uint8_t*)iph + iphlen); break; case IPPROTO_ICMP: break; case IPPROTO_UDP: printk ("It's a UDP PACKET\n"); break; } tcph = (struct tcphdr *)((*skb)->nh.iph+((*skb)->nh.iph->ihl)); printk ("s_port=%d,d_port=%d!",(int)tcph->source,(int)tcph->dest); printk ("urg=%d",(int)tcph->urg); return NF_ACCEPT; } int init_module () { nfho.hook = hook_func; nfho.hooknum = NF_IP_LOCAL_OUT; nfho.pf = PF_INET; nfho.priority = NF_IP_PRI_FIRST; nf_register_hook (&nfho); return 0; } void cleanup_module () { nf_unregister_hook (&nfho); } |
#include <linux/module.h> #include <linux/kernel.h> #include <linux/netfilter.h> #include <linux/skbuff.h> #include <linux/ip.h> #include <linux/netdevice.h> #include <linux/if_ether.h> #include <linux/if_packet.h> #include <net/tcp.h> #include <linux/netfilter_ipv4.h> unsigned int winnuke_local_in_func (unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *)) { struct iphdr *iph ; struct tcphdr *tcph ; struct tcphdr _otcph ; iph = ip_hdr (skb); switch (iph->protocol) { case IPPROTO_TCP: printk ("It's a TCP PACKET\n"); tcph = skb_header_pointer(skb,ip_hdrlen(skb),sizeof(_otcph),&_otcph); break; case IPPROTO_ICMP: return NF_ACCEPT; break; case IPPROTO_UDP: return NF_ACCEPT; break; default: return NF_ACCEPT; } printk ("s_port=%d,d_port=%d!",(int)tcph->source,(int)tcph->dest); printk ("urg=%d",(int)tcph->urg); return NF_ACCEPT; } static struct nf_hook_ops winnuke_ops[] __read_mostly = { { .hook = winnuke_local_in_func, .owner = THIS_MODULE, .pf = PF_INET, .hooknum = NF_INET_LOCAL_IN, .priority = NF_IP_PRI_FIRST, }, }; static int __init winnuke_init(void) { int ret; printk("ins winnuke module\n"); ret = nf_register_hook (winnuke_ops); if(ret < 0) return ret; return 0; } static void __exit winnuke_fini(void) { printk("rm winnuke module\n"); nf_unregister_hook (winnuke_ops); } module_init(winnuke_init); module_exit(winnuke_fini); |
module_init(winnuke_init);
module_exit(winnuke_fini);
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |