Jump to content
Nytro

Rails SQL Injection

Recommended Posts

Posted

Rails SQL Injection

[h=2]Overview[/h] The Ruby on Rails web framework provides a library called ActiveRecord which provides an abstraction for accessing databases.

This page lists many query methods and options in ActiveRecord which do not sanitize raw SQL arguments and are not intended to be called with unsafe user input. Careless use of these methods can open up code to SQL Injection exploits. The examples here do not include SQL injection from known CVEs and are not vulnerabilites themselves, only potential misuses of the methods.

Please use this list as a guide of what not to do.

This list is in no way exhaustive or complete! Please feel free to contribute.

[h=3]Examples[/h] Each method or option described below is accompanied by an example demonstrating how the ActiveRecord interface could be exploited if used unsafely. These are not necessarily the worst exploits, they represent just a small hint of what could be accomplished if one is not careful. The examples on this page were tested with Rails 3.2.13 and SQLite 3.

[h=3]Interactive Version[/h] Clone and run this site from the git repo to try out or modify the examples!

Documentation

[h=3]Calculate Methods[/h] There are several methods based around ActiveRecord::Calculations#calculate.

calculate takes an operation, a column name, and an options hash similar to ActiveRecord::FinderMethods#find. Methods based on calculate are shortcuts for different operations, and take a column name and options hash as arguments.

In addition to the vulnerable options listed for find, the column name argument can also accept SQL!

Calculation methods:

  • average
  • calculate
  • count
  • maximum
  • minimum
  • sum

[h=4]Example[/h]

This example finds the age of a specific user, rather than the sum of all user ages.

params[:column] = "age) FROM users WHERE name = 'Bob';"
Order.calculate(:sum, params[:column])

 Query
SELECT SUM(age) FROM users WHERE name = 'Bob' AS sum_id FROM "orders"
Result
27

Articol complet:

http://rails-sqli.org/

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...