用户从Web服务器下载php文件?

时间:2011-03-12 13:46:51

标签: php

如果用户知道php文件的路径,用户是否可以下载该文件?是否有任何措施可以防止它发生?

3 个答案:

答案 0 :(得分:2)

只要您的网络服务器配置为解释PHP文件,就会对其进行解释 - 这意味着它们的输出,而不是原始内容,将被发送给用户。

当然,如果你有一个脚本将文件路径作为参数,并显示该文件的内容......你必须确保脚本是安全的,不显示PHP文件的内容; - )

答案 1 :(得分:1)

假设您的意思是“访客”,如果您没有<?php readfile($_GET['file']); ?>(或其他类似漏洞)等脚本,那么您就是安全的。

如果“用户”是可以将文件放在服务器上的人(例如,在共享托管环境中),则它取决于服务器。如果每个PHP文件都在同一个用户下运行,并且没有设置严格的open_basedir限制,那么读取其他用户文件变得非常容易。

即使PHP受到open_basedir限制的“保护”,CGI仍然可以绕过这些限制。因此,建议以其他人无法读取文件的方式设置文件权限:文件上的chmod 640,目录上的chmod 750

答案 2 :(得分:0)

他们将能够在您的网站上看到文件,这是我制作的一个脚本,允许用户浏览网站上的文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site</title>
<style type="text/css">
/*CSS Document made by Glenn Dayton 8/14/11*/
body{
 background-color:#999;
}
</style>
</head>

<body>
<h1>.</h1>
<form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">
<label style="color:#FFF;">Area<input type="text" name="t" /></label>
<input type="submit" name="submit" value="S"/>
<label style="color:#FFF;">Text Area<input type="text" name="view" /></label>
<input type="submit" name="submit" value="V"/>
</form>
<?php
echo getcwd();
chdir('C:');
echo "<br />";
$s = scandir(getcwd().$_POST['t']);
echo "<b style=\"color:blue;\">".getcwd().$_POST['t']."</b><br />";
print_r($s);
$filename = getcwd().$_POST['view'];
echo "<b style=\"color:yellow;\">".$filename."</b><br />";
$file = file_get_contents($filename, "r");
echo "<br /><hr />";
if($file){
$ex = stat($filename);
echo "<ul>";
for($i = 0;$i <= count($ex);$i++){
    echo "<li style=\"color:green;\">$ex[$i]</li>";
}
echo "</ul>";
echo $file;
}else{
echo "<br /><span style=\"color:red;\">Problem</span>";
}
fclose($file);
?>

   

这将允许您浏览网站C:驱动器,如果服务器配置不当,它可以修改为基于Linux的服务器。用户是否可以修改文件取决于文件权限,ls -l您的文件并查看权限。您可以禁止执行PHP函数。