Command injection
Stage 3 · Lesson 9 · 20 minutes · Requires Stage 1 Lesson 6
The one-line answer
Command injection happens when user input ends up inside an operating-system command that the server runs, letting an attacker add commands of their own.
The boundary: command injection means running commands on someone else's computer. Labs and your own local machine only — never anything else.
Why you should care
This is usually one of the most severe web bugs there is. SQL injection reaches the database; command injection reaches the whole server. Whatever the application's account can do, the attacker can do.
You're ready for it because of Stage 1 Lesson 6 (Bash): you already know what a shell is, how commands are chained, and what whoami prints.
How it actually works
Why apps shell out
Sometimes developers call an operating-system command instead of writing code:
- a "ping this host" network tool in a router or admin panel,
- converting images or documents with a command-line program,
- checking stock by running a legacy script.
In Python (Stage 1 Lesson 7), the dangerous pattern looks like:
import os
os.system("ping -c 1 " + host)The user supplies host. The program glues it into a command string and passes it to the shell.
How the bug happens
Recall Stage 1 Lesson 6: the shell treats some characters as instructions, not text:
| Character(s) | Shell meaning | |
|---|---|---|
; | run the next command after this one | |
&& | run the next command if this one succeeds | |
| `\ | ` | send this command's output to the next one |
` ... or $(...)` | run this and insert its output |
So if a user sends:
8.8.8.8; whoamithe shell runs:
ping -c 1 8.8.8.8; whoamiTwo commands. The ping, and then whoami — which prints the account the web application runs as. The attacker has stopped supplying data and started supplying commands. It's the same root cause as SQL injection (Lesson 1) and XSS (Lesson 3): data treated as code, this time by the shell.
whoami is the standard harmless proof: it shows the command ran without changing anything.
Blind command injection
Often the command's output isn't shown in the response. As with blind SQLi (Lesson 2), testers infer it — for example from a deliberate delay, or by having the output written somewhere they can read in a lab. PortSwigger's labs walk through these safely.
Impact
With command execution, an attacker can often:
- read any file the application can (config, keys, source code),
- install tools or a persistent backdoor,
- move to other machines on the network,
- delete or encrypt data (this is how some ransomware incidents begin).
See it yourself
Only on PortSwigger labs:
- Read the OS command injection topic introduction (link below).
- Do the apprentice lab OS command injection, simple case. Find the stock-check request, send it to Repeater, and add a shell separator plus
whoamito the parameter. - Read the response: whose account is the application running as?
- Read about the blind variants in the topic page and try one apprentice-level blind lab if available.
- Write the one line of server code you think was being run — and then the safe version.
How attackers abuse this
Attackers target any feature that smells like a command-line tool underneath: network diagnostics (ping, traceroute), file conversion, backup or export features, admin panels on routers and IoT devices. Internet-wide scanning for vulnerable devices is constant, and these bugs are frequently used to build botnets.
The defensive fix:
- Don't shell out. Almost every task has a library function: use a Python/Java/Node library to resolve a hostname, convert an image or read a file, instead of calling a command.
- If you must run a program, don't use a shell. Pass arguments as a list, so the input is one argument and never parsed by a shell:
import subprocess
# Unsafe: string passed to a shell
# subprocess.run("ping -c 1 " + host, shell=True)
# Safer: argument list, no shell — ';' is just part of a (strange) hostname
subprocess.run(["ping", "-c", "1", host], shell=False)- Strict input validation (allowlist). If the input must be an IP address, check it is a valid IP address and reject everything else. Don't try to filter out "dangerous characters" — you'll miss some.
- Least privilege. Run the application as a low-privilege user (Stage 1 Lesson 2) so a successful injection reaches as little as possible. Never run web apps as root.
- Monitor: alerts when a web server process starts unexpected programs (a shell,
whoami, download tools) — a high-value Stage 5 detection.
Common mistakes
- Blocklisting
;— there are other separators and substitution syntaxes. Use argument lists and allowlists. - Escaping input by hand instead of avoiding the shell.
- Running the app as root, turning a bug into total control.
- Testing with destructive commands.
whoamior the lab's intended proof — nothing that changes or deletes anything. - Missing indirect inputs — file names, headers and uploaded file metadata can all reach commands.
Check yourself
- Why does
8.8.8.8; whoamirun two commands? - What is the strongest fix for command injection?
- Why is running the web application as a low-privilege user important even after fixing the bug?
Answers
- The application glues the input into a string executed by a shell, and the shell treats
;as "end this command, start the next one". - Don't call the shell at all — use a library function. If a program must be run, pass arguments as a list without a shell, plus allowlist validation.
- Defence in depth: if another injection or bug slips through later, a low-privilege account limits what the attacker can read or change.
Your lab
- PortSwigger OS command injection: https://portswigger.net/web-security/os-command-injection
Writeup: the feature, the injected separator, the proof (whoami output), impact, and the code-level fix.
Summary in your language
தமிழில் சுருக்கம்
பயனர் input, server இயக்கும் operating-system command-க்குள் சேரும்போது command injection நடக்கிறது — attacker தன் சொந்த command-களைச் சேர்க்க முடியும். Shell ;, &&, | போன்ற எழுத்துகளை வழிமுறைகளாகக் கருதுவதால் இது நிகழ்கிறது. இது பொதுவாக மிகத் தீவிரமான பிழை — முழு server-ஐயும் அடையலாம். தீர்வு: shell-ஐ அழைக்காமல் library function பயன்படுத்துங்கள்; நிரல் இயக்க வேண்டியிருந்தால் shell இல்லாமல் argument பட்டியலாக அனுப்புங்கள்; allowlist validation; குறைந்த உரிமை கொண்ட கணக்கில் app-ஐ இயக்குங்கள்.
हिंदी में सारांश
जब user input उस operating-system command में पहुँच जाता है जिसे server चलाता है, तब command injection होता है — attacker अपनी commands जोड़ सकता है। ऐसा इसलिए होता है क्योंकि shell ;, &&, | जैसे characters को निर्देश मानता है। यह आमतौर पर बहुत गंभीर bug है — पूरा server हाथ में आ सकता है। इलाज: shell की जगह library function इस्तेमाल करें; program चलाना ज़रूरी हो तो बिना shell के argument list में भेजें; allowlist validation रखें; app को कम अधिकार वाले account से चलाएँ।
Want this summary in Malayalam, Telugu, Kannada or another language? Tell us — the most requested languages get added next.
What's next
Not every bug is a technical trick — some are flaws in the rules themselves: Business logic flaws