iptablesログ解析(IPTables log analyzer)

最終更新日: 2012.05.04

<<トップページ <<新着情報 <<質問掲示板 <<アンケート <<リンク集 <<サイト内検索 <<管理人へメール <<Scientific Linuxで自宅サーバー構築 <<Fedoraで自宅サーバー構築

■概要

IPTables log analyzerを導入して、iptablesのログをWebブラウザからリアルタイムに参照することができるようにする。⇒スナップショット

ファイアウォールWebサーバー+PHPMySQLデータベースが構築済であること


■IPTables log analyzerインストール

[root@centos ~]# yum -y install mysql-devel ← IPTables log analyzerインストールに必要なパッケージをインストール

[root@centos ~]# wget http://jaist.dl.sourceforge.net/sourceforge/iptablelog/iptablelog-v0.9.tar.bz2
 ← IPTables log analyzerダウンロード

※最新版のURLはダウンロードページで確認すること

[root@centos ~]# tar jxvf iptablelog-v0.9.tar.bz2 ← IPTables log analyzer展開

[root@centos ~]# mv iptablelog/ /var/www ← IPTables log analyzer展開先ディレクトリを所定のディレクトリへ移動

[root@centos ~]# rm -f iptablelog-v0.9.tar.bz2 ← ダウンロードしたファイルを削除

■IPTables log analyzer用データベース作成

(1)IPTables log analyzer用データベース作成
IPTables log analyzerはiptablesのログをMySQLデータベースに保存するため、IPTables log analyzer用のデータベースをMySQLに作成する。
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン
Enter password:  ← MySQLのrootパスワード応答
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.37 Source distribution

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

mysql> create database iptablelog; ← iptablelogデータベース作成
Query OK, 1 row affected (0.00 sec)

mysql> grant all on iptablelog.* to iptablelog_user@localhost identified by 'パスワード';
 ← iptablelogデータベースアクセスユーザ作成
Query OK, 0 rows affected (0.00 sec)

mysql> exit ← MySQLからログアウト
Bye

[root@centos ~]# cat /var/www/iptablelog/conf/iptables.mysql | mysql -u iptablelog_user -p iptablelog
 ← iptablelogデータベース初期化
Enter password:  ← iptablesデータベースアクセスユーザのパスワード応答

(2)php-mysqlインストール
IPTables log analyzerはPHPで動作するため、PHPからMySQLデータベースへ接続するためのパッケージであるphp-mysqlをインストールする。
[root@centos ~]# rpm -q php-mysql ← php-mysqlインストール確認
パッケージ php-mysql はインストールされていません。

[root@centos ~]# yum -y install php-mysql ← php-mysqlインストール※インストールされていない場合のみ

■ulogdインストール

iptablesのログをulogd経由でMySQLデータベースに保存するため、ulogdをインストールする。
[root@centos ~]# wget ftp://ftp.netfilter.org/pub/ulogd/ulogd-1.24.tar.bz2 ← ulogdダウンロード

※最新版のURLはダウンロードページで確認すること

[root@centos ~]# tar jxvf ulogd-1.24.tar.bz2 ← ulogd展開

[root@centos ~]# cd ulogd-1.24 ← ulogd展開先ディレクトリへ移動

[root@centos ulogd-1.24]# vi configure ← configure編集
mysql_real_escape_string support... strings: invalid option -- r
        MYSQL_FUNCTION_TEST=`strings ${MYSQLLIBS}/libmysqlclient.so | grep mysql_real_escape_string`
        ↓
        MYSQL_FUNCTION_TEST=`strings /usr/lib/mysql/libmysqlclient.so | grep mysql_real_escape_string` ← 上記エラーメッセージ出力対応

ld: bad -rpath option
        MYSQLLIBS=`$d/mysql_config --libs`
        ↓
        MYSQLLIBS=`$d/mysql_config --libs|sed 's/-rdynamic//g'` ← 上記エラーメッセージ出力対応

[root@centos ulogd-1.24]# ./configure --prefix=/usr --sysconfdir=/etc --libdir=/usr/local/lib --with-mysql \
&& make && make install ← ulogdインストール

[root@centos ulogd-1.24]# cp ulogd.init /etc/rc.d/init.d/ulogd ← ulogd起動スクリプトを所定のディレクトリへコピー

[root@centos ulogd-1.24]# cd ← ulogd展開先ディレクトリを抜ける

[root@centos ~]# rm -rf ulogd-1.24 ← ulogd展開先ディレクトリを削除

[root@centos ~]# rm -f ulogd-1.24.tar.bz2 ← ダウンロードしたファイルを削除

■ulogd設定

