安全之sql注入攻擊

下面給您介紹下sql的漏洞(演示一個登陸的的頁面):
<html>
<head>
<title>Sql注入演示</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body >
<form action="validate.php" method="post">
  <fieldset >
    <legend>Sql注入演示</legend>
    <table>
      <tr>
        <td>用戶名:</td>
        <td><input type="text" name="username"></td>
      </tr>
      <tr>
        <td>密  碼:</td>
        <td><input type="text" name="password"></td>
      </tr>
      <tr>
        <td><input type="submit" value="提交"></td>
        <td><input type="reset" value="重置"></td>
      </tr>
    </table>
  </fieldset>
</form>
</body>
</html>
<html>
<head>
<title>登錄驗證</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<?php
       $conn=@mysql_connect("localhost",'root','') or die("數據庫連接失敗!");;
       mysql_select_db("injection",$conn) or die("您要選擇的數據庫不存在");
       # 接收表單傳過的數據
       $name=$_POST['username'];
       $pwd=$_POST['password'];
       $sql="select * from users where username='$name' and password='$pwd'";  
    (親們,這條sql語句就存在這嚴重的錯誤,下面有詳細的解決方案)
$query=mysql_query($sql); $arr=mysql_fetch_array($query); if(is_array($arr)){ header("Location:manager.php"); }else{ echo "您的用戶名或密碼輸入有誤,<a href=\"Login.php\">請重新登錄!</a>"; } ?> </body> </html>

# 演示sql注入常見問題

        首先說一下 ‘OR’ , 大家肯定都知道 ‘或者的意思’ , 那麼大家知道 ‘   OR '1' = '1'  ’ 是什麼意思嗎 ,接下來將演示SQL注入 , 先看一下登錄背後的一些sql語句吧 , 向下看 , 這是我編寫的一些登錄sql語句!

  
填好正確的用戶名(tarena)和密碼(admin)後,點擊提交,將會返回給我們“歡迎管理員”的界面。
  因爲根據我們提交的用戶名和密碼被合成到SQL查詢語句當中之後是這樣的:
      select * from users where username='tarena' and password=md5('admin')
  很明顯,用戶名和密碼都和我們之前給出的一樣,肯定能夠成功登陸。但是,如果我們輸入一個錯誤的用戶名或密碼呢?很明顯,肯定登入不了吧。恩,正常情況下是如此,但是對於有SQL注入漏洞的網站來說,只要構造個特殊的“字符串”,照樣能夠成功登錄。
  比如:在用戶名輸入框中輸入:’or 1=1#,密碼隨便輸入,這時候的合成後的SQL查詢語句爲:
      select * from users where username='' or 1=1#' and password=md5('')
  語義分析:“#”在mysql中是註釋符,這樣井號後面的內容將被mysql視爲註釋內容,這樣就不會去執行了,換句話說,以下的兩句sql語句等價:
      select * from users where username='' or 1=1#' and password=md5('')
  等價於
      select * from users where username='' or 1=1
  因爲1=1永遠都是成立的,即where子句總是爲真,將該sql進一步簡化之後,等價如下select語句:
      select * from users
  沒錯,該sql語句的作用是檢索users表中的所有字段
    果不其然,我們利用萬能語句(’or 1=1#)能夠登錄!看到了吧,一個經構造後的sql語句竟有如此可怕的破壞力,相信你看到這後,開始對sql注入有了一個理性的認識了吧~
    重點:一定要試試這個sql語句,(不知道 this is sql 結果的人 , 將來肯定喫大虧);
    ·  總結:如果sql語句後添加‘   OR '1' = '1'  ’  將不再比對密碼  , 因爲 ‘ 1 ’ 永遠爲真,所以不會再去比對你的密碼啦 , 這是的結果就是查到用戶名爲admin的單條用戶信息啦!!!
select * from users where username='admin' or 1=1#' and password=md5('admin123');   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章