免费注册 查看新帖 |

Chinaunix

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

[内核入门] linux\net\netfilter模块调试求解! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-04 16:51 |只看该作者 |倒序浏览
大家好

   正在学习网络方面的内容,这二天碰到一个问题,请大家帮忙看看

应用层:

   执行 iptables -I FORWARD -p tcp -m webstr --url www.baidu.com -j REJECT --reject-with tcp-reset  //url过滤功能 禁止上baidu网

内核层:

  把附件中的文件新模块对比其它文件规则如(xt_time.c)或模块加载形式加内核里面,这个.c文件主要功能处理匹配到webstr关键字,在进入get_http_info函数,memcmp(data, "GET ", sizeof("GET ") - 1) 。。。比较数据包是否通过

1. linux-2.6.21\net\netfilter  把附件中的文件.c代码里的 #if 0 开启 变成#if 1, 编译通过使用,功能生效

可以看到LOG:每执行一次进入baidu网时,都会看到类GET http://www.baidu.com/,这样数据包在get_http_info函数应该就这样抛去了,达到可以进行url过滤功能


  2.linux-2.6.30\net\netfilter  直接编译通过使用,功能不生效
在这内核版本里,
执行多次进入baidu网时,偶尔看到有GET字,大部分都看不到GET字,原因应该是在get_http_info函数里面让数据包PASS,达不到过滤功能,但不知道为什么会这样

  跟踪代码里打印log:
webstr_mt: get_http_info........................111111111111111111111
webstr_mt: get_http_info........................22222222222222222222222222
webstr_mt: get_http_info........................33333333333333333333
webstr_mt: get_http_info........................aaaaaaaaaaaaaaaaaaaaGET http://www.baidu.com/ HTTP/1.1
Host: www.baidu.com
Proxy-Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.hao123.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Cookie: BAIDUID=EB40A4E70DF1A16CD7C89FF54BD8AB51:FG=1; Hm_lvt_9f14aaa038bbba8b12ec2a4a3e51d254=1354080372; BDUT=6isoFDB3DB5FB93692DD79FE9F30B533690D13a5265c0e71; H_PS_PSSID=1454_1540_1543_1582

65c0e71; H_PS_PSSID=1454_1540_1543_1582

%AB%7C%C9%EE%DB%DA

get_http_info: get_http_info........................44444444444444444444444
get_http_info: get_http_info........................6405555555555555555555555555555GET http://www.baidu.com/ HTTP/1.1
Host: www.baidu.com
Proxy-Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.hao123.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Cookie: BAIDUID=EB40A4E70DF1A16CD7C89FF54BD8AB51:FG=1; Hm_lvt_9f14aaa038bbba8b12ec2a4a3e51d254=1354080372; BDUT=6isoFDB3DB5FB93692DD79FE9F30B533690D13a5265c0e71; H_PS_PSSID=1454_1540_1543_1582

65c0e71; H_PS_PSSID=1454_1540_1543_1582

%AB%7C%C9%EE%DB%DA

get_http_info: get_http_info........................6666666666666666666666666666
find_pattern2: find_pattern2: pattern = 'Host: ', dlen = 640
get_http_info: Host found=1
get_http_info: HOST=www.baidu.com, hostlen=13
find_pattern2: find_pattern2: pattern = ' ', dlen = 637
get_http_info: GET/POST found=1 off=4
get_http_info: URL=www.baidu.comhttp://www.baidu.com/ , urllen=35
webstr_mt: get_http_info........................bbbbbbbbbbbbbbbbbbbbbbwww.baidu.comhttp://www.baidu.com/
webstr_mt: needle.......................ccccccccneedlewww.baidu.com
webstr_mt: keyword=www.baidu.com, nlen=13, hlen=35
search_linear: search_linear: haystack=www.baidu.comhttp://www.baidu.com/ , needle=www.baidu.com
webstr_mt: webstr_mt: Verdict =======> DROP

webstr_mt: get_http_info........................111111111111111111111
webstr_mt: get_http_info........................22222222222222222222222222
webstr_mt: get_http_info........................33333333333333333333
webstr_mt: get_http_info........................aaaaaaaaaaaaaaaaaaaaS
                                                                     get_http_info: get_http_info........................44444444444444444444444
