// the PHP user has access to. If PHP has root access:
// removes a file from the hard drive that
// the PHP user has access to.
$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim
$homedir = "/home/$username";
$file_to_delete = basename("$userfile"); // strip paths
unlink ($homedir/$file_to_delete);
$fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion
$logstring = "$username $homedir $file_to_delete";
fputs ($fp, $logstring);
fclose($fp);
echo "$file_to_delete has been deleted!";
?>
尽管如此,该脚本还是有一些问题。如果您的授权系统允许用户建立他们自己的用户登录,而某用户选择登录“../etc/”,系统将被再次暴露。基于这一点,您可能更愿意编写一个更好的检验: 例子 15-4. 更安全的文件名检验
$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim
$homedir = "/home/$username";
if (!ereg('^[^./][^/]*$', $userfile))
die('bad filename'); //die, do not process
if (!ereg('^[^./][^/]*$', $username))
die('bad username'); //die, do not process
//etc...
?>
根据您的操作系统的不同,您需要考虑许多各种各样的文件,包括设备项目(/dev/ 或 COM1),设置文件(/etc/ 文件和 .ini 文件),通常的文件存储区(/home/,My Documents)等。基于这一点,建立一个策略禁止所有除了您明确的授权的行为可能要更简单一些