简体   繁体   English

使用程序测试 shellcode 出现段错误

[英]Got segfault with a program to test shellcode

I found this C code example to run shellcode, but it cases segmentation fault for me.我找到了这个 C 代码示例来运行 shellcode,但它对我来说是分段错误。 It doesn't cause segmentation fault on my friend's machine though, so I am wondering if it is a version issue.不过,它不会在我朋友的机器上导致分段错误,所以我想知道这是否是版本问题。 Is char causing the segmentation fault? char是否导致分段错误?

#include<stdio.h>
#include<string.h>

unsigned char code[] = \
"\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";

main()
{

  printf("Shellcode Length:  %d\n", strlen(code));

    int (*ret)() = (int(*)())code;

    ret();

}

In order for your shellcode to properly work, you'll have to compile your program with the -z execstack flag.为了让您的 shellcode 正常工作,您必须使用-z execstack标志编译您的程序。 This disables the NX protection which is enabled by default and prevents pages holding data from being marked as executable.这将禁用默认启用的NX 保护,并防止将保存数据的页面标记为可执行文件。 If you don't disable NX the program will segfault because the memory page where your shellcode string is going to be put is not going to be executable, and the process will get killed when trying to execute its content.如果您不禁用 NX,该程序将出现段错误,因为您的 shellcode 字符串将被放置的内存页面将无法执行,并且该进程将在尝试执行其内容时被终止。

I also see an int 0x80 in your shellcode so I assume this is supposed to be an x86 32bit shellcode.我还在你的 shellcode 中看到一个int 0x80 ,所以我假设这应该是一个 x86 32 位 shellcode。 Other than that you're missing an int in front of main() , but I guess that's a copy-paste error.除此之外,您在main()前面缺少一个int ,但我想这是一个复制粘贴错误。

Compile your program with:编译你的程序:

gcc -m32 -z execstack prog.c

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

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