Universal Deserialisation Gadget for Ruby 2.x-3.x
A universal deserialisation gadget chain was found for Ruby 2.x to 3.x that allows arbitrary method calls and code execution using only default classes. An attacker who can supply a malicious serialised object could run commands on the server.
Published on devcraft.io
One of the challenges I wrote for pbctf 2020 involved exploiting deserialisation in a rails app to get code execution and retrieve the flag. The challenge was running with ruby 2.7.2 and rails 6.1, which meant that the existing public gadgets no longer worked and players had to discover a new one.
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
Impact
An attacker could execute arbitrary commands on a Ruby server that deserialises untrusted data, leading to full compromise.
What the attacker needed
Ability to supply serialised data; Ruby 2.x-3.0.2
The fix
The gadget was patched in Ruby 3.0.2 and below, so upgrading to a newer version fixes it.
How it was found
- Autoload the required classes Gem::SpecFetcher and Gem::Installer.
- Prevent the payload from running during Marshal.dump by overriding marshal_dump in Gem::Requirement.
- Create a Net::WriteAdapter with Kernel and :system.
- Create a Gem::RequestSet with @sets set to the write adapter and @git_set set to the command.
- Create a Net::WriteAdapter with the request set and :resolve.
- Build the chain with Gem::Package::TarReader::Entry, Net::BufferedIO, Gem::Package::TarReader, and Gem::Requirement, then Marshal.dump and load it.
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 turns stored data back into objects, it is called deserialisation. If an attacker can control that data, they can sometimes make the program create objects that trigger dangerous actions. This research found a way to do that in Ruby using only built-in classes, leading to command execution.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.