2017-01-16 4 views
-3

내 PHP 코드 :이 mysql 쿼리는 어떻게 작동합니까?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Less-1 SqL Injection master Course by Hitesh Choudhary</title> 
     <link rel="stylesheet" href="../index.html_files/freemind2html.css" type="text/css"/> 
    </head> 
    <body> 
     <div style=" margin-top:70px;color:#FFF; font-size:23px; text-align:center"> 
      <h1><span class="style1">Welcome </span><font color="#FF0000">to SQL injection Master Course </font></h1> 
      <h1><span class="style2">Lesson-1</span></h1> 
      <h1><span class="style4">Hint : Error based string</span> <br> 
       <font size="3" color="#666666"> 
       <?php 
        //including the Mysql connect parameters. 
        include("../sql-connections/sql-connect.php"); 

        // take the variables 
        if(isset($_GET['id'])) 
        { 
         $id=$_GET['id']; 
         //logging the connection parameters to a file for analysis. 
         //$fp=fopen('result.txt','a'); 
         //fwrite($fp,'ID:'.$id."\n"); 
         //fclose($fp); 

         // connectivity 

         $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; 
         $result=mysql_query($sql); 
         $row = mysql_fetch_array($result); 

          if($row) 
          { 
           echo '<font color= "#0000ff">'; 
           echo 'Your Login name:'. $row['username']; 
           echo "<br>"; 
           echo 'Your Password:' .$row['password']; 
           echo "</font>"; 
          } 
          else 
          { 
           echo '<font color= "#900">'; 
           print_r(mysql_error()); 
           echo "</font>"; 
          } 
         } 
         else { echo "Please input the ID as parameter with numeric value";} 

       ?> 
       </font> 
      </h1> 
     </div> 
     <img border="0" src="img1.gif" alt="funny" width="200" height="200"> 
     <div class="botton_fix">For more please visit : <a href="http://www.hiteshchoudhary.com" target="_blank">www.hiteshchoudhary.com</a></div> 
     </br></br></br> 
     <center> 
     </center> 
    </body> 
</html> 

내가

localhost/example/Less-1/index.php?id=1 order by 100

결과는 사용자 이름입니다 다음 쿼리를 사용하여이 페이지를 주입 SQL하려고 : 일부 및 암호 : 일부

localhost/example/Less-1/index.php?id=1

결과는 사용자 이름 : 일부 및 암호 : 일부

$id=2,3,...도 확인했는데 정상적으로 작동합니다. 왜 그렇게 작동합니까? 나는 바로 오류가 발생해야합니까?

+0

과 같이 예외를 던져야합니다. 사용하지 않는 API mysql_ *을 사용하지 말고 myslqi_ * 또는 더 나은 PDO를 사용하는 것이 좋습니다. PHP v7에서 Mysql_ * API가 제거되었습니다. –

+0

작은 따옴표를 놓치셨습니까? ocalhost/example/Less-1/index.php? id = 1 '로 시도하십시오. DROP TABLE 사용자; # – Alex

답변

0

나는 당신이 당신의 DB를 연결하고 연결을 사용하게 될 것입니다. 그래서 코드에서 DB 연결을 썼습니다. 문제가 생기면 그것을 시도하고 알려주십시오. :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Less-1 SqL Injection master Course by Hitesh Choudhary</title> 
     <link rel="stylesheet" href="../index.html_files/freemind2html.css" type="text/css"/> 
    </head> 
    <body> 
     <div style=" margin-top:70px;color:#FFF; font-size:23px; text-align:center"> 
      <h1><span class="style1">Welcome </span><font color="#FF0000">to SQL injection Master Course </font></h1> 
      <h1><span class="style2">Lesson-1</span></h1> 
      <h1><span class="style4">Hint : Error based string</span> <br> 
       <font size="3" color="#666666"> 


       <?php 


       // include("../sql-connections/sql-connect.php"); 


       if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) { 
         echo 'Could not connect to mysql'; 
         exit; 
        } 

        if (!mysql_select_db('mysql_dbname', $link)) { 
         echo 'Could not select database'; 
         exit; 
        } 

        $sql = "SELECT * FROM users WHERE id='".$id."' LIMIT 0,1"; 
        $result = mysql_query($sql, $link); 

        if (!$result) { 
         echo "DB Error, could not query the database\n"; 
         echo 'MySQL Error: ' . mysql_error(); 
         exit; 
        } 

        while ($row = mysql_fetch_assoc($result)) { 
         echo '<font color= "#0000ff">'; 
           echo 'Your Login name:'. $row['username']; 
           echo "<br>"; 
           echo 'Your Password:' .$row['password']; 
           echo "</font>" 
        } 

        mysql_free_result($result); 




       ?> 
       </font> 
      </h1> 
     </div> 
     <img border="0" src="img1.gif" alt="funny" width="200" height="200"> 
     <div class="botton_fix">For more please visit : <a href="http://www.hiteshchoudhary.com" target="_blank">www.hiteshchoudhary.com</a></div> 
     </br></br></br> 
     <center> 
     </center> 
    </body> 
</html> 
+1

감사합니다. 효과가있다. –

0

이 경우 데이터베이스 쿼리로 오류가 발생하지 않습니다. 세션 id와 같지 않은 ID가

if ($_GET['id'] != $_SESSION['id']) { 
    throw \Excetption("You don't have access."); 
} 
관련 문제