iptablesのログをulogd経由でMySQLデータベースへ保存するように設定する。
[root@centos ~]# vi /etc/ulogd.conf ← ulogd設定ファイル編集
#
# ulogd_BASE.so - interpreter plugin for basic IPv4 header fields
#                 you will always need this
plugin="/usr/local/lib/ulogd/ulogd_BASE.so"
plugin="/usr/local/lib/ulogd/ulogd_LOCAL.so" ← 追加

# output plugins.
#plugin="/usr/local/lib/ulogd/ulogd_LOGEMU.so" ← コメントアウト
#plugin="/usr/local/lib/ulogd/ulogd_OPRINT.so"
plugin="/usr/local/lib/ulogd/ulogd_MYSQL.so" ← コメント解除
#plugin="/usr/local/lib/ulogd/ulogd_PGSQL.so"
#plugin="/usr/local/lib/ulogd/ulogd_SQLITE3.so"
#plugin="/usr/local/lib/ulogd/ulogd_PCAP.so"

[MYSQL]
table="ulog"
pass="パスワード" ← iptablelogデータベースアクセスユーザパスワード設定
user="iptablelog_user" ← iptablelogデータベースアクセスユーザ名設定
db="iptablelog" ← iptablelogデータベース名設定
host="localhost"

■ulogd起動

[root@centos ~]# /etc/rc.d/init.d/ulogd start ← ulogd起動
ulogd を起動中:                                            [  OK  ]

[root@centos ~]# chkconfig --add ulogd ← ulogd自動起動設定

■iptables設定

iptablesログをulogdに出力するようにiptablesを設定する。
ここでは、例として、受信パケットのiptablesのログ記録設定を示す。
iptables -A INPUT -m limit --limit 1/s -j ULOG --ulog-nlgroup 1 --ulog-prefix 'INPUT'

ファイアウォール構築(iptables)を参考にしてファイアウォール設定スクリプトを作成している場合は、ファイアウォール設定スクリプトの既存ログ出力コマンド直後へ、ulogdへのログ出力コマンドを追加してファイアウォール設定スクリプトを実行すること(下記参照)
iptables -A INPUT -m limit --limit 1/s -j LOG --log-prefix '[IPTABLES INPUT] : '
iptables -A INPUT -m limit --limit 1/s -j ULOG --ulog-nlgroup 1 --ulog-prefix 'INPUT' ← 追加

■IPTables log analyzer設定

[root@centos ~]# cp /var/www/iptablelog/conf/config.php.default /var/www/iptablelog/conf/config.php

[root@centos ~]# vi /var/www/iptablelog/conf/config.php
# Password of the MySQL database
$db_password="パスワード"; ← iptablelogデータベースアクセスユーザパスワード設定

# File Path to your installation
$file_base="/var/www/iptablelog"; # i.e. "/var/www/html/iptablelog" ← iptablelogWebディレクトリ名設定

[root@centos ~]# cp /var/www/iptablelog/conf/iptables_resolve.default /etc/cron.hourly/iptables_resolve
 ← IPアドレス名前解決スクリプトを定期自動実行ディレクトリへコピー

[root@centos ~]# vi /etc/cron.hourly/iptables_resolve ← IPアドレス名前解決スクリプト編集
$iptablelog_path = "/var/www/iptablelog"; # Change this ← iptablelogWebディレクトリ名設定

db_connect("localhost","iptablelog","iptablelog_user", "パスワード");  # Change these db settings
 ← iptablelogデータベースアクセス情報設定

※IPTables log analyzerバグ対処
[root@centos ~]# vi /etc/php.ini ← PHP設定ファイル編集
short_open_tag = On ← Onにする

[root@centos ~]# vi /var/www/iptablelog/reports/index.php ← Reports設定ファイル編集
require_once("../conf/config.php");
//require_once("conf/config.php"); ← 行頭に//を追加してコメントアウト

#db_disconnect();
  pageFooter();
?>
  pageFooter(); ← 不要な行を削除
?> ← 不要な行を削除


■Apache設定

[root@centos ~]# vi /etc/httpd/conf.d/iptables.conf ← IPTables log analyzer用Apache設定ファイル作成
Alias /iptablelog /var/www/iptablelog

※以下は内部からのみ参照できるようにする場合のみ
<Location /iptablelog>
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
    Allow from 192.168.1.0/24 ← 内部ネットワークアドレスを指定
</Location>

[root@centos ~]# /etc/rc.d/init.d/httpd reload ← Apache設定反映
httpd を再読み込み中:                                      [  OK  ]

■IPTables log analyzer確認

http://サーバー名/iptablelog/でアクセスして、「IPTables logs」ページが表示されることを確認

Tweet Check



▲このページのトップへ戻る

LPIロゴ Copyright© 2012 fallenangels, All rights reserved.
ご自由にリンクしてください(連絡は不要です)
本ページへのご意見・ご要望、誤字・脱字・リンク切れ等のご連絡はこちらからお願いします