Перейти к основному содержимому
Specialist Playbook: Linux, Pwn & Reverse Engineering
Урок 4 из 8

Specialist Playbook: Linux, Pwn & Reverse Engineering

Triage ELF binaries and Linux hosts systematically, extract logic in Ghidra before running unknown code, interpret memory-corruption mitigations with checksec and pwndbg, and audit local privilege boundaries.

Продвинутый 28 мин CTF
Этого урока пока нет на выбранном языке — показана версия на другом языке. Перевод готовится.

Цели обучения

  • Classify Linux, pwn, and reverse-engineering CTF challenges during initial three-minute triage
  • Recover validation logic from a compiled ELF binary using static inspection and Ghidra decompilation
  • Interpret ELF mitigations (NX, PIE, Stack Canary, RELRO) to choose a valid exploitation path in a lab
  • Identify common Linux privilege-escalation misconfigurations such as SUID binaries, sudo rules, and capabilities
  • Apply compiler hardening and least-privilege controls to prevent binary and host compromise

Рекомендуется заранее: CTF Formats, Toolkits, Ethics & Structured Note-TakingPlaybook: Forensics, Steganography & Misc Puzzles

#Why this matters

Web and crypto challenges focus on protocols and application layers, whereas linux, pwn, and teskari-muhandislik (reverse engineering) test what happens at the operating-system and machine-code boundary. In real security work, these are the exact skills used to audit undocumented firmware helpers, investigate malware droppers, and verify memory-safety bugs in C or C++ services.

Building on structured note-taking from earlier lessons, specialist triage prevents wasted hours. Instead of opening every file blindly in a debugger or running random commands on a target shell, you identify the artefact type, check its protections, and pick the shortest path to proof.

#Triage workflow across Linux, RE, and Pwn

Spend the first three minutes classifying what the challenge gives you:

  • teskari-muhandislik (Reverse Engineering): A local binary (often an ELF executable or shared library) with no remote service; recover an embedded secret or satisfy an input check.
  • pwn (Binary Exploitation): A local binary plus a host:port service (sometimes with libc.so.6 and the dynamic linker); hijack control flow or memory to read /flag.txt.
  • linux (Linux Host / Privilege Escalation): SSH credentials or a restricted shell on a lab VM; escalate privileges by auditing OS permissions.
  1. Identifyfile, checksec, or SSH shell
  2. Staticstrings, readelf, Ghidra decompile
  3. Dynamicpwndbg breakpoints or sudo/getcap audit
  4. Automaterepeatable pwntools script and notes

#Reverse engineering: static before dynamic

Never execute an unknown binary on your host before inspecting it. Start with static triage: file shows architecture and whether symbols are stripped, strings surfaces readable prompts, and nm or readelf -s lists unstripped functions.

Next, load the binary into Ghidra. Locate main (or trace cross-references from "Correct" / "Wrong" strings) and read the decompiler view. Intermediate CTF checkers rarely store plaintext flags; they transform input bytes and compare against constants:

c
int check_key(const char *input) {
    unsigned char expected[] = {0x6c, 0x6b, 0x77, 0x62, 0x51, 0x69, 0x68, 0x71};
    for (int i = 0; i < 8; i++) {
        if ((input[i] ^ 0x25) != expected[i]) {
            return 0;
        }
    }
    return 1;
}

Trying random inputs dynamically would take forever, yet static inspection solves it in seconds. Because XOR is its own inverse, expected[i] ^ 0x25 recovers the exact 8-character key without guessing. When constraints combine arithmetic, bit shifts, or multiple equations, write a short Python script to invert them rather than stepping instruction by instruction in a debugger.

#Binary exploitation: reading mitigations and memory layout

In pwn tasks, you must understand which defences the compiler and kernel turned on before choosing an exploit strategy. Run file and checksec on the local lab binary first.

Inspecting a lab pwn binary
$ file ./gatekeeper
./gatekeeper: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, not stripped
$ checksec --file=./gatekeeper
Arch:     amd64-64-little
RELRO:    Partial RELRO
Stack:    No canary found
NX:       NX enabled
PIE:      No PIE (0x400000)

Each line in checksec narrows down what is possible:

Mitigation What it stops What it pushes you toward
NX enabled Executing shellcode placed on the stack or heap ret2win, ret2libc, or Return-Oriented Programming (ROP)
Stack canary Silent linear buffer overflow over saved RIP Format-string leak, off-by-one, or non-contiguous writes
PIE enabled Hardcoding binary function or gadget addresses Leaking a code pointer first to compute the base address
Full RELRO Overwriting the Global Offset Table (GOT) Targeting return addresses, heap hooks, or function pointers

