whoisjoe.com

Ruby open allows command injection if user controlled

  

June 3, 2015

Home About Projects Blog LinkedIn ReThink Security

We’ve been getting a lot of Ruby on Rails Penetration tests and code reviews at Security Innovaiton, and I’ve been writing a decent amount of it myself. In general it’s a great framework, but like any other framework there are a few little gotchas that could lead to a security vulnerability. A colleague of mine, Arvind, wrote a great blog post on the Security Innovaiton blog in which he outlined a few of these check that out here.

I also came across this on a blog post in this case using open('|[my-command]') will cause the code to be executed. open('|ls').each{|i| puts i} would output each item in the current working directory, for example.

Open is commonly used in ruby to open files, of course we all know everything’s a file, but using it this way to get command injection is pretty cool. So any time you see user controlled filenames in an open command it’s not just remote file read any more, it’s full on command injection.

I originally thought that by using the OpenURI gem, which is common in Rails to load remote http content, this file based command injection may be overridden, but that’s not the case.

Instead, I got looking into the difference between the default open (which comes from File.open), and OpenURI (which comes from the open-uri gem). OpenURI is very common, it’s essentially the goto way to read a website (like wget) in Ruby. OpenURI patches the default open (Does not replace functionality) to support URIs, so it’s still vulnerable even after the gem is included. http://sakurity.com/blog/2015/02/28/openuri.html

So, lesson learned, if you see open(my-var) in source code it’s vulnerable. That’s in addition to backticks, eval, Kernel.exec, IO.popen, etc….

Posted By: Joe Basirico

  • 81 More Posts
  • So, You're a Manager Now
  • A Mixtape in 2022
  • The Middle Path of Planning and Reflection
  • Micromanagement and Trust
  • On Giving Advice
  • Emergency Preparedness During Coronavirus Frenzy
  • Mind Map Your Life
  • Start With the Hard Part
  • Delight in the Details
  • Introducing ReThink Security
  • Newsletter & Recommendations
  • Take a Moment
  • Triage Decision Making
  • Show Your Work
  • Getting Back Up
  • Max Out vs. Continuous Development
  • Mental Diet and Exercise
  • Asking for Help Part 2 - Alerting
  • High Water Mark
  • Who Do You Want to Be
  • Presentation Tips
  • Asking for Help
  • China Hijacking the Internet
  • Recording Audio with AirPods in Imovie
  • Active Decisions
  • Create/Publish Scripts
  • Specialize or Do Not Specialize
  • Exactis Breach
  • Optimizing Images
  • What I Track
  • What I’m Thinking About May
  • What I’m Thinking About March
  • What I’m Thinking About January
  • Building a Collaborative & Social Application Security Program
  • Lazy Days in the Cloud
  • Delegate Then Do
  • So you want to be a better programmer
  • Project Success
  • Don't Short Circuit a Lesson
  • Scale Your Solution to the Problem
  • Digital Currencies
  • Fortnightly
  • Why You Should Have Trust Issues with Pokemon Go, and Every Other App on Your Phone
  • In Defense of Reverse Engineering and Responsible Disclosure
  • Ruby open allows command injection if user controlled
  • New Mac Install Guide
  • Understanding Customer Needs and Helping Them Mature
  • My Experiences with IOS8 and Yosemite so far
  • The Importance of Vulnerability Disclosure Programs and Bug Bounties
  • My New Record Player and Beck - Morning Phase (The Vinyl Experience)
  • An Hour of Code with Code.org
  • Gmail Changes to Displays Images by Default
  • Why I Donated to Help Jailbreak iOS7 & You Should Too
  • Email Strategy
  • Shutdown
  • Anatomy of a Distributed Denial of Service (DDoS) Attack
  • NASA Forced to Suspend All Public Outreach & Education Programs
  • Joe_CMS Open Source!
  • Mobile Application Security Testing FAQs: Post #1
  • How Much Security Does Obfuscation Get You?
  • Why Privacy Matters Even if You Have 'Nothing to Hide'
  • What LinkedIn Should Have Done with Your Passwords
  • Constant Vigilance
  • Boeing Paying Hackers to Break into Their Systems
  • My Reading Cycle
  • Developing Tools for Professional Hackers
  • Finding Your Inner Evildoer (4/4): An Evil Streak
  • Finding Your Inner Evildoer (3/4): A Good Imagination
  • When to Rebuild Your Process from Scratch
  • Finding Your Inner Evildoer (2/4): Complete Knowledge of the System
  • Continuous Incremental, Personal Improvement
  • Finding Your Inner Evildoer: Part 1
  • CISCO Password Revealer
  • Battling with Word and Excel
  • Which is More Secure: Windows or Linux?
  • The High Cost of an Application Security Data Breach
  • Using the ConfigurationManager to Access your ConnecitonStrings in the Web.Config
  • New WikiRater Features
  • When is it OK to Build up Technical Debt
  • Time Management with the Pomodoro Technique
  • Manage Energy Not Time
  • Goals, Results and Activities - defining your productivity
© 2022 whoisjoe.com