我目前正在一个网站上工作,我想使用PHP动态网站加载。
现在我想到了这个..。
if (preg_match('/^[a-z0-9]+$/', isset($_GET['site']))) {
$general = realpath('includes/general/' . $_GET['site'] . '.php');
$laboratories = realpath('includes/laboratories/' . $_GET['site'] . '.php');
$validation = realpath('includes/validation/' . $_GET['site'] . '.php');
$training = realpath('includes/training/' . $_GET['site'] . '.php');
$reagents = realpath('includes/reagents/' . $_GET['site'] . '.php');
if ($general) {
include $general;
} else if ($laboratories) {
include $laboratories;
} else if ($validation) {
include $validation;
} else if ($training) {
include $training;
} else if ($reagents) {
include $reagents;
} else {
$default = realpath('includes/general/home.php');
include $default;
}
} else {
$default = realpath('includes/general/home.php');
include $default;
}
你觉得这个怎么样?安全吗?
发布于 2014-04-27 05:15:41
对于$_GET['site']
可以携带的每个值,只有一个文件可以包含,您可以将该映射存储在白名单中:
$include_path = '/path/to/includes';
$include_default = '/general/home.php';
$includes = [
'home' => 'general/home.php',
// ...
'lab-zero' => 'laboratories/lab-zero.php',
// ...
];
$include = $include_default;
if (isset($includes[$_GET['site']])) {
$include = $includes[$_GET['site']];
}
$path = $include_path . $include;
$real = realpath($path);
if ($real === $path && is_readable($real)) {
include $real;
}
然后只允许那些$_GET['site']
参数,这些参数是有意义的(那些已经制定了计划的参数,那些已经在$includes
数组中配置的参数)。
此外,如果白名单包含错误,则不包含该文件(realpath
和is_readable
检查)。
正如您还可以看到的,$_GET['site']
的值已被完全屏蔽,不受包含路径参数的影响。这只有在白名单中才有可能,这使得这种方法相当稳定。
最重要的是,这有效地防止了代码容易遭受的遍历攻击,因为检查中有漏洞,文件系统也很危险。
发布于 2014-04-28 04:32:57
为了让您的代码正常工作,我不得不做一些更改。
<?php
$include_path = 'includes/';
$include_default = 'general/home.php';
$includes = [
'home' => 'general/home.php',
'laboratories' => 'laboratories/laboratories.php',
'validation' => 'validation/validation.php',
'training' => 'training/training.php',
'reagents' => 'reagents/reagents.php',
];
$include = $include_default;
if (isset($_GET['site'])) {
$include = $includes[$_GET['site']];
}
$path = $include_path . $include;
$real = realpath($path);
if (is_readable($real)) {
include $real;
}
?>
这还是安全的吗
if ($real === $path && is_readable($real)) {
因为它在比较两种不同的东西。
https://stackoverflow.com/questions/23323297
复制