PHP安全之文件系統安全——Null字符問題
由于 PHP 的文件系統操作是基于 C 語言的函數的,所以它可能會以您意想不到的方式處理 Null 字符。 Null字符在 C 語言中用于標識字符串結束,一個完整的字符串是從其開頭到遇見 Null 字符為止。 以下代碼演示了類似的攻擊:
Example #1 會被 Null 字符問題攻擊的代碼
<?php $file = $_GET[’file’]; // '../../etc/passwd0' if (file_exists(’/home/wwwrun/’.$file.’.php’)) {// 文件/home/wwwrun/../../etc/passwd存在的話那么file_exists方法會返回trueinclude ’/home/wwwrun/’.$file.’.php’;// the file /etc/passwd will be included }?>
因此,任何用于操作文件系統的字符串(特別是程序外部輸入的字符串)都必須經過適當的檢查。以下是上述例子的改進版本:
Example #2 驗證輸入的正確做法
<?php $file = $_GET[’file’]; // 對字符串進行白名單檢查 switch ($file) {case ’main’:case ’foo’:case ’bar’: include ’/home/wwwrun/include/’.$file.’.php’; break;default: include ’/home/wwwrun/include/main.php’; }?>
相關文章:
1. 基于android studio的layout的xml文件的創建方式2. 解決Android studio xml界面無法預覽問題3. 詳解Android studio 動態fragment的用法4. 圖文詳解vue中proto文件的函數調用5. 什么是python的自省6. Spring Boot和Thymeleaf整合結合JPA實現分頁效果(實例代碼)7. Android如何加載Base64編碼格式圖片8. 使用Android studio查看Kotlin的字節碼教程9. Vuex localStorage的具體使用10. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現

網公網安備