一、rootkit簡介
rootkit是Linux平臺下最常見的一種木馬後門工具,它主要通過替換系統檔案來達到入侵和和隱蔽的目的,這種木馬比普通木馬後門更加危險和隱蔽,普通的檢測工具和檢查手段很難發現這種木馬。rootkit攻擊能力極強,對系統的危害很大,它通過一套工具來建立後門和隱藏行跡,從而讓攻擊者保住許可權,以使它在任何時候都可以使用root許可權登入到系統。
rootkit主要有兩種型別:檔案級別和核心級別,下面分別進行簡單介紹。
1、檔案級別rootkit
檔案級別的rootkit一般是通過程式漏洞或者系統漏洞進入系統後,通過修改系統的重要檔案來達到隱藏自己的目的。在系統遭受rootkit攻擊後,合法的檔案被木馬程式替代,變成了外殼程式,而其內部是隱藏著的後門程式。通常容易被rootkit替換的系統程式有login、ls、ps、ifconfig、du、find、netstat等,其中login程式是最經常被替換的,因為當訪問Linux時,無論是通過本地登入還是遠端登入,/bin/login程式都會執行,系統將通過/bin/login來收集並核對使用者的賬號和密碼,而rootkit就是利用這個程式的特點,使用一個帶有根許可權後門密碼的/bin/login來替換系統的/bin/login,這樣攻擊者通過輸入設定好的密碼就能輕鬆進入系統。此時,即使系統管理員修改root密碼或者清除root密碼,攻擊者還是一樣能通過root使用者登入系統。攻擊者通常在進入Linux系統後,會進行一系列的攻擊動作,最常見的是安裝嗅探器收集本機或者網路中其他伺服器的重要資料。在預設情況下,Linux中也有一些系統檔案會監控這些工具動作,例如ifconfig命令,所以,攻擊者為了避免被發現,會想方設法替換其他系統檔案,常見的就是ls、ps、ifconfig、du、find、netstat等。如果這些檔案都被替換,那麼在系統層面就很難發現rootkit已經在系統中執行了。
這就是檔案級別的rootkit,對系統維護很大,目前最有效的防禦方法是定期對系統重要檔案的完整性進行檢查,如果發現檔案被修改或者被替換,那麼很可能系統已經遭受了rootkit入侵。檢查件完整性的工具很多,常見的有Tripwire、 aide等,可以通過這些工具定期檢查檔案系統的完整性,以檢測系統是否被rootkit入侵。
2、核心級別的rootkit
核心級rootkit是比檔案級rootkit更高階的一種入侵方式,它可以使攻擊者獲得對系統底層的完全控制權,此時攻擊者可以修改系統核心,進而截獲執行程式向核心提交的命令,並將其重定向到入侵者所選擇的程式並執行此程式,也就是說,當使用者要執行程式A時,被入侵者修改過的核心會假裝執行A程式,而實際上卻執行了程式B。
核心級rootkit主要依附在核心上,它並不對系統檔案做任何修改,因此一般的檢測工具很難檢測到它的存在,這樣一旦系統核心被植入rootkit,攻擊者就可以對系統為所欲為而不被發現。目前對於核心級的rootkit還沒有很好的防禦工具,因此,做好系統安全防範就非常重要,將系統維持在最小許可權內工作,只要攻擊者不能獲取root許可權,就無法在核心中植入rootkit。
二、rootkit後門檢測工具chkrootkit
chkrootkit是一個Linux系統下查詢並檢測rootkit後門的工具,它的官方址: http://www.chkrootkit.org/。 chkrootkit沒有包含在官方的CentOS源中,因此要採取手動編譯的方法來安裝,不過這種安裝方法也更加安全。下面簡單介紹下chkrootkit的安裝過程。
1.準備gcc編譯環境
對於CentOS系統,需要安裝gcc編譯環境,執行下述三條命令:
1 2 3 |
[root@server ~]# yum -y install gcc [root@server ~]# yum -y install gcc-c++ [root@server ~]# yum -y install make |
2、安裝chkrootkit
為了安全起見,建議直接從官方網站下載chkrootkit原始碼,然後進行安裝,操作如下:
1 2 3 |
[root@server ~]# tar zxvf chkrootkit.tar.gz [root@server ~]# cd chkrootkit-* [root@server ~]# make sense |
# 注意,上面的編譯命令為make sense
1 2 3 |
[root@server ~]# cd .. [root@server ~]# cp -r chkrootkit-* /usr/local/chkrootkit [root@server ~]# rm -rf chkrootkit-* |
3、使用chkrootkit
安裝完的chkrootkit程式位於/usr/local/chkrootkit目錄下,執行如下命令即可顯示chkrootkit的詳細用法:
[root@server chkrootkit]# /usr/local/chkrootkit/chkrootkit -h
chkrootkit各個引數的含義如下所示。
引數含義
-h顯示幫助資訊
-v顯示版本資訊
-l顯示測試內容
-ddebug模式,顯示檢測過程的相關指令程式
-q安靜模式,只顯示有問題的內容
-x高階模式,顯示所有檢測結果
-r dir設定指定的目錄為根目錄
-p dir1:dir2:dirN指定chkrootkit檢測時使用系統命令的目錄
-n跳過NFS連線的目錄
chkrootkit的使用比較簡單,直接執行chkrootkit命令即可自動開始檢測系統。下面是某個系統的檢測結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@server chkrootkit]# /usr/local/chkrootkit/chkrootkit Checking `ifconfig'... INFECTED Checking `ls'... INFECTED Checking `login'... INFECTED Checking `netstat'... INFECTED Checking `ps'... INFECTED Checking `top'... INFECTED Checking `sshd'... not infected Checking `syslogd'... not tested Checking `tar'... not infected Checking `tcpd'... not infected Checking `tcpdump'... not infected Checking `telnetd'... not found |
從輸出可以看出,此係統的ifconfig、ls、login、netstat、ps和top命令已經被感染。針對被感染rootkit的系統,最安全而有效的方法就是備份資料重新安裝系統。
4、chkrootkit的缺點
chkrootkit在檢查rootkit的過程中使用了部分系統命令,因此,如果伺服器被黑客入侵,那麼依賴的系統命令可能也已經被入侵者替換,此時chkrootkit的檢測結果將變得完全不可信。為了避免chkrootkit的這個問題,可以在伺服器對外開放前,事先將chkrootkit使用的系統命令進行備份,在需要的時候使用備份的原始系統命令讓chkrootkit對rootkit進行檢測。這個過程可以通過下面的操作實現:
1 2 3 4 5 6 |
[root@server ~]# mkdir /usr/share/.commands [root@server ~]# cp `which --skip-alias awk cut echo find egrep id head ls netstat ps strings sed uname` /usr/share/.commands [root@server ~]# /usr/local/chkrootkit/chkrootkit -p /usr/share/.commands/ [root@server share]# cd /usr/share/ [root@server share]# tar zcvf commands.tar.gz .commands [root@server share]# rm -rf commands.tar.gz |
上面這段操作是在/usr/share/下建立了一個.commands隱藏檔案,然後將chkrootkit使用的系統命令進行備份到這個目錄下。為了安全起見,可以將.commands目錄壓縮打包,然後下載到一個安全的地方進行備份,以後如果伺服器遭受入侵,就可以將這個備份上傳到伺服器任意路徑下,然後通過chkrootkit命令的“-p”引數指定這個路徑進行檢測即可。
三、rootkit後門檢測工具RKHunter
RKHunter是一款專業的檢測系統是否感染rootkit的工具,它通過執行一系列的指令碼來確認伺服器是否已經感染rootkit。在官方的資料中,RKHunter可以作的事情有:
MD5校驗測試,檢測檔案是否有改動
檢測rootkit使用的二進位制和系統工具檔案
檢測特洛伊木馬程式的特徵碼
檢測常用程式的檔案屬性是否異常
檢測系統相關的測試
檢測隱藏檔案
檢測可疑的核心模組LKM
檢測系統已啟動的監聽埠
下面詳細講述下RKHunter的安裝與使用。
1、安裝RKHunter
RKHunter的官方網頁地址為:http://www.rootkit.nl/projects/rootkit_hunter.html,建議從這個網站下載RKHunter,這裡下載的版本是rkhunter-1.4.0.tar.gz。RKHunter的安裝非常簡單,過程如下:
1 2 3 4 5 6 7 |
[root@server ~]# ls rkhunter-1.4.0.tar.gz [root@server ~]# pwd /root [root@server ~]# tar -zxvf rkhunter-1.4.0.tar.gz [root@server ~]# cd rkhunter-1.4.0 [root@server rkhunter-1.4.0]# ./installer.sh --layout default --install |
這裡採用RKHunter的預設安裝方式,rkhunter命令被安裝到了/usr/local/bin目錄下。
2、使用rkhunter指令
rkhunter命令的引數較多,但是使用非常簡單,直接執行rkhunter即可顯示此命令的用法。下面簡單介紹下rkhunter常用的幾個引數選項。
[root@server ~]#/usr/local/bin/rkhunter–help
Rkhunter常用引數以及含義如下所示。
引數 含義
-c, –check必選引數,表示檢測當前系統
–configfile <file>使用特定的配置檔案
–cronjob作為cron任務定期執行
–sk, –skip-keypress自動完成所有檢測,跳過鍵盤輸入
–summary顯示檢測結果的統計資訊
–update檢測更新內容
-V, –version顯示版本資訊
–versioncheck檢測最新版本
下面是通過rkhunter對某個系統的檢測示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
[root@server rkhunter-1.4.0]# /usr/local/bin/rkhunter -c [ Rootkit Hunter version 1.4.0 ] #下面是第一部分,先進行系統命令的檢查,主要是檢測系統的二進位制檔案,因為這些檔案最容易被rootkit攻擊。顯示OK字樣表示正常,顯示Warning表示有異常,需要引起注意,而顯示“Not found”字樣,一般無需理會 Checking system commands... Performing 'strings' command checks Checking 'strings' command [ OK ] Performing 'shared libraries' checks Checking for preloading variables [ None found ] Checking for preloaded libraries [ None found ] Checking LD_LIBRARY_PATH variable [ Not found ] Performing file properties checks Checking for prerequisites [ Warning ] /usr/local/bin/rkhunter [ OK ] /sbin/chkconfig [ OK ] ....(略).... [Press <ENTER> to continue] #下面是第二部分,主要檢測常見的rootkit程式,顯示“Not found”表示系統未感染此rootkit Checking for rootkits... Performing check of known rootkit files and directories 55808 Trojan - Variant A [ Not found ] ADM Worm [ Not found ] AjaKit Rootkit [ Not found ] Adore Rootkit [ Not found ] aPa Kit [ Not found ] Apache Worm [ Not found ] Ambient (ark) Rootkit [ Not found ] Balaur Rootkit [ Not found ] BeastKit Rootkit [ Not found ] beX2 Rootkit [ Not found ] BOBKit Rootkit [ Not found ] ....(略).... [Press <ENTER> to continue] #下面是第三部分,主要是一些特殊或附加的檢測,例如對rootkit檔案或目錄檢測、對惡意軟體檢測以及對指定的核心模組檢測 Performing additional rootkit checks Suckit Rookit additional checks [ OK ] Checking for possible rootkit files and directories [ None found ] Checking for possible rootkit strings [ None found ] Performing malware checks Checking running processes for suspicious files [ None found ] Checking for login backdoors [ None found ] Checking for suspicious directories [ None found ] Checking for sniffer log files [ None found ] Performing Linux specific checks Checking loaded kernel modules [ OK ] Checking kernel module names [ OK ] [Press <ENTER> to continue] #下面是第四部分,主要對網路、系統埠、系統啟動檔案、系統使用者和組配置、SSH配置、檔案系統等進行檢測 Checking the network... Performing checks on the network ports Checking for backdoor ports [ None found ] Performing checks on the network interfaces Checking for promiscuous interfaces [ None found ] Checking the local host... Performing system boot checks Checking for local host name [ Found ] Checking for system startup files [ Found ] Checking system startup files for malware [ None found ] Performing group and account checks Checking for passwd file [ Found ] Checking for root equivalent (UID 0) accounts [ None found ] Checking for passwordless accounts [ None found ] ....(略).... [Press <ENTER> to continue] #下面是第五部分,主要是對應用程式版本進行檢測 Checking application versions... Checking version of GnuPG[ OK ] Checking version of OpenSSL [ Warning ] Checking version of OpenSSH [ OK ] #下面是最後一部分,這個部分其實是上面輸出的一個總結,通過這個總結,可以大概瞭解伺服器目錄的安全狀態。 System checks summary ===================== File properties checks... Required commands check failed Files checked: 137 Suspect files: 4 Rootkit checks... Rootkits checked : 311 Possible rootkits: 0 Applications checks... Applications checked: 3 Suspect applications: 1 The system checks took: 6 minutes and 41 seconds |
在Linux終端使用rkhunter來檢測,最大的好處在於每項的檢測結果都有不同的顏色顯示,如果是綠色的表示沒有問題,如果是紅色的,那就要引起關注了。另外,在上面執行檢測的過程中,在每個部分檢測完成後,需要以Enter鍵來繼續。如果要讓程式自動執行,可以執行如下命令:
[root@server ~]# /usr/local/bin/rkhunter –check –skip-keypress
同時,如果想讓檢測程式每天定時執行,那麼可以在/etc/crontab中加入如下內容:
30 09 * * * root /usr/local/bin/rkhunter –check –cronjob
這樣,rkhunter檢測程式就會在每天的9:30分執行一次。
安全更新:
今天剛剛爆出Bash安全漏洞,SSH bash緊急安全補丁!重要!
測試是否存在漏洞,執行以下命令:
1 2 3 |
$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test" vulnerable this is a test |
如果顯示如上,那麼,很遺憾,必須立即打上安全補丁修復,
臨時解決辦法為:
1 |
yum -y update bash |
升級bash後,執行測試:
1 2 3 4 |
$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test" bash: warning: x: ignoring function definition attempt bash: error importing function definition for `x' this is a test |
如果顯示如上,表示已經修補了漏洞。