【conoha VPS AlmaLinux9構築③】セキュリティ設定 その1

AlmaLinux9

基本参考に載せましたサイト様の通りに実施しています

Tripwire設定

# dnf install tripwire

途中、サイトパスフレーズとローカルパスフレーズを聞かれるので、任意の値を設定します

# tripwire-setup-keyfiles
# vi /etc/tripwire/twcfg.txt

LOOSEDIRECTORYCHECKING =true ←falseをtrueへ変更
REPORTLEVEL   =4 ←3を4に変更
# twadmin -m F -c /etc/tripwire/tw.cfg -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt
# rm -f /etc/tripwire/twcfg.txt

ポリシーファイルを作成していきます

# vi /etc/tripwire/twpolmake.pl
#!/usr/bin/perl
# Tripwire Policy File customize tool
# ----------------------------------------------------------------
# Copyright (C) 2003 Hiroaki Izumi
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# ----------------------------------------------------------------
# Usage:
#    perl twpolmake.pl {Pol file}
# ----------------------------------------------------------------
#
$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 /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new

# echo ! "/var/lib/tripwire/`hostname`.twd ;" >> /etc/tripwire/twpol.txt.new
# echo ! "/tmp/tripwire.log ;" >> /etc/tripwire/twpol.txt.new

# twadmin -m P -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key /etc/tripwire/twpol.txt.new

# rm -f /etc/tripwire/twpol.txt*

# tripwire -m i -s -c /etc/tripwire/tw.cfg

自動実行設定をします

# dnf install tmpwatch
# vi tripwire.sh

#!/bin/bash
# 既存のTripwire定期自動実行設定削除
rm -f /etc/cron.daily/tripwire-check

# パスフレーズ設定
LOCALPASS=xxxxxxxx ←設定したパスフレーズ
SITEPASS=xxxxxxxx  ←設定したパスフレーズ

TRIPWIRE=/usr/sbin/tripwire
TWADMIN=/usr/sbin/twadmin
TWPRINT=/usr/sbin/twprint
cd /etc/tripwire

# Tripwireチェック実行
# ※ファイル変更を検知した場合のみroot宛にサマリをメールする
rm -f /var/lib/tripwire/report/`hostname`-`date +%Y%m%d`-*.twr
${TRIPWIRE} -m c -s -c tw.cfg > /tmp/tripwire.log
if [ $(grep "Total violations found" /tmp/tripwire.log | awk '{print $4}') -ne 0 ]; then
    ${TWPRINT} -m r --report-level 1 -c tw.cfg -r /var/lib/tripwire/report/`hostname`-`date +%Y%m%d`-*.twr | \
    mail -s "Tripwire Integrity Check Report from `hostname`" root
fi

# Tripwireチェック実行結果(過去分)削除
# ※過去90日分保管
tmpwatch -m 2160 /var/lib/tripwire/report

# ポリシーファイル最新化
${TWADMIN} -m p -c tw.cfg -p tw.pol -S site.key > twpol.txt
perl twpolmake.pl twpol.txt > twpol.txt.new
${TWADMIN} -m P -c tw.cfg -p tw.pol -S site.key -Q $SITEPASS twpol.txt.new > /dev/null
rm -f twpol.txt* *.bak

# データベース最新化
rm -f /var/lib/tripwire/*.twd*
${TRIPWIRE} -m i -s -c tw.cfg -P $LOCALPASS
# chmod 700 tripwire.sh
# echo "0 3 * * * root /root/tripwire.sh" > /etc/cron.d/tripwire

chkrootkit設定

インストールします

# git clone https://github.com/Magentron/chkrootkit.git
# cp chkrootkit/chkrootkit /usr/local/bin/
# rm -rf chkrootkit

早速チェックをかけると、chshが引っかかります

# chkrootkit | grep INFECTED
Checking `chsh'... INFECTED

chshはログインシェルを変更するコマンドですが、使用しないので削除します

# rm -rf /usr/bin/chsh

自動実行設定をします

# vi /etc/cron.daily/chkrootkit

#!/bin/bash

LOG=/tmp/$(basename ${0})

# chkrootkit実行
chkrootkit > $LOG 2>&1

# ログ出力
cat $LOG | logger -t $(basename ${0})

# SMTPSのbindshell誤検知対応
if [ ! -z "$(grep 465 $LOG)" ] && \
   [ -z $(/usr/sbin/lsof -i:465|grep bindshell) ]; then
        sed -i '/465/d' $LOG
fi

# upstartパッケージ更新時のSuckit誤検知対応
if [ ! -z "$(grep Suckit $LOG)" ] && \
   [ -z "$(rpm -V `rpm -qf /sbin/init`)" ]; then
        sed -i '/Suckit/d' $LOG
fi

# rootkit検知時のみroot宛メール送信
[ ! -z "$(grep INFECTED $LOG)" ] && \
grep INFECTED $LOG | mail -s "chkrootkit report in `hostname`" root
# chmod 700 /etc/cron.daily/chkrootkit

chkrootkitコマンド退避

# mkdir chkrootkitcmd
# cp `which --skip-alias awk cut echo egrep find head id ls netstat ps strings sed ssh uname` chkrootkitcmd/
# zip -r chkrootkitcmd.zip chkrootkitcmd/ && rm -rf chkrootkitcmd

ClamAV設定

インストールします

# dnf -y install clamav clamav-server-systemd clamav-update clamav-scanner-systemd
# vi /etc/freshclam.conf

#NotifyClamd /path/to/clamd.conf
NotifyClamd /etc/clamd.d/scan.conf ←追記
# freshclam

# vi /etc/clamd.d/scan.conf
#User clamscan ←コメントアウトを追加
LocalSocket /run/clamd.scan/clamd.sock ←コメントアウトを外す

サービスを起動します
freshclamをデーモンで起動させておくことで、ウイルス定義ファイルの更新が自動で実行されます

# systemctl start clamd@scan
# systemctl enable clamd@scan
# systemctl start clamav-freshclam
# systemctl enable clamav-freshclam

スキャンしてウイルス検知しないことを確認します

# clamdscan -c /etc/clamd.d/scan.conf --remove

参考

AlmaLinuxで自宅サーバー構築 (centossrv.com)

コメント

タイトルとURLをコピーしました