免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2766 | 回复: 4
打印 上一主题 下一主题

[内核模块] netfilter 修改 tcp 数据包后, 发现打开淘宝, 主机复位 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-02-11 20:35 |只看该作者 |倒序浏览
unsigned int checksum(unsigned int hooknum,
                        struct sk_buff *__skb,
                        const struct net_device *in,
                        const struct net_device *out,
                        int (*okfn)(struct sk_buff *))
{
    struct sk_buff *skb;
    struct iphdr *iph;

    struct ts_state state;

    struct ts_config *ts_conf;
    int rst = 0;
    int flags = TS_AUTOLOAD;

    skb = __skb;
    if(skb == NULL)    return NF_ACCEPT;
    iph = ip_hdr(skb);
    if(iph == NULL)    return NF_ACCEPT;
   
    ts_conf = textsearch_prepare("kmp", "taobao", 6, GFP_KERNEL, flags);
    if(IS_ERR(ts_conf))
    {
        printk("limeng,conf err\n");
        return NF_ACCEPT;
    }

    memset(&state, 0, sizeof(struct ts_state));
/*   
    rst = skb_find_text((struct sk_buff *)skb, 0, 65535, ts_conf, &state);
    if(UINT_MAX != rst)
    {
        printk("limeng,skb_find\n");
        textsearch_destroy(ts_conf);
        return NF_DROP;
    }
*/

    rst = skb_find_text((struct sk_buff *)skb, 0, 65535, ts_conf, &state);
    if(UINT_MAX != rst)
    {
        if (ip_hdr(skb)->protocol == IPPROTO_TCP)
        {
            enum ip_conntrack_info ctinfo;
            struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
            struct iphdr *iph = ip_hdr(skb);

            if(ct != NULL)
            {
                int protoff = ip_hdrlen(skb);
                int matchoff = rst;
                int matchlen = 6;
                char *buffer = "limeng";
        
                //rst = (ct && nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff, matchlen, buffer, strlen(buffer)));
                rst = (ct && nf_nat_mangle_tcp_packet(skb, ct, ctinfo, ip_hdrlen(skb), rst, 0, "limeng", 6));

                if (rst != 1)
                {
                        printk("limeng,nat err\n");
                }
                else
                {
                        printk("limeng,nat ok\n");
                }
            }
            else
            {
                printk("limeng,ct = null\n");
            }
        }
    }

    textsearch_destroy(ts_conf);
    return NF_ACCEPT;
}

static int __init filter_init(void)
{
    int ret;
    nfho.hook = checksum;
    nfho.pf = AF_INET;
    nfho.hooknum = NF_INET_PRE_ROUTING;
    nfho.priority = NF_IP_PRI_FILTER;

    ret = nf_register_hook(&nfho);
    if(ret < 0)
    {
        printk("%s\n", "can't modify skb hook!");
        return ret;
    }
    return 0;
}

然后 打开 taobao, 主机复位...什么情况....

论坛徽章:
0
2 [报告]
发表于 2015-02-11 20:37 |只看该作者
请高手指点一二....网上找了不少资料, 都说是要用nf_nat_mangle_tcp_packet来修改数据包,我确定是这个函数引起的.

论坛徽章:
0
3 [报告]
发表于 2015-02-12 11:48 |只看该作者
今天又比较了没有protoff 的版本和现有的版本, protoff 看来是ip头的长度. 使用 int protoff = iph->ihl*4;后,还是有复位的问题.
请懂的朋友讲解一下.

论坛徽章:
0
4 [报告]
发表于 2015-02-12 18:06 |只看该作者
专家都回家过年了么...

论坛徽章:
0
5 [报告]
发表于 2015-05-09 10:06 |只看该作者
  //rst = (ct && nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff, matchlen, buffer, strlen(buffer)));
                rst = (ct && nf_nat_mangle_tcp_packet(skb, ct, ctinfo, ip_hdrlen(skb), rst, 0, "limeng", 6));

不能从 直接插入,不能从0开始
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP