### 漏洞类型:
文件上传漏洞
### 漏洞概述:
Work the flow是一个基于HTML5的Wordpress插件,它主要向用户提供文件上传功能。但是由于插件开发作者对上传文件的检查使用用户可控的参数导致插件存在文件上传漏洞。
### 漏洞的验证方法:(需登录)
### 1、 我们修改一篇文章,在文章中添加如下代码:
```
[wtf_fu_upload deny_public_uploads="1" wtf_upload_dir="wtf-fu_files" wtf_upload_subdir="default" accept_file_types="jpg|jpeg|mpg|mp3|png|gif|wav|ogg" inline_file_types="jpg|jpeg|mpg|mp3|png|gif|wav|ogg" image_file_types="gif|jpg|jpeg|png" max_file_size="5" max_number_of_files="30" auto_orient="1" create_medium_images="0" medium_width="800" medium_height="600" thumbnail_crop="1" thumbnail_width="80" thumbnail_height="80"]
```
这段代码是调用work-the-flow-file-upload插件的代码。
### 2、 然后回到前台会看到如下界面

点击”Add My files…”选择一个PHP文件,设置浏览器代理并开启burpsuite。点击“Start upload”
### 3、 修改数据包
将数据包中accept_file_types参数中的一个改为php


点击“forward”提交。
### 4、 访问WebShell(假设上传的文件名为test.php)
{网站地址}\wp-content\uploads\1\wtf-fu_files\default\test.php
## 漏洞分析:
下面我们看看漏洞形成的原因:漏洞存在于插件“work-the-flow-file-upload\public\includes”目录下UploadHandler.php文件中的UploadHandler类。
### 1、 wp-admin/admin-ajax.php
由wp-admin/admin-ajax.php调用插件,首先是到目录“work-the-flow-file-upload\public\includes”下的class-wtf-fu-fileupload-shortcode.php文件的wtf_fu_load_ajax_function()函数。代码如下:
```
public static function wtf_fu_load_ajax_function() {
log_me(array("wtf_fu_load_ajax_function REQUEST=" => $_REQUEST));
// Get the option defaults.
$options = Wtf_Fu_Options::get_upload_options();
// Overwrite defaults with options set by the request.
foreach (array_keys($options) as $k) {
if (isset($_REQUEST[$k])) {
$options[$k] = $_REQUEST[$k];
}
}
// put in a fornat suitable for the UploadHandler.
$options = self::massageUploadHandlerOptions($options);
// Include the upload handler.
require_once(wtf_fu_JQUERY_FILE_UPLOAD_HANDLER_FILE);
error_reporting(E_ALL | E_STRICT);
$upload_handler = new UploadHandler($options);
die(); // always exit after an ajax call.
}
```
通过以上代码我们可以看到,$options[accept_file_types]的值已经被设置为
/\.(jpg|jpeg|mpg|mp3|png|gif|wav|php)$/i
接下来通过$upload_handler = new UploadHandler($options);来调用同目录下UploadHandler.php文件的UploadHandler类。
### 2、 UploadHandler类的构造函数
构造函数代码如下:
```
function __construct($options = null, $initialize = true, $error_messages = null) {
$this->options = array(
// these three should always be overwritten,
// default to something safe though just in case values
// dont get through to the handler.
'script_url' => $this->get_full_url() .'/',
'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',
//
'user_dirs' => false,
'mkdir_mode' => 0755,
'param_name' => 'files',
// Set the following option to 'POST', if your server does not support
// DELETE requests. This is a parameter sent to the client:
'delete_type' => 'DELETE',
'access_control_allow_origin' => '*',
'access_control_allow_credentials' => false,
'access_control_allow_methods' => array(
'OPTIONS',
'HEAD',
'GET',
'POST',
'PUT',
'PATCH',
'DELETE'
),
'access_control_allow_headers' => array(
'Content-Type',
'Content-Range',
'Content-Disposition'
),
// Enable to provide file downloads via GET requests to the PHP script:
// 1. Set to 1 to download files via readfile method through PHP
// 2. Set to 2 to send a X-Sendfile header for lighttpd/Apache
// 3. Set to 3 to send a X-Accel-Redirect header for nginx
// If set to 2 or 3, adjust the upload_url option to the base path of
// the redirect parameter, e.g. '/files/'.
'download_via_php' => false,
// Read files in chunks to avoid memory limits when download_via_php
// is enabled, set to 0 to disable chunked reading of files:
'readfile_chunk_size' => 10 * 1024 * 1024, // 10 MiB
// Defines which files can be displayed inline when downloaded:
'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
// Defines which files (based on their names) are accepted for upload:
'accept_file_types' => '/.+$/i',
// The php.ini settings upload_max_filesize and post_max_size
// take precedence over the following max_file_size setting:
'max_file_size' => null,
'min_file_size' => 1,
// The maximum number of files for the upload directory:
'max_number_of_files' => null,
// Defines which files are handled as image files:
'image_file_types' => '/\.(gif|jpe?g|png)$/i',
// Image resolution restrictions:
'max_width' => null,
'max_height' => null,
'min_width' => 1,
'min_height' => 1,
// Set the following option to false to enable resumable uploads:
'discard_aborted_uploads' => true,
// Set to 0 to use the GD library to scale and orient images,
// set to 1 to use imagick (if installed, falls back to GD),
// set to 2 to use the ImageMagick convert binary directly:
'image_library' => 1,
// Uncomment the following to define an array of resource limits
// for imagick:
/*
'imagick_resource_limits' => array(
imagick::RESOURCETYPE_MAP => 32,
imagick::RESOURCETYPE_MEMORY => 32
),
*/
// Command or path for to the ImageMagick convert binary:
'convert_bin' => 'convert',
// Uncomment the following to add parameters in front of each
// ImageMagick convert call (the limit constraints seem only
// to have an effect if put in front):
/*
'convert_params' => '-limit memory 32MiB -limit map 32MiB',
*/
// Command or path for to the ImageMagick identify binary:
'identify_bin' => 'identify',
'image_versions' => array(
// The empty image version key defines options for the original image:
'' => array(
// Automatically rotate images based on EXIF meta data:
'auto_orient' => true
),
// Uncomment the following to create medium sized images:
/*
'medium' => array(
'max_width' => 800,
'max_height' => 600
),
*/
'thumbnail' => array(
// Uncomment the following to use a defined directory for the thumbnails
// instead of a subdirectory based on the version identifier.
// Make sure that this directory doesn't allow execution of files if you
// don't pose any restrictions on the type of uploaded files, e.g. by
// copying the .htaccess file from the files directory for Apache:
//
//'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/thumb/',
//'upload_url' => $this->get_full_url().'/thumb/',
//
// Uncomment the following to force the max
// dimensions and e.g. create square thumbnails:
'crop' => true,
'max_width' => 80,
'max_height' => 80
)
)
);
if ($options) {
$this->options = $options + $this->options;
}
//error_log("in the uploadhandler constructor options =" . print_r($this->options, true));
if ($error_messages) {
$this->error_messages = $error_messages + $this->error_messages;
}
if ($initialize) {
$this->initialize();
}
}
```
构造函数直接使用了传入的$options[accept_file_types]值,接着调用initialize函数;
### 3、initialize函数
```
protected function initialize() {
switch ($this->get_server_var('REQUEST_METHOD')) {
case 'OPTIONS':
case 'HEAD':
$this->head();
break;
case 'GET':
$this->get();
break;
case 'PATCH':
case 'PUT':
case 'POST':
$this->post();
break;
case 'DELETE':
$this->delete();
break;
default:
$this->header('HTTP/1.1 405 Method Not Allowed');
}
}
```
数据使用post方式发送,调用post函数。
### 4、post函数
```
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = $this->get_server_var('HTTP_CONTENT_DISPOSITION') ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$this->get_server_var('HTTP_CONTENT_DISPOSITION')
)) : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
$size = $content_range ? $content_range[3] : null;
$files = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) {
$files[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
$file_name ? $file_name : $upload['name'][$index],
$size ? $size : $upload['size'][$index],
$upload['type'][$index],
$upload['error'][$index],
$index,
$content_range
);
}
} else {
// param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array:
$files[] = $this->handle_file_upload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
$file_name ? $file_name : (isset($upload['name']) ?
$upload['name'] : null),
$size ? $size : (isset($upload['size']) ?
$upload['size'] : $this->get_server_var('CONTENT_LENGTH')),
isset($upload['type']) ?
$upload['type'] : $this->get_server_var('CONTENT_TYPE'),
isset($upload['error']) ? $upload['error'] : null,
null,
$content_range
);
}
return $this->generate_response(
array($this->options['param_name'] => $files),
$print_response
);
}
```
post函数未做任何的校验直接调用handle_file_upload函数。
### 5、handle_file_upload函数
```
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
$index = null, $content_range = null) {
$file = new stdClass();
$file->name = $this->get_file_name($name, $type, $index, $content_range);
$file->size = $this->fix_integer_overflow(intval($size));
$file->type = $type;
if ($this->validate($uploaded_file, $file, $error, $index)) {
$this->handle_form_data($file, $index);
$upload_dir = $this->get_upload_path();
if (!is_dir($upload_dir)) {
error_log("creating file upload dir $upload_dir");
mkdir($upload_dir, $this->options['mkdir_mode'], true);
}
$file_path = $this->get_upload_path($file->name);
$append_file = $content_range && is_file($file_path) &&
$file->size > $this->get_file_size($file_path);
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
// multipart/formdata uploads (POST method uploads)
if ($append_file) {
file_put_contents(
$file_path,
fopen($uploaded_file, 'r'),
FILE_APPEND
);
} else {
move_uploaded_file($uploaded_file, $file_path);
}
} else {
// Non-multipart uploads (PUT method support)
file_put_contents(
$file_path,
fopen('php://input', 'r'),
$append_file ? FILE_APPEND : 0
);
}
$file_size = $this->get_file_size($file_path, $append_file);
if ($file_size === $file->size) {
$file->url = $this->get_download_url($file->name);
if ($this->is_valid_image_file($file_path)) {
$this->handle_image_file($file_path, $file);
}
} else {
$file->size = $file_size;
if (!$content_range && $this->options['discard_aborted_uploads']) {
unlink($file_path);
$file->error = 'abort';
}
}
$this->set_additional_file_properties($file);
}
return $file;
}
```
在handle_file_upload函数里调用validate函数对文件的后缀进行校验
### 6、 validate函数
```
protected function validate($uploaded_file, $file, $error, $index) {
error_log('validate($uploaded_file, $file, $error)=' . print_r(array($uploaded_file, $file, $error, $index) , true));
if ($error) {
$file->error = $this->get_error_message($error);
return false;
}
$content_length = $this->fix_integer_overflow(intval(
$this->get_server_var('CONTENT_LENGTH')
));
$post_max_size = $this->get_config_bytes(ini_get('post_max_size'));
error_log("post_max_size=$post_max_size , content_length= $content_length");
if ($post_max_size && ($content_length > $post_max_size)) {
$file->error = $this->get_error_message('post_max_size');
return false;
}
if (!preg_match($this->options['accept_file_types'], $file->name)) {
$file->error = $this->get_error_message('accept_file_types');
return false;
}
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
$file_size = $this->get_file_size($uploaded_file);
} else {
$file_size = $content_length;
}
if ($this->options['max_file_size'] && (
$file_size > $this->options['max_file_size'] ||
$file->size > $this->options['max_file_size'])
) {
$file->error = $this->get_error_message('max_file_size');
return false;
}
if ($this->options['min_file_size'] &&
$file_size < $this->options['min_file_size']) {
$file->error = $this->get_error_message('min_file_size');
return false;
}
if (is_int($this->options['max_number_of_files']) && (
$this->count_file_objects() >= $this->options['max_number_of_files'])
) {
$file->error = $this->get_error_message('max_number_of_files');
return false;
}
$max_width = @$this->options['max_width'];
$max_height = @$this->options['max_height'];
$min_width = @$this->options['min_width'];
$min_height = @$this->options['min_height'];
if (($max_width || $max_height || $min_width || $min_height)) {
list($img_width, $img_height) = $this->get_image_size($uploaded_file);
}
if (!empty($img_width)) {
if ($max_width && $img_width > $max_width) {
$file->error = $this->get_error_message('max_width');
return false;
}
if ($max_height && $img_height > $max_height) {
$file->error = $this->get_error_message('max_height');
return false;
}
if ($min_width && $img_width < $min_width) {
$file->error = $this->get_error_message('min_width');
return false;
}
if ($min_height && $img_height < $min_height) {
$file->error = $this->get_error_message('min_height');
return false;
}
}
return true;
}
```
其中对文件名的校验为如下代码:
```
if (!preg_match($this->options['accept_file_types'], $file->name)) {
$file->error = $this->get_error_message('accept_file_types');
return false;
}
```
这里直接使用了我们控制的$options[accept_file_types]值。
好了,下面就是将文件写入目录了。
### 总结
这个漏洞在之后的wordpress下无法利用成功(例如:wordpress-4.3.1-zh_CN),
因为在“wp-content\uploads”目录下存在如下内容的.htaccess文件:
```
# BEGIN wtf-fu modifications
<Files *>
SetHandler none
SetHandler default-handler
Options -ExecCGI -Indexes
RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo .asp .aspx
</Files>
# END wtf-fu modifications
```
暂无评论