New Method to Leverage Unsafe Reflection and Deserialisation to RCE on Rails - elttam
The author shows that unsafe reflection of user input into Ruby's constantize can still reach code execution on a default Rails install by initialising a SQLite3::Database object whose extensions option loads an uploaded shared library. The same class can be chained into a Marshal.load deserialisation gadget via the SQLite3Adapter and DeprecatedInstanceVariableProxy, giving remote code execution.
elttam.com
Published on elttam.com
It is a well known security issue that allowing user controlled inputs in calls to the Ruby String#constantize, String#safeconstantize, Module#constget and Module#qualifiedconstget methods are insecure, since these methods allow the arbitrary loading of Ruby classes.For an example, the following code snippet would…
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 application passes user-controlled values into constantize and then calls new on the resulting class, so any installed class with a dangerous constructor (here SQLite3::Database loading extension libraries) can be instantiated with attacker-chosen arguments.
Impact
An attacker could achieve remote code execution on a default Rails application that reflects user input into constantize, and also on one that deserialises user input with Marshal.load, provided the sqlite3 gem is present.
What the attacker needed
Unsafe reflection or Marshal.load of user input; sqlite3 gem installed (default on Rails); ability to upload a file via multipart form; attacker can guess the file descriptor number
How it was found
- Upload a malicious SQLite extension library through a multipart form so Rack writes it to a temporary file
- Find the file descriptor number that points at the uploaded temporary file under /proc/self/fd
- Send a request to the reflection endpoint naming the SQLite3::Database class with the extension path as an argument
- Observe the extension being loaded and the confirmation file created
- For the deserialisation variant, build a Marshal payload chaining the SQLite3Adapter and the deprecation proxy
- Send the base64 payload to the marshal endpoint and observe the confirmation file created
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.
Some Ruby code lets a user pick which class gets created from their input, which is dangerous because many classes do surprising things when built. Here, one database class can be told to load a native library file, and the attacker first uploads that file through a normal file upload so it sits on disk. Loading it runs the attacker's code on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.