-5

I have to make an application that can be SQL injected, but I can't make it possible to SQL inject. I tried everything to SQL inject it, but I didn't succeed. It's possible to write the username like admin'# because that comments out the line.

I hope you can help me. You see my code just underneath.

<?php
    include('include/config.php');
    include('parts/header.php');

    $submit = $_POST['submit'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($submit) {
        if(!empty($username OR !empty($password))) {
            $sqlQuery = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");

            if(mysql_num_rows($sqlQuery) == 1) {
                // Success - admin'#
                echo "LOGGEDIN";
                $_SESSION['loggedin'] = 1;
            }
            else {
                echo "Wrong password or username";
            }
        }
        else {
            echo "You didn't fill every field.";
        }
    }
?>

<div id="container">
    <form action="login.php" method="POST">
        <input type="text" name="username" placeholder="Type user name...">
        <input type="password" name="password" placeholder="Type password...">
        <input type="submit" name="submit" value="Log in">
    </form>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Steen Hansen
  • 555
  • 2
  • 6
  • 17

2 Answers2

-2
<?php
 include('include/config.php');
 include('parts/header.php');


$submit = $_POST['submit'];
$username = (int)$_POST['username'];
$password =(int)$_POST['password'];

if ($submit) {
    if(!empty($username OR !empty($password))) {
        $sqlQuery="SELECT * FROM users  WHERE user_login = '$username' AND password  = '$password'";

        //SELECT * FROM `wp_users` WHERE `user_login`='1' OR '1' = '1' AND `user_pass`='1' OR '1' = '1' 
        //$sqlQuery = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
        echo $sqlQuery;
        exit;
        if(mysql_num_rows($sqlQuery) == 1) {
            // Success - admin'#
            echo "LOGGEDIN";
            $_SESSION['loggedin'] = 1;
        } else {
            echo "Wrong password or username";
        }
    } else {
        echo "You didn't fill every field.";
    }
}
?>

<div id="container">
    <form method="POST">
        <input type="text" name="username" placeholder="Indtast brugernavn..">
        <input type="password" name="password" placeholder="Indtast kodeord..">
        <input type="submit" name="submit" value="Log ind">
    </form>
</div>

You Query output will be this and always true,

SELECT * FROM wp_users WHERE user_login = '7' AND user_pass = '7'
Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52
-9

You can use the mysql_real_escape_string function to make a variable secure:

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Decoder
  • 11
  • 2