I'm doing a SQL Injection project for my security module in college, and I'm trying to learn how it works.
I can see how it works when a script doesn't filter input, and then loops over a DB result set, displaying the data on screen. But as far as I can tell, the following code is NOT susceptible to SQL injection, as it is only expecting to display a single set of values on the screen:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("testdb");
$result = mysql_query("SELECT id, name, description FROM test_table WHERE id = ".$_GET['id']);
list($id, $name, $description) = mysql_fetch_row($result);
echo "ID: $id \n";
echo "Name: $name \n";
echo "Description: $description \n";
?>
If I set the value of id to:
1 OR 1 = 1 UNION SELECT id, username, password FROM users LIMIT 1, 1 --
The values from the UNION part of the query are not displayed, unless I run the mysql_fetch_row($result) statement twice, like so:
<?php
$result = mysql_query("SELECT id, name, description FROM test_table WHERE id = ".$_GET['id']);
list($id, $name, $description) = mysql_fetch_row($result);
echo "ID: $id \n";
echo "Name: $name \n";
echo "Description: $description \n";
list($id, $name, $description) = mysql_fetch_row($result);
echo "ID: $id \n";
echo "Name: $name \n";
echo "Description: $description \n";
?>
Only then are the values from the UNION part of the statement displayed, (i.e. username, password).
If anyone knows a thing or two about this, can you confirm that I am correct in saying that the above code is NOT susceptible to SQL injection, as it is only expecting to display a single set of values on the screen.
Please correct me if I'm wrong.
Thanks for your help.