1. 程式人生 > >常見sql註入原理詳解!

常見sql註入原理詳解!

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:http://localhost/mysql/index.php?id=1


結果展示:

array (size=2) ‘article_id‘ => string ‘1‘ (length=1) ‘title‘ => string ‘思夢php編寫:PHP操作Redis詳解案例‘ (length=44)


(1)/當我們在在url上稍作修改時:

http://localhost/mysql/index.php?id=1‘ //加一個單引號

這樣你的sql語句就會報錯


(2)我們再次修改url的時候

http://localhost/mysql/index.php?id=-1 or 1=1 //後面參數修改成這樣


結果展示了所有的文章列表

D:\wamp\www\mysql\index.php:11:array (size=2) ‘article_id‘ => string ‘1‘ (length=1) ‘title‘ =>string ‘思夢php編寫:PHP操作Redis詳解案例‘ (length=44)

D:\wamp\www\mysql\index.php:11:array (size=2) ‘article_id‘ => string ‘2‘ (length=1) ‘title‘ =>string ‘Mysql存儲過程從0開始(上)‘ (length=36)

D:\wamp\www\mysql\index.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語句


技術分享


技術分享


本文出自 “13274080” 博客,請務必保留此出處http://13284080.blog.51cto.com/13274080/1982615

常見sql註入原理詳解!