Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

CentOS 7 安全加固:Rootkit 偵測、完整性稽核與入侵檢測

RKHunter:偵測 Rootkit

專案首頁:https://rkhunter.sourceforge.net/ ,Rootkit 原理可參考鳥哥的 https://linux.vbird.org/linux_security/0420rkhunter.php

安裝:

yum --enablerepo=epel -y install rkhunter

配置與使用。常規檢查腳本位於 cron.daily,由 cron 每日執行:

vi /etc/sysconfig/rkhunter
MAILTO=root@localhost   # 報告收件者
DIAG_SCAN=no            # 設 yes 可做更細緻的掃描

rkhunter --update       # 更新特徵資料庫
rkhunter --propupd      # 更新系統檔案屬性基線
rkhunter --check --sk   # 執行檢查,--sk 跳過按 Enter;加 --rwo 只顯示警告

Lynis:安全稽核

官方網站:https://cisofy.com/lynis/

yum --enablerepo=epel -y install lynis
lynis audit system

Lynis 會輸出一份含 hardening index 的報告與具體建議,逐項改善即可提升分數。教學可參考 https://netsecurity.51cto.com/art/201410/455466.htm

AIDE:檔案完整性檢測

AIDE(Advanced Intrusion Detection Environment)是入侵檢測工具,主要用途是檢查檔案完整性。下載點:https://sourceforge.net/projects/aide/

yum -y install aide

預設配置可直接使用;若要自訂監控規則,編輯 /etc/aide.conf,規則集中在第 26 至 84 行附近,例如把 /var/log 的監控設為:

/var/log    p+u+g+i+n+acl+selinux+xattrs

初始化資料庫並複製為主庫:

aide --init
# AIDE, version 0.15.1
# AIDE database at /var/lib/aide/aide.db.new.gz initialized.
cp -p /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz

執行檢查:

aide --check
# All files match AIDE database. Looks okay!

改動過檔案後再檢查,就會列出差異,例如調整 /root/anaconda-ks.cfg 權限後:

AIDE 0.15.1 found differences between database and filesystem!!
Summary:
  Total number of files: 31983
  Added files: 0
  Removed files: 0
  Changed files: 1

Changed files:
changed: /root/anaconda-ks.cfg

Detailed information:
File: /root/anaconda-ks.cfg
  Perm : -rw------- , -rw-r-----
  Ctime: 2016-10-25 04:52:57 , 2016-10-27 04:44:25

確認差異屬正常變更後,更新基線:

aide --update
cp -p /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz

加入排程做每日檢查。注意 /var/log/aide/aide.log 每次都被覆寫,無差異時會是零位元組,若要保留歷史,需自行寫腳本或寄信:

vi /etc/cron.d/aide
00 01 * * * /usr/sbin/aide --update | mail -s 'Daily Check by AIDE' root

Tripwire:文件系統完整性檢查

Tripwire 是最知名的 Unix 檔案完整性檢查工具,核心做法是為每個受監控檔案產生數字簽名並留存,當目前的簽名與留存值不一致,即代表檔案被改動。官網:https://www.tripwire.com/

yum --enablerepo=epel -y install tripwire

產生金鑰並建立配置:

tripwire-setup-keyfiles
# 依序設定 site keyfile 與 local keyfile 的密碼短語

cd /etc/tripwire
vi twcfg.txt      # 第 12 行 REPORTLEVEL = 4(最高等級)
twadmin -m F -c tw.cfg -S site.key twcfg.txt
# 寫出 /etc/tripwire/tw.cfg

接著用下列 Perl 腳本優化策略檔,把不存在的路徑自動註解掉,避免檢查時滿屏錯誤:

vi twpolmake.pl
#!/usr/bin/perl
# Tripwire Policy File customize tool
$POLFILE=$ARGV[0];
open(POL,"$POLFILE") or die "open error: $POLFILE" ;
my($myhost,$thost) ;
my($sharp,$tpath,$cond) ;
my($INRULE) = 0 ;
while (<POL>) {
    chomp;
    if (($thost) = /^HOSTNAME\s*=\s*(.*)\s*;/) {
        $myhost = `hostname` ; chomp($myhost) ;
        if ($thost ne $myhost) { $_="HOSTNAME=\"$myhost\";" ; }
    } elsif ( /^{/ ) { $INRULE=1 ; }
    elsif ( /^}/ ) { $INRULE=0 ; }
    elsif ($INRULE == 1 and ($sharp,$tpath,$cond) = /^(\s*\#?\s*)(\/\S+)\b(\s+->\s+.+)$/) {
        $ret = ($sharp =~ s/\#//g) ;
        if ($tpath eq '/sbin/e2fsadm' ) { $cond =~ s/;\s+(tune2fs.*)$/; \#$1/ ; }
        if (! -s $tpath) { $_ = "$sharp#$tpath$cond" if ($ret == 0) ; }
        else { $_ = "$sharp$tpath$cond" ; }
    }
    print "$_\n" ;
}
close(POL) ;

perl twpolmake.pl twpol.txt > twpol.txt.new
twadmin -m P -c tw.cfg -p tw.pol -S site.key twpol.txt.new
# 寫出 /etc/tripwire/tw.pol

建立資料庫:

tripwire -m i -s -c tw.cfg
# Please enter your local passphrase:

手動執行檢查(套件內已附 cron 每日檢查腳本):

tripwire -m c -c tw.cfg

四套工具定位互補:RKHunter 與 Lynis 定期體檢,AIDE 與 Tripwire 專盯檔案是否被動過。建議至少啟用其中一套完整性檢測並配置每日排程寄信,否則基線再完整也沒人看。

Copyright © Linux教程網 All Rights Reserved