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.

Lesson 18 of 69 · 13 min read · practice only with authorization

Diagram

From a form field to a shell command

Follow the path. One example at a time.

Step 1 of 4

Feature

A diagnostics page pings a host the user enters.

Illustration: Feature
# Feature
# A diagnostics page pings a host the user enters.
What changes the outcome
Call

os.system, shell=True, exec with a string

Input

Parsed by the shell

Outcome

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.

  1. 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.
  2. 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.
  3. 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.

1 / 3

Which call avoids shell parsing in Python?

Go deeper

Specific techniques, exercises, and glossary cards for this topic.

  1. 01Argument injection without a shellSkipping the shell is necessary and not sufficient. Includes the diagram and a defensive fix.
  2. 02Filenames that become commandsAn upload’s original name is data. Includes the diagram and a defensive fix.

Exercises

  1. 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.
  2. 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.

Glossary

  1. 01Command injectionUser input is concatenated into a shell command, so the server runs instructions the developer did not plan.