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

来源:互联网 发布:买家淘宝怎么刷心快 编辑:程序博客网 时间:2024/05/22 06:42

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

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

用户端打标签程序:

 

[cpp] view plaincopy
  1. /* 
  2. *   author: xiongyp86 at 163 dot com 
  3. * insmod gt_client.ko markdport=80 markdip="192.168.1.26" 
  4. */  
  5. #include <linux/netfilter.h>  
  6. #include <linux/netfilter_ipv4.h>  
  7. #include <linux/module.h>  
  8. #include <linux/moduleparam.h>  
  9. #include <linux/kernel.h>  
  10. #include <linux/inet.h>  
  11. #include <linux/ip.h>  
  12. #include <linux/tcp.h>  
  13. #include <net/checksum.h>  
  14. #include <net/tcp.h>  
  15. #include <net/ip.h>  
  16. MODULE_LICENSE("GPL");  
  17. MODULE_AUTHOR("Xiong");  
  18. static char *            markdip= "192.168.1.1";  
  19. static unsigned short    markdport=10000;  
  20. module_param(markdport,ushort,S_IRUSR);     
  21. module_param(markdip,charp,S_IRUSR);    
  22.   
  23. #define PRINT(fmt,args...) printk("Marker: " fmt, ##args)  
  24.   
  25. unsigned int hook_mark_packet(unsigned int hookunm,struct sk_buff **skb,  
  26. const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *))  
  27. {  
  28. struct    iphdr *iph;  
  29. struct    tcphdr *tcph;  
  30. int        datalen;  
  31. iph=(struct iphdr *)(*skb)->nh.iph;  
  32. /*PRINT("IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr));*/  
  33. if(iph->daddr == in_aton(markdip))  
  34. {         
  35. if(iph->protocol==6)  
  36. {  
  37. tcph=(struct tcphdr*)((__u32 *)iph+iph->ihl);  
  38. if(ntohs(tcph->dest) == markdport)  
  39. {  
  40. tcph->res1 = 5; /*修改tcp保留位*/  
  41. /*    PRINT("IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]:%d/n",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr),ntohs(tcph->res1));*/  
  42. ip_send_check(iph); /*重新计算IP包头校验和,这一步貌似并不需要,因为IP包校验和只与IP包头有关*/  
  43. datalen = (*skb)->len - iph->ihl*4;  
  44. tcph->check = 0;  
  45. /*重新计算TCP包头校验和,这一步很需要,否则会被接收端丢弃,TCP包校验和不仅涉及包头也包括payload*/  
  46. tcph->check = tcp_v4_check(tcph, datalen, iph->saddr, iph->daddr,  
  47. csum_partial((char *)tcph, datalen, 0));  
  48. }  
  49. }  
  50. }  
  51. return NF_ACCEPT;  
  52. }  
  53. static struct nf_hook_ops nfho_marker;  
  54. static int init_marker(void)  
  55. {  
  56. nfho_marker.hook=hook_mark_packet;  
  57. nfho_marker.hooknum=NF_IP_POST_ROUTING; /* check all forwarded packets*/  
  58. nfho_marker.pf=PF_INET;  
  59. nfho_marker.priority=NF_IP_PRI_LAST;  
  60. nf_register_hook(&nfho_marker);  
  61. PRINT("Initialized successfully, and we mark packet to %s:%u/n",markdip,markdport);  
  62. return 0;  
  63. }  
  64. static void exit_marker(void)  
  65. {  
  66. PRINT("Exit/n");  
  67. nf_unregister_hook(&nfho_marker);  
  68. }  
  69. module_init(init_marker);  
  70. module_exit(exit_marker);  

 

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

 

[cpp] view plaincopy
  1. /* 
  2. *   author: xiongyp86 at 163 dot com 
  3. * insmod gt_server.ko markdport=80 markdip="192.168.1.26" 
  4. */  
  5. #include <linux/netfilter.h>  
  6. #include <linux/netfilter_ipv4.h>  
  7. #include <linux/module.h>  
  8. #include <linux/moduleparam.h>  
  9. #include <linux/kernel.h>  
  10. #include <linux/inet.h>  
  11. #include <linux/ip.h>  
  12. #include <linux/tcp.h>  
  13. MODULE_LICENSE("GPL");  
  14. MODULE_AUTHOR("Xiong");  
  15. static char *            markdip= "192.168.1.1";  
  16. static unsigned short    markdport=10000;  
  17. module_param(markdport,ushort,S_IRUSR);     
  18. module_param(markdip,charp,S_IRUSR);    
  19.   
  20. #define PRINT(fmt,args...) printk("Marker: " fmt, ##args)  
  21.   
  22. unsigned int hook_mark_packet(unsigned int hookunm,struct sk_buff **skb,  
  23. const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *))  
  24. {  
  25. struct    iphdr *iph;  
  26. struct    tcphdr *tcph;  
  27. iph=(struct iphdr *)(*skb)->nh.iph;  
  28. /*PRINT("IP: [%u.%u.%u.%u]-->[%u.%u.%u.%u]",NIPQUAD(iph->saddr),NIPQUAD(iph->daddr));*/  
  29. if(iph->daddr == in_aton(markdip))  
  30. {     
  31. if(iph->protocol==6)  
  32. {  
  33. tcph=(struct tcphdr*)((__u32 *)iph+iph->ihl);  
  34. if(ntohs(tcph->dest) == markdport)  
  35. {         
  36. 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));  
  37. if(tcph->res1 == 0) /*这里是校验tcp 保留位*/  
  38. {  
  39. return NF_DROP;  
  40. }  
  41. }             
  42. }  
  43. }  
  44. return NF_ACCEPT;  
  45. }  
  46. static struct nf_hook_ops nfho_marker;  
  47. static int init_marker(void)  
  48. {  
  49. nfho_marker.hook=hook_mark_packet;  
  50. nfho_marker.hooknum=NF_IP_PRE_ROUTING; /* check all forwarded packets*/  
  51. nfho_marker.pf=PF_INET;  
  52. nfho_marker.priority=NF_IP_PRI_FIRST;  
  53. nf_register_hook(&nfho_marker);  
  54. PRINT("Initialized successfully, and we mark packet to %s:%u/n",markdip,markdport);  
  55. return 0;  
  56. }  
  57. static void exit_marker(void)  
  58. {  
  59. PRINT("Exit/n");  
  60. nf_unregister_hook(&nfho_marker);  
  61. }  
  62. module_init(init_marker);  
  63. 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

 

insmod gt_server/client.ko markdport=80 markdip="123.123.123.123"

0 0
原创粉丝点击