RedTeam Nexus Labs
Module 12 · Lesson 03 // Command Injection

argument separation

Command Injection · Web Security

Freereading · 10 min
On this page
LESSON

Overview

The core technique for preventing command injection is argument separation — passing the program and each of its arguments as distinct values rather than as one shell-interpreted string. This lesson explains how it works and why it is safe.

Learning objectives

  • Explain argument separation (array-based execution).
  • Describe why it prevents command injection.
  • Contrast it with shell string construction.

Core concept

Argument separation means invoking a command by specifying the executable and each argument as separate elements (an argument array/list), executed directly by the OS without a shell. Because there is no shell to interpret the string, metacharacters in an argument are treated as literal data belonging to that single argument — they cannot introduce new commands or operators. This enforces the code/data boundary structurally, just as parameterized queries do for SQL.

How it works

When a program executes a binary with an argument array (no shell), the operating system launches exactly that binary and hands it the array elements as its arguments verbatim. There is no parsing of separators, pipes, or substitution, because no shell is involved. So even if an argument's value contains characters that would be special in a shell, they are just part of that argument's data. This removes the possibility of injection rather than trying to sanitize it — the same "separate structure from data" principle behind prepared statements.

Practical example

Instead of building "tool " + user_input and running it via a shell, the application calls the executable directly, passing tool as the program and the user's value as a single separate argument in an array. The OS runs tool with that one argument; any shell-special characters in the value are treated as literal text within that argument, not as operators. The same input that was dangerous under shell concatenation is now inert data — argument separation neutralizes it by design.

Cybersecurity relevance

Argument separation (array-based, shell-free execution) is the primary, recommended defense against command injection, mirroring parameterization for SQL. Most languages provide APIs to execute a program with an explicit argument list and without invoking a shell — these should be used instead of shell-string functions whenever untrusted input is involved. Where a shell truly must be used, additional care (strict allowlisting/escaping) is required, but avoiding the shell is far more reliable. Testers check whether commands are built as shell strings; defenders use argument separation.

Common mistakes

  • Using shell-invoking execution functions with untrusted input instead of array-based, shell-free execution.
  • Believing escaping metacharacters is as reliable as avoiding the shell entirely.
  • Passing an entire command as one string rather than program + separate arguments.

Key takeaways

  • Argument separation invokes the executable and each argument as distinct elements, executed directly without a shell.
  • With no shell to interpret them, metacharacters in an argument are literal data and cannot introduce new commands — preventing injection by design.
  • It is the primary command-injection defense (analogous to parameterized queries); prefer shell-free, array-based execution APIs over shell-string functions with untrusted input.

Check your understanding

  • Why can shell metacharacters in an argument not cause injection when a command is executed directly with an argument array?
  • How is argument separation analogous to parameterized queries for SQL injection?
FINISH THIS LESSON

Sign in to save your progress.