SlideShare a Scribd company logo
Rails Security   : It’s Not Just a Good Idea
           By David Keener
Introduction


Why security matters
Realistic goals
Learning good practices
…by looking at some bad practices

Resources
Some final thoughts
Who Am I?


Long-time Ruby/Rails developer
Founder/Organizer – RubyNation &
DevIgnition conferences
Last 3 projects…
 - Online Video Contest: With $10K-plus prizes…
 - Bank: Online charitable donations via credit card
 - Cyber Security: <cannot discuss details>

“School of hard knocks” for security
Why Security Matters
More personal data is moving online than
ever before
Social networks, retail sites, mobile devices, etc.
The community of “bad actors” is growing
and it’s international
The Internet is everywhere
If the data in your app has value, someone
WILL try to get to it
RSA is THE ultimate example
There are serious legal ramifications to
security breaches
Realistic Goals
  Security is a BIG topic
  Nobody can cover it all in one talk



Goal 1: Illustrate how important security is
Goal 2: Demonstrate some good practices
        (and a few bad mistakes)
Goal 3: Point you to some awesome resources
“…out of 300 audited sites,
  97% are vulnerable to attack”

             - From a Gartner Group survey
1. A Silly Vulnerability

                                                                A newbie
                                                                   mistake…




…here’s the exploit:
   curl –d ‘forum_id=1&message[subject]=foobar’ http://yoursite.com/forums
1. Mitigation
Option 1: Include the before filter in proper controller

Option 2: Create a NoAuth controller…
- ApplicationController:   before_filter :login_required
- NoAuthController:        skip_before_filter :login_required


General Rule: “Default Deny” is your friend.

Also: Be aware of code smells such as out-of-control
before-filter stacks, filters with non-obvious side effects,
etc.
2. Mass Assignment
How many times have we seen this:




           Mass assignment is convenient…but it’s not safe.
2. Mitigation

Mass assignment – only for fields with no
security impact

ALL models should use attr_accessible to
specify fields that can be mass-assigned
attr_accessible :first_name, :last_name, :email


Other fields can be individually assigned if
needed
3. Regexes

What’s wrong with this code?
3. Mitigation

 The regular expression uses ^ and $ to
 match the start and end of the string
 In Ruby, this only matches a single line if
 multi-line input is provided


Use A and z instead for input validation
4. File Uploads



                                        views/uploads/new.html.erb




                                                controllers/uploads.rb

Uploads are prone to numerous potential security issues…
4. Problems

The original file is left in the /tmp directory
- Under a name like Rack*multipart*{random stuff}
- Executable files could theoretically be executed by someone (BAD)

The uploaded file is copied to
$RAILS_ROOT/public/data
-   Under a name like Rack*multipart*{random stuff}
-   The file is web-accessible
-   Embedded JavaScript will have server access (BAD)
-   Could potentially see files uploaded by other users (PRIVACY)

Uploaded files are never cleaned up
File names can have collisions
4. Mitigations

File in /tmp directory
- Remove the file immediately after it has been copied
File is copied to $RAILS_ROOT/public/data
- Copy files to a non-web-accessible dir
- Validate file types and eliminate undesirable files
- Ensure that files are never left as executable
Uploaded files never cleaned up
- Delete files when no longer needed
File names can have collisions
- Add a unique ID as a filename prefix to prevent name collisions
5. Uploading XML Files

What if I want to …
           upload and parse an XML File?




     It’s called an XML External Entity Expansion Attack…
5. Mitigation Options

Option 1: Before parsing, regex for
!DOCTYPE, !ELEMENT or !ENTITY and
immediately reject the file
- No need to be nice to the user


Option 2: Disable entity expansions
- Differs based on XML parser used
5. Mitigation - Nokogiri


                                                 Parse settings




-   Validates the XML against the XSD
-   noent => No entity expansions
-   nonet => No network access
-   Does not actually do external entity expansions, but
    the infrastructure is there
5. Mitigation - REXML




- Non-validating parser
- entity_expansion_limit: raises exception if it finds
  any entity expansions
- Not actually required to do entity expansions, but
   it seems to have some of the infrastructure
Some Best Practices

Here are a few more best practices
 Always sanitize user-provided input
 Rolling your own authentication is an
 anti-pattern…think twice
 - Use Devise, restful_authentication, CanCan, etc.

 Use database-backed session storage
 “Default Deny” is your friend
 Use SSL for secure logins in production
Resources

Rails Security Guide
http://guides.rubyonrails.org/security.html


OWASP Ruby on Rails Security Guide V2
https://www.owasp.org/index.php/Category:OWASP_Ruby_on_Ra
ils_Security_Guide_V2


DHS Sensitive Systems Policy Directive 4300A
http://www.uscg.mil/hq/cg9/NAIS/RFP/SectionJ/dhs-4300A-
policy.pdf
Conclusion

