xxx.xx.xx.xx - - [02/Apr/2004:17:13:06 +0800] "SEARCH /

回覆文章
yehlu
Site Admin
文章: 3244
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

xxx.xx.xx.xx - - [02/Apr/2004:17:13:06 +0800] "SEARCH /

文章 yehlu »

相關資料
http://free.tnc.edu.tw/modules/newbb/vi ... 9&forum=17

http://free.tnc.edu.tw/modules/newbb/vi ... 2&forum=16

#!/bin/sh
#
# 軟體需求: Linux kernel 2.4.x , iptables , bash ,
# awk , date , find , grep , sort , touch 等.
#
# 使用前請先建立一個目錄, 預設為 /var/spool/banips
#
# 將這個 script 用 crontab 執行, 建議每天 0:00~1:00 執行
# 例如: 13 0 * * * /usr/local/sbin/banip
#
# 變數區
# iptables 指定過濾用的指令
# spool 指定存放被檔掉的 IP 位置
# datebe 昨天的日期(error_log 用)
# dateymd 昨天的日期(被擋掉的 IP 存檔檔名)
# days 同一個 IP 擋掉的天數
iptables=/usr/sbin/iptables
spool=/var/spool/banips
days=3
datebe=`date -d yesterday +"%b %e"`
dateymd=`date -d yesterday +%Y%m%d`

#檢查 iptables 是否為可執行檔
if [ ! -x $iptables ]; then
echo "Please use "$iptables" to filter.";
exit
fi

#檢查存放的目錄是否存在
if [ ! -d $spool ]; then
echo "Please create spool directory "$spool" or change one.";
exit
fi

#將超過時間的檔案刪掉
find $spool -type f -ctime +"$days" -exec rm {} \;

#建立新檔案
touch $spool/$dateymd

#檢查檔案是否已建立
if [ ! -r $spool/$dateymd ]; then
echo "Sorry, can not create "$spool"/"$dateymd;
exit
fi

#找出 error_log 紀錄中, 有問題的連線 IP 或 Domain Name
#URI too long
cat /var/log/apache/error_log \
|grep "URI too long" \
|awk '{FS="client ";print $2}' \
|awk '{FS="] request";print $1}' \
>> $spool/$dateymd

#cltreq.asp
cat /var/log/apache/error_log \
|grep "cltreq.asp" \
|awk '{FS="client ";print $2}' \
|awk '{FS="] File does not exist";print $1}' \
>> $spool/$dateymd

#找出 maillog 紀錄中, 不明的連線
#註: 我有使用 tcpwrappers 擋掉沒有 FQDN 的連線
grep "$datebe" /var/log/maillog \
|grep "unknown" \
|grep "tcpwrappers" \
|awk '{FS="unknown, ";print $2}' \
|awk '{FS=") rejection";print $1}' \
>> $spool/$dateymd

#找出 /var/log/secure 中, 試著入侵 proftpd 的連線
grep "$datebe" /var/log/secure \
|grep "refused connect from" \
|awk '{FS="refused connect from ";print $2}' \
>> $spool/$dateymd

#找出 /var/log/syslog 中, 試著入侵 sshd 的連線
grep "$datebe" /var/log/syslog \
|grep "refused connect from" \
|awk '{FS="refused connect from ";print $2}' \
>> $spool/$dateymd

#清除舊的設定
$iptables -F INPUT

#執行新的設定
cat $spool/* \
|grep "\." \
|sort \
|uniq \
|xargs -l1 $iptables -A INPUT -j DROP -s

#若有任何電腦不想讓他連線, 同樣丟到 $spool 指定的目錄即可, 會擋掉三天
#高雄市新興社區大學 薛百鈞
回覆文章

回到「Linux」