Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 5

Common Syscalls in Exploitation

Recommended resources

In our previous lesson, we explored the mechanics of how a program communicates with the kernel through system calls, tracing the journey from a user-space function to the execution of kernel code and back. We saw that this is a highly controlled process, managed by the CPU and OS to maintain system stability and security.

Today, we shift our focus from the "how" to the "what." Now that you understand the syscall mechanism, we will identify the specific system calls that form the core toolkit for an exploit developer. Your ultimate goal in many binary exploitation challenges is not just to hijack control flow, but to force the compromised program to execute a specific, powerful syscall on your behalf. This lesson covers the most common and critical syscalls—read, write, execve, mmap, and mprotect—and explains why they are the fundamental building blocks for taking control of a system.

The Attacker's Objective: Weaponizing System Calls

Before we dive into individual syscalls, let's consider the high-level goals of an attacker. Once an attacker finds a vulnerability like a buffer overflow, they typically want to achieve one of three things:

  1. Read arbitrary data: To steal secrets like flags, passwords, or memory layout information.
  2. Write to arbitrary memory: To alter program logic, overwrite critical data structures, or set up a later stage of the attack.
  3. Execute arbitrary code: To gain full control of the process, most often by spawning a shell.

These goals are almost impossible to achieve from user space alone due to the security restrictions we discussed. The user program must ask the kernel to perform these actions. Therefore, the attacker's true objective is to manipulate the program into making a syscall that serves their purpose.

This diagram shows the boundary between user space, where applications run, and kernel space, which manages the system's core resources. As we learned, the only legitimate way to cross this boundary is via a system call.

The following research paper excerpt formalizes this idea, highlighting that while attack techniques vary, the endgame is almost always the abuse of a few critical system calls.

[PDF] Protect the System Call, Protect (Most of) the World with BASTION

This paper frames system calls as the critical final step in most security attacks.

Read the section "System Call Usage in Attacks" to understand why a small subset of the 400+ available syscalls are of particular interest to exploit developers. Then, review the table which classifies these "sensitive" syscalls. You will see our targets for today—execve, mprotect, mmap, read, and write—listed prominently.

As the paper makes clear, if you can control the arguments to these critical syscalls, you are very close to achieving your objective. Let's examine them one by one.


Core I/O: read and write

The most fundamental interactions with a process involve input and output. The read and write syscalls are the foundation for these operations and are indispensable tools in exploitation.

