Chapter 1 Securing Your Server and Network(14):限制功能——xp_cmdshell 和OPENROWSET,openrowset

來源:互聯網
上載者:User

Chapter 1 Securing Your Server and Network(14):限制功能——xp_cmdshell 和OPENROWSET,openrowset
原文出處:http://blog.csdn.net/dba_huangzj/article/details/38656615,專題目錄:http://blog.csdn.net/dba_huangzj/article/details/37906349

未經作者同意,任何人不得以“原創”形式發布,也不得已用於商業用途,本人不負責任何法律責任。

        前一篇:http://blog.csdn.net/dba_huangzj/article/details/38489765

 

前言:

 

基於安全性原因,某些功能在安裝SQL Server時就被禁用,從2008開始,所有敏感選項可以通過一個叫【介面區配置器】的【方面】進行管理,這個功能在2005的時候以獨立工具的形式出現過,在2008又取消了。

 

實現:

 

1. 在SQL Server Management Studio(SSMS)中,右鍵【伺服器】節點,選擇【方面】:

 

2. 在【查看方面】對話方塊中,選擇【介面區配置器】:

 原文出處:http://blog.csdn.net/dba_huangzj/article/details/38656615

3. 把【AdHocRemoteQueriesEnabled】、【OleAutomationEnabled 】和【XPCmdShellEnabled 】的屬性設為False:

 

可以使用下面語句來查詢這些【方面】的資訊:

SELECT * FROM sys.system_components_surface_area_configuration WHERE component_name IN (     'Ole Automation Procedures',     'xp_cmdshell' );


 

除了外圍組態管理員,還可以使用【策略管理,PBM】來管理這些,將在第七章介紹。

 

4. 還可以使用T-SQL檢查狀態:

EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'Ad Hoc Distributed Queries'; EXEC sp_configure 'Ole Automation Procedures'; EXEC sp_configure 'xp_cmdshell';

原文出處:http://blog.csdn.net/dba_huangzj/article/details/38656615

 

5. 上面結果中,run_value為1 即啟用,0為禁用,如果需要禁用這些,可以使用下面語句,記得使用RECONFIGURE命令讓更改生效:

EXEC sp_configure 'Ad Hoc Distributed Queries', 0; EXEC sp_configure 'Ole Automation Procedures', 0; EXEC sp_configure 'xp_cmdshell', 0; RECONFIGURE;


 

原理:

 

Ad hoc分散式查詢允許在T-SQL語句內使用串連目標資料來源的字串,可以使用OPENROWSET/OPENDATASOURCE關鍵字,通過OLEDB訪問遠端資料庫,如下:

SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=SERVER2;Trusted_Connection=yes;', 'SELECT * FROM AdventureWorks.Person.Contact') AS a;


這種寫法的許可權基於授與類型,如果使用SQL Server身分識別驗證,那麼許可權是SQL Server服務的帳號許可權,如果是Windows 身分識別驗證,許可權是Windows帳號的許可權。

OLE自動化程式(OLE automation procedures)是系統預存程序,允許T-SQL代碼使用OLE Automation 物件,然後在SQL Server上下文外部運行,如sp_OACreate用於執行個體化對象並操作這個對象。下面代碼示範如何使用OLE自動化程式刪除檔案夾:

 

EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'Role Automation Procedures', 1; RECONFIGURE; GO DECLARE @FSO int, @OLEResult int; EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FSO OUTPUT; EXECUTE @OLEResult = sp_OAMethod @FSO, 'DeleteFolder', NULL, 'c:\ sqldata'; SELECT @OLEResult; EXECUTE @OLEResult = sp_OADestroy @FSO;


只有sysadmin伺服器角色的成員才能使用這些程式。

xp_cmdshell擴充預存程序允許使用T-SQL訪問底層作業系統,如:

exec xp_cmdshell 'DIR c\*.*';


 

限制這些程式的許可權,可以一定程度上保護伺服器的安全。

 

更多:

 原文出處:http://blog.csdn.net/dba_huangzj/article/details/38656615

為了允許非sysadmin登入使用xp_cmdshell,可以把它封裝到預存程序中並用EXECUTE AS 。如果你希望他們運行任意命令,必須定義一個代理帳號:

EXEC sp_xp_cmdshell_proxy_account 'DOMAIN\user','user password';


可用下面語句查詢:

SELECT * FROM sys.credentials WHERE name = '##xp_cmdshell_proxy_account##';


可用下面語句移除:

EXEC sp_xp_cmdshell_proxy_account NULL;


 

另外,你不能禁止sysadmin成員使用xp_cmdshell。即使禁用了,sysadmin角色成員還是可以啟用。


下一篇:http://blog.csdn.net/dba_huangzj/article/details/38657111




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.