ThinkPHP5 SQL注入漏洞 && 敏感信息泄露——vulhub漏洞复现 008

前言

Vulhub是一个基于docker和docker-compose的漏洞环境集合,进入对应目录并执行一条语句即可启动一个全新的漏洞环境,让漏洞复现变得更加简单,让安全研究者更加专注于漏洞原理本身

Voulhub靶机平台环境搭建和使用:

https://blog.csdn.net/qq_41832837/article/details/103948358

目录:

漏洞原理

传入的某参数在绑定编译指令的时候又没有安全处理,预编译的时候导致SQL异常报错。然而thinkphp5默认开启debug模式,在漏洞环境下构造错误的SQL语法会泄漏数据库账户和密码。

漏洞详情

影响版本:ThinkPHP < 5.1.23

漏洞代码分析:

<?php
namespace app\index\controller;
use app\index\model\User;
class Index{
    public function index()
    {
        $ids = input('ids/a');
        $t = new User();
        $result = $t->where('id', 'in', $ids)->select();
}
}

由上述代码可知,这里用助手函数input定义了参数ids的类型是数组。接着去找where(‘id’, ‘in’, $ids)定义的内容,找到了最核心的方法buildWhere 和 parseWhereItem

protected function parseMhere ($where, $options)
{
	$whereStr =
	$this -> buildWhere($where, $options);
	if (! empty($options['soft_delete '])) {
		list ($field, $condition) = $options['soft_delete'];
		$binds = $this -> query ->getF ieldsBind($optlons);
		$whereStr = $whereStr ? '(' .$whereStr . ') AND ' : ' ';
		$whereStr = $whereStr. $this -> parseWhereIten($field, $condition, ' ', $options, $binds);
	}
	return empty($wherestr) ? ' ' : ' WHERE ' . $uhereStr;
}

接着找到定义’in’的位置

<?php
...
$bindName = $bindName ?: 'where_' . str_replace(['.', '-'], '_', $field);if (preg_match('/\W/', $bindName)) {
    // 处理带非单词字符的字段名
    $bindName = md5($bindName);}...} elseif (in_array($exp, ['NOT IN', 'IN'])) {
    // IN 查询
    if ($value instanceof \Closure) {
        $whereStr .= $key . ' ' . $exp . ' ' . $this->parseClosure($value);
    } else {
        $value = is_array($value) ? $value : explode(',', $value);
        if (array_key_exists($field, $binds)) {
            $bind  = [];
            $array = [];
            foreach ($value as $k => $v) {
                if ($this->query->isBind($bindName . '_in_' . $k)) {
                    $bindKey = $bindName . '_in_' . uniqid() . '_' . $k;
                } else {
                    $bindKey = $bindName . '_in_' . $k;
                }
                $bind[$bindKey] = [$v, $bindType];
                $array[]        = ':' . $bindKey;
            }
            $this->query->bind($bind);
            $zone = implode(',', $array);
        } else {
            $zone = implode(',', $this->parseValue($value, $field));
        }
        $whereStr .= $key . ' ' . $exp . ' (' . (empty($zone) ? "''" : $zone) . ')';
    }

这段代码当引入了in 或者 not in的时候遍历value的key和value。而key在绑定编译指令的时候又没有安全处理,所以导致了在预编译的时候SQL异常。

漏洞复现

1 进入漏洞环境
在这里插入图片描述2 通过构造url成功能访问
在这里插入图片描述3 构造payload:http://192.168.232.135/index.php?ids[0,updatexml(0,concat(0xa,user()),0)]=1
在这里插入图片描述暴露出了敏感信息
在这里插入图片描述

END

每天积累一点点,终究有一天爆发出来强大的力量。我是jammny,喜欢的点个赞!加个关注吧!持续更新vulhub漏洞复现系列。

发布了21 篇原创文章 · 获赞 57 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41832837/article/details/104066647