Nytro Posted April 26, 2013 Report Posted April 26, 2013 Microsoft SQL Server and IBM DB2 data-type injection attacks In the http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-3221entry, what we meant is that CVE-2013-3221 is exclusively about thebehavior of Ruby on Rails as discussed in the listedMLIST:[rubyonrails-security] 20130207 reference. If a reference isabout a data-type injection impact in an application other than a Rubyon Rails application, it should not be mapped to this CVE. However, anapplicable reference about interaction between Ruby on Rails andMicrosoft SQL Server (or interaction between Ruby on Rails and IBMDB2) should be mapped to this CVE.(There might be a misinterpretation that CVE-2013-3221 is only aboutinteraction with MySQL.http://twitter.com/dakull/statuses/326633931636084736 possiblysuggests that, but we're bringing this up mostly because of a commentthat someone else sent directly to MITRE.)Common patterns used in Ruby on Rails applications could allow anattacker to generate SQL that, when combined with some databaseserver's typecasting code, generates queries that match incorrect records.Note: This is a code and best-practise advisory, there is no patch toapply or updated version to install.Databases Affected: MySQL, SQLServer and some configurations of DB2Not affected: SQLite, PostgreSQL, OracleOutline- -------When comparing two values of differing types most databases willeither generate an error or return 'false'. Other databases willattempt to convert those values to a common type to enable comparison.For example in MySQL comparing a string with an integer will cast thestring into an integer. Given that any string which isn't an invalidinteger will convert to 0, this could allow an attacker to bypasscertain queries.If your application has XML or JSON parameter parsing enabled, anattacker will be able to generate queries like this unless you takecare to typecast your input values. For example:User.where(:login_token=>params[:token]).firstCould be made to generate the query:SELECT * FROM `users` WHERE `login_token` = 0 LIMIT 1;Which will match the first value which doesn't contain a validinteger. This vulnerability affects multiple programming languages,and multiple databases, be sure to audit your other applications tosee if they suffer the same issues.Work Arounds- ------------There are two options to avoid these problems. The first is todisable JSON and XML parameter parsing. Depending on the version ofrails you use you will have to place one of the following snippets inan application initializerRails 3.2, 3.1 and 3.0:ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::XML)ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::JSON)Rails 2.3:ActionController::Base.param_parsers.delete(Mime::XML)ActionController::Base.param_parsers.delete(Mime::JSON)If your application relies on accepting these formats you will have totake care to explicitly convert parameters to their intended types.For example:User.where(:login_token=>params[:token].to_s)Fixes- -----Unfortunately it is not possible for ActiveRecord to automaticallyprotect against all instances of this attack due to the API we expose.For example:User.where("login_token = ? AND expires_at > ?", params[:token],Time.now)Without parsing the SQL fragments it is not possible to determine whattype params[:token] should be cast to.Future releases of Rails will contain changes to mitigate the risk ofthis class of vulnerability, however as long as this feature is stillsupported this risk will remain.Credits- -------Thanks to joernchen of Phenoelit for reporting this to us and toJonathan Rudenberg for helping to review the advisory.- -- Cheers,KozReferences:http://twitter.com/dakull/statuses/326633931636084736http://seclists.org/oss-sec/2013/q2/170http://cve.mitre.org/cve/request_id.htmlSursa: Microsoft SQL Server and IBM DB2 data-type injection attacks - CXSecurity.com Quote