Skip to content
RCE Write-up Advanced

[Blog] Java and Command Line Injections in Windows

Java's ProcessBuilder on Windows fails to properly quote command-line arguments, allowing an attacker to inject additional arguments or even commands when the executable is a .bat or .cmd file. This can lead to arbitrary command execution.

code-white.com

Published on code-white.com

Everyone knows that incorporating user provided fragments into a command line is dangerous and may lead to command injection. That’s why in Java many suggest using ProcessBuilder instead where the program’s arguments are supposed to be passed discretely in separate strings.

The author, on code-white.com

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

Java's ProcessImpl class on Windows does not correctly quote command-line arguments according to Windows' parsing rules, and it fails to apply additional quoting when the executable is a .bat or .cmd file that is run via cmd.exe.

Impact

An attacker could inject additional arguments or execute arbitrary commands, but only if they can control an argument passed to ProcessBuilder and the executable is a .bat or .cmd file (or if argument injection is possible in other cases).

What the attacker needed

Attacker controls an argument; executable is .bat or .cmd (for command injection)

The fix

Java should properly quote command-line arguments according to Windows' parsing rules, and for .bat/.cmd files it should apply additional quoting to prevent command injection.

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 Java program on Windows starts another program, it builds a single text string containing the program name and its arguments. If an attacker can control one of those arguments, they might be able to sneak in extra arguments or even commands, because Java does not quote the arguments correctly. This is like writing a shopping list where someone can add extra items by using clever punctuation.

What is RCE?

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

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.