欺骗库函数
我们可以愚弄那些比较傻的用户。如果你利用LD_PRELOAD环境变量来捉弄他,你可以让他执行你自己的代码,你已经利用LD_PRELOAD环境变量把标准的库函数替换成了你自己的程序,挺有意思吧?LD_PRELOAD并不是linux系统特有的,并且它一般用在一些应用程序(比如老版本的StarOffice需要运行在较新版本的Redhat系统上)必须用他们自己的(或者比较老的版本,或者修改过的)库函数,因为在安装的时候没有满足他们的需求。Quantum(Staog病毒的作者)在Unix病毒的邮件列表里面提供了这些代码,我做了修改,也是为了便于大家理解:
------------------------tryld.c------------------------------
extern int __open(char *, int, int);
extern int execv(char *, char *[], char *envp[]);
int open(char *path, int flags,
int mode){
printf("open: %s\n", path);
return __open(path, flags, mode);
}
/* 注意这里,原来它放弃了envp参数,这样会...:(?!
呵呵,所以我修改了一下,影响不大:)*/
int execve(char *path, char *args[],
char *envp[]){
printf("execve: %s\n", path);
return execv(path, args, envp);
}
-------------------------------------------------------------- |
------------------------main.c--------------------------------
/*test only,by e4gle*/
#include
#include
#include
#include
main()
{
int fd;
execve("/bin/date","",NULL);
if(fd=open("/etc/inittab",O_RDONLY)!=-1)
{
fprintf(stdout,"open file succeed!\n");
}
else
fprintf(stderr,"open error!\n");
close(fd);
return 0;
}
---------------------------------------------------------------- |
这段代码获取open和execv并且改变了它们的输出,好,我们测试一下:
[e4gle@redhat62 elf]$ gcc -o main main.c
[e4gle@redhat62 elf]$ ./main
open file succeed! <--证明我们打开文件成功了,open调用正常工作
[e4gle@redhat62 elf]$ ldd -v -r ./main
libc.so.6 => /lib/libc.so.6 (0x4001c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Version information:
./main:
libc.so.6 (GLIBC_2.0) => /lib/libc.so.6
/lib/libc.so.6:
ld-linux.so.2 (GLIBC_2.1.1) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.1) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.0) => /lib/ld-linux.so.2
/*跟踪一下库函数的利用,都是glibc的标准库函数,solaris里面是sotruss,我个人认为sotruss
比较好用:)*/
[e4gle@redhat62 elf]$ gcc -shared -o tryld tryld.c
[e4gle@redhat62 elf]$ export LD_PRELOAD=./tryld <--用我们自己的lib
[e4gle@redhat62 elf]$ ./main
execve: /bin/date
open: /etc/inittab
open file succeed!
/*注意!不一样了吧?已经在用我们自己的open和execve了,呵呵:)*/ |
以上仅仅是个测试代码并且没有任何危害性,但是它足以说明我们利用欺骗函数库来攻击程序的原理。
欺骗库函数
我们可以愚弄那些比较傻的用户。如果你利用LD_PRELOAD环境变量来捉弄他,你可以让他执行你自己的代码,你已经利用LD_PRELOAD环境变量把标准的库函数替换成了你自己的程序,挺有意思吧?LD_PRELOAD并不是linux系统特有的,并且它一般用在一些应用程序(比如老版本的StarOffice需要运行在较新版本的Redhat系统上)必须用他们自己的(或者比较老的版本,或者修改过的)库函数,因为在安装的时候没有满足他们的需求。Quantum(Staog病毒的作者)在Unix病毒的邮件列表里面提供了这些代码,我做了修改,也是为了便于大家理解:
------------------------tryld.c------------------------------
extern int __open(char *, int, int);
extern int execv(char *, char *[], char *envp[]);
int open(char *path, int flags,
int mode){
printf("open: %s\n", path);
return __open(path, flags, mode);
}
/* 注意这里,原来它放弃了envp参数,这样会...:(?!
呵呵,所以我修改了一下,影响不大:)*/
int execve(char *path, char *args[],
char *envp[]){
printf("execve: %s\n", path);
return execv(path, args, envp);
}
-------------------------------------------------------------- |
------------------------main.c--------------------------------
/*test only,by e4gle*/
#include
#include
#include
#include
main()
{
int fd;
execve("/bin/date","",NULL);
if(fd=open("/etc/inittab",O_RDONLY)!=-1)
{
fprintf(stdout,"open file succeed!\n");
}
else
fprintf(stderr,"open error!\n");
close(fd);
return 0;
}
---------------------------------------------------------------- |
这段代码获取open和execv并且改变了它们的输出,好,我们测试一下:
[e4gle@redhat62 elf]$ gcc -o main main.c
[e4gle@redhat62 elf]$ ./main
open file succeed! <--证明我们打开文件成功了,open调用正常工作
[e4gle@redhat62 elf]$ ldd -v -r ./main
libc.so.6 => /lib/libc.so.6 (0x4001c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Version information:
./main:
libc.so.6 (GLIBC_2.0) => /lib/libc.so.6
/lib/libc.so.6:
ld-linux.so.2 (GLIBC_2.1.1) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.1) => /lib/ld-linux.so.2
ld-linux.so.2 (GLIBC_2.0) => /lib/ld-linux.so.2
/*跟踪一下库函数的利用,都是glibc的标准库函数,solaris里面是sotruss,我个人认为sotruss
比较好用:)*/
[e4gle@redhat62 elf]$ gcc -shared -o tryld tryld.c
[e4gle@redhat62 elf]$ export LD_PRELOAD=./tryld <--用我们自己的lib
[e4gle@redhat62 elf]$ ./main
execve: /bin/date
open: /etc/inittab
open file succeed!
/*注意!不一样了吧?已经在用我们自己的open和execve了,呵呵:)*/ |
以上仅仅是个测试代码并且没有任何危害性,但是它足以说明我们利用欺骗函数库来攻击程序的原理。
|