Security matters...
Compromising PII or financial info will always be BAD

It’s easier to build security in from the
beginning than to retrofit it later
Make good security practices second
nature now…they will pay off later
The security of your app must be TESTED
RSpec, Cucumber and similar tools are essential
It’s smooth sailing
If you build good
security practices
into your app…
Questions

   Feel free to contact me:

     dkeener@keenertech.com
     david.keener@gd-ais.com
     Twitter: dkeener2010
     Blog: http://www.keenertech.com



We’re also looking for some good Rubyists…

More Related Content

PPTX
Intro to Malware Analysis
PDF
Understanding CryptoLocker (Ransomware) with a Case Study
PDF
Hunting Layered Malware by Raul Alvarez
PPTX
Advanced Malware Analysis Training Session 1 - Detection and Removal of Malwares
PPTX
Advanced Malware Analysis Training Session 3 - Botnet Analysis Part 2
PPTX
Reversing & Malware Analysis Training Part 11 - Exploit Development [Advanced]
PDF
Pentesting with Metasploit
ODP
Malware analysis
Intro to Malware Analysis
Understanding CryptoLocker (Ransomware) with a Case Study
Hunting Layered Malware by Raul Alvarez
Advanced Malware Analysis Training Session 1 - Detection and Removal of Malwares
Advanced Malware Analysis Training Session 3 - Botnet Analysis Part 2
Reversing & Malware Analysis Training Part 11 - Exploit Development [Advanced]
Pentesting with Metasploit
Malware analysis

What's hot (20)

PPTX
Reversing & malware analysis training part 1 lab setup guide
PPTX
Advanced Malware Analysis Training Session 6 - Malware Sandbox Analysis
PPTX
Dll preloading-attack
PPTX
Advanced Malware Analysis Training Session 5 - Reversing Automation
PPTX
Primer on password security
PPTX
Reversing & Malware Analysis Training Part 9 - Advanced Malware Analysis
PPTX
Reversing malware analysis training part10 exploit development basics
PPTX
Reversing & Malware Analysis Training Part 13 - Future Roadmap
PPTX
Hunting Rootkit From the Dark Corners Of Memory
PPT
Bruteforce basic presentation_file - linx
PPTX
Advanced Malware Analysis Training Session 7 - Malware Memory Forensics
PPTX
Reversing & malware analysis training part 2 introduction to windows internals
PPTX
Advanced Malware Analysis Training Session 8 - Introduction to Android
PPTX
Advanced Malware Analysis Training Session 4 - Anti-Analysis Techniques
PPTX
Anatomy of Exploit Kits
PPTX
Reversing malware analysis training part6 practical reversing
PDF
So. many. vulnerabilities. Why are containers such a mess and what to do abou...
PPTX
Malware analysis
PPTX
Docker and-daily-devops
PDF
Bh us 11_tsai_pan_weapons_targeted_attack_wp
Reversing & malware analysis training part 1 lab setup guide
Advanced Malware Analysis Training Session 6 - Malware Sandbox Analysis
Dll preloading-attack
Advanced Malware Analysis Training Session 5 - Reversing Automation
Primer on password security
Reversing & Malware Analysis Training Part 9 - Advanced Malware Analysis
Reversing malware analysis training part10 exploit development basics
Reversing & Malware Analysis Training Part 13 - Future Roadmap
Hunting Rootkit From the Dark Corners Of Memory
Bruteforce basic presentation_file - linx
Advanced Malware Analysis Training Session 7 - Malware Memory Forensics
Reversing & malware analysis training part 2 introduction to windows internals
Advanced Malware Analysis Training Session 8 - Introduction to Android
Advanced Malware Analysis Training Session 4 - Anti-Analysis Techniques
Anatomy of Exploit Kits
Reversing malware analysis training part6 practical reversing
So. many. vulnerabilities. Why are containers such a mess and what to do abou...
Malware analysis
Docker and-daily-devops
Bh us 11_tsai_pan_weapons_targeted_attack_wp
Ad

Viewers also liked (20)

PPT
Bhajan Poorn Hogi Aas
PPT
Beautiful British Landscape Photographs
PPTX
PDF
Bitter Pill: Why Medical Costs Are Killing Us?
PDF
James Metcalfe's February Real Estate Update
PPTX
Food Advertising Awards
PPT
Adjuvant Therapy In Uterine Sarcomas
PPT
Power monica lat
PPTX
PDF
Exploring spatial pattern formation using a simple individual-based model
PDF
Jupiterimages Calendar
PDF
SEO On A Budget
PPT
20091217_EPE-B.Aigaiou_Tsigos
PPT
Healthy nutrition rules
PDF
Five Disruptive Forces that are Shaking Up Indian Pharma!
PPTX
2010 Uni Serv Skills Session Meeting Potential Of Online Meeting Spaces
 
