表达式不能作为函数使用,c++机器码函数
Expression cannot be used as a function, c++ machine code function
我正在为缓冲区溢出执行生成机器代码,并且想要一种快速简便的方法将字节码插入程序并查看它如何在我的主机上运行。
我寻找在 C++ 中动态生成函数的方法,并遇到了这个问题 very interesting answer。它得到了相当多的赞成,似乎没有人质疑他们所说的话。
然而,当我尝试在我自己的程序中实现他们编写的内容时,我得到了错误 "Expression cannot be used as a function"。
这是我的代码:
int main()
{
uint8_t machinecode[] = {0x90, 0x0F, 0x01};
*reinterpret_cast<void**>(&machinecode)();
return 0;
}
就编译的代码有效性而言,希望我能正确理解您的问题,您需要转换为可调用对象,在本例中为 void(*)()
,而不仅仅是 void*
, 你需要一组额外的括号:
(*reinterpret_cast<void(*)()>(bytecode))();
参见 here live,但我不确定这是否是您真正想要 运行 的任何内容,即使在您提供的上下文中也是如此。
SkepticalEmpiricist 的回答是正确的,并且解决了编译问题,所以我将其标记为正确。
然而,我必须做的是创建一个函数来分配可执行内存 VirtualAlloc
:
uint8_t* alloc_executable(uint32_t alloc_size)
{
if(!alloc_size)
return nullptr;
return reinterpret_cast<uint8_t*>(VirtualAlloc(NULL, alloc_size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE));
}
我的main
函数:
int main()
{
/*
* nop - 0x90
* ret - 0xC3
*/
uint8_t machinecode[] = {0x90, 0xC3};
uint32_t machinecode_size = ARRAYSIZE(machinecode);
uint8_t* exec_mem = alloc_executable(machinecode_size);
memcpy(exec_mem, bytecode, machinecode_size);
FlushInstructionCache(GetCurrentProcess(), exec_mem, machinecode_size);
auto func = reinterpret_cast<void(*)()>(exec_mem);
func();
return 0;
}
处理 returns 0
没有错误。
另外:显然这是 Windows 具体的。我的 目标 平台是 x64 Windows 10.
我正在为缓冲区溢出执行生成机器代码,并且想要一种快速简便的方法将字节码插入程序并查看它如何在我的主机上运行。
我寻找在 C++ 中动态生成函数的方法,并遇到了这个问题 very interesting answer。它得到了相当多的赞成,似乎没有人质疑他们所说的话。
然而,当我尝试在我自己的程序中实现他们编写的内容时,我得到了错误 "Expression cannot be used as a function"。
这是我的代码:
int main()
{
uint8_t machinecode[] = {0x90, 0x0F, 0x01};
*reinterpret_cast<void**>(&machinecode)();
return 0;
}
就编译的代码有效性而言,希望我能正确理解您的问题,您需要转换为可调用对象,在本例中为 void(*)()
,而不仅仅是 void*
, 你需要一组额外的括号:
(*reinterpret_cast<void(*)()>(bytecode))();
参见 here live,但我不确定这是否是您真正想要 运行 的任何内容,即使在您提供的上下文中也是如此。
SkepticalEmpiricist 的回答是正确的,并且解决了编译问题,所以我将其标记为正确。
然而,我必须做的是创建一个函数来分配可执行内存 VirtualAlloc
:
uint8_t* alloc_executable(uint32_t alloc_size)
{
if(!alloc_size)
return nullptr;
return reinterpret_cast<uint8_t*>(VirtualAlloc(NULL, alloc_size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE));
}
我的main
函数:
int main()
{
/*
* nop - 0x90
* ret - 0xC3
*/
uint8_t machinecode[] = {0x90, 0xC3};
uint32_t machinecode_size = ARRAYSIZE(machinecode);
uint8_t* exec_mem = alloc_executable(machinecode_size);
memcpy(exec_mem, bytecode, machinecode_size);
FlushInstructionCache(GetCurrentProcess(), exec_mem, machinecode_size);
auto func = reinterpret_cast<void(*)()>(exec_mem);
func();
return 0;
}
处理 returns 0
没有错误。
另外:显然这是 Windows 具体的。我的 目标 平台是 x64 Windows 10.