# yum -y install swatch
# mkdir /etc/swatch # mkdir /etc/swatch/scripts
# mkdir -p /var/log/swatch
vi /etc/rc.d/init.d/swatch #!/bin/bash # # swatch # # chkconfig: 2345 90 35 # description: swatch start/stop script # # origial script by http://centossrv.com/swatch.shtml # # $Revision: 4 $ # $Date:: 2009-12-03 18:08:11 #$ System House ACT ($Author: tyamaguchi $) # Source function library. . /etc/rc.d/init.d/functions PATH=/sbin:/usr/local/bin:/bin:/usr/bin PROG="swatch" LOCKFILE="/var/lock/subsys/swatch" start() { # Start daemons. ls /var/run/swatch_*.pid > /dev/null 2>&1 if [ $? -ne 0 ]; then echo -n $"Starting $PROG: " PNO=0 for CONF in /etc/swatch/*.conf do PNO=`expr $PNO + 1` WATCHLOG=`grep "^# logfile" $CONF | awk '{ print $3 }'` swatch --config-file $CONF \ --tail-file $WATCHLOG \ --script-dir=/tmp \ --awk-field-syntax \ --use-cpan-file-tail \ --daemon \ --pid-file /var/run/swatch_$PNO.pid \ >> /var/log/swatch/swatch.log 2>&1 RETVAL=$? [ $RETVAL != 0 ] && return $RETVAL done if [ $RETVAL = 0 ]; then echo_success touch $LOCKFILE else echo_failure fi echo return $RETVAL else echo "$PROG is already started" fi } stop() { # Stop daemons. ls /var/run/swatch_*.pid > /dev/null 2>&1 if [ $? -eq 0 ]; then echo -n $"Stopping $PROG: " for PID in /var/run/swatch_*.pid do kill $(cat $PID) rm -f $PID done echo_success rm -f $LOCKFILE /tmp/.swatch_script.* echo else echo "$PROG is not running" fi } status() { ls /var/run/swatch_*.pid > /dev/null 2>&1 if [ $? -eq 0 ]; then echo -n "$PROG (pid" for PID in /var/run/swatch_*.pid do echo -n " `cat $PID`" done echo ") is running..." else echo "$PROG is stopped" fi } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo "Usage: $PROG {start|stop|restart|status}" exit 1 esac exit $RETVAL
# chmod 755 /etc/rc.d/init.d/swatch # chkconfig swatch on
vi /etc/logrotate.d/swatch /var/log/swatch/swatch.log { missingok notifempty sharedscripts postrotate service swatch restart > /dev/null endscript }
# vi /etc/swatch/scripts/drop_ip_address #!/bin/bash # # IPアドレスによりサーバーへの接続を一定時間拒否する # 引数 : arg1 ... IPアドレスのセパレータ # arg2 ... IPアドレスが存在する位置 # arg3 ... 拒否時間 # arg4 ... 拒否時間単位 # arg5 ... 拒否要因 # # $Revision: 7 $ # $Date:: 2009-12-04 18:27:27 #$ System House ACT ($Author: tyamaguchi $) # 拒否しないIPアドレスリスト(ホワイトリスト) # 複数指定するときは、空白で区切ってください # 前方一致でチェックされます #WHITEIPLISTS="192.168.1." WHITEIPLISTS="" # ログを標準入力から取得 read LOG # ログからIPアドレスを抽出 IPADDR=`echo "$LOG" | sed 's/ */ /g' | cut -d "$1" -f $2 | sed -e 's/[^0-9]*\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)[^0-9]*.*/\1/p' -e d` [ "$IPADDR" = "" ] && exit 0 # ホワイトリストに存在すれば拒否しない for WHITEIP in $WHITEIPLISTS; do [ "$WHITEIP" = $(expr substr "$IPADDR" 1 $(expr length "$WHITEIP")) ] && exit 0 done # 当該IPアドレスからの接続を一定持間拒否する iptables -L -n | grep "DROP.* $IPADDR " > /dev/null if [ $? != 0 ]; then EXPIRE=$(date '+%R' -d "$3 $4") iptables -I INPUT -i eth0 -s $IPADDR -j DROP echo "iptables -D INPUT -i eth0 -s $IPADDR -j DROP" | at "$EXPIRE" > /dev/null logger -p authpriv.info -t SWATCH "Blocking IP $IPADDR for $EXPIRE by $5" fi # chmod +x /etc/swatch/scripts/drop_ip_address
named[xxxx]: client xxx.xxx.xxx.xxx#xxxxx: view ...: query ... denied
swatch の定義ファイルを作成します。# vi /etc/swatch/messages.conf # logfile /var/log/messages # DNSサーバーへの不正アクセス watchfor /named.*client.*query.*denied/ pipe "/etc/swatch/scripts/drop_ip_address \$' ' 7 1 day DNS_Attack"
# service swatch restart
sshd[xxxx]: Invalid user xxxxxxxx from xxx.xxx.xxx.xxx
sshd[xxxx]: Did not receive identification string from xxx.xxx.xxx.xxx
sshd[xxxx]: Received disconnect from xxx.xxx.xxx.xxx
sshd[xxxx]: User xxxxxxxx from xxx.xxx.xxx.xxx not allowed ...
# vi /etc/swatch/secure.conf # logfile /var/log/secure # SSHサーバーへの不正アクセス watchfor /sshd.*Invalid user .* from .*/ pipe "/etc/swatch/scripts/drop_ip_address \$' ' 10 1 day SSH_Attack" watchfor /sshd.*Did not receive identification string from .*/ pipe "/etc/swatch/scripts/drop_ip_address \$' ' 12 1 day SSH_Attack" watchfor /sshd.*Received disconnect from .*/ pipe "/etc/swatch/scripts/drop_ip_address \$' ' 9 1 day SSH_Attack" watchfor /sshd.*User .* from .* not allowed .*/ pipe "/etc/swatch/scripts/drop_ip_address \$' ' 9 1 day SSH_Attack"
# service swatch restart
System House ACT Weblog 内記事 : ログ監視ツール swatch
トラックバックまたはコメントにてお寄せください。| ログ監視ツール導入(SWATCH) - CentOSで自宅サーバー構築 | : | http://centossrv.com/swatch.shtml |
| -- Contents -- |
| Top |
| サーバー構築 |
| メールサーバー |
| ウェブサーバー |
| 開発環境(Linux) |
| 開発環境(Windows) |
| Eclipse |
| 製品情報 |
| BOINC(WCG) |
| Archives |
| Weblog |