Created
July 2, 2012 12:57
-
-
Save brokendish/3033145 to your computer and use it in GitHub Desktop.
syslogの情報からリアルタイムにアクセス制御
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#-------------------------------------------------------------------- | |
#リアルタイムアクセス拒否β(syslog又はmail.log専用) | |
# | |
#処理概要: | |
# ①syslogを(ほぼ)リアルタイムで監視して、拒否したいアクセスをしてきたIPアドレスを | |
# 「iptables」に登録して、全ての通信を拒否する。 | |
# ②アクセス拒否IPを累積する | |
# ③アクセス拒否IP登録メールをroot宛に送信する | |
# | |
#注意: | |
# β版なので、期待しない。 | |
# アクセス拒否には「iptables」を使用しています。 | |
# | |
#使用条件: | |
# ファイルの更新監視には「inotify」を使用しているので、inotifyがインストールされていること。 | |
# Debian、Ubuntuの場合、「apt-get install inotify-tools」でインストール! | |
# ※カーネル2.6.13 以降 | |
# 「iptables」を使用しているのでインストールされていること。 | |
# | |
#使用方法: | |
# 実行例1:バックグラウンドで実行 | |
# ./inotifywait-syslog.sh & | |
# | |
#実行例2:フォアグラウンドで実行 | |
# ./inotifywait-syslog.sh | |
# | |
#-------------------------------------------------------------------- | |
#---------------------------------------- | |
# 初期設定 | |
#---------------------------------------- | |
#対象ログファイル | |
#chkLogFile=/var/log/syslog | |
chkLogFile=/var/log/mail.warn | |
#chkLogFile=/var/log/mail.log | |
#chkLogFile=/home/ほげ/hoge/test_syslog | |
#アクセス拒否IP累積リスト | |
denyIpFile=/var/log/apache2/denyIP | |
#denyIpFile=/home/ほげ/hoge/denyIP | |
#アクセス拒否判定文字列:[authentication failed]出したヤツのIP取得 | |
chkStr='.+authentication failed.+' | |
#認証エラーの上限回数 | |
limit=4 | |
#---------------------------------------- | |
touch $denyIpFile | |
while inotifywait -e modify $chkLogFile; | |
do | |
#アクセス拒否判定文字列の検索(累積リストにあるものは対象外) | |
cnt=`grep -f $denyIpFile -v $chkLogFile|egrep "$chkStr"|wc -l|cut -d " " -f 1` | |
#引っかかった場合 | |
if [ "$cnt" != "0" ]; | |
then | |
#作業リストとしてIPアドレスを保持する | |
grep -f $denyIpFile -v $chkLogFile | | |
egrep "$chkStr" | | |
sed -e 's/\[/@/g' -e 's/\]/@/g' | | |
awk 'BEGIN{FS="@"}{print $4}' | | |
sort | | |
awk 'BEGIN{cnt=1;buf=$1;}{if($1==buf){cnt=cnt+1;}else{if($limit<cnt){print buf;}cnt=1;}buf=$1;}END{if(4<cnt){print buf;}}'>$denyIpFile$$ | |
#累積リストと今回のリストを比較して新規をiptablesに登録 | |
for inIp in `comm -23 $denyIpFile$$ $denyIpFile`; | |
do | |
iptables -I INPUT -s $inIp -j DROP | |
# echo "TEST--@@@@@@@@@@@@@@@iptables -I INPUT -s $inIp -j DROP@@@@@@@@@@@@@@@@@" | |
#メール通知 | |
echo "inotifywait-syslog LOCK IP" '\n' `whois $inIp | | |
grep -i country` "=" $inIp '\n' "----Sumally----" '\n' `cat $denyIpFile`| | |
mail -s "inotifywait-syslog LOCK IP!!" "root" | |
#echo "inotifywait-syslog LOCK IP" '\n' `whois $inIp|grep -i country` "=" $inIp '\n' "----Sumally----" '\n' `cat $denyIpFile` | |
done | |
#累積リストに追加 | |
cat $denyIpFile>>$denyIpFile$$ | |
cat $denyIpFile$$|sort|uniq>$denyIpFile | |
#作業リストを削除 | |
rm $denyIpFile$$ | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment