Skip to content
RCE Write-up Advanced

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.

William Bowling Follow

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.

William Bowling / vakzz (@wcbowling), on devcraft.io

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

  1. Autoload the required classes Gem::SpecFetcher and Gem::Installer.
  2. Prevent the payload from running during Marshal.dump by overriding marshal_dump in Gem::Requirement.
  3. Create a Net::WriteAdapter with Kernel and :system.
  4. Create a Gem::RequestSet with @sets set to the write adapter and @git_set set to the command.
  5. Create a Net::WriteAdapter with the request set and :resolve.
  6. 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.

More by William Bowling

View all

4 more disclosed reports, best first.

More from Ruby

View all

The newest disclosed reports for this program.

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.