通过在Visual C++中强制转换为函数指针来执行shellcode

jca*_*cai 6 c++ casting shellcode

在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
Run Code Online (Sandbox Code Playgroud)

但是当它放在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
Run Code Online (Sandbox Code Playgroud)

谁知道为什么会这样?

Nik*_* B. 12

一个合适的调试器会告诉你出了什么问题.我只能猜测您的代码导致访问冲突,因为您要跳转到的缓冲区不可执行.

可能你是一个默认的启用DEP的系统,比如Vista或7,所以你必须确保你的shellcode是可执行的.为此,首先使用VirtualAlloc分配新的可执行缓冲区并将shellcode复制到其中,然后执行它:

void *exec = VirtualAlloc(0, sizeof b, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(exec, b, sizeof b);
((void(*)())exec)();
Run Code Online (Sandbox Code Playgroud)

顺便说一下,你不需要null终止shellcode(C++会自动为你终止字符串文字,但这不是必需的).您也不需要指定大小:

unsigned char b[] = "\xcc";
Run Code Online (Sandbox Code Playgroud)