DOC
приложение 4
PPTX
One week job india
PPTX
Chefs Choice Magazine Editorial Coverage Part II
Bhajan Poorn Hogi Aas
Beautiful British Landscape Photographs
Bitter Pill: Why Medical Costs Are Killing Us?
James Metcalfe's February Real Estate Update
Food Advertising Awards
Adjuvant Therapy In Uterine Sarcomas
Power monica lat
Exploring spatial pattern formation using a simple individual-based model
Jupiterimages Calendar
SEO On A Budget
20091217_EPE-B.Aigaiou_Tsigos
Healthy nutrition rules
Five Disruptive Forces that are Shaking Up Indian Pharma!
2010 Uni Serv Skills Session Meeting Potential Of Online Meeting Spaces
 
приложение 4
One week job india
Chefs Choice Magazine Editorial Coverage Part II
Ad

Similar to Rails Security (20)

PDF
Rails Security
PDF
Securing Rails
PPT
Defending Against Attacks With Rails
PPT
Ruby Security
 
PDF
Ruby on-rails-security
PDF
Ruby on Rails Security
PDF
Ruby on Rails Security
PDF
Ruby On Rails Security 9984
PDF
Ruby on Rails Security Guide
PDF
Rails Security
PPT
StartPad Countdown 2 - Startup Security: Hacking and Compliance in a Web 2.0 ...
ODP
Security on Rails
PPTX
Security engineering 101 when good design & security work together
PPTX
Started In Security Now I'm Here
PDF
Windy City Rails - Layered Security
PPTX
Ruby on Rails Penetration Testing
PDF
Ruxmon feb 2013 what happened to rails
PDF
State of Web Security RailsConf 2016
ODP
OWASP Secure Coding
PDF
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective
Rails Security
Securing Rails
Defending Against Attacks With Rails
Ruby Security
 
Ruby on-rails-security
Ruby on Rails Security
Ruby on Rails Security
Ruby On Rails Security 9984
Ruby on Rails Security Guide
Rails Security
StartPad Countdown 2 - Startup Security: Hacking and Compliance in a Web 2.0 ...
Security on Rails
Security engineering 101 when good design & security work together
Started In Security Now I'm Here
Windy City Rails - Layered Security
Ruby on Rails Penetration Testing
Ruxmon feb 2013 what happened to rails
State of Web Security RailsConf 2016
OWASP Secure Coding
HES2011 - joernchen - Ruby on Rails from a Code Auditor Perspective

More from David Keener (20)

PPTX
Writing Killer Fight Scenes
PPTX
Build a Space Battle
PPTX
Creating an Adaptive Setting
PDF
Public Speaking for Writers
PPTX
21st Century Writer
PPTX
Titanic: The Forgotten Passengers
PDF
Rails Tips and Best Practices
PDF
Elevator Up, Please!
PDF
Rails and the Apache SOLR Search Engine
PDF
Killer Business Models
PDF
Building Facebook Apps
PDF
Leveraging Rails to Build Facebook Apps
PPT
Quick Start: ActiveScaffold
PPT
Creating Custom Charts With Ruby Vector Graphics
PPT
A Tour of Ruby On Rails
PPT
Using Rails to Create an Enterprise App: A Real-Life Case Study
PPT
Practical JRuby
PPT
Implementing OpenID for Your Social Networking Site
PDF
Creating Dynamic Charts With JFreeChart
PPT
Quick Start: Rails
Writing Killer Fight Scenes
Build a Space Battle
Creating an Adaptive Setting
Public Speaking for Writers
21st Century Writer
Titanic: The Forgotten Passengers
Rails Tips and Best Practices
Elevator Up, Please!
Rails and the Apache SOLR Search Engine
Killer Business Models
Building Facebook Apps
Leveraging Rails to Build Facebook Apps
Quick Start: ActiveScaffold
Creating Custom Charts With Ruby Vector Graphics
A Tour of Ruby On Rails
Using Rails to Create an Enterprise App: A Real-Life Case Study
Practical JRuby
Implementing OpenID for Your Social Networking Site
Creating Dynamic Charts With JFreeChart
Quick Start: Rails

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MIND Revenue Release Quarter 2 2025 Press Release
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
MYSQL Presentation for SQL database connectivity
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
The Rise and Fall of 3GPP – Time for a Sabbatical?
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm

Rails Security

  • 1. Rails Security : It’s Not Just a Good Idea By David Keener
  • 2. Introduction Why security matters Realistic goals Learning good practices …by looking at some bad practices Resources Some final thoughts
  • 3. Who Am I? Long-time Ruby/Rails developer Founder/Organizer – RubyNation & DevIgnition conferences Last 3 projects… - Online Video Contest: With $10K-plus prizes… - Bank: Online charitable donations via credit card - Cyber Security: <cannot discuss details> “School of hard knocks” for security
  • 4. Why Security Matters More personal data is moving online than ever before Social networks, retail sites, mobile devices, etc. The community of “bad actors” is growing and it’s international The Internet is everywhere If the data in your app has value, someone WILL try to get to it RSA is THE ultimate example There are serious legal ramifications to security breaches
  • 5. Realistic Goals Security is a BIG topic Nobody can cover it all in one talk Goal 1: Illustrate how important security is Goal 2: Demonstrate some good practices (and a few bad mistakes) Goal 3: Point you to some awesome resources
  • 6. “…out of 300 audited sites, 97% are vulnerable to attack” - From a Gartner Group survey
  • 7. 1. A Silly Vulnerability A newbie mistake… …here’s the exploit: curl –d ‘forum_id=1&message[subject]=foobar’ http://yoursite.com/forums
  • 8. 1. Mitigation Option 1: Include the before filter in proper controller Option 2: Create a NoAuth controller… - ApplicationController: before_filter :login_required - NoAuthController: skip_before_filter :login_required General Rule: “Default Deny” is your friend. Also: Be aware of code smells such as out-of-control before-filter stacks, filters with non-obvious side effects, etc.
  • 9. 2. Mass Assignment How many times have we seen this: Mass assignment is convenient…but it’s not safe.
  • 10. 2. Mitigation Mass assignment – only for fields with no security impact ALL models should use attr_accessible to specify fields that can be mass-assigned attr_accessible :first_name, :last_name, :email Other fields can be individually assigned if needed
  • 11. 3. Regexes What’s wrong with this code?
  • 12. 3. Mitigation The regular expression uses ^ and $ to match the start and end of the string In Ruby, this only matches a single line if multi-line input is provided Use A and z instead for input validation
  • 13. 4. File Uploads views/uploads/new.html.erb controllers/uploads.rb Uploads are prone to numerous potential security issues…
  • 14. 4. Problems The original file is left in the /tmp directory - Under a name like Rack*multipart*{random stuff} - Executable files could theoretically be executed by someone (BAD) The uploaded file is copied to $RAILS_ROOT/public/data - Under a name like Rack*multipart*{random stuff} - The file is web-accessible - Embedded JavaScript will have server access (BAD) - Could potentially see files uploaded by other users (PRIVACY) Uploaded files are never cleaned up File names can have collisions
  • 15. 4. Mitigations File in /tmp directory - Remove the file immediately after it has been copied File is copied to $RAILS_ROOT/public/data - Copy files to a non-web-accessible dir - Validate file types and eliminate undesirable files - Ensure that files are never left as executable Uploaded files never cleaned up - Delete files when no longer needed File names can have collisions - Add a unique ID as a filename prefix to prevent name collisions
  • 16. 5. Uploading XML Files What if I want to … upload and parse an XML File? It’s called an XML External Entity Expansion Attack…
  • 17. 5. Mitigation Options Option 1: Before parsing, regex for !DOCTYPE, !ELEMENT or !ENTITY and immediately reject the file - No need to be nice to the user Option 2: Disable entity expansions - Differs based on XML parser used
  • 18. 5. Mitigation - Nokogiri Parse settings - Validates the XML against the XSD - noent => No entity expansions - nonet => No network access - Does not actually do external entity expansions, but the infrastructure is there
  • 19. 5. Mitigation - REXML - Non-validating parser - entity_expansion_limit: raises exception if it finds any entity expansions - Not actually required to do entity expansions, but it seems to have some of the infrastructure
  • 20. Some Best Practices Here are a few more best practices Always sanitize user-provided input Rolling your own authentication is an anti-pattern…think twice - Use Devise, restful_authentication, CanCan, etc. Use database-backed session storage “Default Deny” is your friend Use SSL for secure logins in production
  • 21. Resources Rails Security Guide http://guides.rubyonrails.org/security.html OWASP Ruby on Rails Security Guide V2 https://www.owasp.org/index.php/Category:OWASP_Ruby_on_Ra ils_Security_Guide_V2 DHS Sensitive Systems Policy Directive 4300A http://www.uscg.mil/hq/cg9/NAIS/RFP/SectionJ/dhs-4300A- policy.pdf
  • 22. Conclusion Security matters... Compromising PII or financial info will always be BAD It’s easier to build security in from the beginning than to retrofit it later Make good security practices second nature now…they will pay off later The security of your app must be TESTED RSpec, Cucumber and similar tools are essential
  • 23. It’s smooth sailing If you build good security practices into your app…
  • 24. Questions Feel free to contact me: dkeener@keenertech.com david.keener@gd-ais.com Twitter: dkeener2010 Blog: http://www.keenertech.com We’re also looking for some good Rubyists…