1. Clam AntiVirus
Clam AntiVirus は Linux系で使えるアンチウイルスソフト。
sudo dnf --enablerepo=epel -y install clamav clamav-scanner-systemd clamav-update
1.1. 設定変更
vi /etc/clamd.d/scan.conf
### 以下はコメント解除
LogFile /var/log/clamd.scan
LogFileMaxSize 2M
LogTime yes
LogRotate yes
LocalSocket /run/clamd.scan/clamd.sock
FixStaleSocket yes
ExcludePath ^/proc/
ExcludePath ^/sys/
### 以下は変更
User clamscan
↓
User root
1.2. ウイルス定義ファイルの更新設定
vi /etc/freshclam.conf
### 以下はコメント解除
UpdateLogFile /var/log/freshclam.log
LogFileMaxSize 2M
LogTime yes
LogRotate yes
### 以下は変更
#DatabaseOwner clamupdate
↓
DatabaseOwner root
DatabaseMirror database.clamav.net
↓
DatabaseMirror db.jp.clamav.net
#NotifyClamd /path/to/clamd.conf
↓
NotifyClamd /etc/clamd.d/scan.conf
ここまで出来たら、ウイルス定義ファイルを更新し、サービスを有効化、起動しておく。
sudo freshclam -u root
sudo systemctl enable clamd@scan
sudo systemctl start clamd@scan
手動スキャンは以下のコマンド。
clamscan --infected --remove --recursive [ディレクトリ名]
あとは、スキャン用のスクリプトを作って、cronで定期チェックをかけるようにする。
作るファイルは2つ。cronで回すので、root でファイルの作成& crontab の設定まで行う。
まずはスクリプト本体。
vi /root/script/clamscan
#!/bin/bash
PATH=/usr/bin:/bin
# clamd update
yum -y update clamav-server clamav-data clamav-update clamav-filesystem clamav clamav-scanner-systemd clamav-devel clamav-lib clamav-server-systemd > /dev/null 2>&1
# excludeopt setup
excludelist=/root/script/clamscan.exclude
if [ -s $excludelist ]; then
for i in `cat $excludelist`
do
if [ $(echo "$i"|grep \/$) ]; then
i=`echo $i|sed -e 's/^\([^ ]*\)\/$/\1/p' -e d`
excludeopt="${excludeopt} --exclude-dir=^$i"
else
excludeopt="${excludeopt} --exclude=^$i"
fi
done
fi
# virus scan
CLAMSCANTMP=`mktemp`
clamscan --recursive --remove ${excludeopt} / > $CLAMSCANTMP 2>&1
[ ! -z "$(grep FOUND$ $CLAMSCANTMP)" ] && \
# report mail send
grep FOUND$ $CLAMSCANTMP | mail -s "Virus Found in `hostname`" root
rm -f $CLAMSCANTMP
お次は、除外ディレクトリの設定ファイル。
vi /root/script/clamscan.exclude
/dev/
/etc/
/media/
/mnt/
/opt/
/proc/
/run/
/srv/
/sys/
/usr/
/var/
ファイル名は cron からアクセスできる場所にあれば別に何でもよい。スクリプト内に除外ファイルのパスを直書きしてるので、そこに齟齬が無いように。
あとは、crontab で登録。
chmod +x clamscan
crontab -e
===
MAILTO="test@hogehoge.com"
0 3 * * * /root/script/clamscan
===
これで、スクリプトに問題が無ければ、毎日午前3時にスキャンが走るようになる。
2. logwatch
設定ファイルは、デフォルトを全コピーして必要箇所だけ変える…という情報が多かったが、別にローカル設定ファイルに最小限だけ書けば良さそうなので、必要な項目だけ、ローカル設定ファイルに書いてみる。
dnf install -y logwatch
vi /etc/logwatch/conf/logwatch.conf
### 通知メールの送信先
MailTo = test@hogehoge.com
### ログの出力レベル
Detail = High (Low/Med/High)
### 通知メールの差出人
MailFrom = (任意)
動作確認は、下記で出来る。なお、cron にはインストールした時点で登録されている。
### 標準出力に表示
logwatch --output stdout
### 設定したアドレスにメールで送信
logwatch --output mail
### cronに登録されているのが確認できる
ls -la /etc/cron.daily/0logwatch
-rwxr-xr-x 1 root root 434 5月 11 2019 /etc/cron.daily/0logwatch
試してみたら、メール送信のところで、下記のようなエラーが出た。
logwatch --output mail
postdrop: warning: unable to look up public/pickup: No such file or directory
なんのことはない、postfix のサービスが起動して無いだけだった。
3. RKHunter
Webサイトなどを運営していると、ファイル改ざんの被害にあうこともあるが、そういった改ざん事案に使われる、rootkit の検出ツールに RKHunter というのがあるので、それも入れておく。
dnf --enablerepo=epel install -y rkhunter
vi /etc/sysconfig/rkhunter
===
MAILTO=root@localhost → 必要に応じて、メールアドレスを変更。
DIAG_SCAN=no → yes に変更して、詳細スキャンできるようにする。
===
### データベース更新
rkhunter --update
### 現時点のシステムのファイル情報を反映
rkhunter --propupd
### チェックの実行 逐一Enter押下が求められるので、うっとうしければ"--sk"オプションを付ける。
rkhunter --check --sk
4. Tripwire
4.1. インストール
dnf install -y --enablerepo=epel tripwire
4.2. 初期設定
4.2.1. サイトキーとローカルキー
以下のコマンドで「サイトキー」と「ローカルキー」という、2種のパスフレーズを設定する。
tripwire-setup-keyfiles
----------------------------------------------
The Tripwire site and local passphrases are used to sign a variety of
files, such as the configuration, policy, and database files.
Passphrases should be at least 8 characters in length and contain both
letters and numbers.
See the Tripwire manual for more information.
----------------------------------------------
(略)
Enter the site keyfile passphrase: ### サイトキーを登録
Verify the site keyfile passphrase: ### サイトキーを再入力
Generating key (this may take several minutes)...Key generation complete.
(略)
Enter the local keyfile passphrase: ### ローカルキーを登録
Verify the local keyfile passphrase: ### ローカルキーを再入力
Generating key (this may take several minutes)...Key generation complete.
----------------------------------------------
Signing configuration file...
Please enter your site passphrase: ### 登録したサイトキーを入力
Wrote configuration file: /etc/tripwire/tw.cfg
A clear-text version of the Tripwire configuration file:
/etc/tripwire/twcfg.txt
has been preserved for your inspection. It is recommended that you
move this file to a secure location and/or encrypt it in place (using a
tool such as GPG, for example) after you have examined it.
----------------------------------------------
Signing policy file...
Please enter your site passphrase: ### 登録したサイトキーを入力
Wrote policy file: /etc/tripwire/tw.pol
A clear-text version of the Tripwire policy file:
/etc/tripwire/twpol.txt
has been preserved for your inspection. This implements a minimal
policy, intended only to test essential Tripwire functionality. You
should edit the policy file to describe your system, and then use
twadmin to generate a new signed copy of the Tripwire policy.
(略)
4.2.2. 設定ファイル
設定する内容は、”man twconfig”を参照。
tripwireは設定ファイルがバイナリ化されているので、「元になるテキストを編集」→「バイナリ化」という手順が必要になる。
vi /etc/tripwire/twcfg.txt ### 元になるテキストファイル
twadmin -m F -c tw.cfg -S site.key twcfg.txt
Please enter your site passphrase: ### 登録したサイトキーを入力
Wrote configuration file: /etc/tripwire/tw.cfg
4.2.3. ポリシーファイル
どのファイル・ディレクトリをどのようなルールで監視するか、を定義するのがポリシーファイル。
ポリシーファイルもバイナリ化されているので、「元になるテキストを編集」→「バイナリ化」という手順が必要になる。
最初からある”twpol.txt”はサンプルなので、そのままデータベースを作成すると、「そのようなファイルやディレクトリはありません」というワーニングが大量に出てしまう。
なので、以下のスクリプトでサンプルを元に最適化して、ポリシーファイルを作成する。
#!/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 twpolmake.pl twpol.txt > twpol.txt.new ### ポリシーファイルの元になるテキストを生成
twadmin -m P -c tw.cfg -p tw.pol -S site.key twpol.txt.new
Please enter your site passphrase: ### 登録したサイトキーを入力
Wrote policy file: /etc/tripwire/tw.pol
4.3. データベースの生成
tripwire -m i -s -c tw.cfg
Please enter your local passphrase: ### 登録したローカルキーを入力
4.4. 運用
セットアップできたら運用に乗せる。定期的にチェック処理をすればいいが、yum / dnf でインストールした場合、自動的に cron のジョブが生成されているようだ。
ll /etc/cron.daily
(略)
-rwxr-xr-x. 1 root root 532 9月 11 2019 tripwire-check ### cron.dairy に登録済みなのがわかる。
手動で行う場合は、以下のコマンド。
tripwire -m c -s -c /etc/tripwire/tw.cfg
レポートは下記に出力される。
ll /var/lib/tripwire/report
-rw-r--r--. 1 root root 6062 1月 15 23:42 [hostname]-20210115-234200.twr
レポートを確認して、問題があればファイルシステムを修正後、問題がなければ、対象となるレポートを指定して、データベースを更新する。
tripwire -m u -a -s -c /etc/tripwire/tw.cfg -r /var/lib/tripwire/report/[対象のレポートファイル名]
Please enter your local passphrase: ### 登録したローカルキーを入力
5. firewalld
当初は firewalld は切っていたが、logwatch の結果を見ると、思いのほか SSH へのログイン試行が数多く記録されていたので、念のため、SSHは指定のIPからしか接続できないようにしておく。
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --add-service=http --zone=public --permanent
firewall-cmd --add-service=https --zone=public --permanent
firewall-cmd --reload
firewalld 有効にすると、デフォルトでは”cockpit dhcpv6-client ssh”のみが開放されているので、httpやhttpsも解放しておく。
メール関連も後々解放しないとダメだと思うが、まだ運用始めてないので、その時に開放すれば良さそう。
「特定IPだけ許可」というルールを登録したい場合は、「全て拒否」→「特定IP許可」の順で登録する。
あくまでもIPアドレスでしか指定できないので、一般のプロバイダでグローバルIPが変わる可能性がある場合は注意が必要。ルーター再起動などでIP変わったら、自宅からSSH接続できない、という事態もありうる。
なので、完璧ではないが、許可したい拠点のIPアドレスを元に、接続しているプロバイダーのIP範囲を調べて、ネットマスクつけてIP指定をする、という形にすると良い。
firewall-cmd --remove-service=ssh --zone=public --permanent
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.0/24" port protocol="tcp" port="22" accept" ### 複数拠点あって、プロバイダも違う場合、これを必要なだけ行う。
firewall-cmd --reload
とりあえず、上記の設定で、再度SSH接続してみても、問題なく接続できたので、明日以降の logwatch の結果を確認して、変化があるかどうか。
*firewalld で弾いてもログに残るなら一緒かもしれないが・・・現に「ログイン試行したが失敗」がログに残ってるわけだし。