2016-09-15 60 views
-5

我必须创建一个可以注入SQL的应用程序,但是我不能让SQL注入成为可能。我尝试了所有的SQL注入,但我没有成功。可以编写像admin'#这样的用户名,因为这会注释掉该行。为什么不能SQL注入?

我希望你能帮助我。你可以在下面看到我的代码。

<?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> 
+1

这是托管在活的网站的某个地方? – RamRaider

+0

你的AND条件使它始终为假 –

+3

你试图注入它的是什么? – Phiter

回答

-9

可以使用mysql_real_escape_string功能,使一个变量安全:

$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']); 
+0

我的任务是让一个应用程序不安全,但我的代码不能sql注入:/ –

+0

你需要使用htmlspecialchars()或strip_tags ()。 1st将会转换特殊的字符,如<<,它会显示为<,但不会被执行。第二只是将所有标签剥离。 – Decoder

+0

检查此链接http://stackoverflow.com/questions/6857817/a-php-function-to-prevent-sql-injections-and-xss – Decoder

-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> 

您查询输出将是这一点,并始终为真,

SELECT * FROM wp_users WHERE user_login = '7' AND user_pass = '7' 
+0

Doenst工作:/给我错误的密码声明。 –

+0

你检查了链接吗? –

+0

为什么,我可以为了投票而选择吗? –