kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

gcc编译安装

ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.gz

ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2

ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2

ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz

安装依赖

1
gcc configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+

从错误中可以看出:GCC编译需要GMP, MPFR, MPC这三个库(有的系统已经安装了就没有这个提示,我的没有安装),有两种安装方法(建议第二种):

手动安装

我使用的版本为gmp-4.3.2,mpfr-2.4.2和mpc-0.8.1,在 ftp://gcc.gnu.org/pub/gcc/infrastructure/ 下载,根据提示的顺序分别安装GMP,MPFR和MPC(mpfr依赖gmp,mpc依赖gmp和mpfr),这里全部自己指定了安装目录,如果没有指定则默认分装在在/usr/include、/usr/lib和/usr/share,管理起来不方便,比如想卸载的时候还得一个个去找:

1
2
3
安装gmp:  ./configure --prefix=/usr/local/gmp-4.3.2; make install
安装mpfr: ./configure --prefix=/usr/local/mpfr-2.4.2 --with-gmp=/usr/local/gmp-4.3.2/; make install
安装mpc:  ./configure --prefix=/usr/local/mpc-0.8.1 --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-2.4.2/; make install

gcc自带脚本安装

gcc源码包中自带了一个gcc依赖库安装脚本download_prerequisites,位置在gcc源码目录中的contrib/download_prerequisites,因此只需要进入该目录,直接运行脚本安装即可:./download_prerequisites

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MPFR=mpfr-2.4.2
GMP=gmp-4.3.2
MPC=mpc-0.8.1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2 || exit 1
tar xjf $MPFR.tar.bz2 || exit 1
ln -sf $MPFR mpfr || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2 || exit 1
tar xjf $GMP.tar.bz2  || exit 1
ln -sf $GMP gmp || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz || exit 1
tar xzf $MPC.tar.gz || exit 1
ln -sf $MPC mpc || exit 1

rm $MPFR.tar.bz2 $GMP.tar.bz2 $MPC.tar.gz || exit 1

配置环境变量

我这里指定了安装位置,如果没有指定则这几个库的默认位置是/usr/local/include和/usr/local/lib,不管有没有指定GCC编译时都可能会找不到这三个库,需要确认库位置是否在环境变量LD_LIBRARY_PATH中,查看环境变量内容可以用命令

1
echo $LD_LIBRARY_PATH

设置该环境变量命令如下:

1
2
3
指定安装:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib:/usr/local/mpc-0.8.1/lib

默认安装:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

不指定环境变量会出错:

1
configure: error: cannot compute suffix of object files: cannot compile

2.编译gcc

1)建立一个objdir来存放目标文件 然后进入该文件夹输入
1
2
/home/wulei/sourcecode/gcc-4.6.2/configure --prefix=/usr/local/gcc-4.6.2 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-2.4.2/ --with-mpc=/usr/local/mpc-0.8.1/
最终用:../gcc-4.6.2/configure --prefix=/usr/gcc-4.6.9 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c --with-gmp=/usr/gmp-4.3.2 --with-mpfr=/usr/mpfr-2.4.2 --with-mpc=/usr/mpc-0.8.1
2)
1
2
3
make
make check
make install
错误1
1
2
/usr/bin/ld: .libs/expat_justparse_interface.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
.libs/expat_justparse_interface.o: could not read symbols: Bad value

解决

