Skip to content
RCE Write-up Advanced

Cmd Hijack - a command/argument confusion with path traversal in cmd.exe

cmd.exe, when invoked with /c and a double-quoted command line, treats the whole string as a relative path before splitting on spaces, so a user-controlled argument containing path traversal can redirect execution to a different executable. An attacker who controls part of the argument could run an arbitrary program on the same disk, leading to code execution depending on the calling application.

Julian Horoszkiewicz Follow

Published on hackingiscool.pl

This one is about an interesting behavior 🤭 I identified in cmd.exe in result of many weeks of intermittent (private time, every now and then) research in pursuit of some new OS Command Injection attack vectors.

Julian Horoszkiewicz, on hackingiscool.pl

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely cmd.exe's SearchForExecutable resolves the entire quoted command line as a relative path (honouring '..' traversal) before falling back to splitting on spaces, so a traversed argument can name the executable.

Impact

Depending on the calling application, this could cause denial of service, information disclosure or arbitrary code execution; Microsoft judged it did not meet the bar for a security fix, and quoting the argument prevents it.

What the attacker needed

Attacker controls part of a command argument; calling app uses cmd.exe /c with quotes; argument not itself quoted

The fix

Applications should enclose user-controlled arguments in quotes (escapeshellarg rather than escapeshellcmd), which makes cmd.exe identify the intended command correctly.

How it was found

  1. Run cmd.exe with the /c switch and a double-quoted command such as ping followed by an argument.
  2. Supply an argument that appends path traversal segments ending in the path of another executable.
  3. cmd.exe resolves the traversed path and executes that executable instead of the intended one.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When a program runs a system command, it often passes the whole command line to cmd.exe in quotes. cmd.exe first tries to read that whole line as a file path, and if the path contains '..' segments it can climb out of the intended folder and land on a different program. So a value the user supplies, meant only as an argument, can end up choosing which program actually runs.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More from Microsoft

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.