2.6内核基于NetFilter处理框架修改TCP数据包实现访问控制

本文介绍了一种通过在TCP包头加入特殊标记来实现网络内容过滤的方法。该方案涉及内核模块编程,利用NETFILTER框架对特定教育局视频资源进行访问控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转帖与http://hi.baidu.com/xyp86/blog/item/4e502b185ea296bd4bedbc48.html ,感谢

 

征战论文的途中,以前公司的人来找我说要给之前我设计的网络内容过滤产品添加一个功能,只允许使用了我们产品的用户才能访问某教育局提供的视频教育资源。相比写论文,这种工程复杂性接近于O(1)或顶多是O(t)。 有两种方法可以实现:
1)在产品中添加VPN功能,将所有用户虚拟成一个局域网,需要做较多工作,虽然可以向公司要一笔钱,但眼下确实没时间了,可惜啊!
2)在用户出口,在产品上给去往教育局视频资源网站的访问流打一个标签,并在教育局网站前端以透明网桥方式加入我们的产品,并检查访问流是否有特殊的标签决定是否放行。该方法比较简单,原来想是修改http请求头,加入一个特殊fingerprint,有点复杂,需要在内核做字符串匹配,每个包都需要判断是否是HTTP的GET请求,后来直接在TCP包头上加入一个特殊标记,利用tcp的保留位置入一个特殊标记。

实现:基于NETFILTER的数据包处理框架,和2.6的内核模块,代码如下:

用户端打标签程序:
--------------------------------------------------------------------------------------------
/*
*   author: xiongyp86 at 163 dot com
* insmod gt_client.ko markdport=80 markdip="192.168.1.26"
*/
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/checksum.h>
#include <net/tcp.h>
#include <net/ip.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Xiong");

static char *            markdip= "192.168.1.1";
static unsigned short    markdport=10000;
    
module_param(markdport,ushort,S_IRUSR);   

module_param(markdip,charp,S_IRUSR);  


#define PRINT(fmt,args...) printk("Marker: " fmt, ##args)


unsigned int hook_mark_packet(unsigned int hookunm,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;
    int        datalen;

    iph=(struct iphdr *)(*skb)->nh.iph;

    /*PRINT("IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr));*/

    if(iph->daddr == in_aton(markdip))
    {       
        if(iph->protocol==6)
        {
            tcph=(struct tcphdr*)((__u32 *)iph+iph->ihl);
            if(ntohs(tcph->dest) == markdport)
            {
                tcph->res1 = 5; /*修改tcp保留位*/
            /*    PRINT("IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]:%d/n",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr),ntohs(tcph->res1));*/

                ip_send_check(iph); /*重新计算IP包头校验和,这一步貌似并不需要,因为IP包校验和只与IP包头有关*/

                datalen = (*skb)->len - iph->ihl*4;
                tcph->check = 0;
                /*重新计算TCP包头校验和,这一步很需要,否则会被接收端丢弃,TCP包校验和不仅涉及包头也包括payload*/
                tcph->check = tcp_v4_check(tcph, datalen, iph->saddr, iph->daddr,
                                   csum_partial((char *)tcph, datalen, 0));
            }
        }
    }
    return NF_ACCEPT;
}

static struct nf_hook_ops nfho_marker;

static int init_marker(void)
{
    nfho_marker.hook=hook_mark_packet;
    nfho_marker.hooknum=NF_IP_POST_ROUTING; /* check all forwarded packets*/
    nfho_marker.pf=PF_INET;
    nfho_marker.priority=NF_IP_PRI_LAST;

    nf_register_hook(&nfho_marker);

    PRINT("Initialized successfully, and we mark packet to %s:%u/n",markdip,markdport);
   
    return 0;
}

static void exit_marker(void)
{
    PRINT("Exit/n");
    nf_unregister_hook(&nfho_marker);
}

module_init(init_marker);
module_exit(exit_marker);


---------------------------------------------------------------------------------------------------------------------
教育局网站前端代码也类似,核心hook函数修改:


/*
*   author: xiongyp86 at 163 dot com
* insmod gt_server.ko markdport=80 markdip="192.168.1.26"
*/
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Xiong");

static char *            markdip= "192.168.1.1";
static unsigned short    markdport=10000;
    
module_param(markdport,ushort,S_IRUSR);   

module_param(markdip,charp,S_IRUSR);  


#define PRINT(fmt,args...) printk("Marker: " fmt, ##args)


unsigned int hook_mark_packet(unsigned int hookunm,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;

    iph=(struct iphdr *)(*skb)->nh.iph;

    /*PRINT("IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr));*/

    if(iph->daddr == in_aton(markdip))
    {   
        if(iph->protocol==6)
        {
            tcph=(struct tcphdr*)((__u32 *)iph+iph->ihl);
            if(ntohs(tcph->dest) == markdport)
            {       
                PRINT("Drop IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]:%u,%u/n",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr),ntohs(tcph->dest), ntohs(tcph->res1));

                if(tcph->res1 == 0) /*这里是校验tcp 保留位*/
                {
                    return NF_DROP;
                }
            }           
        }
    }
    return NF_ACCEPT;

}

static struct nf_hook_ops nfho_marker;

static int init_marker(void)
{
    nfho_marker.hook=hook_mark_packet;
    nfho_marker.hooknum=NF_IP_PRE_ROUTING; /* check all forwarded packets*/
    nfho_marker.pf=PF_INET;
    nfho_marker.priority=NF_IP_PRI_FIRST;

    nf_register_hook(&nfho_marker);

    PRINT("Initialized successfully, and we mark packet to %s:%u/n",markdip,markdport);
   
    return 0;
}

static void exit_marker(void)
{
    PRINT("Exit/n");
    nf_unregister_hook(&nfho_marker);
}

module_init(init_marker);
module_exit(exit_marker);


-------------------------------------------------------------------------------
编译Makefile:
ifneq ($(KERNELRELEASE),)
    obj-m := gt_client.o gt_server.o
else
    KERNELDIR = /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
---------------------------------------------------------------------------------------
Make
insmod gt_server/client.ko markdport=80 markdip="123.123.123.123"

Done!

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值