Chinaunix
标题:
[求助]关于使用NetFilter Hook进行FTP地址伪装的问题
[打印本页]
作者:
skywjf
时间:
2004-04-22 15:29
标题:
[求助]关于使用NetFilter Hook进行FTP地址伪装的问题
各位:
我想利用Linux内核中的NetFilter提供的Hook函数实现FTP地址伪装。
基本想法是:
1、在数据包即将离开本机之前设置Hook,对特定的数据包(FTP)进行编辑
2、编辑内容:将源IP地址(本机地址)改成伪装地址,重新计算checksum
3、编辑完成后让该数据包继续在网路上传播
4、由于MAC地址是正确的,所以本机理论上应该能收到FTP服务器的答复
5、在数据包到达本机的时候设置Hook,对特定的数据包(FTP)进行编辑
6、编辑内容:将目标IP地址(伪装地址)改成本机地址,重新计算checksum
7、编辑完成后让将数据包继续交给下一个协议层
源代码如下:
#include <linux/module.h>;
#include <linux/kernel.h>;
#include <linux/skbuff.h>;
#include <linux/if_ether.h>;
#include <linux/ip.h>;
#include <linux/tcp.h>;
#include <linux/in.h>;
#include <linux/netfilter.h>;
#include <linux/netfilter_ipv4.h>;
#include <linux/netdevice.h>;
#include <net/tcp.h>;
#include <asm/checksum.h>;
MODULE_AUTHOR("SKY <skywjf@hotmail.com>;");
MODULE_DESCRIPTION("NerFilter Hook Reserch Test");
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif /* MODULE_LICENSE */
static char local_ip[] = { 0xc0, 0xa8, 0x03, 0x78 }; /* 192.168.3.120 */
static char target_ip[] = { 0xc0, 0xa8, 0x03, 0x3c }; /* 192.168.3.60 */
static char foo_ip[] = { 0xc0, 0xa8, 0x03, 0x6f }; /* 192.168.3.111 */
/*
* ip_post_fn
* out packet hook function:
* catch the out ftp packet, change the source IP to foo.
*/
unsigned int
ip_post_fn(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;
struct iphdr *ihead = (struct iphdr *)sb->;nh.iph;
struct tcphdr *thead = (struct tcphdr *)((unsigned int *)ihead + ihead->;ihl);
/* is a ftp packet to target host? */
if (ihead->;saddr != *(unsigned int *)local_ip ||
ihead->;daddr != *(unsigned int *)target_ip ||
ihead->;protocol != IPPROTO_TCP || thead->;dest != htons(21)) {
return NF_ACCEPT;
}
/* change it */
ihead->;saddr = *(unsigned int *)foo_ip; /* fooip */
thead->;check = 0;
thead->;check =
tcp_v4_check(thead, sb->;len - ihead->;ihl * 4, ihead->;saddr, ihead->;daddr,
csum_partial((char *)thead, sb->;len - ihead->;ihl * 4, 0));
ihead->;check = 0;
ihead->;check = ip_fast_csum((unsigned char *)ihead, ihead->;ihl);
return NF_ACCEPT;
}
/*
* ip_pre_fn
* in packet hook function:
* catch the in ftp packet, change the dest IP.
*/
unsigned int
ip_pre_fn(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;
struct iphdr *ihead = (struct iphdr *)sb->;nh.iph;
struct tcphdr *thead = (struct tcphdr *)((unsigned int *)ihead + ihead->;ihl);
if (ihead->;saddr != *(unsigned int *)target_ip ||
ihead->;daddr != *(unsigned int *)foo_ip ||
ihead->;protocol != IPPROTO_TCP || thead->;source != htons(21)) {
return NF_ACCEPT;
}
ihead->;daddr = *(unsigned int *)local_ip; /* fooip */
thead->;check = 0;
thead->;check =
tcp_v4_check(thead, sb->;len - ihead->;ihl * 4, ihead->;saddr, ihead->;daddr,
csum_partial((char *)thead, sb->;len - ihead->;ihl * 4, 0));
ihead->;check = 0;
ihead->;check = ip_fast_csum((unsigned char *)ihead, ihead->;ihl);
return NF_ACCEPT;
}
static struct nf_hook_ops ip_post_ops =
{ {NULL, NULL}, ip_post_fn, PF_INET, NF_IP_POST_ROUTING, NF_IP_PRI_FIRST };
static struct nf_hook_ops ip_pre_ops =
{ {NULL, NULL}, ip_pre_fn, PF_INET, NF_IP_PRE_ROUTING, NF_IP_PRI_FIRST };
/*
* init_module
* module init function
*/
int
init_module()
{
int ret = 0;
if ((ret = nf_register_hook(&ip_post_ops)) < 0) {
printk("can't register ip_post_ops hook\n");
return ret;
}
if ((ret = nf_register_hook(&ip_pre_ops)) < 0) {
printk("can't register ip_pre_ops hook\n");
nf_unregister_hook(&ip_post_ops);
return ret;
}
return 0;
}
/*
* cleanup_module
* module destroy function
*/
void
cleanup_module()
{
nf_unregister_hook(&ip_post_ops);
nf_unregister_hook(&ip_pre_ops);
}
/*----- eof -----*/
复制代码
编译并安装模块后,执行FTP命令,访问target_ip:
ftp 192.168.3.60
结果没反应,在192.168.3.60端抓包发现,FTP包过来了,并且是伪装的IP地址,
checksum也都没有问题。奇怪的是FTP服务器收到这个包不作任何响应。抓包结果如下:
Frame 1 (74 bytes on wire, 74 bytes captured)
Arrival Time: Apr 22, 2004 15:10:28.868024000
Time delta from previous packet: 0.000000000 seconds
Time since reference or first frame: 0.000000000 seconds
Frame Number: 1
Packet Length: 74 bytes
Capture Length: 74 bytes
Ethernet II, Src: 00:90:27:08:90:bc, Dst: 00:00:e2:47:3b:d5
Destination: 00:00:e2:47:3b:d5 (00:00:e2:47:3b:d5)
Source: 00:90:27:08:90:bc (00:90:27:08:90:bc)
Type: IP (0x0800)
Internet Protocol, Src Addr: 192.168.3.111 (192.168.3.111), Dst Addr: 192.168.3.60 (192.168.3.60)
Version: 4
Header length: 20 bytes
Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00)
0000 00.. = Differentiated Services Codepoint: Default (0x00)
.... ..0. = ECN-Capable Transport (ECT): 0
.... ...0 = ECN-CE: 0
Total Length: 60
Identification: 0xeca8 (60584)
Flags: 0x04
0... = Reserved bit: Not set
.1.. = Don't fragment: Set
..0. = More fragments: Not set
Fragment offset: 0
Time to live: 64
Protocol: TCP (0x06)
Header checksum: 0xc617 (correct)
Source: 192.168.3.111 (192.168.3.111)
Destination: 192.168.3.60 (192.168.3.60)
Transmission Control Protocol, Src Port: 1126 (1126), Dst Port: 21 (21), Seq: 0, Ack: 0, Len: 0
Source port: 1126 (1126)
Destination port: 21 (21)
Sequence number: 0
Header length: 40 bytes
Flags: 0x0002 (SYN)
0... .... = Congestion Window Reduced (CWR): Not set
.0.. .... = ECN-Echo: Not set
..0. .... = Urgent: Not set
...0 .... = Acknowledgment: Not set
.... 0... = Push: Not set
.... .0.. = Reset: Not set
.... ..1. = Syn: Set
.... ...0 = Fin: Not set
Window size: 5840
Checksum: 0x5150 (correct)
Options: (20 bytes)
Maximum segment size: 1460 bytes
SACK permitted
Time stamp: tsval 39767606, tsecr 0
NOP
Window scale: 0 (multiply by 1)
0000 00 00 e2 47 3b d5 00 90 27 08 90 bc 08 00 45 00 ...G;...'.....E.
0010 00 3c ec a8 40 00 40 06 c6 17 c0 a8 03 6f c0 a8 .<..@.@......o..
0020 03 3c 04 66 00 15 60 3d 22 9e 00 00 00 00 a0 02 .<.f..`=".......
0030 16 d0 51 50 00 00 02 04 05 b4 04 02 08 0a 02 5e ..QP...........^
0040 ce 36 00 00 00 00 01 03 03 00 .6........
复制代码
请教这是为什么?
作者:
好好先生
时间:
2004-04-22 15:30
标题:
[求助]关于使用NetFilter Hook进行FTP地址伪装的问题
不懂编程,替你顶一下。等待高手出现。
作者:
gowind
时间:
2004-04-22 15:34
标题:
[求助]关于使用NetFilter Hook进行FTP地址伪装的问题
iptables不是已经有这个功能,是自己练手?
作者:
skywjf
时间:
2004-04-22 15:40
标题:
[求助]关于使用NetFilter Hook进行FTP地址伪装的问题
自己练习,感觉很简单的样子,只是出了这样的问题很不解。
作者:
skywjf
时间:
2004-04-22 17:07
标题:
[求助]关于使用NetFilter Hook进行FTP地址伪装的问题
这是一个TCP握手包,看样子数据包在到达TCP协议层之前就被丢了,很不解:为什么checksum都是对的,包还会丢呢?还是有其他的原因?
作者:
zhanghx1977
时间:
2006-09-15 09:33
学习,顶一下,支持,也想知道,各位大虾请多指教!qq:254836615 email:
zhanghx1977@163.com
作者:
zhubaining
时间:
2006-12-12 16:16
鄙人最近在研究用netfilter实现类似NAT功能的应用。。。
也在对数据包进行修改/转发。。。
建议你先查看一下那个FTP server的ARP表格,看看它上面192.168.3.111对应的MAC地址是不是192.168.3.120的。
另外,似乎对数据包不能直接修改吧,之前要调用skb_make_writable()之类的函数吧。
我的MSN:zhubaining(at)hotmail.com,可以讨论讨论。。。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2