Search code examples
phphtmlsqlsql-injection

sql syntax error while creating a demo for sql injection


I am trying to create a login page to demonstrate a smple sql injection attack. Below is the php code that receives input from the login.html:

<?php 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'sql-injection'); 
define('DB_USER','root'); 
define('DB_PASSWORD',''); 
$con=@mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); 
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error()); 
/* $ID = $_POST['user']; $Password = $_POST['pass']; */ 

function SignIn() 
{ 
session_start(); //starting the session for user profile page 

if(!empty($_POST['user']) and !empty($_POST['pass'])) //checking the username and password which is coming from Sign-In.html, are they empty or have some text
 {      

        $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]'") or die(mysql_error()); 

        $row = mysql_fetch_array($query) or die(mysql_error()); 

        if(!empty($row['userName']) AND !empty($row['pass'])) 
        { 
                if ($_POST['pass'] == $row['pass'])
                {
                    echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
                }
                else
                {
                    echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; 
                }

        } 
        else 
        {   
                    echo "The db is corrupt";
        } 
} 
else{
    echo "The username and password fields cant be empty";
}
} 
if(isset($_POST['submit'])) 
{ 

SignIn(); 

} 

?>

The db has one table username with columns

UsernameID userName   pass
1          mrboolnew  mrbool123

I am trying to enter this in the username column:

Y';UPDATE username SET userName='mrboolnew1' WHERE userName = 'mrboolnew';

but I get the below error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE username SET userName='mrboolnew1' WHERE userName = 'mrboolnew';'' at line 1

Can someone help me out with the injection query. Thanks in advance.


Solution

  • You have added protection for protecting against injection attacks, perhaps without even knowing in. mysql_query() sends only a single query to the database (see here). Semicolons are not allowed; nor are multiple queries.

    You'll have to try harder to inject your own code.