Contents
1. Tripwire インストール
Tripwireとはファイルやディレクトリを監視し、変更などがあった場合に通知するホストベースの侵入検知システム(IDS)
1.1 インストール、設定
① ダウンロード、インストール
# wget https://rpmfind.net/linux/epel/8/Everything/x86_64/Packages/t/tripwire-2.4.3.7-5.el8.x86_64.rpm
# rpm -Uvh tripwire-2.4.3.7-5.el8.x86_64.rpm
② パスフレーズの設定
———————————————-
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.
———————————————-
Creating key files…
(When selecting a passphrase, keep in mind that good passphrases typically
have upper and lower case letters, digits and punctuation marks, and are
at least 8 characters in length.)
Enter the site keyfile passphrase: ←任意の「サイトパスフレーズ」を入力
Verify the site keyfile passphrase: ←再度任意の「サイトパスフレーズ」を入力
Generating key (this may take several minutes)…Key generation complete.
(When selecting a passphrase, keep in mind that good passphrases typically
have upper and lower case letters, digits and punctuation marks, and are
at least 8 characters in length.)
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
~中略~
default values from the current configuration file are used.
③ Tripwire の設定
■9 行目あたり
行頭に「#」を追加し、その下の行に「LOOSEDIRECTORYCHECKING =true」を追加します。
LOOSEDIRECTORYCHECKING =true
■13 行目あたり
行頭に「#」を追加し、その下の行に「REPORTLEVEL =4」を追加します。
レベル4 にすることで「0 」~「4 」までの5 段階中、最も詳細なレポートが表示されます。
#REPORTLEVEL =3
REPORTLEVEL =4
④ Tripwire 設定ファイル(暗号署名版)を作成
Please enter your site passphrase: ←設定したサイトパスフレーズを入力
Wrote configuration file: /etc/tripwire/tw.cfg
⑤Tripwire 設定ファイル(テキスト版)削除
参考) Tripwire 設定ファイル(テキスト版)を復元する場合は下記コマンドを実行します
# twadmin -m f -c /etc/tripwire/tw.cfg > /etc/tripwire/twcfg.txt
⑥ ポリシーファイル設定
# cd /etc/tripwire/
# vi twpolmake.txt
twpolmake.txtの内容 ↓
# Tripwire Policy File customize tool
# —————————————————————-
# 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
# twadmin -m P -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key /etc/tripwire/twpol.txt.new
Please enter your site passphrase: ←②で設定したサイトパスフレーズを入力
Wrote policy file: /etc/tripwire/tw.pol
ポリシーファイル(テキスト版)削除
Please enter your local passphrase: ←設定したローカルパスフレーズを入力
Report generated by: root
Report created on: Sun Jan 16 22:28:26 2022
Database last updated on: Never
==============================================================
Report Summary:
==============================================================
Host name: Lepard
Host IP address: Unknown IP
Host ID: None
Policy file used: /etc/tripwire/tw.pol
Configuration file used: /etc/tripwire/tw.cfg
Database file used: /var/lib/tripwire/Lepard.twd
Command line used: tripwire -m c -s -c /etc/tripwire/tw.cfg
・・・・・省略・・・・・・
——————————————————————————-
Added:
“/root/test.txt”
==============================================================
Error Report:
==============================================================
No Errors
——————————————————————————-
*** End of report ***
Open Source Tripwire 2.4 Portions copyright 2000-2018 Tripwire, Inc. Tripwire is a registered
trademark of Tripwire, Inc. This software comes with ABSOLUTELY NO WARRANTY;
for details use –version. This is free software which may be redistributed
or modified only under certain conditions; see COPYING for details.
All rights reserved.
1.2 Tripwire を定期実行させる
# vi tripwire.sh
パスフレーズ設定のLOCALPASS、SITEPASS それぞれの「xxxxxxxx」に設定したローカルパスフレーズ、サイトパスフレーズを入力
#!/bin/bash
PATH=/usr/sbin:/usr/bin:/bin:/usr/local/tripwire/sbin
# パスフレーズ設定
LOCALPASS= xxxxxxxx # ローカルパスフレーズ
SITEPASS= xxxxxxxx # サイトパスフレーズ
cd /etc/tripwire
# Tripwireチェック実行
tripwire -m c -s -c tw.cfg|mail -s “Tripwire(R) Integrity Check Report in `hostname`” root
# ポリシーファイル最新化
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 /usr/local/tripwire/lib/tripwire/*.twd*
tripwire -m i -s -c tw.cfg -P $LOCALPASS
Tripwire 自動実行スクリプト実行設定
0 3 * * * /srv/www/system/tripwire.sh
参考: メールで結果報告用スクリプト
#!/bin/bash
PATH=/usr/sbin:/usr/bin:/bin:/usr/local/tripwire/sbin
# パスフレーズ設定
LOCALPASS=xxxxx # ローカルパスフレーズ
SITEPASS=xxxxx # サイトパスフレーズ
#通知先メールアドレス指定
MAIL=”<your mail address> “
cd /etc/tripwire
# Tripwireチェック実行
tripwire -m c -s -c tw.cfg|mail -s “Tripwire(R) Integrity Check Report in `hostname`” $MAIL
# ポリシーファイル最新化
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 /usr/local/tripwire/lib/tripwire/*.twd*
tripwire -m i -s -c tw.cfg -P $LOCALPASS
2. chkrootkit インストール
①chkrootkit をダウンロード、インストール
# zypper refresh
# zypper install chkrootkit
➁/root/bin ディレクトリにchkrootkit コマンドを移動
#!/bin/bash
PATH=/usr/bin:/bin:/root/bin
TMPLOG=`mktemp`
# chkrootkit実行
chkrootkit > $TMPLOG
# ログ出力
cat $TMPLOG | logger -t chkrootkit
# SMTPSのbindshell誤検知対応
if [ ! -z “$(grep 465 $TMPLOG)” ] && \
[ -z $(/usr/sbin/lsof -i:465|grep bindshell) ]; then
sed -i ‘/465/d’ $TMPLOG
fi
# upstartパッケージ更新時のSuckit誤検知対応
#if [ ! -z “$(grep Suckit $TMPLOG)” ] && \
# [ -z $(rpm -V `rpm -qf /sbin/init`) ]; then
# sed -i ‘/Suckit/d’ $TMPLOG
#fi
# rootkit検知時のみroot宛メール送信
[ ! -z “$(grep INFECTED $TMPLOG)” ] && \
grep INFECTED $TMPLOG | mail -s “chkrootkit report in `hostname`” root
rm -f $TMPLOG
0 2 * * * /srv/www/system/chkrootkit.sh ←追加
⑥chkrootkit が使用するコマンドをバックアップ
chkrootkit が使用するコマンドが改ざんされた場合、rootkit を検出できなくなるので、これらのコマンドをバックアップしておきます。必要な場合は、バックアップしたコマンドを使用してchkrootkit を実行します
# mkdir /root/chkrootkit_cmd
# cp `which –skip-alias awk cut echo egrep find head id ls snmpnetstat ps strings sed ssh uname` chkrootkit_cmd/
# ls -l /root/chkrootkit_cmd/
total 2404
-rwxr-xr-x 1 root root 614520 Jan 9 18:48 awk
-rwxr-xr-x 1 root root 47952 Jan 9 18:48 cut
-rwxr-xr-x 1 root root 31408 Jan 9 18:48 echo
-rwxr-xr-x 1 root root 28 Jan 9 18:48 egrep
-rwxr-xr-x 1 root root 304024 Jan 9 18:48 find
-rwxr-xr-x 1 root root 43792 Jan 9 18:48 head
-rwxr-xr-x 1 root root 39760 Jan 9 18:48 id
-rwxr-xr-x 1 root root 138904 Jan 9 18:48 ls
-rwxr-xr-x 1 root root 130200 Jan 9 18:48 ps
-rwxr-xr-x 1 root root 155032 Jan 9 18:48 sed
-rwxr-xr-x 1 root root 70088 Jan 9 18:48 snmpnetstat
-rwxr-xr-x 1 root root 793432 Jan 9 18:48 ssh
-rwxr-xr-x 1 root root 31896 Jan 9 18:48 strings
-rwxr-xr-x 1 root root 35568 Jan 9 18:48 uname
openSUSE15.3にはnetstatが標準ではインストールされていないので下記を先に実行しておく
chkrootkit_cmd/
chkrootkit_cmd/awk
chkrootkit_cmd/cut
chkrootkit_cmd/echo
chkrootkit_cmd/egrep
chkrootkit_cmd/find
chkrootkit_cmd/head
chkrootkit_cmd/id
chkrootkit_cmd/ls
chkrootkit_cmd/ps
chkrootkit_cmd/strings
chkrootkit_cmd/sed
chkrootkit_cmd/ssh
chkrootkit_cmd/uname
chkrootkit_cmd/snmpnetstat
# rm -rf /home/lan/chkrootkit_cmd.tar.gz
# mv chkrootkit.sh chkrootkit.sh.bak
# vi chkrootkit.sh
#!/bin/bash
#通知先メールアドレス指定
MAIL=”lan@korodes.com”
PATH=/usr/bin:/bin:/root/bin
TMPLOG=`mktemp`
# chkrootkit実行
chkrootkit > $TMPLOG
# ログ出力
cat $TMPLOG | logger -t chkrootkit
# SMTPSのbindshell誤検知対応
if [ ! -z “$(grep 465 $TMPLOG)” ] && \
[ -z $(/usr/sbin/lsof -i:465|grep bindshell) ]; then
sed -i ‘/465/d’ $TMPLOG
fi
# rootkit検知時のみroot宛メール送信
[ ! -z “$(grep INFECTED $TMPLOG)” ] && \
grep INFECTED $TMPLOG | mail -s “chkrootkit report in `hostname`” $MAIL
rm -f $TMPLOG
3. Logwatch 導入
# vi /etc/logwatch/conf/logwatch.conf
■52 行目あたり
「MailTo = root」の行頭に「#」を挿入し、その下行に通知を受け取りたいメールアドレスを設定
#MailTo = root
MailTo = <mail.address>
■84 行目あたり
ログ通知の詳細度を設定
#Detail = Low
Detail = High
# logwatch –output stdout
④ 設定したアドレスにレポートが届くかテストを行う
4. DiCEのインストール
4.1 Diceのダウンロード、インストール
# wget https://centos.rcg.jp/download/DiCE.tar.gz
# tar -xzvf DiCE.tar.gz
# cd /usr/local/bin/DiCE
DiCEの出力文字はEUCのため、文字化けしてしまう。
UTF-8に変換するために、nkfをインストールしておく
# ./diced | nkf -uw
4.2 イベントの追加
: add
DynamicDNSサービス名を入力してください
“?”で対応しているサービスを一覧表示します
(P)戻る
>VALUEDOMAIN
ドメイン名を入力してください
“?”でドメイン一覧を表示します
(P)戻る
><ドメイン名> ←VALUEDOMAIN
ホスト名を入力してください
(P)戻る
><ホスト名> ←VALUEDOMAINに登録したホスト名
ログインユーザ名を入力してください
(P)戻る
><ユーザー名> ←VALUEDOMAINに登録したユーザー名
ログインパスワードを入力してください
(P)戻る
><passwd> ←VALUEDOMAINにログインするパスワード
登録するIPアドレスを入力してください
空白にすると現在のIPアドレスを自動検出します
(P)戻る
>空白
このイベントに題名を付けてください
(P)戻る
>xxxxxxxxx(適当に付ける)
実行する頻度を指定してください (番号入力)
(0)1回のみ (1)1日1回 (2)1週間に1回 (3)1ヵ月に1回
(4)その他の周期 (5)IPアドレス変化時 (6)起動時
(P)戻る
>5 ←任意
IPアドレスがあまり変化しない環境の場合、更新せずに一定期間を過ぎると
アカウントを削除されてしまうことがあります
IPアドレスの変化が無い時に実行する間隔を指定してください
(0)7日毎 (1)14日毎 (2)21日毎 (3)28日毎
(4)35日毎 (5)56日毎 (6)84日毎
(P)戻る
>0(任意に決める) ←任意
このイベントを有効にしますか? (Y/N)
(イベントの有効/無効は”EN/DIS”コマンドで切替えられます)
>y
イベントを保存しますか? (Y/N)
>y
:list
(No.) (イベント名) (スケジュール) (次回予定)
0 * xxxxxxxxx IPアドレス変化時 (7日毎) 01/25 17:43
手動実行
:ex 0
+ 1/19 13:13 にxxxxxxxxxが実行されました
IPアドレスを更新しました
4.3 Diceの自動実行
DiCEデーモンを起動する
起動しているか確認
root 29500 0.0 0.0 7816 632 pts/0 S+ 13:18 0:00 grep –color=auto diced
自動で起動するよう設定
/usr/local/bin/DiCE/diced -d -l ←追加