是否可以在執行之前清理包含以確保它在服務器上存在?在使用白名單執行之前清理包含文件
我想避免攻擊者使用某種白名單來危害文件路徑,這可能嗎?
我有看起來像這樣:
require_once('../includes/front/header.php');
是否可以在執行之前清理包含以確保它在服務器上存在?在使用白名單執行之前清理包含文件
我想避免攻擊者使用某種白名單來危害文件路徑,這可能嗎?
我有看起來像這樣:
require_once('../includes/front/header.php');
路徑怎麼會受到損害? (除非你require_once
包含用戶輸入 - 避免這種情況!)
你可以只檢查文件是否存在使用file_exists
:
如。
if(file_exists('../includes/front/header.php')) {
require_once('../includes/front/headers.php');
}
如果你確實想要一個白名單,雖然你可以只創建允許路徑/文件名的array
,然後只用in_array
檢查它的有效性。
好吧,您說有沒有必要創建白名單,因爲沒有用戶輸入? – user1278496 2012-04-10 15:12:45
是的,這幾乎是:) – billyonecan 2012-04-10 15:17:58
好吧,你可以告訴在什麼情況下require_once包含用戶輸入? :■ – user1278496 2012-04-10 15:19:29
應儘可能用PHP和白名單中的file_exists功能:
$allowed_files = array('../includes/front/header.php','../includes/front/footer.php');
$include_file = <string with file/path>; //EG '../includes/front/header.php'
if (in_array($include_file,$allowed_files && file_exists($include_file)) {
require_once($include_file);
}
如果你的路徑是硬編碼到PHP腳本,不接受用戶輸入一個文件名,那麼你不應該消毒你自己的文件名/路徑
你可以寫你自己的包括可以驗證阿蓋伊列出「允許」路徑。
即使用上述,但將其包裝在一個函數中。
'require'的要點是,如果因爲文件是必需的,找不到它就會停止執行。如果你不想這樣做,只需使用'include'與'file_exists()'或'is_file()'結合使用。 – jeroen 2012-04-10 15:08:21