1
make CXXFLAGS=-fPIC CFLAGS=-fPIC
出现问题make的时候提示如下:
1
2
3
4
5
6
7
8
Checking for suffix of object files... configure: error: in `/home/wulei/sourcecode/gcc-4.6.2/i686-pc-linux-gnu/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make[2]: *** [configure-stage1-target-libgcc] 错误 1
make[2]:正在离开目录 `/home/wulei/sourcecode/gcc-4.6.2'
make[1]: *** [stage1-bubble] 错误 2
make[1]:正在离开目录 `/home/wulei/sourcecode/gcc-4.6.2'
make: *** [all] 错误 2

于是 进入/home/wulei/sourcecode/gcc-4.6.2/i686-pc-linux-gnu/libgcc查看这个路径下的config.log
发现如下的错误提示:

1
/home/wulei/sourcecode/gcc-4.6.2/host-i686-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libmpfr.so.1: cannot open shared object file: No such file or directory

原因是因为linux在make的时候没有自动寻找新加入的库所以要用命令加入

1
export LD_LIBRARY_PATH=/usr/local/mpc-0.8.1/lib:/usr/local/mpfr-2.4.2/lib:/usr/local/gmp-4.3.2/lib

Makefile:161: ../.././gcc/libgcc.mvars: No such file or directory

编译gcc时,需要注意一个原则:不要再gcc的源码中直接执行./configure、make、make install等命令,需要在源码目录下另外新建一个目录,在新建的目录中执行以上命令。

--prefix

以安装supersparrow-0.0.0为例,我们打算把他安装到目录 /usr/local/supersparrow,于是在supersparrow-0.0.0目录执行带选项的脚本

1
./configure –prefix=/usr/local/supersparrow

执行成功后再编译、安装(make,make install);安装完成将自动生成目录supersparrow,而且该软件任何的文档都被复制到这个目录。为什么要指定这个安装目录?是为了以后的维护方便,假如没有用这个选项,安装过程结束后,该软件所需的软件被复制到不同的系统目录下,很难弄清楚到底复制了那些文档、都复制到哪里去了—基本上是一塌糊涂。

用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就能够把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统,不同系统用–target XXX)。

一个小选项有这么方便的作用,建议在实际工作中多多使用。

LD_PRELOAD环境变量

在Linux的动态链接库的世界中,LD_PRELOAD就是这样一个环境变量,它可以影响程序的运行时的链接(Runtime linker),它允许你定义在程序运行前优先加载的动态链接库。这个功能主要就是用来有选择性的载入不同动态链接库中的相同函数。通过这个环境变量,我们可以在主程序和其动态链接库的中间加载别的动态链接库,甚至覆盖正常的函数库。一方面,我们可以以此功能来使用自己的或是更好的函数(无需别人的源码),而另一方面,我们也可以以向别人的程序注入恶意程序,从而达到那不可告人的罪恶的目的。

我们知道,Linux的用的都是glibc,有一个叫libc.so.6的文件,这是几乎所有Linux下命令的动态链接中,其中有标准C的各种函数。对于GCC而言,默认情况下,所编译的程序中对标准C函数的链接,都是通过动态链接方式来链接libc.so.6这个函数库的。

OK。还是让我用一个例子来看一下用LD_PRELOAD来hack别人的程序。

示例一

我们写下面一段例程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 文件名:verifypasswd.c */
/* 这是一段判断用户口令的程序,其中使用到了标准C函数strcmp*/
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
	char passwd[] = "password";
	if (argc < 2) {
		printf("usage: %s <password>/n", argv[0]);
		return 1;
	}
	if (!strcmp(passwd, argv[1])) {
		printf("Correct Password!/n");
		return 1;
	}
	printf("Invalid Password!/n");
	return 0;
}

在上面这段程序中,我们使用了strcmp函数来判断两个字符串是否相等。下面,我们使用一个动态函数库来重载strcmp函数:

1
2
3
4
5
6
7
8
9
10
/* 文件名:hack.c */
#include <stdio.h>

#include <string.h>
int strcmp(const char *s1, const char *s2)
{
	printf("hack function invoked. s1=<%s> s2=<%s>/n", s1, s2);
	/* 永远返回0,表示两个字符串相等 */
	return 0;
}

编译程序:

1
2
$ gcc -o verifypasswd verifypasswd.c
$ gcc -shared -o hack.so hack.c

测试一下程序:(得到正确结果)

1
2
$ ./verifypasswd asdf
Invalid Password!
设置LD_PRELOAD变量:

(使我们重写过的strcmp函数的hack.so成为优先载入链接库)

1
$ export LD_PRELOAD="./hack.so"

再次运行程序:

1
2
3
$ ./verifypasswd  asdf
hack function invoked. s1=<password> s2=<asdf>
Correct Password!

我们可以看到,
1)我们的hack.so中的strcmp被调用了。
2)主程序中运行结果被影响了。
如果这是一个系统登录程序,那么这也就意味着我们用任意口令都可以进入系统了。