WordPress核心功能SQL注入漏洞分析
威脅響應(yīng)中心研究員對(duì)Wordpress核心功能SQL注入漏洞(編號(hào)為CVE-2015-5623和CVE-2015-2213)進(jìn)行了詳細(xì)的分析
0x00 漏洞概述
在twitter上看到Wordpress核心功能出現(xiàn)SQL注入漏洞,想學(xué)習(xí)下,就深入的跟了下代碼,結(jié)果發(fā)現(xiàn)老外留了好大的一個(gè)坑。雖然確實(shí)存在注入問(wèn)題,但是卻沒(méi)有像他blog中所說(shuō)的那樣可以通過(guò)訂閱者這樣的低權(quán)限來(lái)觸發(fā)SQL注入漏洞。
這個(gè)Wordpress漏洞系列的文章目前更新了兩個(gè)部分,一個(gè)是通過(guò)權(quán)限繞過(guò)實(shí)現(xiàn)訂閱者權(quán)限寫(xiě)一篇文章到回收站,另一個(gè)就是通過(guò)寫(xiě)入的這篇文章來(lái)實(shí)現(xiàn)SQL注入漏洞。這兩個(gè)漏洞的描述,TSRC的phithon寫(xiě)的文章其實(shí)已經(jīng)很清楚了,我這里從我分析的角度來(lái)介紹這兩個(gè)漏洞的形成、利用以及phithon省略掉的原文部分內(nèi)容。
0x01 越權(quán)提交文章
_wpnonce的獲取
在講越權(quán)漏洞之前,我們需要介紹一下Wordpress后臺(tái)的_wpnonce參數(shù),這個(gè)參數(shù)主要是用來(lái)防止CSRF攻擊的token。后臺(tái)大多數(shù)敏感功能都會(huì)通過(guò),當(dāng)前用戶信息、功能名稱、操作對(duì)象id等內(nèi)容生成token,所以我們很難在沒(méi)有token的時(shí)候進(jìn)行某些功能的使用。這個(gè)CSRF的防護(hù)機(jī)制間接地導(dǎo)致之后SQL注入很難再低權(quán)限情況下觸發(fā)(因?yàn)榭床坏絫oken),后面講SQL注入漏洞時(shí),我們會(huì)詳細(xì)的聊這個(gè)問(wèn)題有多坑。
之所以要把_wpnonce的獲取放在前面講,是因?yàn)椋覀冃枰粋€(gè)讓我們可以使用編輯提交文章功能的token。這個(gè)功能的token我們可以通過(guò)訪問(wèn)后臺(tái)post.php的post-quickdraft-save功能來(lái)獲取,嚴(yán)格的來(lái)說(shuō)這個(gè)獲取方式也算是一個(gè)信息泄露漏洞,官方也在新版本中進(jìn)行了修補(bǔ)。下面我我們來(lái)看下這個(gè)token泄露的原因。部分代碼如下:
case 'post-quickdraft-save': // Check nonce and capabilities $nonce = $_REQUEST['_wpnonce']; $error_msg = false; // For output of the quickdraft dashboard widget require_once ABSPATH . 'wp-admin/includes/dashboard.php'; if ( ! wp_verify_nonce( $nonce, 'add-post' ) ) $error_msg = __( 'Unable to submit this form, please refresh and try again.' ); if ( ! current_user_can( 'edit_posts' ) ) $error_msg = __( 'Oops, you don’t have Access to add new drafts.' ); if ( $error_msg ) return wp_dashboard_quick_press( $error_msg );
從上面代碼我們可以看出這個(gè)功能在發(fā)現(xiàn)出現(xiàn)錯(cuò)誤時(shí),會(huì)將錯(cuò)誤信息通過(guò)wp_dashboard_quick_press這個(gè)函數(shù)打印到頁(yè)面上,而這個(gè)函數(shù)生成的頁(yè)面的代碼中有這樣一行:
- PHP wp_nonce_field( 'add-post' ); 1 wp_nonce_field('add-post
生成了add-post功能的_wpnonce,也就是說(shuō),即使我們做了一些被禁止的操作,返回頁(yè)面中也會(huì)出現(xiàn)這個(gè)_wpnonce。
越權(quán)提交與條件競(jìng)爭(zhēng)
如phithon文章所說(shuō),由于GP使用邏輯混亂而導(dǎo)致的驗(yàn)證繞過(guò)。我們來(lái)看post.php文件在開(kāi)始檢驗(yàn)文章是否存在的代碼:
if ( isset( $_GET['post'] ) ) $post_id = $post_ID = (int) $_GET['post'];elseif ( isset( $_POST['post_ID'] ) ) $post_id = $post_ID = (int) $_POST['post_ID'];else $post_id = $post_ID = 0; global $post_type, $post_type_object, $post; if ( $post_id ) $post = get_post( $post_id ); if ( $post ) { $post_type = $post->post_type; $post_type_object = get_post_type_object( $post_type );}
可以看到在先提取GET中的post參數(shù)作為文章id來(lái)提取文章信息,如果沒(méi)有GET的post參數(shù),再去用POST的post_ID參數(shù)來(lái)提取。而真正檢驗(yàn)用戶是否對(duì)文章具有編輯權(quán)限,則是在edit_post中進(jìn)行的,而這里的判斷參數(shù)是從POST中提取的:
function edit_post( $post_data = null ) { global $wpdb; if ( empty($post_data) ) $post_data = &$_POST; // Clear out any data in internal vars. unset( $post_data['filter'] ); $post_ID = (int) $post_data['post_ID']; $post = get_post( $post_ID ); $post_data['post_type'] = $post->post_type; $post_data['post_mime_type'] = $post->post_mime_type; if ( ! empty( $post_data['post_status'] ) ) { $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); if ( 'inherit' == $post_data['post_status'] ) { unset( $post_data['post_status'] ); } } $ptype = get_post_type_object($post_data['post_type']); if ( !current_user_can( 'edit_post', $post_ID ) ) { if ( 'page' == $post_data['post_type'] ) wp_die( __('You are not allowed to edit this page.' )); else wp_die( __('You are not allowed to edit this post.' )); }
我們繼續(xù)看這段代碼最后的if判斷,判斷當(dāng)前用戶是否有編輯這篇文章的權(quán)限。這個(gè)判斷最終的操作是在map_meta_cap這個(gè)函數(shù)中進(jìn)行的:
case 'edit_post': case 'edit_page': $post = get_post( $args[0] ); if ( empty( $post ) ) break; if ( 'revision' == $post->post_type ) { $post = get_post( $post->post_parent ); }
可以看出如果文章是不存在的,那么就會(huì)break出switch,在函數(shù)結(jié)束時(shí)返回$caps變量,而$caps在函數(shù)開(kāi)始的時(shí)候已經(jīng)被定義為一個(gè)空的數(shù)組了,也就是說(shuō),這里會(huì)返回一個(gè)空數(shù)組。下面我們來(lái)向前走,返回到調(diào)用map_meta_cap的has_cap函數(shù)中,看看后續(xù)的操作
public function has_cap( $cap ) { if ( is_numeric( $cap ) ) { _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') ); $cap = $this->translate_level_to_cap( $cap ); } $args = array_slice( func_get_args(), 1 ); $args = array_merge( array( $cap, $this->ID ), $args ); $caps = call_user_func_array( 'map_meta_cap', $args ); // Multisite super admin has all caps by definition, Unless specifically denied. if ( is_multisite() && is_super_admin( $this->ID ) ) { if ( in_array('do_not_allow', $caps) ) return false; return true; } $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); $capabilities['exist'] = true; // Everyone is allowed to exist foreach ( (array) $caps as $cap ) { if ( empty( $capabilities[ $cap ] ) ) return false; } return true; }
看最后那個(gè)foreach,它檢測(cè)$caps中的各個(gè)元素在$capabilities中是否有不存在的,如果有那么就return false,但是$caps是個(gè)空數(shù)組,這樣很容易我們就獲得了一個(gè)true,權(quán)限校驗(yàn)成功繞過(guò)。那么這樣我們可以得到的一個(gè)信息就是,我們可以通過(guò)這個(gè)缺陷去嘗試update一個(gè)并不存在的文章。
那么現(xiàn)在問(wèn)題來(lái)了,直接update一個(gè)不存在的文章是沒(méi)有意義的,因?yàn)閿?shù)據(jù)庫(kù)執(zhí)行SQL是肯定會(huì)報(bào)錯(cuò),我們要怎么才能成功創(chuàng)建一篇文章呢?
在校驗(yàn)權(quán)限之后,數(shù)據(jù)庫(kù)執(zhí)行操作之前,post.php中有這樣一段代碼:
if ( isset( $post_data['tax_input'] ) ) { foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) { // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary. if ( is_taxonomy_hierarchical( $taxonomy ) ) { continue; } if ( ! is_array( $terms ) ) { $comma = _x( ',', 'tag delimiter' ); if ( ',' !== $comma ) { $terms = str_replace( $comma, ',', $terms ); } $terms = explode( ',', trim( $terms, " /n/t/r/x0B," ) ); } $clean_terms = array(); foreach ( $terms as $term ) { // Empty terms are invalid input. if ( empty( $term ) ) { continue; } $_term = get_terms( $taxonomy, array( 'name' => $term, 'fields' => 'ids', 'hide_empty' => false, ) );
如果在POST的數(shù)據(jù)中存在名為tax_input的數(shù)組參數(shù),那么就對(duì)參數(shù)中的值使用逗號(hào)進(jìn)行切割,然后對(duì)分割出來(lái)的每個(gè)內(nèi)容進(jìn)行一次select查詢。那么這里我們可以想象一下,如果我們post_ID中填寫(xiě)的是對(duì)當(dāng)前最新文章ID+1的數(shù)值,并且在tax_input這個(gè)參數(shù)添加特別多的內(nèi)容,導(dǎo)致它不停地select查詢,我們是不是在這個(gè)停滯的時(shí)間當(dāng)中插入一篇文章(這篇文章的ID剛好是最新文章ID+1),那么后面的update是不是就有意義了?