关于内核数据包劫持、修改、转发的问题
时间:2010-06-15
来源:互联网
我们想在a(192.168.1.204)连接b(192.168.1.200)时,欺骗a,让a于c(192.168.1.202)相连。当我们把自己的模块挂上以后,源目的的ip和 mac都修改了(192.168.1.200被修改成了192.168.1.202,对应mac修改正确),但是通过wireshark观察数据包发现,ping能正常使用;用ssh连接时,第一个syn报文能够发送到c,但是c没有回应。下面是我们些的代码,能帮忙看看是什么问题吗?谢谢了。
复制代码
- /*#define _KERNEL_*/
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/netfilter.h>
- #include <linux/skbuff.h>
- #include <linux/ip.h>
- #include <linux/netdevice.h>
- #include <linux/if_ether.h>
- #include <linux/if_packet.h>
- #include <net/tcp.h>
- #include <linux/netfilter_ipv4.h>
-
-
- #include <linux/timer.h>
- #include <net/xfrm.h>
- #include <linux/netdevice.h>
- #define NF_IP_PRE_ROUTING 0
- #define NF_IP_FORWARD 2
- static struct nf_hook_ops nfho;
-
-
- 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 *))
- {
- struct sk_buff *sb = skb;
- unsigned char src_ip[4],src_mac[6];
- unsigned char des_ip[4],des_mac[6];
- *(unsigned int *)src_ip = ip_hdr(sb)->saddr;
- *(unsigned int *)des_ip= ip_hdr(sb)->daddr;
- unsigned int tcphoffset;
- struct tcphdr*tcph;
- int ret;
- tcphoffset=0;
- if(des_ip[0]==192&&des_ip[1]==168&&des_ip[2]==1&&des_ip[3]==200) //将目的地址是192.168.1.200的数据包截获下来,将目的地址改为202,同时修改目的地址的mac
- {
- des_ip[0]=192;
- des_ip[1]=168;
- des_ip[2]=1;
- des_ip[3]=202;
- memcpy(&ip_hdr(sb)->daddr,des_ip,4);
-
- des_mac[0]=0x00;
- des_mac[1]=0x16;
- des_mac[2]=0x3e;
- des_mac[3]=0x7b;
- des_mac[4]=0x32;
- des_mac[5]=0xef;
- memcpy(eth_hdr(sb)->h_dest,des_mac,6);
- if(ip_hdr(sb)->protocol == IPPROTO_TCP){//如果接到的数据包是tcp包,重新计算tcp校验和
- tcph = tcp_hdr(sb);
- tcphoffset = ip_hdr(sb)->ihl * 4;
-
- sb->csum = 0;
- sb->csum = skb_checksum (sb, tcphoffset, sb->len - tcphoffset, 0);
-
- tcph->check=0;
- tcph->check=tcp_v4_check(sb->len-tcphoffset,ip_hdr(sb)->saddr,ip_hdr(sb)->daddr,csum_partial(tcph,sb->len-tcphoffset,0));
-
- }
- ip_hdr(sb)->check=0;//计算ip校验和
- ip_hdr(sb)->check = ip_fast_csum((void *) ip_hdr(sb), ip_hdr(sb)->ihl);
- }
- if(src_ip[0]==192&&src_ip[1]==168&&src_ip[2]==1&&src_ip[3]==202)//取出源地址为192.168.1.202的数据包,将源地址改为192.168.1.200,同时修改目的mac
- {
- src_ip[0]=192;
- src_ip[1]=168;
- src_ip[2]=1;
- src_ip[3]=200;
- memcpy(&ip_hdr(sb)->saddr,src_ip,4);
-
- src_mac[0]=0x00;
- src_mac[1]=0x16;
- src_mac[2]=0x36;
- src_mac[3]=0x6a;
- src_mac[4]=0x55;
- src_mac[5]=0x5d;
- memcpy(eth_hdr(sb)->h_source,src_mac,6);
- if(ip_hdr(sb)->protocol == IPPROTO_TCP){//如果是tcp包,计算tcp的校验和
- tcph = tcp_hdr(sb);
- tcphoffset = ip_hdr(sb)->ihl * 4;
- sb->csum = 0;
- sb->csum = skb_checksum (sb, tcphoffset, skb->len - tcphoffset, 0);
- tcph->check=0;
- tcph->check=tcp_v4_check(sb->len-tcphoffset,ip_hdr(sb)->saddr,ip_hdr(sb)->daddr,csum_partial(tcph,sb->len-tcphoffset,0));
-
-
- }
- ip_hdr(sb)->check=0;//计算ip校验和
- ip_hdr(sb)->check = ip_fast_csum((void *) ip_hdr(sb), ip_hdr(sb)->ihl);
- }
- skb_push(sb , ETH_HLEN);
-
- /*直接调用该函数,将数据包从网卡上发送出去*/
-
- ret = dev_queue_xmit(sb);
- // nf_reset(sb);
- //netif_rx(sb);
- return NF_STOLEN;
- }
-
-
- int init_module()
- {
-
- nfho.hook = hook_func;
- nfho.hooknum = NF_IP_PRE_ROUTING;
- nfho.pf = PF_INET;
- nfho.priority = NF_IP_PRI_FIRST;
-
- nf_register_hook(&nfho);
-
- return 0;
- }
-
- void cleanup_module()
- {
- nf_unregister_hook(&nfho);
- }
作者: jhl19880722 发布时间: 2010-06-15
上面的代码,我在NF_IP_LOCAL_IN上面能够正常的运行。但是在 NF_IP_PRE_ROUTING不行,我用wireshark查看接受到的数据包,同时打印出来在程序中接收到的数据包,发现接收到的数据包和wireshark捕获到的不一样,用wireshark在不同位置上捕获,确定是接收到的不对。有谁知道原因
作者: jhl19880722 发布时间: 2010-08-12
LZ 你的模块挂载到那台机子上了?
作者: Godbach 发布时间: 2010-08-12
你需要同时修改prerouting 和output,
并且你要iptable -F
清掉 Iptables规则。
并且你要iptable -F
清掉 Iptables规则。
作者: souldemo 发布时间: 2010-08-12
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28