Sea*_*ney 27 php security sanitization
问候,我希望我的小程序安全,以便潜在的恶意用户无法查看服务器上的敏感文件.
$path = "/home/gsmcms/public_html/central/app/webroot/{$_GET['file']}";
if(file_exists($path)) {
echo file_get_contents($path);
} else {
header('HTTP/1.1 404 Not Found');
}
Run Code Online (Sandbox Code Playgroud)
在我的脑海中,我知道像'../../../../../../etc/passwd'这样的输入会有麻烦,但想知道我应该期待什么其他有意义的输入以及如何防止他们.
Ber*_*amb 36
realpath()将允许您将任何可能包含相对信息的路径转换为绝对路径...然后您可以确保该路径位于您要允许下载的某个子目录下.
OP解决方案:
$baseDir = "/home/gsmcms/public_html/central/app/webroot/";
$path = realpath($baseDir . $_GET['file']);
// if baseDir isn't at the front 0==strpos, most likely hacking attempt
if(strpos($path, $baseDir) !== 0 || strpos($path, $baseDir) === false) {
die('Invalid Path');
} elseif(file_exists($path)) {
echo file_get_contents($path);
} else {
header('HTTP/1.1 404 Not Found');
echo "The requested file could not be found";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20431 次 |
| 最近记录: |