Exploiting SSH via ProxyCommand
OpenSSH did not filter control characters such as newlines from usernames passed to ProxyCommand, allowing an attacker to inject shell commands when a user clones a malicious git repository with a crafted submodule URL. This could lead to remote code execution under certain configurations.
dgl.cx
Published on dgl.cx
I sometimes look for vulnerabilities that are just small twists on existing ones, where some angle such as particular character escaping has been missed. CVE-2023-51385 turned up some fruit.
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 the username validation function in ssh.c did not reject control characters, allowing a newline to break out of the exec line and run subsequent shell commands.
Impact
This can result in RCE, with the right conditions, but those conditions depend on the user having a particular configuration and the attacker knowing hostnames used in match conditions.
What the attacker needed
Victim has ProxyCommand with %r in SSH config; attacker knows hostnames in match conditions; victim clones malicious git repo recursively; shell is bash, fish, or csh (not zsh)
How it was found
- Create a git repository with a submodule whose URL contains a malicious username with a newline and shell commands.
- Ensure the victim's SSH config uses a ProxyCommand that includes the %r token.
- The victim clones the repository recursively, causing git to invoke SSH with the malicious username.
- The shell executes the injected commands after a syntax error on the exec line.
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 you connect to a server with SSH, a setting called ProxyCommand can run a helper program, and it inserts your username into that command. The username was not checked for hidden control characters like newlines, so an attacker could sneak extra shell commands into it. If you cloned a malicious git repository, those extra commands could run on your computer.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.