get_http_info: get_http_info: Not enough length, ignore it!


webstr_mt: get_http_info........................111111111111111111111
webstr_mt: get_http_info........................22222222222222222222222222
webstr_mt: get_http_info........................33333333333333333333
webstr_mt: get_http_info........................aaaaaaaaaaaaaaaaaaaa                                                                     get_http_info: get_http_info........................44444444444444444444444
get_http_info: get_http_info: Not enough length, ignore it!


webstr_mt: get_http_info........................111111111111111111111
webstr_mt: get_http_info........................22222222222222222222222222
webstr_mt: get_http_info........................33333333333333333333
webstr_mt: get_http_info........................aaaaaaaaaaaaaaaaaaaa                                                                     get_http_info: get_http_info........................44444444444444444444444
get_http_info: get_http_info: Not enough length, ignore it!

webstr_mt: get_http_info........................111111111111111111111
webstr_mt: get_http_info........................22222222222222222222222222
webstr_mt: get_http_info........................33333333333333333333
webstr_mt: get_http_info........................aaaaaaaaaaaaaaaaaaaa                                                                     get_http_info: get_http_info........................44444444444444444444444
get_http_info: get_http_info: Not enough length, ignore it!


。。。







123.zip

4.88 KB, 下载次数: 27

.c 文件

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
2 [报告]
发表于 2012-12-04 22:13 |只看该作者
回复 1# sgq2012
这是你自己实现的 match 吗


   

论坛徽章:
0
3 [报告]
发表于 2012-12-05 11:13 |只看该作者
回复 2# Godbach


    在http://www.kernel.org/ 找了没有类似的文件 webstr ,这个应该是前辈实现遗留下的,如果把这个文件从低点的版本移到2.6.30高点版本不生效,看起来好像这其中得到HTTP头的地方出了问题,或新内核版本有所变化

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
4 [报告]
发表于 2012-12-05 13:16 |只看该作者
回复 3# sgq2012
webstr 应该是你们自己开发的。在不同的内核版本之间要考虑兼容性。


   

论坛徽章:
0
5 [报告]
发表于 2012-12-05 15:00 |只看该作者
在2.6.30的这个模块里面,下面二种方法得到HTTP不知道正确不,还是有别的方法,请看看

const struct iphdr *iph = ip_hdr(skb);
unsigned int tcplen = skb->len - iph->ihl * 4;
const struct tcphdr *tcph =(struct tcphdr *)(skb_network_header(skb) + iph->ihl * 4);
datalen = tcplen - tcph->doff * 4;          //得到http长度
data = (void *)tcph + tcph->doff * 4;     //得到http数据部分



const struct iphdr *iph = ip_hdr(skb);
unsigned int tcplen = skb->len - iph->ihl * 4;
const struct tcphdr *tcph;
unsigned int nexthdr_off = iph->ihl * 4;
struct tcphdr _tcph;
tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
datalen = tcplen - tcph->doff * 4;               //得到http长度
data = (void *)tcph + tcph->doff * 4;       //得到http数据部分

论坛徽章:
6
金牛座
日期:2013-10-08 10:19:10技术图书徽章
日期:2013-10-14 16:24:09CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2013-11-24 19:26:19未羊
日期:2014-01-23 15:50:002015年亚洲杯之阿联酋
日期:2015-05-09 14:36:15
6 [报告]
发表于 2012-12-05 16:11 |只看该作者
回复 5# sgq2012
const struct iphdr *iph = ip_hdr(skb);
unsigned int tcplen = skb->len - iph->ihl * 4;
const struct tcphdr *tcph =(struct tcphdr *)(skb_network_header(skb) + iph->ihl * 4);
datalen = tcplen - tcph->doff * 4;          //得到http长度
data = (void *)tcph + tcph->doff * 4;     //得到http数据部分


有点小的问题:
1. 获取tcp头之前,最好先判断一下ip层的协议是不是tcp
2. 获取tcp头最好用skb_header_pointer函数,这个函数会做很多校验,比如:长度是否够tcp头的大小、非线性skb情况等等。

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP