【问题标题】:Run shellcode in cpp在 cpp 中运行 shellcode
【发布时间】:2018-01-16 10:26:09
【问题描述】:

我正在尝试在 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();

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

【问题讨论】:

  • 您能提供确切的症状吗?
  • char 数组初始化unsigned char 数组不是一个好主意。
  • 这是异常 - ClientSide.exe 中 0x0113F005 处的未处理异常:0xC0000005:访问冲突写入位置 0x81D9BAC0。如果有这个异常的处理程序,程序可以安全地继续。
  • 那是因为 DEP(数据执行保护)。如果你真的想执行那个 shellcode,那么禁用 DEP 应该允许你的 shellcode 从数据部分执行
  • 是的,但我创建了一个新的可执行部分并将数据存储在它们的(.shell 部分)上

标签: c++ function-pointers shellcode


【解决方案1】:

你写的都是对的。仅因为您的 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 之后,您尝试在eax 地址0x00000004 获得价值,Windows 页面保护被放置在该位置。 现在你有了0xC0000005: Access violation on write "0x0000000000000004"

ret 添加到您的shellcode:

unsigned char code[] = ""\xB8\x04\x00\x00\x00\xC3"

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

【讨论】:

  • @yedidya-kfir,如果您有任何疑问,请随时提出,如果此答案有帮助,请标记为正确答案。
猜你喜欢
  • 1970-01-01
  • 2016-01-16
  • 2020-09-14
  • 1970-01-01
  • 2017-04-01
  • 2019-02-19
  • 2020-03-19
  • 2022-09-25
相关资源
最近更新 更多