Digging in to SCP Command Injection
The JSch library's example code for SCP file transfer concatenated user-supplied paths directly into a shell command without sanitisation, allowing command injection. An attacker could execute arbitrary commands on the remote server by providing a crafted path.
Published on dylankatz.com
I was recently investigating Java file transfer when I stumbled across an interesting set of issues. The discovery of these issues was sparked by a post with some example code.
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
The code concatenated an untrusted path directly into a shell command string without escaping or sanitising it.
Impact
An attacker could execute arbitrary commands on the remote server where the SCP command runs.
What the attacker needed
Attacker controls the file path; SCP command runs on a remote server
The fix
The JSch sample code was fixed in version 1.5.5 to properly escape the path.
How it was found
- Provide a path containing a semicolon followed by a command.
- The library builds the SCP command by appending the path.
- The remote server executes the SCP command and then the injected command.
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 builds a command by gluing together text from a user, special characters like semicolons can trick the computer into running extra commands. Here, the JSch library's sample code did exactly that with file paths, so a malicious path could run any command on the server. This is called command injection.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.