Ell*_*ink 7 c++ linux assembly gcc 32-bit
我认为我的问题可能看起来有点奇怪,但在这里; 我正在尝试用C++动态创建一个程序(主要是为了它的乐趣,但也是出于编程原因)并且它并不像听起来那么难.要做到这一点,你必须在运行时使用程序集,如下所示:
byte * buffer = new byte[5];
*buffer = '0xE9'; // Code for 'jmp'
*(uint*)(buffer + 1) = 'address destination'; // Address to jump to
Run Code Online (Sandbox Code Playgroud)
这比看起来容易得多,因为我只针对一个平台和编译器; 使用Linux 32位的GCC(也只有一个调用约定,cdecl).所以我试图创建一个动态汇编函数来重定向来自触发器的调用,所以我可以使用类方法作为回调(即使使用C API库(当然还有cdecl)).我只需要它来支持指针和本机类型(char,int,short等......).
ANYTHING MyRedirect(ANY AMOUNT ARGUMENTS)
{
return MyClassFunc('this', ANY AMOUNT ARGUMENTS);
}
Run Code Online (Sandbox Code Playgroud)
上面的函数是我想要在纯汇编中创建的函数(在内存中使用C++).由于函数非常简单,因此ASM也很简单(取决于参数).
55 push %ebp
89 e5 mov %esp,%ebp
83 ec 04 sub $0x4,%esp
8b 45 08 mov 0x8(%ebp),%eax
89 04 24 mov %eax,(%esp)
e8 00 00 00 00 call <address>
c9 leave
c3 ret
Run Code Online (Sandbox Code Playgroud)
所以在我的程序中,我创建了一个ASM模式生成器(因为我不太了解ASM,我搜索模式).此函数可以通过指定函数所需的参数量来生成汇编代码(以字节为单位,对于上面的确切情况,即重定向和返回的函数).这是我的C++代码的片段.
std::vector<byte> detourFunc(10 + stackSize, 0x90); // Base is 10 bytes + argument size
// This becomes 'push %ebp; move %esp, %ebp'
detourFunc.push_back(0x55); // push %ebp
detourFunc.push_back(0x89); // mov
detourFunc.push_back(0xE5); // %esp, %ebp
// Check for arguments
if(stackSize != 0)
{
detourFunc.push_back(0x83); // sub
detourFunc.push_back(0xEC); // %esp
detourFunc.push_back(stackSize); // stack size required
// If there are arguments, we want to push them
// in the opposite direction (cdecl convention)
for(int i = (argumentCount - 1); i >= 0; i--)
{
// This is what I'm trying to implement
// ...
}
// Check if we need to add 'this'
if(m_callbackClassPtr)
{
}
}
// This is our call operator
detourFunc.push_back(0xE8); // call
// All nop, this will be replaced by an address
detourFunc.push_back(0x90); // nop
detourFunc.push_back(0x90); // nop
detourFunc.push_back(0x90); // nop
detourFunc.push_back(0x90); // nop
if(stackSize == 0)
{
// In case of no arguments, just 'pop'
detourFunc.push_back(0x5D); // pop %ebp
}
else
{
// Use 'leave' if we have arguments
detourFunc.push_back(0xC9); // leave
}
// Return function
detourFunc.push_back(0xC3); // ret
Run Code Online (Sandbox Code Playgroud)
如果我指定零,因为stackSize这将是输出:
55 push %ebp
89 e5 mov %esp,%ebp
e8 90 90 90 90 call <address>
5d pop %ebp
c3 ret
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这是完全有效的32位ASM,如果它具有零参数且不需要'this'指针,它将充当'MyRedirect'.问题是,我想实现它生成ASM代码的部分,具体取决于我指定的'redirect'函数将接收的参数数量.我已经在我的小C++程序中成功完成了这个(破解了模式).
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int val = atoi(argv[1]);
printf("\tpush %%ebp\n");
printf("\tmov %%esp,%%ebp\n");
if(val == 0)
{
printf("\tcall <address>\n");
printf("\tpop %%ebp\n");
}
else
{
printf("\tsub $0x%x,%%esp\n", val * sizeof(int));
for(int i = val; i > 0; i--)
{
printf("\tmov 0x%x(%%ebp),%%eax\n", i * sizeof(int) + sizeof(int));
printf("\tmov %%eax,0x%x(%%esp)\n", i * sizeof(int) - sizeof(int));
}
printf("\tcall <address>\n");
printf("\tleave\n");
}
printf("\tret\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此函数打印出与'objdump'生成的ASM代码完全相同的模式.所以我的问题是; 如果我只想要一个上面的重定向函数,无论参数如何,如果只是在Linux 32bit下,或者我需要知道哪些陷阱,这将在所有情况下都有效吗?例如; 将生成的ASM与'short'或'chars'区分开来,或者这是否有效(我只用整数测试),如果我调用一个返回'void'的函数(这将如何影响ASM)?
我可能已经解释了一切有点模糊,所以请问而不是任何误解:)
注意:我不想知道其他选择,我喜欢我目前的实现,并认为这是一个非常有趣的,我会非常感谢你对这个主题的帮助.
编辑:如果有兴趣,这里有一些转储上面的C++代码:链接
正如丹建议的那样,您需要将内存标记为可执行文件。我写了一些你可以使用的代码。(它适用于 GNU/Linux 和 Windows。)如果您打算永远不支持 ARM、x86-64 或其他平台,那么我没有看到您的代码有任何缺陷(添加了可执行部分),而且似乎它应该“永远有效”。(当然,假设其他一切都正常工作。)
#include <sys/mman.h>
...
n = <size of code buffer>;
p = mmap(0, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, 0, 0);
Run Code Online (Sandbox Code Playgroud)
“fish”建议您使用asmjit。我必须同意这一点;它比你的方法更便携。然而,你说你对替代品不感兴趣。
您可能对“ Thunking ”(某种意义上的)感兴趣。它基本上试图完成“用 C++ 方法替换 C 回调”。这实际上非常有用,但对于您的应用程序来说并不是一个好的设计。
希望有帮助。