LoginSignup
12
13

More than 5 years have passed since last update.

Linuxセキュリテイ(Tripwireで行う改ざん検知)

Last updated at Posted at 2018-07-16

はじめに

ファイアウォール等のセキュリテイ対策を回避して進入された不正なアクセスを検知する有効な手段として、改ざん検知や侵入検知が挙げられます。

本記事は、改ざん検知ソフトウェアであるTripwireの導入手順と使い方に関するまとめです。

環境

OS:CentOS Linux release 7.3.1611 (Core)

改ざん検知

システム内のファイルに対する追加・変更・削除といった事象を検知することで、不正アクセス等によるファイルの改ざんを検知します。

Tripwireによる改ざん検知

Tripwireは、システムに加えられた変更を検知するオープンソースの改ざん検知ソフトウェアです。商用化されているものもありますが、GPLの元に無償で利用することができます。オープンソース版はSourceForge.netより入手できます。

仕組みとしては、正常な状態でのシステムのスナップショット(ベースラインデータベース)を内部データベースとして保持し、現在の状態でのシステムのスナップショットと比較することで改ざんを検知します。

Tripwireは以下のような事象を発見することができます。

  • ファイルの内容が変更された
  • ファイルやディレクトリが追加された
  • ファイルやディレクトリが削除された
  • ファイルやディレクトリのオーナー・パーミッションが変更された

導入は簡単ですが、あくまで検知することを目的に作られているので、チューニングを適切に行わないと条件(ポリシーファイル)に該当さえすれば、正常なファイルでも差分として検知されます。

Tripwireのインストール

CentOS用のTripwireパッケージは用意されていませんが、EPELを使用することでyumでインストールできます。

  • はじめにEPELをインストールする。
    # rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

  • yumコマンドの実行時にEPELのリポジトリを使用するために、オプションを付けてTripwireをインストールする。
    # yum install --enablerepo=epel tripwire

  • tripwireの初期設定
    # tripwire-setup-keyfiles

インストール後は初期設定として、tripwire-setup-keyfilesコマンドを実行し、site.keyファイルを生成します。コマンド実行時に、サイトパスフレーズとローカルパスフレーズの入力が求められるので、それぞれパスフレーズを入力します。コマンド終了後、/etc/tripwire/ディレクトリ配下にsite.keyファイルが生成されます。

[root@localhost ~]# 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.

----------------------------------------------
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
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.

Once you have a satisfactory Tripwire policy file, you should move  the
clear-text version to a secure location  and/or  encrypt  it  in  place
(using a tool such as GPG, for example).

Now run "tripwire --init" to enter Database Initialization  Mode.  This
reads the policy file, generates a database based on its contents,  and
then cryptographically signs the resulting  database.  Options  can  be
entered on the command line to specify which policy, configuration, and
key files are used  to  create  the  database.  The  filename  for  the
database can be specified as well. If no  options  are  specified,  the
default values from the current configuration file are used.
  • ポリシーファイルの作成
    # twadmin --create-polfile --site-keyfile /etc/tripwire/site.key /etc/tripwire/twpol.txt

Tripwireはポリシーファイルに従い、ファイル・ディレクトリの検査を行います。上記、tripwire-setup-keyfilesコマンドを実行して生成されたsite.keyファイルと、ポリシーファイルの元となるtwpol.txtファイルを指定して、ポリシーファイル(tw.pol)を作成します。

[root@localhost ~]# twadmin --create-polfile --site-keyfile /etc/tripwire/site.key /etc/tripwire/twpol.txt
Please enter your site passphrase: 
Wrote policy file: /etc/tripwire/tw.pol
  • データベースの初期化
    # tripwire --init

ポリシーファイルを元に、現時点でのシステムのファイル・ディレクトリ情報をデータベース化します。
以下のように、ポリシーファイルにチェックが入っていて、実際にファイルやディレクトリが存在しない場合は「Warning」出力されるので、ポリシーファイルを修正し、再度データベースの初期化を行うことで、意図した改ざん検知を行うことができるようになります。

Please enter your local passphrase: 
Parsing policy file: /etc/tripwire/tw.pol
Generating the database...
*** Processing Unix File System ***
### Warning: File system error.
### Filename: /usr/sbin/fixrmtab
### そのようなファイルやディレクトリはありません
### Continuing...
### Warning: File system error.
### Filename: /usr/bin/vimtutor
### そのようなファイルやディレクトリはありません
### Continuing...
### Warning: File system error.
### Filename: /usr/share/grub/i386-redhat/e2fs_stage1_5
### そのようなファイルやディレクトリはありません
### Continuing...
(中略)
Wrote database file: /var/lib/tripwire/localhost.localdomain.twd
The database was successfully generated.

Tripwireの使い方

  • 整合性チェック
    # tripwire --check

tripwire --checkコマンドを実行することで、改ざん検知を行います。Tripwireはリアルタイムでの改ざん検知機能はないので、自動で改ざん検知を行うためにはcron等の登録が必要です。

[root@localhost ~]# tripwire --check
Parsing policy file: /etc/tripwire/tw.pol
*** Processing Unix File System ***
Performing integrity check...
(中略)
*** 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.
Integrity check complete.
  • レポートの確認
    # twprint --print-report --twrfile /var/lib/tripwire/report/localhost.localdomain-20180716-111854.twr

チェック終了後、/var/lib/tripwire/report/ホスト名-YYYYMMDD-HHMMSS.twrというレポートファイルが生成されるので、twprint --print-reportコマンドを実行して、結果を確認します。

--report-levelを指定することで情報の出力を変更(※)することができます。
以下の場合はlevel 0を指定することでサマリ表示にしています。

(※)0が簡易表示で4が詳細表示

[root@localhost ~]# twprint --print-report --report-level 0 --twrfile /var/lib/tripwire/report/localhost.localdomain-20180716-111854.twr 
Note: Report is not encrypted.
TWReport localhost.localdomain 20180716111854 V:3 S:100 A:1 R:0 C:2

レポート結果の見方は以下の通りです。

V S A R C
ポリシーファイル違反数 重要度 追加されたファイル・ディレクトリ数 削除されたファイル・ディレクトリ数 変更されたファイル・ディレクトリ数

以下の場合はlevel 4を指定することで詳細表示にしています。
試しに/rootディレクトリ配下に作成したtestfileが、検知されたのを確認できます。

(中略)
===============================================================================
Object Summary: 
===============================================================================

-------------------------------------------------------------------------------
# Section: Unix File System
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
Rule Name: Root config files (/root)
Severity Level: 100
-------------------------------------------------------------------------------

Added:
"/root/testfile"

Modified:
"/root"
"/root/.lesshst"

===============================================================================
Object Detail: 
===============================================================================

-------------------------------------------------------------------------------
  Section: Unix File System
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
Rule Name: Root config files (/root)
Severity Level: 100
-------------------------------------------------------------------------------
  ----------------------------------------
  Added Objects: 1
  ----------------------------------------

Added object name:  /root/testfile

  Property:            Expected                    Observed                    
  -------------        -----------                 -----------                 
* Object Type          ---                         Regular File                
* Device Number        ---                         2049                        
* File Device Number   ---                         0                           
* Inode Number         ---                         4229528                     
* Mode                 ---                         -rw-r--r--                  
* Num Links            ---                         1                           
* UID                  ---                         root (0)                    
* GID                  ---                         root (0)                    
* Size                 ---                         0                           
* Modify Time          ---                         2018年07月16日 11時18分49秒
* Change Time          ---                         2018年07月16日 11時18分49秒
* Blocks               ---                         0                           
* CRC32                ---                         D/////                      
* MD5                  ---                         DUHYzZjwCyBOmACZjs+EJ+      



  ----------------------------------------
  Modified Objects: 2
  ----------------------------------------

Modified object name:  /root

  Property:            Expected                    Observed                    
  -------------        -----------                 -----------                 
  Object Type          Directory                   Directory                   
  Device Number        2049                        2049                        
  File Device Number   0                           0                           
  Inode Number         4194369                     4194369                     
  Mode                 dr-xr-x---                  dr-xr-x---                  
  Num Links            4                           4                           
  UID                  root (0)                    root (0)                    
  GID                  root (0)                    root (0)                    
* Size                 236                         252                         
* Modify Time          2018年07月16日 11時17分05秒
                                                   2018年07月16日 11時18分49秒
* Change Time          2018年07月16日 11時17分05秒
                                                   2018年07月16日 11時18分49秒
  Blocks               0                           0                           


Modified object name:  /root/.lesshst

  Property:            Expected                    Observed                    
  -------------        -----------                 -----------                 
  Object Type          Regular File                Regular File                
  Device Number        2049                        2049                        
  File Device Number   0                           0                           
  Inode Number         4201689                     4201689                     
  Mode                 -rw-------                  -rw-------                  
  Num Links            1                           1                           
  UID                  root (0)                    root (0)                    
  GID                  root (0)                    root (0)                    
  Size                 55                          55                          
* Modify Time          2018年07月16日 11時16分47秒
                                                   2018年07月16日 11時18分17秒
* Change Time          2018年07月16日 11時16分47秒
                                                   2018年07月16日 11時18分17秒
  Blocks               8                           8                           
  CRC32                CeUElJ                      CeUElJ                      
  MD5                  CtRlqg8aZBolt8lXcoAu5L      CtRlqg8aZBolt8lXcoAu5L 
  • データベースの更新
    # tripwire --update --twrfile /var/lib/tripwire/report/localhost.localdomain-20180716-111854.twr

整合性チェックはベースラインデータベースと現在のシステムの状態を比較して行われます。
次回チェック時に先ほどのレポートで検知されたファイルを除外する場合は、ベースラインデータベースを更新します。データベースを更新することで、次回チェック時にtestfileは検知されないようになります。

[root@localhost ~]# tripwire --update --twrfile /var/lib/tripwire/report/localhost.localdomain-20180716-111854.twr 
Please enter your local passphrase: 
Wrote database file: /var/lib/tripwire/localhost.localdomain.twd

以下はデータベース更新後に再度、チェックを行った際のレポートです。
testfileは検知されていません。

[root@localhost ~]# twprint --print-report --report-level 0 --twrfile /var/lib/tripwire/report/localhost.localdomain-20180716-113037.twr 
Note: Report is not encrypted.
TWReport localhost.localdomain 20180716113037 V:0 S:0 A:0 R:0 C:0

しかし、このtestfileに対して削除等何らかの操作を行うとまた、検知されるため監視対象外とする場合は、対象ファイルを除外するなどチューニングが必要です。

以下は削除した後に再度、チェックを行い検知された例です。

[root@localhost ~]# twprint --print-report --report-level 0 --twrfile /var/lib/tripwire/report/localhost.localdomain-20180716-113349.twr 
Note: Report is not encrypted.
TWReport localhost.localdomain 20180716113349 V:2 S:100 A:0 R:1 C:1

先ほどのtestfileを監視対象から除外する場合の例を以下に記載します。

  • ポリシーファイルの編集
    # vi /etc/tripwire/twpol.txt

rootセクションの最後に、「!/root/testfile ;」を追記します。

(中略)
# These files change the behavior of the root account
(
  rulename = "Root config files",
  severity = 100
)
{
     /root                             -> $(SEC_CRIT) ; # Catch all additions to /root
     /root/.Xresources                 -> $(SEC_CONFIG) ;
     /root/.bashrc                     -> $(SEC_CONFIG) ;
     /root/.bash_profile               -> $(SEC_CONFIG) ;
     /root/.bash_logout                -> $(SEC_CONFIG) ;
     /root/.cshrc                      -> $(SEC_CONFIG) ;
     /root/.tcshrc                     -> $(SEC_CONFIG) ;
    #/root/Mail                        -> $(SEC_CONFIG) ;
    #/root/mail                        -> $(SEC_CONFIG) ;
    #/root/.amandahosts                -> $(SEC_CONFIG) ;
    #/root/.addressbook.lu             -> $(SEC_CONFIG) ;
    #/root/.addressbook                -> $(SEC_CONFIG) ;
     /root/.bash_history               -> $(SEC_CONFIG) ;
    #/root/.elm                        -> $(SEC_CONFIG) ;
     /root/.esd_auth                   -> $(SEC_CONFIG) ;
    #/root/.gnome_private              -> $(SEC_CONFIG) ;
    #/root/.gnome-desktop              -> $(SEC_CONFIG) ;
     /root/.gnome                      -> $(SEC_CONFIG) ;
     /root/.ICEauthority               -> $(SEC_CONFIG) ;
    #/root/.mc                         -> $(SEC_CONFIG) ;
    #/root/.pinerc                     -> $(SEC_CONFIG) ;
    #/root/.sawfish                    -> $(SEC_CONFIG) ;
     /root/.Xauthority                 -> $(SEC_CONFIG) -i ; # Changes Inode number on login
    #/root/.xauth                      -> $(SEC_CONFIG) ;
    #/root/.xsession-errors            -> $(SEC_CONFIG) ;
    !/root/testfile ;
}
  • ポリシーファイルの更新
    # tripwire --update-policy -Z low /etc/tripwire/twpol.txt

ポリシーファイルの更新は、-Z lowオプションを付けます。オプションを付けない場合は以下のように、エラーとなります。

[root@localhost ~]# tripwire --update-policy /etc/tripwire/twpol.txt 
Parsing policy file: /etc/tripwire/twpol.txt
Please enter your local passphrase: 
Please enter your site passphrase: 
======== Policy Update: Processing section Unix File System.
======== Step 1: Gathering information for the new policy.
(中略)
======== Step 2: Updating the database with new objects.
======== Step 3: Pruning unneeded objects from the database.
Policy update failed; policy and database files were not altered.

Tripwireの仕組みとしては、ポリシーファイル更新時にベースラインデータベースも合わせて更新しています。データベースを更新しないと、ポリシーファイルの変更が反映されないため、新しいルールを追加しても検知されるためです。

Tripwireの動作設定

Tripwireの動作設定は/etc/tripwire/twcfg.txtファイルを参照するか、以下のコマンドを実行することで設定値を確認できます。

  • 動作設定ファイルの表示
    # twadmin --print-cfgfile
ROOT                   =/usr/sbin
POLFILE                =/etc/tripwire/tw.pol
DBFILE                 =/var/lib/tripwire/$(HOSTNAME).twd
REPORTFILE             =/var/lib/tripwire/report/$(HOSTNAME)-$(DATE).twr
SITEKEYFILE            =/etc/tripwire/site.key
LOCALKEYFILE           =/etc/tripwire/$(HOSTNAME)-local.key
EDITOR                 =/bin/vi
LATEPROMPTING          =false
LOOSEDIRECTORYCHECKING =false
MAILNOVIOLATIONS       =true
EMAILREPORTLEVEL       =3
REPORTLEVEL            =3
MAILMETHOD             =SENDMAIL
SYSLOGREPORTING        =false
MAILPROGRAM            =/usr/sbin/sendmail -oi -t
  • 動作設定ファイルの編集
    # vi /etc/tripwire/twcfg.txt
  • 動作設定ファイルの更新
    # twadmin --create-cfgfile --site-keyfile /etc/tripwire/site.key /etc/tripwire/twcfg.txt
12
13
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
12
13