【问题标题】:Load a Mac binary as a dynamic library将 Mac 二进制文件加载为动态库
【发布时间】:2023-03-04 12:55:01
【问题描述】:
我正在使用没有源的二进制可执行文件进行一些逆向工程。在 Windows 上,我可以做的是使用 LoadLibrary 加载一个可执行文件 (EXE),就像它是一个 DLL 文件一样。如果加载的文件不可重定位,我可以简单地将我的加载器代码重定位为其他模块“腾出空间”。当我加载了二进制文件后,我可以调用它的函数(当然,假设我在哪里),然后做其他事情。
有没有办法在 Mac 上做同样或类似的事情?我有一个 mach-o 可执行文件,我想加载它,因为它是一个动态库 (DYLIB)。或者有什么方法可以将可执行文件转换为 DYLIB?可执行文件和 DYLIB 之间的真正区别是什么?
【问题讨论】:
标签:
macos
reverse-engineering
dylib
dynamic-library
【解决方案1】:
好的,所以我做了一些实验,看看这个。文件“bin1.c”包含:
#include <stdio.h>
int main() {
printf("I am bin1.\n");
return 0;
}
而“bin2.c”是:
#include <stdio.h>
#include <dlfcn.h>
int main() {
printf("I am bin2.\n");
void *l = dlopen("bin1", RTLD_NOW);
if (l == NULL) {
printf("dlopen failed: %s\n", dlerror());
return -1;
}
void *f = dlsym(l, "main");
if (f == NULL) {
printf("dlsym failed: %s\n", dlerror());
return -1;
}
int (*main)() = f;
main();
return 0;
}
在我的 Mac 上,所有编译都很好,并且确实加载了另一个可执行文件,因为它是一个可加载的库,我可以在另一个二进制文件中调用 main 函数:
Johanka:Desktop newacc$ uname -a
Darwin Johanka.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64
Johanka:Desktop newacc$ gcc bin1.c -o bin1 && ./bin1
I am bin1.
Johanka:Desktop newacc$ gcc bin2.c -o bin2 && ./bin2
I am bin2.
I am bin1.
但不确定是否有限制,以及是否可以使用不可重定位的二进制文件来完成。但这个例子表明,至少在某些情况下,这是可能的。