【问题标题】:How to use netfilter to drop some package have some spec data on Linux?如何使用 netfilter 删除一些包在 Linux 上有一些规范数据?
【发布时间】:2016-01-03 22:29:01
【问题描述】:

我搜索了很长时间,但现在只能获取 IP 并记录它们。

    __be32 sip,dip;
 if(skb){
   struct sk_buff *sb = NULL;
   sb = skb;
   struct iphdr *iph;
   iph  = ip_hdr(sb);
   sip = iph->saddr;
   dip = iph->daddr;
   printk("Packet for source address: %d.%d.%d.%d\n destination address: %d.%d.%d.%d\n ", NIPQUAD(sip), NIPQUAD(dip));
        }
 return NF_ACCEPT;

我尝试sb->data,但无法获得包数据之类的任何内容..

我只想删除包含这些数据的包,data.data == 25:3f:08:52:45:47:49:53:54:45:52:46:4d:4c:00:46:4d:4c:7c:48:53:00:46:4f:52:47:45:00:42:75:6e:67:65:65:43:6f:72:64,因为这些包可能来自某些攻击者..

我应该使用netfilter(Linux kernel) 吗?我无法更改程序的代码,所以我想通过 Centos 删除这些包..

【问题讨论】:

    标签: linux kernel netfilter


    【解决方案1】:

    这是一个简单的模块:

    #include <linux/kernel.h>
    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/netfilter.h>
    #include <linux/netfilter_ipv4.h>
    #include <linux/ip.h>
    #include <linux/fs.h>
    #include <linux/device.h>
    #include <linux/slab.h>
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Reuven Plevinsky");
    
    static struct nf_hook_ops nfho;
    
    int search_str(struct sk_buff *skb, char* str, int len)
    {
        int buf_len = skb->tail - skb->data;
        int  i, j, offset;
        if (len > buf_len)
            return -1;
        for (i = 0, j = 0, offset = 0; (i < buf_len && j < len);)
        {
            if (skb->data[i] == str[j])
            {
                if (j == 0)
                    offset = i;
                if (j == len - 1)
                    return offset;
                else
                {
                    i++;
                    j++;
                }
            }
            else
            {
                if (j != 0)
                {
                    i = offset + 1;
                    j = 0;
                }
                else
                {
                    i++;
                }
            }
        }
        return -1;
    }
    
    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 *))
    {
        __u32 src_add;
        __u32 dst_add;
        struct iphdr *ip_header;
        char* mal = "%?REGISTERFMLFML|HSFORGEBungeeCord";
        int len = 34, ret;
        ip_header = (struct iphdr *)skb_network_header(skb);    
        src_add = ip_header->saddr;
        dst_add = ip_header->daddr;
        ret = search_str(skb, mal, len);
        if (ret == -1)
        {
            printk(KERN_INFO "no match\n");
            return NF_ACCEPT;                                           
        }
        else
        {
            printk(KERN_INFO "match at offset %d\n", ret);
            return NF_DROP;
        }
    }
    
    
    int init_module()
    {
        printk(KERN_DEBUG "init module\n");
        nfho.hook = hook_func;            
        nfho.hooknum = NF_INET_PRE_ROUTING;     
        nfho.pf = PF_INET;                        
        nfho.priority = NF_IP_PRI_FIRST;      
        nf_register_hook(&nfho);              
        return 0;                 
    }
    
    void cleanup_module()
    {
        printk(KERN_DEBUG "cleanup module\n");
        nf_unregister_hook(&nfho);
    } 
    

    注意:所有 IP 都采用大端序。如果要检查源 IP,必须使用 ntohl 进行转换。

    该字符串并不完全是您要查找的内容,而是类似的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多