我正在尝试在cpp中运行shellcode(shellcode来自用户,因此程序应该是动态的)
当我尝试运行程序时,我想起一个异常,该异常告诉我无法从数据部分运行代码。
之后,我尝试创建一个新的可切除部分并将数据放在此处,但是它不起作用

#pragma section(".shell",read,execute)
__declspec(allocate(".shell"))
unsigned char code[] =
"\xB8\x04\x00\x00\x00";

// Function pointer points to the address of function.
int(*shell)(); //Function pointer
// Initializing a function pointer  with the address of a shellcode
shell = ((int(*)())&code);
// Execute shellcode
int a = shell();

有人可以向我解释我做错了什么吗?

最佳答案

您所写的一切都是正确的。仅由于您的shellcode仅包含mov eax, 4,因此引发了异常。 Windows分配器将您的部分与页面大小对齐,并用零填充,但是0x00add byte ptr [rax], al的操作码。现在,您的shellcode中不仅包含mov eax, 4,而且:

mov eax, 4
add byte ptr [rax],al
add byte ptr [rax],al
....

mov之后,您尝试在放置Windows页面保护的eax addres 0x00000004上获取值(value)。
现在您有了0xC0000005: Access violation on write "0x0000000000000004"

ret添加到您的shellcode中:
unsigned char code[] = ""\xB8\x04\x00\x00\x00\xC3"

而且,您将不会执行未使用的命令并成功退出。

10-07 19:23
查看更多