Netfilter字符串模块示例用法

任何人都可以指出一些使用xt_string模块和netfilter的例子或提供一个例子。 我想要做的是编写netfilter模块,它将在skb-> data字段中丢弃包含某个字符串的数据包。

我最初尝试简单地strnstr(skb->data, "mystring", strlen("mystring"))但这似乎是不正确的方法解决这个问题(它似乎没有工作,因为我没有看到任何数据包被丢弃) 。

提前致谢

如果你的意思是在用户空间中使用iptables字符串匹配,这是一个例子:

 iptables -I INPUT 1 -p tcp --dport 80 -m string --string "domain.com" --algo kmp -j DROP 

或者,如果您在内核空间中指的是,您可以使用提供KMP / BM / FSM算法的textsearch API,以下示例来自内核源代码lib / textsearch.c:

 int pos; struct ts_config *conf; struct ts_state state; const char *pattern = "chicken"; const char *example = "We dance the funky chicken"; conf = textsearch_prepare("kmp", pattern, strlen(pattern), GFP_KERNEL, TS_AUTOLOAD); if (IS_ERR(conf)) { err = PTR_ERR(conf); goto errout; } pos = textsearch_find_continuous(conf, &state, example, strlen(example)); if (pos != UINT_MAX) panic("Oh my god, dancing chickens at %d\n", pos); textsearch_destroy(conf); 

您正在寻找的可能就是这个,“skb_find_text”。 它使用@Cong Wang提到的linux中的infra。 您还可以在内核代码中找到一些示例。

这里是netfilter的源代码。 它是一个放弃收到的ICMP ECHO的模块

您可以使用此代码来帮助您开发模块。 你只需要从skb获取数据然后检查它。

 #define __KERNEL__ #define MODULE #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  MODULE_LICENSE("GPL"); static struct nf_hook_ops netfilter_ops_in;/* IP PRE ROUTING */ static struct nf_hook_ops netfilter_ops_out; /* NF_IP_POST_ROUTING */ struct sk_buff *sock_buff; struct iphdr *ip_header; struct net_device *dev; char *in_face = "eth0"; char *out_face = "eth1"; void log_ip(int sadd,int dadd) { int b1,b2,b3,b4; b1 = 255 & sadd; b2 = (0xff00 & sadd) >> 8; b3 = (0xff0000 & sadd) >> 16; b4 = (0xff000000 &sadd) >>24; printk("SrcIP: %d.%d.%d.%d",b1,b2,b3,b4); b1 = 255 & dadd; b2 = (0xff00 & dadd) >> 8; b3 = (0xff0000 & dadd) >> 16; b4 = (0xff000000 & dadd) >>24; printk(" DstIP: %d.%d.%d.%d",b1,b2,b3,b4); } unsigned int main_hook(unsigned int hooknum, const struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int(*okfn)(struct sk_buff*)) { struct icmphdr* icmp; sock_buff = skb_copy(skb,GFP_ATOMIC); ip_header = (struct iphdr*)(sock_buff->network_header); //ip_header = ip_hdr(sock_buff); icmp = (struct icmphdr*) ((char*)ip_header + sizeof(struct iphdr)); //icmp = icmp_hdr(skb); /* do not return a good value in all cases*/ log_ip(ip_header->saddr,ip_header->daddr); printk(" Dev:%s\n",sock_buff->dev); if (icmp->type == ICMP_ECHO) { printk("ICMP ECHO received and droped\n"); return NF_DROP; } return NF_ACCEPT; } int init_module(void) { netfilter_ops_in.hook = main_hook; netfilter_ops_in.pf = PF_INET; netfilter_ops_in.hooknum = NF_INET_PRE_ROUTING; /*NF_INET_PRE_ROUTING;*/ netfilter_ops_in.priority = NF_IP_PRI_FIRST; nf_register_hook(&netfilter_ops_in); printk(KERN_INFO "sw: init_module() called\n"); return 0; } void cleanup_module(void) { printk(KERN_INFO "sw: cleanup_module() called\n"); nf_unregister_hook(&netfilter_ops_in); //nf_unregister_hook(&netfilter_ops_out); printk(KERN_INFO "sw: hook unregisted, quit called\n"); }