OS command injection: when input reaches a shell
How features that call system tools — ping, image conversion, PDF generation, archive handling — pass user input to a shell, how to recognize it in code and in an authorized test with a timing probe, and how argument arrays and libraries remove the shell.
Diagram
From a form field to a shell command
Step 1 of 4
Feature
A diagnostics page pings a host the user enters.
# Feature # A diagnostics page pings a host the user enters.
os.system, shell=True, exec with a string
Parsed by the shell
Extra commands possible
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Find the shell
- What fails
- os.system, subprocess with shell=True, Runtime.exec with a single string, child_process.exec, and backticks in Ruby all invoke a shell.
- Who is affected
- The server and whatever its account can reach.
- What you would notice
- Code search hits for those calls with request data nearby.
- What you change
- Use the argument-array forms and remove shell=True.
Options are injection too
- What fails
- Even without a shell, input starting with a dash can become an option.
- Who is affected
- Files and systems the tool can touch.
- What you would notice
- A filename argument that the tool interprets as a flag.
- What you change
- Put -- before user values and validate formats.
Use a library instead
- What fails
- Many shell calls replace something a library does better.
- Who is affected
- Everyone, through reduced attack surface.
- What you would notice
- Calling convert, zip, or curl from application code.
- What you change
- Use the language’s image, archive, and HTTP libraries.
Confirming in an authorized test
A timing probe is the least invasive check: in a lab or staging copy, append a shell sleep separator to the field and measure whether the response is delayed by the expected seconds. A delay that tracks the number you chose confirms command execution without running anything else.
baseline: host=192.0.2.10 -> 0.4 s probe (lab only): host=192.0.2.10 plus a 5 s sleep -> 5.4 s probe (lab only): same with 10 s -> 10.4 s -> delay tracks the injected value; report the parameter
Detection
On the host, web server processes spawning sh, bash, cmd.exe, or powershell are rare and worth alerting on. Application logs should record the validated value, not the raw input.
Related pages
Command injection without a shell covers argument injection, and the filenames page covers names that become options.
Check yourself
Recognition and the fix. Nothing here is a procedure.
Which call avoids shell parsing in Python?
Go deeper
Specific techniques, exercises, and glossary cards for this topic.
- 01Argument injection without a shellSkipping the shell is necessary and not sufficient. Includes the diagram and a defensive fix.
- 02Filenames that become commandsAn upload’s original name is data. Includes the diagram and a defensive fix.
Exercises
- 01Terminal mission: An avatar that runs commandsA profile-picture upload accepted a .php file. Find the shell on disk, then use the access log to see who called it and which command ran first.
- 02File upload validationNine uploads hit a profile picture endpoint: two real photos and seven files that should never be stored as sent. Turn on checks until every one is handled.