【发布时间】:2009-10-16 07:51:05
【问题描述】:
我在我的一个项目中破解了 glibc 的 printf() 并遇到了一些问题。你能提供一些线索吗?我担心的一个问题是为什么同样的 malloc/free 解决方案可以完美运行!
如附件,“PrintfHank.c”包含我自己的 printf() 解决方案,它将在标准库之前预加载;而“main.c”只是使用 printf() 输出一个句子。编辑两个文件后,我发出以下命令:
- 编译main.c gcc –Wall –o main main.c
- 创建我自己的库 gcc –Wall –fPIC –shared –o PrintfHank.so PrintfHank.c –ldl
- 测试新库 LD_PRELOAD=”$mypath/PrintfHank.so” $mypath/main
但我在控制台中收到“hello world”而不是“within my own printf”。破解 malloc/free 函数时,没关系。
我以“root”身份登录系统并使用 2.6.23.1-42.fc8-i686。任何 cmets 将不胜感激!!
main.c
#include <stdio.h>
int main(void)
{
printf("hello world\n");
return 0;
}
PrintfHank.c
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <dlfcn.h>
static int (*orig_printf)(const char *format, ...) = NULL;
int printf(const char *format, ...)
{
if (orig_printf == NULL)
{
orig_printf = (int (*)(const char *format, ...))dlsym(RTLD_NEXT, "printf");
}
// TODO: print desired message from caller.
return orig_printf("within my own printf\n");
}
【问题讨论】:
标签: hook code-injection glibc