色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

不要輕信 PHP_SELF的安全問題

復(fù)制代碼 代碼如下:
<html>
<body>
<?php
if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
echo "Form submitted!";
}
?>
<form action="<?php echo $_SERVER['php_SELF']; ?>">
<input type="hidden" name="submitted" value="1" />
<input type="submit" value="Submit!" />
</form>
</body>
</html>

看似準(zhǔn)確無誤的代碼,但是暗藏著危險。讓我們將其保存為 foo.php ,然后放到 php 環(huán)境中使用

foo.php/%22%3E%3Cscript%3Ealert('xss')%3C/script%3E%3Cfoo

訪問,會發(fā)現(xiàn)彈出個 Javascript 的 alert -- 這很明顯又是個 XSS 的注入漏洞。究其原因,發(fā)現(xiàn)是在

echo $_SERVER['php_SELF'];

這條語句上直接輸出了未過濾的值。追根數(shù)源,我們看下 php 手冊的描述

'php_SELF'

The filename of the currently executing script, relative to the document root.
For instance, $_SERVER['php_SELF'] in a script at the address
http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__
constant contains the full path and filename of the current (i.e. included) file.
If php is running as a command-line processor this variable contains the script
name since php 4.3.0. Previously it was not available.

原因很明確了,原來是 $_SERVER['php_SELF'] 雖然“看起來”是服務(wù)器提供的環(huán)境變量,但這的確和 $_POST 與 $_GET 一樣,是可以被用戶更改的。

其它類似的變量有很多,比如 $_COOKIE 等(如果用戶想“把玩”他們的 cookie,那我們也是沒有辦法)。解決方案很簡單,使用 strip_tags、htmlentities 等此類函數(shù)過濾或者轉(zhuǎn)義。

echo htmlentities($_SERVER['php_SELF']); 

-- Split --

上述的例子讓我們需要時刻保持謹(jǐn)慎 coding 的心態(tài)。Chris Shiflett 在他的 Blog 總結(jié)的相當(dāng)直白,防止 XSS 的兩個基本的安全思想就是

Filter input
Escape output

我將上面翻譯成 “過濾輸入,轉(zhuǎn)義輸出”。詳細(xì)的內(nèi)容,可以參考他 Blog 的這篇文章,此處略。

php技術(shù)不要輕信 PHP_SELF的安全問題,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 左贡县| 绿春县| 乌恰县| 涞水县| 邻水| 隆子县| 黔西县| 新晃| 阿合奇县| 那曲县| 周至县| 新乡县| 突泉县| 靖安县| 芦溪县| 双桥区| 合肥市| 沁水县| 乌拉特后旗| 三穗县| 哈巴河县| 天峨县| 白河县| 宜兰市| 威信县| 朔州市| 津南区| 银川市| 澄江县| 金沙县| 汝阳县| 台东市| 石狮市| 偏关县| 镇原县| 廊坊市| 西乡县| 宣化县| 土默特左旗| 西藏| 澳门|