2015-07-19 80 views
0

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

这里我做了什么:

executeBash.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该漏洞完全运行。

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 
+0

所以,你抱怨_Undefined behaviour_行为会不确定? – Olaf

+0

@RossRidge amm,这是事实......那么问题是什么? – Joe

+0

@RossRidge我编辑了这行,因为你说我的代码不在堆栈中。现在用最新的参数进行编辑。分段故障。 – Joe

回答

6

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

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

+0

完美。但excve()的EDX是什么?换句话说,为什么excve()执行必须为零? – Joe

+1

'edx'是第三个参数,指向环境变量的指针。它也可能是'NULL'。参见[man execve](http://linux.die.net/man/2/execve)。 – Jester