close

1.

第一招:不用特殊字元也能駭

正常

$id='5'

$sql="select * from `users` where` id` = $id"

若injection就

$id='5 or 1=1'

就會成為

$sql="select * from `users` where` id` =5 or 1=1 "

當where 1=1 就會取得users表所有資料

1.1防的話

參數化查詢

mysql command

set @account := 'ab512';

set @ps := 'password';

select *from `users` where `account` = @account and `password` = @ps

 

使用 PDO:

$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
 
$stmt->execute(array(':name' => $name));
 
foreach ($stmt as $row) {
    // do something with $row
}

使用 mysqli

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);
 
$stmt->execute();
 
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // do something with $row
}
//该代码片段来自于: http://www.sharejs.com/codes/php/8230

mysqli支援,mysql lib 不支援

pdo支援

2.

第二招 特殊字元

$account="  ' or 1=1    "

$sql="select * from `users` where` account` = '$account'    "

會變成

select * from users where account='' or 1=1

就駭進去了

 

2.1防的話

加mysql_escape_string

就會變成

$es_account=mysql_escape_string($account)

$es_account="  \' or 1=1    "

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 學習程式 的頭像
    學習程式

    程式學習日記,如果我幫助了你請讓我知道

    學習程式 發表在 痞客邦 留言(0) 人氣()