西厢记之 Ubuntu Linux下编译安装西_厢_计_划

 

 

介绍:

 

由于众所周知的原因,我们上网的时候经常会遇到连接重置的情况,西_厢_计_划(west-chamber)是一个旨在通过技术手段解决此类问题的开源项目。项目主页:http://code.google.com/p/scholarzhang

 

为便于说明而不带来问题,我把重置连接的系统称之为maxtrix,就是电影《黑客帝国》中的那个,你懂的。

 

我花了几天的时间把程序编译并安装成功,不过使用的时候发现访问网站成功率不高。试了几个网站,youtube(可连,但不稳定),blooger(一段时间能上),facebook(一段时间能上),twitter(不能上)。查看TCP,虽然程序能够按照既定方案“注入”TCP报文段,但是经常还是重置连接,目前还不能找到是何原因。

 

虽然如此,我仍然相信开发者会不断完善这个项目,与matrix战斗到底,因此我 把在Ubuntu Linux下的安装过程和方法拿出来,以供爱好者参考。

 

注意:阅读本文,需要对Linux系统及其防火墙netfilter有一定的了解,且务必先阅读官方的安装和使用说明文档,否则会难以理解。

 

---------------------------------------------------------------------------------------

 

介绍一下安装前的环境:

环境是在VirtualBox 4.x上搭建的全新虚拟机。

ubuntu desktop 10.10

linux kernel 2.6.35-28-generic

iptables v1.4.4

 

安装过程一波三折,分别两次编译不同版本的代码,最后成功。

第一次编译的代码是官网给出的下载 west-chamber-20100405.tar.bz2,

第二次是从SVN签出的最新的代码,版本REV106。

如果你和我有接近的环境,并想直接安装成功的话,可以直接跳到第二段。

 

 

第一次编译安装过程:

------------------------------------------------------------------------

 

从项目主页下载源代码包:west-chamber-20100405.tar.bz2

解压缩后按照INSTALL文件中的步骤安装

 

 

./autogen.sh
./configure CFLAGS="..." --prefix=/usr ...
make && sudo make install
 

 

执行到make的时候出错

make  all-recursive

make[1]: Entering directory `/home/username/gfw/west-chamber-20100405'

Making all in extensions

make[2]: Entering directory `/home/username/gfw/west-chamber-20100405/extensions'

if [ -n "/lib/modules/2.6.35-22-generic/build" ]; then make -C /lib/modules/2.6.35-22-generic/build M=/home/username/gfw/west-chamber-20100405/extensions modules; fi;

make[3]: Entering directory `/usr/src/linux-headers-2.6.35-22-generic'

 CC [M]  /home/username/gfw/west-chamber-20100405/extensions/compat_xtables.o

In file included from /home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:21:

/home/username/gfw/west-chamber-20100405/extensions/compat_xtnu.h:87: warning: ‘struct xt_match_param’ declared inside parameter list

/home/username/gfw/west-chamber-20100405/extensions/compat_xtnu.h:87: warning: its scope is only this definition or declaration, which is probably not what you want

/home/username/gfw/west-chamber-20100405/extensions/compat_xtnu.h:103: warning: ‘struct xt_target_param’ declared inside parameter list

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:193: warning: ‘struct xt_target_param’ declared inside parameter list

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c: In function ‘xtnu_target_run’:

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:207: error: dereferencing pointer to incomplete type

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:216: warning: passing argument 2 of ‘nt->target’ from incompatible pointer type

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:216: note: expected ‘const struct xt_target_param *’ but argument is of type ‘const struct xt_target_param *’

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c: In function ‘xtnu_register_target’:

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:294: warning: assignment from incompatible pointer type

/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.c:299: warning: assignment from incompatible pointer type

make[4]: *** [/home/username/gfw/west-chamber-20100405/extensions/compat_xtables.o] Error 1

make[3]: *** [_module_/home/username/gfw/west-chamber-20100405/extensions] Error 2

make[3]: Leaving directory `/usr/src/linux-headers-2.6.35-22-generic'

make[2]: *** [modules] Error 2

make[2]: Leaving directory `/home/username/gfw/west-chamber-20100405/extensions'

make[1]: *** [all-recursive] Error 1

make[1]: Leaving directory `/home/username/gfw/west-chamber-20100405'

make: *** [all] Error 2

 

看错误信息是无法找到xt_match_param的结构体定义,感觉像是xtables的头文件的问题,

于是查看/lib/modules/2.6.35-28-generic/build/include/下的linux/netfilter/x_tables.h发现其中没有xt_match_param这个结构体定义。

再查看extensions/compat_xtnu.h,发现其中已有xt_match_param的定义,而且位于出错位置之前,怎么会找不到呢,再仔细一看,

其中有这么一句:

 

 

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 27)
 

 

原来代码中判定内核版本小于2.6.27才可以编译,我的内核是2.6.35,可能新的内核把这几个定义结构体改名了,那我就简单修改试试看:

加上代码把我的内核版本加上去:

 

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 27) || LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 35)
//把原代码块中两个新内核没有的定义移过来
#endif
 

把内核中没有的两个定义加上去,再make,编译通过。

再按照官方给的方法执行ipset成功。

再执行iptables设定防火墙规则时发生错误: Input/output error.

怀疑是ipset版本问题,尝试编译安装最新版本ipset:

 

wget http://ipset.netfilter.org/ipset-6.3.tar.bz2
tar jxvf ipset-6.3.tar.bz2
cd ipset-6.3.tar.bz2/
./authogen.sh
./configure --prefix=/usr
make
 

make的时候产生错误信息:

configure: error: The kernel source directory /lib/modules/2.6.35-28-generic/build is not patched with netlink.patch to support ipset

看来这个版本的内核无法安装最新的ipset,这条路看来行不通。

先不管,重新编译iptalbes和xtables-addons-common试试看:

 

 

wget http://netfilter.org/projects/iptables/files/iptables-1.4.10.tar.bz2
tar jxvf iptables-1.4.10.tar.bz2 
cd iptables-1.4.10
./configure --prefix=/usr -libexecdir=/lib -libdir=/lib -sbindir=/sbin 
make && sudo make install 


wget http://downloads.sourceforge.net/project/xtables-addons/1.34/xtables-addons-1.34.tar.xz
xz -d xtables-addons-1.34.tar.xz
tar xvf xtables-addons-1.34.tar
cd xtables-addons-1.34
./configure -prefix=/usr -libexecdir=/lib -libdir=/lib -sbindir=/sbin 
make && sudo make install 
 

 

 

编译完成后重新编译west-chamber,再执行iptables 配置,发生错误:

iptables: target "ZHANG" has version "libxtables.so.2", but "libxtables.so.5" is required.

iptables: match "gfw" has version "libxtables.so.2", but "libxtables.so.5" is required.

iptables: match "gfw" has version "libxtables.so.2", but "libxtables.so.5" is required.

 

第一次尝试安装失败。

 

 

 

 

第二次编译安装:

----------------------------------------------------------------------------------------------------------

从第一次的安装情况推测,可能是源代码太老,于是从SVN签出最新代码(REV106),开始编译安装:

 

 

cd west-chamber-src/
./autogen.sh
./configure --with-xtlibdir=/lib --prefix=/usr --libexecdir=/lib
make && sudo make install
 

 

 

在这之前先编译安装iptables和xtables-addons:

 

 

cd iptables-1.4.10
./configure --prefix=/usr -libexecdir=/lib -libdir=/lib -sbindir=/sbin 
make && sudo make install 

cd ../xtables-addons-1.34
./configure --prefix=/usr -libexecdir=/lib -libdir=/lib -sbindir=/sbin 
make && sudo make install 
 

 

不过很不幸,还是出现之前的错误:

iptables: target "ZHANG" has version "libxtables.so.2", but "libxtables.so.5" is required.

iptables: match "gfw" has version "libxtables.so.2", but "libxtables.so.5" is required.

iptables: match "gfw" has version "libxtables.so.2", but "libxtables.so.5" is required.

 

 

把/lib/libxtables.so.2.0.0删除,再运行iptables设置,发现新的错误:

/lib/xtables/libxt_ZHANG.so: libxtables.so.2: cannot open shared object file: No such file or directory

/lib/xtables/libxt_ZHANG.so: libxtables.so.2: cannot open shared object file: No such file or directory

iptables v1.4.10: Couldn't load target `ZHANG':/lib/xtables/libipt_ZHANG.so: cannot open shared object file: No such file or directory

 

Try `iptables -h' or 'iptables --help' for more information.

/lib/xtables/libxt_gfw.so: libxtables.so.2: cannot open shared object file: No such file or directory

iptables v1.4.10: Couldn't load match `gfw':/lib/xtables/libipt_gfw.so: cannot open shared object file: No such file or directory

 

Try `iptables -h' or 'iptables --help' for more information.

/lib/xtables/libxt_gfw.so: libxtables.so.2: cannot open shared object file: No such file or directory

iptables v1.4.10: Couldn't load match `gfw':/lib/xtables/libipt_gfw.so: cannot open shared object file: No such file or directory

 

这说明west-chamber编译的有问题,它引用了版本2的iptables库,但是最新的iptable库是版本5。

思考了一下,觉得是因为iptables版本太高(1.4.10),但是iptables-dev是低版本的(1.4.4),编译west-chamber的时候按照低版本的编译的,因此产生了不匹配。

考虑把iptables弄回1.4.4版本再试试,但是没找到降版本方法,于是直接把系统恢复成刚安装的状态(我是虚拟机,直接恢复,很快~~)。

 

这次直接编译west-chamber,不编译最新的iptables和xtables-addons-common.

顺利通过! :)  这时再执行iptables命令时就不提示错误信息了。

 

 

 

使用结果

---------------------------------------------------------------------------------------------------------

 

安装启动成功后用以下命令查看防火墙状态

$ sudo iptables -L -n -v

可以看到三条规则已经设置正确。

Chain INPUT (policy ACCEPT 189 packets, 29303 bytes)

 pkts bytes target     prot opt in     out     source               destination         

    0     0 ZHANG      tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:80 flags:0x17/0x12 state ESTABLISHED match-set NOCLIP src 

    0     0 LOG        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:80 state ESTABLISHED gfw LOG flags 0 level 6 prefix `gfw: ' 

    0     0 DROP       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp spt:53 state ESTABLISHED gfw 

 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         

 

Chain OUTPUT (policy ACCEPT 274 packets, 204K bytes)

 pkts bytes target     prot opt in     out     source               destination         


打开Wireshark开启抓包,打开浏览器,尝试访问www.blogger.com,显示连接被重置。查看wireshark,可以看到以下内容:

wireshark抓取blogger.com的包

 

我现在还不能分析清楚什么原因,要分析这个除了了解TCP之外,还要了解GFW,并且有一些想象力的。继续努力~

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值