使用dlopen来解决外部注入mtrace
时间:2007-04-04 来源:loughsky
因为想做一个不需要改动程序源码,就可以使用mtrace来跟踪其内存分配。如果只是简单的编写一段C,然后找到对应的调用汇编语言,肯定是不成的。因为在程序loader到内存,开始运行执行,要做动态链接,找到mtrace的真实地址。
而我们通过注入mtrace时,程序已经开始运行,动态链接完成,因此它找不到对应函数地址。
因此计划采用dlopen的方式,进行修改:
#include <stdio.h>
#include <stdlib.h>
#include <mcheck.h>
#include <dlfcn.h> void *FunctionLib;
void (*Function)();
const char *dlError; int main(void)
{
// mtrace();
FunctionLib = dlopen("/lib/i686/libc-2.3.2.so",RTLD_LAZY);
dlError = dlerror();
if(dlError)
{
printf("error load library\n");
return 0;
}
printf("success load library\n");
Function = dlsym(FunctionLib,"mtrace");
dlError = dlerror();
if(dlError)
{
printf("error load function mtrace\n");
return 0;
}
(*Function)();
printf("success load function\n");
char *p=malloc(10);
// dlclose(FunctionLib);
return 0;
} 注意编译的时候需要加上-ldl选项。 gcc -o t2 -g t2.c -ldl
输出如下: success load library
success load function
= Start
@ ./t2:(dlerror+0x1a9)[0x8048545] + 0x9b2d050 0xa
@ /lib/i686/libc.so.6:[0xfccae8] - 0x9b2d070
mtrace的连接问题是没有了,现在的问题是我现在用到的dlopen等函数的连接怎么来解决。 看来自己写shellcode,还要学很多东西啊。
#include <stdlib.h>
#include <mcheck.h>
#include <dlfcn.h> void *FunctionLib;
void (*Function)();
const char *dlError; int main(void)
{
// mtrace();
FunctionLib = dlopen("/lib/i686/libc-2.3.2.so",RTLD_LAZY);
dlError = dlerror();
if(dlError)
{
printf("error load library\n");
return 0;
}
printf("success load library\n");
Function = dlsym(FunctionLib,"mtrace");
dlError = dlerror();
if(dlError)
{
printf("error load function mtrace\n");
return 0;
}
(*Function)();
printf("success load function\n");
char *p=malloc(10);
// dlclose(FunctionLib);
return 0;
} 注意编译的时候需要加上-ldl选项。 gcc -o t2 -g t2.c -ldl
输出如下: success load library
success load function
= Start
@ ./t2:(dlerror+0x1a9)[0x8048545] + 0x9b2d050 0xa
@ /lib/i686/libc.so.6:[0xfccae8] - 0x9b2d070
mtrace的连接问题是没有了,现在的问题是我现在用到的dlopen等函数的连接怎么来解决。 看来自己写shellcode,还要学很多东西啊。
相关阅读 更多 +