常見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語句

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM