rootkit後門檢測工具rootkit和RKHunter

一、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編譯環境,執行下述三條命令:
[root@server ~]# yum -y install gcc
[root@server ~]# yum -y install gcc-c++
[root@server ~]# yum -y install make

2、安裝chkrootkit

爲了安全起見,建議直接從官方網站下載chkrootkit源碼,然後進行安裝,操作如下:
[root@server ~]# tar zxvf chkrootkit.tar.gz
[root@server ~]# cd chkrootkit-*
[root@server ~]# make sense
# 注意,上面的編譯命令爲make sense
[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
Usage: /usr/lib64/chkrootkit-0.49/chkrootkit [options] [test ...]
Options:
        -h                show this help and exit
        -V                show version information and exit
        -l                show available tests and exit
        -d                debug
        -q                quiet mode
        -x                expert mode
        -r dir            use dir as the root directory
        -p dir1:dir2:dirN path for the external commands used by chkrootkit
        -n                skip NFS mounted dirs


chkrootkit各個參數的含義如下所示。

-h:顯示幫助信息
-v:顯示版本信息
-l:顯示測試內容
-d:debug模式,顯示檢測過程的相關指令程序
-q:安靜模式,只顯示有問題的內容
-x:高級模式,顯示所有檢測結果
-r:dir設置指定的目錄爲根目錄
-p:dir1:dir2:dirN指定chkrootkit檢測時使用系統命令的目錄
-n:跳過NFS連接的目錄

chkrootkit的使用比較簡單,直接執行chkrootkit命令即可自動開始檢測系統。下面是某個系統的檢測結果:


[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進行檢測。這個過程可以通過下面的操作實現:

[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可以作的事情有:
1.MD5校驗測試,檢測文件是否有改動
2.檢測rootkit使用的二進制和系統工具文件
3.檢測特洛伊***程序的特徵碼
4.檢測常用程序的文件屬性是否異常
5.檢測系統相關的測試
6.檢測隱藏文件
7.檢測可疑的核心模塊LKM
8.檢測系統已啓動的監聽端口

下面詳細講述下RKHunter的安裝與使用。


1、安裝RKHunter


RKHunter的官方網頁地址爲:http://www.rootkit.nl/projects/rootkit_hunter.html,建議從這個網站下載RKHunter,這裏下載的版本是rkhunter-1.4.0.tar.gz。RKHunter的安裝非常簡單,過程如下:

[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
Usage: rkhunter {--check | --unlock | --update | --versioncheck |
                 --propupd [{filename | directory | package name},...] |
                 --list [{tests | {lang | languages} | rootkits | perl | propfiles}] |
                 --config-check | --version | --help} [options]
Current options are:
         --append-log                  Append to the logfile, do not overwrite
         --bindir <directory>...       Use the specified command directories
     -c, --check                       Check the local system
     -C, --config-check                Check the configuration file(s), then exit
  --cs2, --color-set2                  Use the second color set for output
         --configfile <file>           Use the specified configuration file
         --cronjob                     Run as a cron job
                                       (implies -c, --sk and --nocolors options)
         --dbdir <directory>           Use the specified database directory
         --debug                       Debug mode
                                       (Do not use unless asked to do so)
         --disable <test>[,<test>...]  Disable specific tests
                                       (Default is to disable no tests)
         --display-logfile             Display the logfile at the end
         --enable  <test>[,<test>...]  Enable specific tests
                                       (Default is to enable all tests)
         --hash {MD5 | SHA1 | SHA224 | SHA256 | SHA384 | SHA512 |
                 NONE | <command>}     Use the specified file hash function
                                       (Default is SHA1, then MD5)
     -h, --help                        Display this help menu, then exit
 --lang, --language <language>         Specify the language to use
                                       (Default is English)
         --list [tests | languages |   List the available test names, languages,
                 rootkits | perl |     rootkit names, perl module status
                 propfiles]            or file properties database, then exit
     -l, --logfile [file]              Write to a logfile
                                       (Default is /var/log/rkhunter.log)
         --noappend-log                Do not append to the logfile, overwrite it
         --nocf                        Do not use the configuration file entries
                                       for disabled tests (only valid with --disable)
         --nocolors                    Use black and white output
         --nolog                       Do not write to a logfile
--nomow, --no-mail-on-warning          Do not send a message if warnings occur
   --ns, --nosummary                   Do not show the summary of check results
 --novl, --no-verbose-logging          No verbose logging
         --pkgmgr {RPM | DPKG | BSD |  Use the specified package manager to obtain or
                   SOLARIS | NONE}     verify file property values. (Default is NONE)
         --propupd [file | directory | Update the entire file properties database,
                    package]...        or just for the specified entries
     -q, --quiet                       Quiet mode (no output at all)
  --rwo, --report-warnings-only        Show only warning messages
   --sk, --skip-keypress               Don't wait for a keypress after each test
         --summary                     Show the summary of system check results
                                       (This is the default)
         --syslog [facility.priority]  Log the check start and finish times to syslog
                                       (Default level is authpriv.notice)
         --tmpdir <directory>          Use the specified temporary directory
         --unlock                      Unlock (remove) the lock file
         --update                      Check for updates to database files
   --vl, --verbose-logging             Use verbose logging (on by default)
     -V, --version                     Display the version number, then exit
         --versioncheck                Check for latest version of program
     -x, --autox                       Automatically detect if X is in use
     -X, --no-autox                    Do not automatically detect if X is in use

Rkhunter常用參數以及含義如下所示。

-c, –check必選參數,表示檢測當前系統
–configfile <file>使用特定的配置文件
–cronjob作爲cron任務定期運行
–sk, –skip-keypress自動完成所有檢測,跳過鍵盤輸入
–summary顯示檢測結果的統計信息
–update檢測更新內容
-V, –version顯示版本信息
–versioncheck檢測最新版本


3.下面是通過rkhunter對某個系統的檢測示例:

[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分運行一次。


 


4.安全更新:


今天剛剛爆出Bash安全漏洞,SSH bash緊急安全補丁!重要!

測試是否存在漏洞,執行以下命令:
$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
 vulnerable
 this is a test
如果顯示如上,那麼,很遺憾,必須立即打上安全補丁修復,


臨時解決辦法爲:

yum -y update bash
升級bash後,執行測試:
$ 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
如果顯示如上,表示已經修補了漏洞。


5.解決思路:

    如果您的系統經過 rkhunter 的檢測之後,卻發現很多的『紅字』時,該怎麼辦?很簡單, 可以參考這個網頁提供的方法:

    http://www.rootkit.nl/articles/rootkit_hunter_faq.html

    基本上,官方網站與一般網管老手的建議都一樣,如果被 rootkit 之類的程序包***後 ( 也就是上一節的檢測表中的第二部分所***時 ),那麼最好最好直接重新安裝系統, 不要存在說可以移除 rootkit 或者***程序的幻想,因爲,『隱藏』本來就是 rootkit 與***程序的拿手好戲! 我們不知道到底這個 rootkit 或者***程序有多剽悍,爲了保險起見,還是重灌系統吧!如何重灌?簡單的說:

 

1.將原主機的網絡線拔除;
2.備份您的數據,最好備份成兩部分,一部份是全部的系統內容,越詳盡越好,包括 binary files 與 logfile 等等, 至於另一部份則可以考慮僅備份重要的數據文件即可!
3.將上個步驟的數據備份(僅重要數據部分!)進行整體的檢查,察看是否有怪異的數據存在(這部分可能會花去不少時間!)
4.重新安裝一部完整的系統,這包括:
o僅安裝需要的套件在服務器上面;
o先進行 簡單的防火牆 設定後才進行聯機;
o以 APT/YUM 之類的工具進行在線更新;
o執行類似 rkhunter/nessus 之類的軟件,檢驗系統是否處在較爲安全的狀態
5.將原本的重要數據移動至上個步驟安裝好的系統當中,並啓動原本服務器上面的各項服務;
6.以 rkhunter/nessus 之類的軟件檢驗系統是否處在較爲安全的環境,並且加強防火牆的機制!
7.最後,將原本完整備份的數據拿出來進行分析,尤其是 logfile 部分,試圖找出 cracker 是藉由那個服務?那個時間點? 以那個遠程 IP 聯機進入本機等等的信息,並針對該信息研擬預防的方法,並應用在已經運作的機器上。
    
參考文章:http://blog.jobbole.com/77663/
     http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21868571&id=159026



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章