Skip to content

Newline and CRLF command injection

Newline / CRLFLinux / UnixWindows

Newline command injection uses a line-feed (`%0a`) or carriage-return/line-feed (`%0d%0a`) character to end one command and begin another. A raw newline is a command terminator in the shell just like a semicolon, so when `;`, `&&` and `|` are filtered, an injected newline — often URL-encoded through an HTTP parameter — still splits the command.

How it works

In a shell script or a `sh -c` string, a newline terminates the current command. If user input containing a literal LF is concatenated into a command, everything after the newline is parsed as a new command line. Delivered over HTTP, the newline is usually percent-encoded as `%0a` (LF) or `%0d%0a` (CRLF) and decoded into the command string by the application.

This is especially relevant where input is written into a config file, a crontab, or a command executed line-by-line, and where filters target the obvious operators but forget that a newline is itself a separator. It overlaps with CRLF injection in other contexts, but here the sink is a command interpreter.

Representative payloads

Illustrative fragments for authorized testing. The injected marker commands (id, whoami) are harmless proofs of execution — swap in your engagement’s agreed markers.

  • %0aid

    Linux / UnixURL-encoded LF terminates the line; id runs on the next line. Bypasses ;/&&/| filters.

  • %0d%0aid

    Linux / UnixWindowsCRLF variant for sinks that expect Windows-style line endings.

  • value\nid

    Linux / UnixLiteral newline (shown as \n) injected where the application decodes escape sequences or accepts multiline input.

  • %0a/bin/id%0a

    Linux / UnixNewlines on both sides isolate the injected command on its own line to avoid syntax errors.

When it works

  • User input (often from an HTTP parameter) is decoded and concatenated into a shell command or a line-oriented file that gets executed.
  • Other separators are filtered but newline/CRLF characters are not.

When it fails

  • No shell/line-oriented interpreter processes the input.
  • Newlines are stripped or rejected, or the value is allowlisted.

Detection

Flag encoded and literal newlines in command-driving inputs and watch for multi-line command strings reaching exec.

Signals to look for

  • Parameters containing %0a, %0d, or literal CR/LF where single-line values are expected.
  • Command strings or config/cron entries that gain extra lines after a request.
  • Process telemetry showing a second command that corresponds to post-newline content.

Where to look

WAF / application logsFile-integrity monitoring (cron, config)auditd / Sysmon

Remediation

The correct fix: Execute shell-free with an argv array and reject CR/LF in single-line values; a newline is then just a data byte.

Principles

  • Shell-free execution treats embedded newlines as literal argument content, not a new command.
  • Strip or reject CR/LF in inputs that must be single-line (hostnames, filenames, IDs).
  • When writing user input into config/cron files, encode it and never let it introduce new directives.

Safe APIs by runtime

Node.js

execFile/spawn with args array; validate values are single-line.

if (/[\r\n]/.test(v)) reject(); execFile("host", ["--", v]);
Python

subprocess.run([...], shell=False); reject CR/LF in single-line fields.

if any(c in v for c in "\r\n"): abort()

Not a fix: Filtering only ';' and '&&'. A newline is an equally valid command terminator and is easy to deliver as %0a over HTTP.

For the full secure-coding matrix across languages, see the command injection prevention guide.

References

Related techniques