PHP 小心urldecode引發的SQL注入漏洞_php技巧

來源:互聯網
上載者:User
Ihipop 學校的 Discuz X1.5 論壇被黑,在那裡吵了一個下午。Google 一下“Discuz! X1-1.5 notify_credit.php Blind SQL injection exploit”,你就知道。

Discuz 是國內很流行的論壇系統,被黑的網站應該會很多吧。不過我對入侵別人的網站不感興趣,同時也鄙視那些代碼都不會寫只會使用別人放出的工具攻擊的所謂的“駭客”。


粗略看了一下代碼,這個 SQL 注入漏洞是 urldecode 函數造成的。在 PHP 手冊中,urldecode 函數下面有一個警告:

The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.

而 Discuz 的開發人員(估計是新手)畫蛇添足,多加了一個 urldecode:
複製代碼 代碼如下:

foreach($_POST as $k => $v) {
$value = urldecode($v);
$this->setParameter($k, $value);
}

單引號被 urlencode 兩次以後是 %2527,然後 POST,PHP 內部在產生全域變數 $_POST 的時候會先 urldecode,得到 %27,然後 PHP 會檢查 Magic Quotes 的設定,但是無論是否開啟 Magic Quotes,%27 都不會被 addslashes,因為這時根本沒有單引號。但是這時如果你在 PHP 代碼中畫蛇添足的加上 urldecode,%27就變成單引號了,然後……你懂的。

在我初學 PHP 的時候,看的是學校圖書館的一本爛書,裡面根本就沒寫 PHP 在處理表單的時候會自動 urldecode,所以自己用 urldecode 函數來解碼(依稀記得書上好像也是這麼寫的,真是誤人子弟啊)。

總結一下,就是:1、選擇一本好書非常重要;2、慎用 urldecode 函數。3、注意 PHP 手冊中的警告。
原文來自 http://demon.tw/programming/php-urldecode-sql-injection.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.