file_put_contens小trick

file_put_contents tricks

0x01 trick1

来自于P神的实例:php

<?php
$text = $_GET['text']; if(preg_match('[<>?]', $text)) { die('error!'); } file_put_contents('config.php', $text);

但愿getshell,可是限制了<>,没法写入PHP标准代码。html

来自于P神的小密圈分享:linux

          file_put_contents的第二个参数,能够是数组,而且会将数组转化成字符串写入。而且得益于PHP弱类型,在正则匹配前,传入的数组会被强制转化成字符串,也就是Array。所以能够绕过正则匹配,而且可以正常写入。shell

 

实例:windows

    

 

 

 

0x02 trick2

另外一个关于file_put_contents的小trick,当file_put_contents、copy、file_get_contents等读取写入操做与unlink、file_exists等删除判断文件函数之间对于路径处理的差别致使的删除绕过数组

<?php
$user=$_GET['user']; var_dump($user); echo $user['name']; $filename = __DIR__.'\\'.$user['name']; echo $filename; $data = $user['info']; file_put_contents($filename, $data); if(file_exists($filename)){ unlink($filename); }

p牛在小密圈说过相似问题app

查看php源码,其实咱们能发现,php读取、写入文件,都会调用php_stream_open_wrapper_ex来打开流,而判断文件存在、重命名、删除文件等操做则无需打开文件流。函数

咱们跟一跟php_stream_open_wrapper_ex就会发现,其实最后会使用tsrm_realpath函数来将filename给标准化成一个绝对路径。而文件删除等操做则不会,这就是两者的区别。学习

因此,若是咱们传入的是文件名中包含一个不存在的路径,写入的时候由于会处理掉“../”等相对路径,因此不会出错;判断、删除的时候由于不会处理,因此就会出现“No such file or directory”的错误。spa

因此,linux能够经过xxxxx/../test.php、test.php/. 来绕过删除

windows能够经过test.php:test test.ph<来绕过文件删除

拿windows实例:

http://127.0.0.1/l.php?user[name]=2.php:test&user[info]=2y 会生成2.php

http://127.0.0.1/l.php?user[name]=2.ph<&user[info]=2y 会写入内容

 

 

学习资料:http://www.am0s.com/functions/386.html

相关文章
相关标签/搜索