upload-labs 05 源码:
$is_upload = false;
$msg = null;
if (isset($_POST['submit'])) {
if (file_exists(UPLOAD_PATH)) {
$deny_ext = array(".php",".php5",".php4",".php3",".php2",".html",".htm",".phtml",".pht",".pHp",".pHp5",".pHp4",".pHp3",".pHp2",".Html",".Htm",".pHtml",".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jtml",".jSp",".jSpx",".jSpa",".jSw",".jSv",".jSpf",".jHtml",".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",".cer",".aSp",".aSpx",".aSa",".aSax",".aScx",".aShx",".aSmx",".cEr",".sWf",".swf",".htaccess");
$file_name = trim($_FILES['upload_file']['name']);
$file_name = deldot($file_name);
$file_ext = strrchr($file_name, '.');
$file_ext = strtolower($file_ext);
$file_ext = str_ireplace('::$DATA', '', $file_ext);
$file_ext = trim($file_ext);
if (!in_array($file_ext, $deny_ext)) {
$temp_file = $_FILES['upload_file']['tmp_name'];
$img_path = UPLOAD_PATH.'/'.$file_name;
if (move_uploaded_file($temp_file, $img_path)) {
$is_upload = true;
} else {
$msg = '上传出错!';
}
} else {
$msg = '此文件类型不允许上传!';
}
} else {
$msg = UPLOAD_PATH . '文件夹不存在,请手工创建!';
}
}
代码分析: 这关过滤了.htaccess,而没有过滤.user.ini,我们来了解一下.user.ini在文件上传中的作用。
php.ini 是php默认的配置文件,其中包括了很多php的配置,我们可以在 php.ini 配置PHP的基本功能,而.user.ini 实际上就是一个可以由用户"自定义"的php.ini .
auto_append_file 、 auto_prepend_file 比较有意思,官方文档是这样描述 的,它可以在PHP文件执行时,去包含一个指定的文件当做代码去执行,就相当于 require()。
那在 .user.ini 中写入这句话,是不是就相当于在其他PHP文件执行时会自动包含 demo.jpg ,也就是说在其他PHP文件执行的时候,demo.jpg中的内容会被当做代码 去执行。 值得注意的是在我们测试upload-labs-03时,.htaccess需要在线程安全下才可以解析特殊后 缀,而这里恰恰相反,想要包含demo.jpg需要在非线程安全版本中才可以包含成功, 所以我们要将phpstudy调到nts版本。
上传绕过: 首先上传包含上述内容的.user.ini内容文件,然后上传包含有一句话木马的demo.jpg 然后去访问/upload/readme.php,成功包含demo.jpg
|