Here gatekeeper has NX enabled, No canary found, and No PIE. Because PIE is disabled, a helper function like win() keeps a fixed virtual address (for example 0x401196 in nm or Ghidra). Using pwndbg inside GDB, you can generate a De Bruijn cyclic pattern (cyclic 128), feed it to the vulnerable gets() or read() call, and inspect RIP or RSP at the crash with cyclic -l to measure the exact byte offset to the saved return address.

Once the offset is known (say 72 bytes on x86-64), pwntools turns the manual observation into a clean, repeatable script against a local process or an authorised lab socket:

python
from pwn import *

context.binary = elf = ELF("./gatekeeper", checksec=False)
# Add a single 'ret' gadget before win() if x86-64 movaps stack alignment crashes
offset = 72
payload = b"A" * offset + p64(elf.symbols["win"])

io = remote("lab.local", 1337)
io.sendlineafter(b"Name: ", payload)
print(io.recvall(timeout=2).decode(errors="replace"))

If the binary crashes inside system() or printf() on x86-64 even though win() was reached, check 16-byte stack alignment: inserting one ret gadget before win() shifts RSP by 8 bytes and resolves movaps faults.

#Linux host enumeration and privilege boundaries

Linux privilege-escalation challenges mirror common system-administration mistakes. When you land on an authorised lab container or VM, verify who you are and which trust boundaries exist before modifying anything:

Checking local privilege boundaries
$ id
uid=1001(ctfuser) gid=1001(ctfuser) groups=1001(ctfuser)
$ sudo -l
User ctfuser may run the following commands on labhost:
    (root) NOPASSWD: /usr/bin/python3 /opt/scripts/cleanup.py
$ getcap -r / 2>/dev/null
/usr/bin/python3.11 cap_setuid=ep

Focus on four high-signal checks:

  1. sudo -l rules: If a script or interpreter runs as root, check file permissions on the script and imported modules, plus preserved variables (PYTHONPATH, LD_PRELOAD).
  2. SUID / SGID binaries (find / -perm -4000 2>/dev/null): Custom SUID binaries run with effective UID 0. Relative commands like tar instead of /usr/bin/tar allow PATH hijacking.
  3. Linux capabilities (getcap): Capabilities grant discrete root powers without SUID; cap_setuid=ep on an interpreter lets it switch UID to 0.
  4. Scheduled jobs and services: Cron jobs (/etc/crontab, /etc/cron.d/) or systemd units running as root that point to user-writable scripts.

#Defending and common mistakes

On the defence side, modern compilers and Linux hardening stop these classes of compromise when applied consistently:

  • Compile C/C++ services with full mitigations: enable stack canaries (-fstack-protector-strong), PIE (-fPIE -pie), full RELRO (-Wl,-z,relro,-z,now), and FORTIFY_SOURCE, and replace unbounded functions (gets, strcpy, sprintf) with length-checked alternatives.
  • Minimise host privileges: drop SUID bits, avoid assigning dangerous capabilities (cap_setuid, cap_dac_override) to interpreters, lock down sudoers entries with exact absolute paths and NOEXEC where appropriate, and keep cron/service scripts root-owned and non-writable by service accounts.

Common CTF mistakes that derail intermediate solvers:

  • Ignoring PIE or libc version mismatches: an exploit that works locally often fails against the lab server because libc offsets differ; verify the provided libc.so.6 with pwninit or patchelf inside your lab workspace.
  • Jumping straight to dynamic debugging: spending 30 minutes single-stepping obfuscated loops in GDB when Ghidra's cross-references and decompiler show the branch condition immediately.
  • Forgetting stack alignment on x86-64: assuming an exploit target address is wrong when adding one ret gadget fixes a movaps alignment crash.

#Summary

  • Classify linux, pwn, and teskari-muhandislik tasks in the first three minutes using file, checksec, and role context before choosing tools.
  • Use static analysis (strings, readelf, Ghidra) to map logic and solve input checks mathematically before running unknown binaries.
  • Read NX, Stack canary, PIE, and RELRO together to decide whether you need a leak, a ret2win/ROP chain, or a GOT target, and automate payloads with pwndbg + pwntools.
  • Audit Linux hosts systematically via id, sudo -l, SUID files, getcap, and scheduled jobs, and harden production systems with full compiler protections and strict least privilege.

Проверка знаний

Войдите, чтобы проходить тест, завершать уроки и сохранять прогресс. Войти в систему

Практика: проверьте свои знания