LoginSignup
0
0

More than 1 year has passed since last update.

Ubuntu Server 20.04.3 LTS 構築#12 セキュリティ設定5-5

Last updated at Posted at 2022-02-10

実施日:2022/02/08~2022/02/10

<<今回の内容>>
『定義ファイルを更新』⇒『ウイルススキャン』⇒『結果をメールで通知』をシェルスクリプトで行う

【イメージ】
image.png

上記、イメージを実行するためにシェルスクリプトを作成しました。

修正 2022/02/19

当初、最初に設定したスクリプトだと隔離したディレクトリにファイルが存在しても、defected判定されてしまったりして
挙動がおかしいためスクリプト内を修正しました。修正箇所はif~の部分です。

SCAN_RESULTS=/home/ユーザー名/Documents/virus/scanlog.log if [ ! -z \"\$(grep FOUNDS\$ ${SCAN_RESULTS})" ];then { cat \${SCAN_RESULTS} | mail -s 'Warning' ユーザー名@localhost } else { cat ${SCAN_RESULTS} | mail -s 'Safe' ユーザー名@localhost } fi grep FOUNDS$はclamscan実行でウイルスとして検知したもののファイルの語尾にFOUNDSと付記されるため、 スキャン時にFOUNDSのものを拾ってinfectedで通知してもらうための設定です。

#!/bin/bash

FILE=/home/ユーザー名/Documents/virus/

sudo systemctl stop clamav-freshclam
sudo freshclam
sudo systemctl start clamav-freshclam

if [ $?=0 ];then
{
echo 'success'
}
else
{
echo 'fail'
echo 'update NG!!' | mail -s 'freshclam NG!!' ユーザー名@localhost
}
fi

clamscan \
-i \
-r/ \
--log=/home/ユーザー名/Documetns/scanlog.log \
--exclude-dir=/sys/ \
--exclude-dir=/proc/ \
--exclude-dir=/home/ユーザー名/Documetns/virus/ \
--remove=/home/ユーザー名/Documetns/virus/ \
2>/dev/null

if [ -e ${FILE} ];then
{
ls -a /home/ユーザー名/Documents/virus/
echo 'virus infected' | mail -s 'warning' ユーザー名@localhost
echo 'Scan NG!!'
}
else
{
ls -a /home/ユーザー名/Documents/virus/
echo 'virus detected' | mail -s 'warning' ユーザー名@localhost
}
fi

最初は各コマンドをif [ \$?=0 ]で成功又は失敗でclamscanコマンドを条件分岐して実行しようとしましたが、
隔離先ディレクトリに無害ウイルスを格納しているにも関わらず、感染したファイルはありませんと表示されました。
exclude-dir=のオプションを入れてません。
途中でコマンド自体の成功か失敗かを判断して実行していることに気づき、変数を[ \$?=0 ]ではなく、
[ -e \${FILE} ]に修正して隔離先ディレクトリのファイルの有無で判定するようにしたら条件分岐するようになりました。

最後に/etc/cron.d/内にある実行ファイルに作成したシェルスクリプトを実行するように設定して、自動で実行が出来るのかを
確認します。ここでcronが動作してくれれば今回の作業は完了となります。
cronの結果も予定通りの挙動になってくれました。

参考にさせて頂きましたサイト様
稲葉サーバーデザイン様
@kannkyo様

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0