On x86-64 Linux, their details are:

  • write (syscall #1): ssize_t write(int fd, const void *buf, size_t count);

    • Purpose: Writes count bytes from the buffer buf to the file descriptor fd.
    • Exploitation Use: Primarily for exfiltration. If you can control the arguments to write, you can leak the contents of memory. For example, by setting buf to the address of a stack canary or a Global Offset Table (GOT) entry and fd to 1 (standard output), you can print these secrets to your terminal, defeating security mechanisms like canaries and ASLR.
  • read (syscall #0): ssize_t read(int fd, void *buf, size_t count);

    • Purpose: Reads up to count bytes from the file descriptor fd into the buffer buf.
    • Exploitation Use: Primarily for staging. If you find a small buffer overflow, you might not have enough space for your full payload. A common technique is to use the initial vulnerability to call read, setting fd to 0 (standard input) and buf to a larger, known memory location. This allows you to send a much larger second-stage payload (like complex shellcode) into a predictable spot in memory.

Since you're comfortable with assembly, you know how to invoke these. For write(1, "hello", 5), you would set:

  • RAX = 1 (syscall number for write)
  • RDI = 1 (file descriptor for stdout)
  • RSI = address of "hello"
  • RDX = 5 (number of bytes to write)
    ...and then execute the syscall instruction.

Memory Manipulation: mmap and mprotect

Modern systems use memory protections like NX (Non-executable bit) to prevent code execution from data segments like the stack and heap. To run shellcode, an attacker must first bypass this protection. The mmap and mprotect syscalls are the primary tools for this.

  • mmap (syscall #9): void *mmap(void *addr, size_t length, int prot, int flags, ...);

    • Purpose: Requests that the kernel map a new memory region into the process's address space.
    • Exploitation Use: Can be used to allocate a new chunk of memory with Read, Write, and Execute permissions (PROT_READ | PROT_WRITE | PROT_EXEC). An attacker could then copy their shellcode into this new region and jump to it. However, mmap is less common in exploits because it requires controlling six arguments, which can be difficult.
  • mprotect (syscall #10): int mprotect(void *addr, size_t len, int prot);

    • Purpose: Changes the permissions of an existing memory region.
    • Exploitation Use: This is a far more popular target. It only requires three arguments. The typical strategy is:
      1. Inject shellcode into a writable area, such as the stack or heap.
      2. Hijack control flow to call mprotect.
      3. Set addr to the start of the memory page containing the shellcode, len to the size of the region, and prot to include PROT_EXEC.
      4. Once mprotect returns, the memory is now executable. The attacker can then jump to the shellcode.

This technique is a classic way to bypass NX. The image below shows a conceptual stack layout for an exploit that calls mprotect to make the stack executable before running shellcode.

A conceptual stack layout for an exploit chain that calls `mprotect`. The payload includes values for the `len` and `prot` arguments, along with the necessary "gadgets" (which we will cover later) to load them into the correct registers (`rsi` and `rdx`). The goal is to make a memory region containing shellcode executable.

The Grand Prize: execve

While reading and writing memory are powerful, the ultimate goal for many exploits is to gain interactive control of the target system. The execve syscall is the most direct way to achieve this.

  • execve (syscall #59): int execve(const char *pathname, char *const argv[], char *const envp[]);
    • Purpose: Replaces the current running process with a new one, specified by pathname.
    • Exploitation Use: This is the quintessential "get a shell" primitive. The goal is to call execve("/bin/sh", NULL, NULL). This command replaces the vulnerable process with a shell (/bin/sh), giving the attacker an interactive command prompt.

Crafting a payload that correctly calls execve is a foundational skill in binary exploitation. The arguments must be set up precisely in memory:

  1. The string "/bin/sh" must exist somewhere in memory, terminated by a null byte.
  2. pathname (in RDI) must be a pointer to this string.
  3. argv (in RSI) must be a pointer to an array of pointers. This array should contain at least one element: a pointer to the "/bin/sh" string, followed by a NULL pointer to terminate the array.
  4. envp (in RDX) can simply be a NULL pointer.

A major challenge is that strings and pointers often contain null bytes (0x00), which can terminate string-based inputs prematurely in many vulnerabilities. Therefore, shellcode must be crafted to be null-free. The following video is a classic tutorial that demonstrates how to create null-free execve shellcode. While it uses the 32-bit x86 architecture (with int 0x80 and registers like eax, ebx), the core logic of setting up arguments in memory and avoiding nulls is universal and directly applicable to your x86-64 work. Your assembly background will make it easy to translate these concepts.

Buffer Overflow Primer Part 5 (Shellcode For Execve)

This video from TheSecurityTube walks through crafting self-modifying, null-free shellcode to spawn a shell with execve.

Watch the entire video, paying close attention to these key techniques: The Problem: The initial segment (here) outlines why basic, compiler-generated assembly is unsuitable for exploits due to null bytes and hardcoded addresses. Relative Addressing: The next part explains the JMP-CALL-POP trick. This is a crucial technique for getting the address of your own payload on the stack, enabling you to reference strings and data relative to your code's location. Constructing Arguments: The main segment shows how to use this relative address to dynamically build the "/bin/sh" string and the argv and envp pointer arrays in memory at runtime, cleverly using placeholder values and self-modifying code to insert nulls where needed. The Syscall: Finally, watch how the arguments are loaded into the correct registers (ebx, ecx, edx in this 32-bit example) before issuing the int 0x80 syscall. For x86-64, you would use rdi, rsi, rdx and the syscall instruction.


Conclusion

In this lesson, we've identified the "crown jewels" of system calls for an exploit developer. You've seen that what might seem like mundane I/O or memory management functions are, from an attacker's perspective, powerful tools for information leakage, protection bypass, and code execution.

Key Takeaways:

  • Exploitation often culminates in forcing a program to execute a critical syscall with attacker-controlled arguments.
  • read and write are the workhorses for staging payloads and exfiltrating data.
  • mprotect is the primary tool for bypassing memory protections like the NX bit, by making writable memory regions executable.
  • execve is the ultimate goal for many exploits, as it allows an attacker to replace the vulnerable process with an interactive shell.
  • Crafting payloads for these syscalls, especially execve, requires careful attention to argument structure and avoiding null bytes.

In our next lesson, we will explore the significance of SUID/SGID binaries. This will add another layer to our understanding: if we successfully use execve to get a shell from a SUID binary, we might not just be a regular user anymore—we might be root.