利用file_get_contents()

时间:2018-01-27 08:36:02

标签: php apache security php-7 exploit

是否可以在以下脚本中从服务器读取任何文件(不仅是扩展名为 .html 的文件)?

<?php
echo file_get_contents($_GET['display'].'.html');
?>

我知道包装器(php://file://等),但实现的并不多。

我渴望听到所有可能的攻击媒介。

PHP配置是默认的: allow_url_fopen On,我们假设版本为>= 7.0,因此空字符%00无效。

3 个答案:

答案 0 :(得分:1)

不,那只会读取以&#39; .html&#39;结尾的文件,但这并不一定意味着它是安全的!通常,您可以消毒和限制输入越多越好。

此外,对于计划使用file_get_contents这样的人来说,记住从file_get_contents提供服务时,您可以提供通常无法访问的文件,这一点总是很好服务器配置,例如.htaccess或文件权限。

答案 1 :(得分:0)

正如@David所说,这只会得到以'.html'结尾的文件,但这不是一个好习惯,如果你有html文件夹并且你希望用户只获取该文件夹中的文件,那么你不应该这样做,通过使用这种方法,黑客可以访问服务器中的任何.html文件,而不仅仅是您希望他看到的文件。

我建议如果您有一个特定的文件夹,希望用户能够从中获取文件,扫描目录并检查文件名。

这是一个例子:

<?php 

$paths = scandir('/html');

$file  = isset($_GET['display']) : $_GET['display'] ? null;

if(!$file) 
{
 die('no display provided');
}

$html = '';

foreach($paths as $path) {

   if($path !== '.' && $path !== '..' && $path === $file.'.html') {
     $html = file_get_contents($path);
   }

}


echo $html;

?>

答案 2 :(得分:0)

Exploidale作为代理:

  

http://example.com/script.php?display=https://hackme.com/passwords%3Extension%3D

echo file_get_contents("https://hackme.com/passwords?Extension=.html")

您的IP将登录hackme.com机器并返回一些密码(如果幸运的话)。