【发布时间】:2012-03-06 22:57:41
【问题描述】:
在 gcc 中这很好用。代码类似于:
unsigned char b[50] = "\xda\xd1 ... \x0"; //some shellcode with terminating \x0
( (void(*)())b )(); //cast b to function pointer from void to void, then run it
但是当它被放到 Visual C++ 中时,它会吐出这个错误信息:
1>..\test.cpp(132): error C2440: 'type cast' : cannot convert from 'unsigned char [50]' to 'void (__cdecl *)(void)'
1> There is no context in which this conversion is possible
有人知道为什么会这样吗?
【问题讨论】:
-
即使你让它以某种方式工作,这也是一个非常糟糕的主意。
-
或:
reinterpret_cast<void(*)()>(static_cast<char*>(b))()。 -
@NiklasB。
(*(void(*)())&b[0])()在运行时崩溃。首先,&b[0] 与 b 相同。其次,开头多余的 * 解引用了函数指针;我假设它会尝试将 shellcode 解释为地址。 -
奇迹不是它在 Visual C++ 中崩溃,奇迹是它在 gcc 中工作。你所说的“shellcode”到底是什么意思?
-
@user49164 取消引用函数指针是无害的。它很快衰减回函数指针。这就是经典的
***********************************fun_ptr示例的来源。