A Ruby gem is a standard packaging format used for Ruby libraries and applications. This packaging format allows Ruby software developers a clearly defined format in which they can reliably build and distribute software. Developers push Ruby gems to a distribution server (aka: a gem server) where users can then install the Ruby application using "gem install gem_name".
In a recent presentation, "Trojaned Gems: You can't tell you're using one" at THOTCON 0x6, we (Brandon Myers and Jonathan Claudius) shared some of our research looking at the security of the Ruby gem ecosystem. The original goal of the research was to improve our own Ruby gem security, but we felt what we discovered was worth sharing to help better protect others.
Summary of our Ruby Gem research
The RubyGems client has a "Gem Server Discovery" functionality, which uses a DNS SRV request for finding a gem server. This functionality does not require that DNS replies come from the same security domain as the original gem source, allowing arbitrary redirection to attacker controlled gem servers (aka: CVE-2015-3900).
CVE-2015-3900 allows an attacker to redirect a RubyGem client that is using HTTPS to an attacker-controlled gem server. This effectively bypasses HTTPS verification on the original HTTPS gem source allowing an attacker to force the user to install malicious/trojaned gems.
We wrote a fully functional "Gem Trojan-ing" service that demonstrates how an attacker could simply send Trojan Ruby gems transparently over the wire while the user was installing them.
Ruby gem signing is an obvious mitigation strategy for the above mentioned transport security issues. However, gem signing is not common in the Ruby gem ecosystem (not one of the top 10 gems are signed). See our blog post on how to sign gems here.
We demonstrated that even if you are using signed gems, by using CVE-2015-3900, you must be using the HighSecurity trust policy or gems can still be trojaned in transit due to a signing downgrade attack.
After getting CVE-2015-3900 fixed, we identified a bypass allowing attackers to redirect users to domains that end with the original security domain (aka: CVE-2015-4020). For example: attackercontrolledrubygems.org.
These issues affect the RubyGems client and any environment that embeds the RubyGems client. Ruby, JRuby, and Rubinius all embed the RubyGems client and are affected by CVE-2015-3900.
The mechanism for updating to a fixed version of RubyGems also uses the same vulnerable functionality we're trying to secure.
Anthony was able to confirm that OpenDNS sees roughly 24,000 requests per day for the DNS SRV record in question (inferring 24,000 gem installations per day if we discount local system caches, gem dependencies, and gem installation typos). Given that OpenDNS sees about two percent of the world's Internet traffic—assuming each region of the world has the same likelihood of installing gem packages—that's a possible 1.2 million gem installations per day across the entire Internet (or 438 million gem installs per year) that could be affected.
We've also deployed signatures to detect this vulnerability with our managed IDS/IPS service and have monitored for exploitation attempts for some time. Thankfully, at the time of this writing, we've yet to see any "in-the-wild" exploitation attempts outside of our research lab, which is great news. However, we believe it to be only a matter of time before criminals get wise to this attack vector and make use of it, so we recommend that everyone deploy detections now. For more information on Trustwave IDS/IPS solutions, see here. For existing Trustwave IDS/IPS customers, the signature name is "RubyGems DNS hijack attempt detected (CVE-2015-3900)".
What should you do?
Upgrade your RubyGem client in all of your Ruby environments to 2.4.8 or later. The most common way to do so is by running the "gem update –system" command. You can verify the update with "gem –-version". You can have more than one RubyGem client installed per system.
Verify that all your Ruby gem sources are using HTTPS. This can be verified using the "gem sources" command, which will list all your gem sources. Using the '—add' or '—remove" switches will help you manage the list.
As a gem producer, consider signing your gems to provide your users the option to verify the integrity of your gem.
As a gem consumer, start using gem installation trust policies and use the strongest policy supported by your gem provider. Trust policies can be used with the "gem install" command by using the –P switch and will look something like this "gem install –P HighSecurity gem_name".
Consider reading up on or contributing to TUF (The Update Framework) to better protect your update mechanisms to help protect against the attacks mentioned above.
Slides, Advisories, and Demonstration Material
Here are the slides that we delivered at THOTCON 0x6 for your viewing pleasure:
Here are the three demonstration videos we covered during the presentation:
Gem Install Request Hijacking
Trojaning a Gem in Transit
Bypassing Signed Gems (on MediumSecurity)
We'd like to thank following for their contributions to this research effort: