iptables ログ解析ツールの導入 [IPTables log analyzer]

インストール

1. IPTables log analyzer をダウンロードします。

# wget -P /tmp http://jaist.dl.sourceforge.net/sourceforge/iptablelog/iptablelog-v0.9.tar.gz
--07:57:03--  http://jaist.dl.sourceforge.net/sourceforge/iptablelog/iptablelog-v0.9.tar.gz
Resolving jaist.dl.sourceforge.net... 150.65.7.130
Connecting to jaist.dl.sourceforge.net|150.65.7.130|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 320654 (313K) [application/x-gzip]
Saving to: `/tmp/iptablelog-v0.9.tar.gz'

100%[=================================================>] 320,654     1.65M/s   in 0.2s

07:57:04 (1.65 MB/s) - `/tmp/iptablelog-v0.9.tar.gz' saved [320654/320654]

2. ダウンロードしたファイルを解凍して Apache のドキュメントルート配下へ配置します。

# tar xzf /tmp/iptablelog-v0.9.tar.gz -C /var/www/html

3. ダウンロードしたファイルを削除します。

# rm -f /tmp/iptablelog-v0.9.tar.gz

4. IPTables log analyzer の動作に必要な PHP の MySQL サポートや ulogd パッケージなどをインストールします。

# yum -y install php-mysql ulogd ulogd-mysql
Loading "fastestmirror" plugin
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
updates                   100% |=========================| 1.2 kB    00:00
core                      100% |=========================| 1.1 kB    00:00
extras                    100% |=========================| 1.1 kB    00:00
Determining fastest mirrors
Reading repository metadata in from local files
primary.xml.gz            100% |=========================| 491 kB    00:00
updates   : ################################################## 1225/1225
primary.xml.gz            100% |=========================| 1.6 MB    00:00
extras    : ################################################## 5158/5158
Excluding Packages in global exclude list
Finished
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for php-mysql to pack into transaction set.
php-mysql-5.1.6-3.6.fc6.i 100% |=========================|  17 kB    00:00
---> Package php-mysql.i386 0:5.1.6-3.6.fc6 set to be updated
---> Downloading header for ulogd to pack into transaction set.
ulogd-1.24-2.fc6.i386.rpm 100% |=========================| 7.3 kB    00:00
---> Package ulogd.i386 0:1.24-2.fc6 set to be updated
---> Downloading header for ulogd-mysql to pack into transaction set.
ulogd-mysql-1.24-2.fc6.i3 100% |=========================| 3.9 kB    00:00
---> Package ulogd-mysql.i386 0:1.24-2.fc6 set to be updated
--> Running transaction check

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
 php-mysql               i386       5.1.6-3.6.fc6    updates            83 k
 ulogd                   i386       1.24-2.fc6       extras            134 k
 ulogd-mysql             i386       1.24-2.fc6       extras            7.8 k

Transaction Summary
=============================================================================
Install      3 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 225 k
Downloading Packages:
(1/3): php-mysql-5.1.6-3. 100% |=========================|  83 kB    00:00
(2/3): ulogd-1.24-2.fc6.i 100% |=========================| 134 kB    00:00
(3/3): ulogd-mysql-1.24-2 100% |=========================| 7.8 kB    00:00
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: ulogd                        ######################### [1/3]
  Installing: php-mysql                    ######################### [2/3]
  Installing: ulogd-mysql                  ######################### [3/3]

Installed: php-mysql.i386 0:5.1.6-3.6.fc6 ulogd.i386 0:1.24-2.fc6 ulogd-mysql.i386 0:1.24-2.fc6
Complete!

データベースの作成

1. MySQL へログインして IPTables log analyzer 用のデータベースとデータベースユーザを作成します。

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.27

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

iptablelog データベースを作成します。
mysql> CREATE DATABASE iptablelog;
Query OK, 1 row affected (0.00 sec)

iptablelog ユーザを作成します。
mysql> GRANT ALL ON iptablelog.* TO iptablelog@localhost IDENTIFIED BY 'パスワード';
Query OK, 0 rows affected (0.00 sec)

ログアウトします。
mysql> exit
Bye

2. iptablelog データベースへ IPTables log analyzer 用のテーブル群を作成します。

# mysql -u iptablelog -p < /var/www/html/iptablelog/conf/iptables.mysql iptablelog
Enter password:

ULOG 設定

iptables のログを ulogd 経由で MySQL データベースへ出力するように設定します。

1. ulogd 設定ファイルを編集します。

# vi /etc/ulogd.conf
# Example configuration for ulogd
# $Id: ulogd.conf.in 5267 2005-02-19 21:33:43Z laforge $
#

[global]
######################################################################
# GLOBAL OPTIONS
######################################################################

# netlink multicast group (the same as the iptables --ulog-nlgroup param)
nlgroup=1

# logfile for status messages
logfile="/var/log/ulogd/ulogd.log"

# loglevel: debug(1), info(3), notice(5), error(7) or fatal(8)
loglevel=5

# socket receive buffer size (should be at least the size of the
# in-kernel buffer (ipt_ULOG.o 'nlbufsiz' parameter)
rmem=131071

# libipulog/ulogd receive buffer size, should be > rmem
bufsize=150000

######################################################################
# PLUGIN OPTIONS
######################################################################

# We have to configure and load all the plugins we want to use

# general rules:
# 1. load the plugins _first_ from the global section
# 2. options for each plugin in seperate section below


#
# ulogd_BASE.so - interpreter plugin for basic IPv4 header fields
#                 you will always need this
plugin="/usr/lib/ulogd/ulogd_BASE.so"


# output plugins.
plugin="/usr/lib/ulogd/ulogd_LOGEMU.so"
#plugin="/usr/lib/ulogd/ulogd_OPRINT.so"
MySQL 用モジュールを有効にします。
plugin="/usr/lib/ulogd/ulogd_MYSQL.so"
#plugin="/usr/lib/ulogd/ulogd_PGSQL.so"
#plugin="/usr/lib/ulogd/ulogd_SQLITE3.so"
#plugin="/usr/lib/ulogd/ulogd_PCAP.so"


[LOGEMU]
file="/var/log/ulogd/ulogd.syslogemu"
sync=1

[OPRINT]
file="/var/log/ulogd/ulogd.pktlog"

データベースへの接続情報を設定します。
[MYSQL]
table="ulog"
pass="パスワード"
user="iptablelog"
db="iptablelog"
host="localhost"

[PGSQL]
table="ulog"
schema="public"
pass="changeme"
user="postgres"
db="ulogd"
host="localhost"

[SQLITE3]
table="ulog"
db="/path/to/sqlite/db"
buffer=200

[PCAP]
file="/var/log/ulogd/ulogd.pcap"
sync=1

2. ulogd を起動します。

# service ulogd start
Starting ulogd:                                            [  OK  ]

3. ulogd の自動起動を有効にします。

# chkconfig ulogd on

4. iptables のログを ulogd へ出力するように変更します。
※以下では入力に対する簡易的なルール設定をしています。必要により適宜変更してください。

# iptables -A INPUT -j ULOG --ulog-nlgroup 1 --ulog-prefix 'INPUT'

設定

1. IPTables log analyzer 設定ファイルの雛形をコピーします。

# cp /var/www/html/iptablelog/conf/config.php.default /var/www/html/iptablelog/conf/config.php

2. IPTables log analyzer 設定ファイルを編集します。

# vi /var/www/html/iptablelog/conf/config.php
<?php

/////////////////////////////////////////////////////////////////////////////////////
// IPTable log analyzer
// Copyright (C) 2002 Gerald GARCIA
//
// 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 Plac<B2>e - Suite 330, Boston, MA  02111-1307, USA.
//
// This release put together by: junk@futurewest.ca
// Contact original author : gege@gege.org
/////////////////////////////////////////////////////////////////////////////////////

// $Id: config.php.default,v 1.3 2007/02/23 22:44:52 tarbuck Exp $

# Host of the MySQL database
$db_host="localhost";

# User of the MySQL database
$db_user="iptablelog";

# Password of the MySQL database
$db_password="パスワード";

# Name of the database
$db_name="iptablelog";

# Table format (ulog or native, native is default)
# Note: ulog schema is not compatible with ignored ports
$host_resolution_avail = 1;

# URL Path to your installation
$url_base="/iptablelog/";

# File Path to your installation
$file_base="/var/www/html/iptablelog"; # i.e. "/var/www/html/iptablelog"

(以下略)

3. IPTables log analyzer 用 IP アドレス名前解決用スクリプトの雛形をコピーします。

# cp /var/www/html/iptablelog/conf/iptables_resolve.default /var/www/html/iptablelog/conf/iptables_resolve

4. IPTables log analyzer 用 IP アドレス名前解決用スクリプトを編集します。

# vi /var/www/html/iptablelog/conf/iptables_resolve
#!/usr/bin/php -q
<?

$iptablelog_path = "/var/www/html/iptablelog"; # Change this
include_once ("$iptablelog_path/utils/db_mysql.php");

db_connect("localhost","iptablelog","iptablelog", "パスワード");  # Change these db settings

5. IPTables log analyzer 用 IP アドレス名前解決用スクリプトを Cron へ登録します。

# ln -s /var/www/html/iptablelog/conf/iptables_resolve /etc/cron.hourly/iptables_resolve

6. レポート設定ファイルの雛形をコピーします。

# cp /var/www/html/iptablelog/reports/conf/config.php.default /var/www/html/iptablelog/reports/conf/config.php

7. レポート雛形ファイルを編集します。

# vi /var/www/html/iptablelog/reports/conf/config.php
<?php
/*
 *  Report specific configuration details go here
 *
 *  format of variable names is $reports_[report name]_[variable name]
 */

# user report
ドメイン名を指定します。
$reports_user_localdomain = "orangesignal.com";


Apache 設定

1. IPTables log analyzer 用 Apache 設定ファイルを作成します。

# vi /etc/httpd/conf.d/iptablelog.conf
<Directory /var/www/html/iptablelog>
    LAN 環境からのみアクセス可能とします。
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1 192.168.11
</Directory>

2. 設定変更に間違いがないか検証します。

# apachectl configtest
Syntax OK

3. Apache 設定を再読み込みします。

# service httpd reload
Reloading httpd:                                           [  OK  ]

Web ブラウザから http://サーバー名/iptablelog/ へアクセスします。

Google