Php 动态站点安全风险
我听说我的网站或整个服务器很容易被黑客攻击,因为我的index.php 这是:Php 动态站点安全风险,php,security,dynamic,include,Php,Security,Dynamic,Include,我听说我的网站或整个服务器很容易被黑客攻击,因为我的index.php 这是: <?php include_once 'config.php'; include_once 'includes/mysqlconnect.php'; $url_slash=$_SERVER['REQUEST_URI']; $url= rtrim($url_slash, '/'); ?> <head> <meta charset="UTF-8"> <title
<?php
include_once 'config.php';
include_once 'includes/mysqlconnect.php';
$url_slash=$_SERVER['REQUEST_URI'];
$url= rtrim($url_slash, '/');
?>
<head>
<meta charset="UTF-8">
<title>Site Title</title>
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
</head>
<body class="body">
<div class="container-all">
<?php include_once 'includes/header.php';?>
<div class="container">
<?php include_once 'includes/navigationbar.php';?>
<?php include_once 'includes/rightsidebar.php';?>
<div class="content"><?php
if ($url==''){
include_once "sites/home.php";
}
elseif (file_exists("sites/$url.php") && is_readable("sites/$url.php")){
include_once "sites/$url.php";
}
else {
include_once 'sites/404.php';
}
?></div>
<?php include_once 'includes/footer.php';?>
</div>
</div>
</body>
网站名称
还有我的.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
重新启动发动机
重写基/
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]
如何防止人们键入example.com/../../../../../somefilepeoplemaynotsee.txt
我真的很想保持我的URL干净,所以没有关于example.com?page=example的内容
我只想通过在/var/www/html/sites/dir中创建一个文件来添加一个站点,然后添加一个页面。这不是它的工作原理,而是如前所述。这是一个很大的安全风险:/
我真的很想保持我的网站动态
任何想法使用
basename
,请参阅:
例如:
$url = rtrim($url_slash, '/');
$url = basename($url);
(注意:这是最简单的动态级别。使用类似MVC的已知路由可能更安全)为了防止本地文件包含漏洞,您必须先验证用户输入,然后再尝试将其用作要包含的文件名
if (!preg_match("/[^A-Za-z0-9]/", $url)){
// user input is valided to A-Za-z0-9
if(file_exists("sites/$url.php")){
include_once "sites/$url.php";
}
}
这假设您将只放置要包含在
站点
目录中的脚本。显然,如果你有其他代码没有被包括在内,那么你需要使用一个白名单来代替。所以如果我添加了$url=basename($url)代码>这将是固定的??你能发送一个示例url吗?不要发布指向易受攻击网站的链接,除非你想让人们试图入侵你的服务器。不,一个示例url。。。例如example.com/foo您正在使用用户提供的数据(REQUEST\u URI
)来执行include()
。这意味着有人可能会破坏对您服务器的请求,并可能包括您服务器上的任何文件。我知道这一点。这就是问题所在。你知道如何修复它吗?这如何保护他不被任何人访问系统任何部分的文件?你是说任何只有'A-Za-z0-9'的文件都是安全的?如果../../../etc/passwd不起作用呢./../etc/passwd
包含无法通过此验证的不可接受字符。Emm如果我想要一个像example.com/something/some-dir/some_file这样的url,(!preg_match(“/[^a-Za-z0-9-----]/”,$url))该怎么办
?@KaareZ要实现这一点,我建议拆分用户输入并分别验证每个目录名,最后分别验证文件名。