常見sql注入原理詳解!
1、首先我們創建一個mysqli的鏈接
/**數據庫配置*/
$config = ['hostname'=>"localhost", 'port'=>"3306", 'username'=>"root",'password'=>'','db'=>'sql'];
/**接收參數*/
$id = $_GET['id']?$_GET['id']:"";
if(empty($id)){
echo "article is not def"
}
/**鏈接數據庫*/
$mysqli = new mysqli($config['hostname'],$config['username'],$config['password'],$config['db']);
/**設置編碼*/
$mysqli->set_charset("utf8");
url數字注入結果測試
我們訪問url:https://localhost/mysql/index.php?id=1
結果展示:
array (size=2) 'article_id' => string '1' (length=1) 'title' => string '思夢php編寫:PHP操作Redis詳解案例' (length=44)
(1)/當我們在在url上稍作修改時:
https://localhost/mysql/index.php?id=1‘ //加一個單引號
這樣你的sql語句就會報錯
(2)我們再次修改url的時候
https://localhost/mysql/index.php?id=-1 or 1=1 //後麵參數修改成這樣
結果展示了所有的文章列表
D:wampwwwmysqlindex.php:11:array (size=2) 'article_id' => string '1' (length=1) 'title' =>string '思夢php編寫:PHP操作Redis詳解案例' (length=44)
D:wampwwwmysqlindex.php:11:array (size=2) 'article_id' => string '2' (length=1) 'title' =>string 'Mysql存儲過程從0開始(上)' (length=36)
D:wampwwwmysqlindex.php:11:array (size=2) 'article_id' => string '3' (length=1) 'title' =>string '思夢php編寫:PHP排序的幾種方法' (length=42).............
2、表單注入,主要利用sql語句的注釋
$username = $_POST['username']?$_POST['username']:"";
$password = $_POST['password']?$_POST['password']:"";
$sql = "select * from tb_member where account='$username'AND password='$pass'";
$res = $mysqli->query($sql);
$row = $res->fetch_assoc();
if($row){
echo "登錄成功!";
}else{
echo "賬號密碼錯誤!";
}
正常輸入
打印:登錄成功!
我們簡單修改一下
打印:登錄成功!
原理:首先使用單引號結束sql語句,然後加#注釋後麵的sql語句
同理另一種方式為
打印:登錄成功!
原理:首先使用單引號結束sql語句,然後加(-- )注釋後麵的sql語句
最後更新:2017-11-19 20:03:52