Skip to content
RCE Write-up Intermediate

Leveraging ssh-keygen for Arbitrary Execution (and Privilege Escalation)

The ssh-keygen command can load a shared library via the -D flag, and if an attacker can control the library path, they can execute arbitrary code. The author used this to escalate privileges from a low-privileged user to root on a Linux IoT device by patching an existing library to export the expected function and call a shell.

Sean Pesce Follow

Published on seanpesce.blogspot.com

The ssh-keygen command can be used to load a shared library with the -D flag. This can be useful for privilege escalation (described below), or to translate to arbitrary code execution from argument injection, file overwrites, etc.

Sean Pesce (@SeanPesce), on seanpesce.blogspot.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

Likely the sudo configuration allowed a low-privileged user to run ssh-keygen as root, and ssh-keygen's -D option loads a shared library from a user-specified path without restriction, enabling arbitrary code execution.

Impact

An attacker with low privileges could escalate to root on the device, gaining full control.

What the attacker needed

Low-privilege SSH access; sudo permission to run ssh-keygen as root

How it was found

  1. Check sudo permissions and find that ssh-keygen can be run as root.
  2. Discover that ssh-keygen's -D flag loads a shared library.
  3. Create or patch a shared library that exports C_GetFunctionList and calls a shell.
  4. Upload the library to the target device.
  5. Run sudo ssh-keygen -D /path/to/library to execute the shell as root.

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.

A shared library is a file containing code that programs can load and run. The ssh-keygen tool has a feature to load such a library, and if an attacker can make it load their own library, they can run any code with the same permissions as ssh-keygen. In this case, a low-privileged user was allowed to run ssh-keygen as root, so loading a malicious library gave them a root shell.

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.