c - 执行shellcode段错误

标签 c linux gcc assembly x86

<分区>

我已经编译了一个基本的漏洞利用(基本上,C 中的源代码不会利用任何东西,只需执行执行 Bash 的操作码)。问题是当我执行二进制文件时:“段错误”。

这是我所做的:

执行 Bash.asm (NASM)

section .text
global _start
_start:
xor EAX, EAX           ; EAX = 0
push EAX               ; "\0\0\0\0"
push DWORD 0x68732F2F  ; "//sh"
push DWORD 0x6E69622F  ; "/bin"
mov EBX, ESP           ; arg1 = "/bin//sh\0"
push EAX     ; NULL -> args[1]
push EBX     ; "/bin//sh\0" -> args[0]
mov ECX, ESP ; arg2 = args[]
mov AL, 0X0B ; syscall 11
int 0x80     ; excve("/bin//sh", args["/bin//sh", NULL], NULL)

在终端中:

prompt$ nasm -f elf32 executeBash.asm
prompt$ ld -m elf_i386 executeBash.o -o executeBash
prompt$ objdump -M intel,i386 -d executeBash

executeBash:     file format elf32-i386


Disassembly of section .text:

08048060 <_start>:
 8048060:   31 c0                   xor    eax,eax
 8048062:   50                      push   eax
 8048063:   68 2f 2f 73 68          push   0x68732f2f
 8048068:   68 2f 62 69 6e          push   0x6e69622f
 804806d:   89 e3                   mov    ebx,esp
 804806f:   50                      push   eax
 8048070:   53                      push   ebx
 8048071:   89 e1                   mov    ecx,esp
 8048073:   b0 0b                   mov    al,0xb
 8048075:   cd 80                   int    0x80
prompt$ # "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80"
prompt$ ./executeBash
$ exit
prompt$ 

ASM 中的 exploit 运行完美。

exploitBash.c

void main()
{
    char shellcode[] = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69"
                       "\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80";
    void(*fp) (void);
    fp = (void *)&shellcode;
    fp();
}


prompt$ gcc -m32 -fno-stack-protector -z execstack exploitBash.c -o exploitBash
prompt$ ./exploitBash
Segmentation fault

最佳答案

您忘记设置 edx,因此它包含了 C 代码最后使用它的任何内容,这不太可能是有效的环境指针。在独立代码中,由于程序的初始启动状态,edx 恰好为零。如果您使用 strace,您可以看到 execve 返回 -EFAULT,然后执行继续经过您的代码进入垃圾,然后真正发生段错误。例如,您可以像这样修复 shellcode:

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

(我在 int 0x80 之前包含了一个 xor edx, edx。)

关于c - 执行shellcode段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504984/

相关文章:

c - 对于作业,我必须比较两个日期,使用 C 中的结构。我不确定我的逻辑是否错误

c - 隐式函数声明在 C89 中合法吗?

c - "Syscall param open(mode) contains uninitialised byte(s)"在打开的系统调用中

xml - 使用sed命令删除xml文件中的特殊字符

java - 无法打开共享对象文件: No such file or directory error while run time

c++ - 其他文件中的静态函数访问

c - c 编译器是否可以将 'if-elseif' block 转换为 'switch' block 以优化代码?

c中的圆形数组用于延迟线

windows - 删除文本文件中的前导空格

gcc - gcc FLTK编译错误