» 您尚未 登录   注册 | 社区服务 | 帮助 | 社区 | 无图版


常州五颜六色网络技术有限公司 -> 网站建设 -> linux自动屏蔽IP工具
 XML   RSS 2.0   WAP 

--> 本页主题: linux自动屏蔽IP工具 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题
deserts


头衔:大客部大客部
该用户目前不在线
级别: 总版主
精华: 0
发帖: 622
威望: 2 点
金钱: 1096 RMB
贡献值: 0 点
在线时间:1766(小时)
注册时间:2006-01-01
最后登录:2026-07-26
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

linux自动屏蔽IP工具

另存为 firewall.sh 给执行的权限 XHYVcwmDz-  
MB!$s_~o#L  
#!/bin/sh ~]C m   
# this program is used to check tcp/ip connections /5r!Fhx  
# and block those ip with excessive connections tY0C& u2  
Y4,LXuQ  
# my version Uv%?z0F<C  
myver="1.0RC1" |iGfWJ^+  
HN~  
# wake up every 120s if last check found abuse client /p7-D;  
wakeup_time_min=120 KV]8 o'  
d!#qBn$*[  
# wake up every 300s if last check found no abuse client Hl b%/&  
wakeup_time_max=300 HRF;qR9v  
qJq2Z.>hy  
# rule timeout 3600s pqnZ:'V  
rule_timeout=3600 #b5V/)K  
U]W+ers  
# check port list E'wJ+X9 +  
portlist="80" #azD& 6`  
uwhb-.w  
# max established connection per ip 9Netnzv%  
max_active_conn=8 ZINqIfc  
iR6w )  
# iptables chain name 3|9 U`@  
iptables_chain_name="RH-Lokkit-0-50-INPUT" kSq1Q#Bxq  
Qz9*o  
# log facility z-;2)RkV2  
log_facility="local0" 0OAHD'  
RT$.r5l_@  
# Block policy ~.L\f%<  
ipchains_block_policy="DENY" '980.  
iptables_block_policy="REJECT" wR*>9LjeG  
( S=RFd  
# myself )[d>?%vfd  
myself=`basename $0` 2c4x=%  
4g}'/  
mylogger_info() ?O.1HEr  
{ <C6*-j1oz  
logger -p $log_facility.info -t $myself $@ 2>/dev/null s+l3]Hd  
} 6 x8P}?  
ERQc1G]3Dd  
mylogger_debug() :^[HDI-[2  
{ Ccx1#^`  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null b w2KD7  
} X cDu&6Dy  
jt?.g'  
mylogger_notice() M)v4>Rw+  
{ %=GF  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null ;%_s4  
} MrGq{,6C  
h.ln%6:d  
dotimeout() 7\xGMC ctM  
{ . vJlTg  
mylogger_info "reset firewall when timeout arrives" OqcM3#  
case "$firewall" in ~R!M.gY[rK  
ipchains) ]#*S.  r]  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null  5gZ6H/.  
  if [ $? = 0 ] ; then <*'%Xgm  
    mylogger_info "ipchains restarted" f}C$!Lhs  
  else o&Vti"fpC  
    mylogger_notice "ipchains restart failed" 2uln)]  
  fi O7of9F~"  
  ;; b2<((H  
iptables) *@1(!A  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null 6J""gyK.  
  if [ $? = 0 ] ; then E@(nKe&6T_  
    mylogger_info "iptables restarted" jr`;H  
  else <Mq vGXI  
    mylogger_notice "iptables restart failed" V }?MP-.c  
  fi XGCjB{IV  
  ;; yPXa  
*) %UXmWXF4$  
  mylogger_notice "neither ipchains nor iptables" fb*h.6^y9  
  ;; :H(w W   
esac H,q-*Kk  
9m+ejTK{U  
} !-cK@>.pE  
1&.q#,EMn(  
blockclient() N7 ox#=g  
{ ,%X"Caz  
if [ -z "$1" ] || [ -z "$2" ]; then WM8])}< L  
mylogger_notice "blockclient() missing client or port to block" &)ED||r,  
return .:I^O[k  
fi Q*54!^l+_r  
local ip port vKFEA7  
lK%)a +2  
ip=$1 ;BmPP,  
port=$2 VYw<8AEFY  
!~_6S*~  
case "$firewall" in ym;I(TC+  
ipchains) V M{Sng  
  mylogger_notice "blocking $1 to $2 via ipchains" lKBI3oYn  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` Y1-=H)G  
if [ -z "$found" ] ; then m`9P5[m#x>  
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" m}32ovpw  
mylogger_debug "cmd: $cmd" +#Ga} e CM  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` 6ee1^>  
if [ $? != 0 ] ; then [5;_XMj%  
mylogger_notice "$cmd call failed" /:ju/ ~R}  
return 4K0Fc^-  
fi ?`3G5at)9f  
new_block=1 y3Ul}mVhA  
ever_block=1 C {G647  
else l{8t;!2t  
mylogger_info "$ip already blocked to $port" Z5` U+ (  
fi [H=l# W@  
  ;; ?OBB)hj  
iptables) G7k.YtW  
  mylogger_notice "blocking $1 to $2 via iptables" :a*F>S!  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` Z|BOuB^   
if [ -z "$found" ] ; then 5|g#>sx>`q  
cmd="iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null" !|-:"hE1h  
mylogger_debug "cmd: $cmd" J:dNV <A^  
`iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` (AT)w/  
if [ $? != 0 ] ; then :auq#$B  
mylogger_notice "$cmd call failed" NC%)SG \  
return yJ2A!id  
fi s@K #M  
new_block=1 [(iJj3s!  
ever_block=1 66'AaA;0^i  
else V&R_A~<T  
mylogger_info "$ip already blocked to $port" t Z]b0T(e  
fi fH 0&Wc3yC  
  ;; `_E@cZ4  
*) aq$adPtu  
  mylogger_notice "neither ipchains nor iptables" 84y#L[  
  ;; dUP8[y  
esac N  Bpf  
} p} i5z_tS  
3k* U/*  
restartservice() AjMx\'(C  
{ $qk(yzY  
local service u =| A  
if [ -z "$1" ] ; then r8vF I6J  
mylogger_notice "no port given to see which service to be restart" /;xmM 2B'  
return `YPNVm<3)  
fi Y?V>%eBu  
>TwL&la  
case "$1" in \bumB<w(]  
80) @(s"5i.`)  
  service="httpd" Oz7WtN  
  ;; :EZ"D#>y~  
25) i>L>3]SRr{  
  service="postfix" ($Y6hn+  
  ;; |T^c(RpOE  
110) P]||Xbbp  
  service="courier-pop3d" w|WehNGr  
  ;; jwZBWt )5  
21) 3*$)9'  
  service="muddleftpd" K(B|o6[  
  ;; rg{|/ ;imT  
53) Z4@GcdZ  
  service="named" f S(IN~  
  ;; B&RgUIrFoY  
3306) 0N19R5NN8  
  service="mysqld" & !I$  
  ;; sy;_%,}N  
esac 6_h'0~3?`  
if [ ! -z "$service" ] ; then m6oaO9"K  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null +nT(>RJR  
if [ $? = 0 ] ; then b 6B5  
  mylogger_notice "$service restarted" k(.6K[ b  
else PhOtSml0  
  mylogger_notice "$service restart failed" :gJ?3LwTf  
fi Z&E!m   
fi  uWE :3  
}  *c6o#[l  
j"c30AY  
docheckport()  N\9 Wxz$  
{ QZs ]'*=#  
mylogger_info "do check port $1" 5p7 ?e3  
local port last_client count client total_count lEe<!B$d"  
@ =M:RA  
if [ -z "$1" ] ; then OEFAL t  
mylogger_notice "docheckport() port not given" C[xY 0<^B  
return ?|;q=p`t-  
fi m=hUHA,p4  
~d072qUos  
port=$1 }; ;Thfd  
KtO|14R:  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` EDf"1b{PX  
if [ $? != 0 ] ; then 9$[PA jwk  
mylogger_notice "netstat call failed" ?8GggJC  
return 1\aJ[t  
fi zY=eeG+4s  
#echo $clientlist  4,]z  
# reset new_block zv8AvNDK  
new_block=0 ._i|+[  
count=0 <n-}z[09  
total_count=0 s9)U",  
last_client="" ZZu{c t9  
for client in $clientlist 7H l>UX,|  
do ni gn" r  
#echo "client is $client" \Qvo L  
if [ -z "$last_client" ] ; then <1%(%KdN[  
  count=$((count+1)) aVvma=  
  total_count=$((total_count+1)) . \t8s0A  
  last_client=$client Oe~x,=X)  
else PptVneujI  
  if [ "$client" = "$last_client" ] ; then 6Lb(oY}\3  
  count=$((count+1)) 2Pm[ kD4E=  
  total_count=$((total_count+1)) u _mtdB'  
  else 2t[P-on  
  mylogger_debug "$last_client $count connections" }B e;YIhG  
  if [ $count -ge $max_active_conn ] ; then ZO#f)>s2  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" kfV}ta'^S  
    blockclient $last_client $port B{ Ab #  
  fi 'EHt A9M  
  count=1 uo8[,'  
  total_count=$((total_count+1)) Cvp!(<<gK  
  last_client=$client 3%[;nhbA7  
  fi ;p+[R+ )  
fi :;hz!6!  
done lsaA    
# check the last client lD SF  
if [ ! -z "$client" ] ; then 0MI4"<  
count=$((count+1)) Uc;~q-??#  
total_count=$((total_count+1)) ZfMs6`Wv 1  
mylogger_debug "$client $count connections" B[,AR"#b  
if [ $count -ge $max_active_conn ] ; then %E \zR/  
  mylogger_notice "client $client connection $count >= $max_active_conn" AP:Q]A6}  
  blockclient $client $port \$4 [qG=  
fi J%u,qF}h  
fi T-LX>*  
mylogger_info "total connections on port $port: $total_count" c'.XC}  
!-t,r%CG  
if [ $new_block = 1 ] ; then M#_|WL~  
restartservice $port dEZUK vo  
fi -VeC X]  
} Q5^ #:uZ  
Z]e`bfNnI  
docheckall() s&hr$`V4  
{ en'"" w  
# reset wakeup_time =EYWiK77a  
wakeup_time=$wakeup_time_max Cz)&R^  
for port in $portlist gBky ZK  
do 1ysQvz  
docheckport $port $J^fpXO  
if [ $new_block = 1 ] ; then m"T}em#   
  # set wakeup_time shorter cause we found some abuse client u0GHcpOm  
  wakeup_time=$wakeup_time_min Z\$M)e8n  
fi lm6hFvEZ  
done ME$J42  
} 0,nz*UDk  
,3!$mQL=  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then HhL;64OYa  
firewall="ipchains" @)BO`;*$fF  
fi oNtoqYwH  
#1/~eIEY  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then f"KrPx!^b  
firewall="iptables" <^\rv42'(2  
fi Bb:C^CHIQm  
'L7qf'RV  
if [ -z "$firewall" ] ; then h~m,0nGO  
echo "Error: This machine does not have ipchains or iptables firewall support" mUbm3JIjJ  
exit 1 g_1#if&  
fi dYwkP^KB  
b?l>vUgAg  
mylogger_info "firewall.sh v$myver ValueOf.com starting" 0muC4  
mylogger_info "Firewall is:       $firewall" {Q3OT  
mylogger_info "Port protected:     $portlist" |82q|@e  
mylogger_info "Max connection per ip: $max_active_conn" ~PI 2G 9  
mylogger_info "Min time to check:   $wakeup_time_min""s" | L8 [+_m  
mylogger_info "Max time to check:   $wakeup_time_max""s" pY`$k#5  
mylogger_info "Timeout circle:     $rule_timeout""s" g5|~ i{"0  
mylogger_info "Output is logged to:   $log_facility" =nGFLH6)  
!1G KpL  
# if new ip blocked at this check run? ZQ]qJDk  
new_block=0 Ifn|wrx;g  
# if new ip blocked at this timeout run? 393c |8M  
ever_block=0 ^o{{kju  
# reset wakeup_time x%l(0K  
wakeup_time=$wakeup_time_max J5G<Y*q  
w6i2>nu_O  
lasttime=`date +%s` b^VRpv  
|Dn Zk3M,  
while [ 1 ] LUbj^iQ9  
do sfyLG3$/  
curtime=`date +%s` 5rows]EJJl  
timediff=$((curtime-lasttime)) Y(g_h:lf,]  
#echo "timediff: $timediff" Vr EGR$  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then ?$|tT\SFV  
lasttime=$curtime d}\]!x3t  
ever_block=0 :)~l3:O  
dotimeout DK74s  
fi h mRmU{(Y  
docheckall 8ttJ\m  
mylogger_info "sleep for $wakeup_time""s" 9\4x<*  
sleep $wakeup_time ij $NTY=u  
done ZaYiby@Ci  
&<N8d(  
^ Kz ?SO  
#yochxF_  
1. 说明 /x??J4r0  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, )/jDt dI  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 R! M'  
该程序可以同时保护多个端口 HPt3WBRzS;  
6{I7)@>N   
2. 安装 9WG=3!-@  
tar zxf firewall-1.0b.tar.gz oJTEN}fL  
cd firewall-1.0b /(^-= pAX  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh =3035{\  
Z ?+ )ox  
3. 配置 xw8k<`  
主要配置项目如下: ! V.]mI  
# 最小检查周期,缺省为120秒 > SZ95@Oh  
wakeup_time_min=120 nGVr\u9z  
8'Q+%{?1t  
# 最大检查周期,缺省为300秒 vFe=AY<Rt|  
wakeup_time_max=600 E <yQB39  
fz|*Plv  
# 重设防火墙状态的时间,缺省为3600秒 `:iMGq ZN  
rule_timeout=3600 cIO/8D#zU  
%g7j7$c  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) )j8'6tk)Z  
# 一般的网络攻击都是针对80和25,又以80居多 Zna6-0o  
portlist="80 25" Z2W&_(^.h  
@g[ijs\  
# 每个ip可占用的最大活动(Established)连接数 h Wt_}'  
max_active_conn=8 ikZYc ${  
gX!K%qJBg  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 A5R"|<UPR  
# 如果用的是ipchains,可以忽略此项 Ge)G.>c  
iptables_chain_name="RH-Lokkit-0-50-INPUT" ar@,SKU'K  
MTwzL<@$  
# 日志输出目标 Nn_b  
log_facility="local0" }D1? Z7p  
s[4qC  
**** 关于检查周期 **** p!E*A NwX  
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 &kf \[|y  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 VnN(lJ  
Oh}52=  
**** ipchains vs iptables **** p+Yy"wH:h{  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 FpB3SJ6 B  
/etc/sysconfig/iptables都没有检测到,则报错退出。 fhk(<KZvJ  
\HD:#a  
**** 日志输出 **** wI7.M Gt  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf rhF2U  
中加入一条: D{7sfkcJ  
local0.*                 /var/log/firewall.log oVl:g:K40  
然后重新启动syslog =m|<~t  
/etc/init.d/syslog restart nF-l4=  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 Nt tu)wr  
S!-t{Q+j^  
4. 运行 9QD+  
/usr/prima/sbin/firewall.sh & r" |do2s  
9r:|u:i7m  
范例输出: ~Y1nU-  
*** firewall.sh v1.0b ValueOf.com*** a U<+ `  
Firewall is:       ipchains [:izej(\  
Port protected:     80 25 (\'lV8} U  
Max connection per ip: 8 pgUjje>#  
Min time to check:   120s %U[H`E  
Max time to check:   300s 5IgO4<B  
Timeout circle:     3600s TCgW^iu  
Output is logged to:   local0 \+ Es e-la  
+Lr`-</VF  
察看/var/log/firewall.log,可以看到: Q*f0YjH!  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 xgsEe3|  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 p`"Ic2xPJ  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 F)aF.'$-/  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 l?X)]1  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 9*s:Vff{  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 Tj2pEOu  
zq$0 ?vGd  
5. 停止 n "I{aJ]K  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 +wGvY r  
# ps auxww|grep firewall.sh M>*xbBl  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh $}H,g}@0  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh msq2/sS~  
第一行即firewall.sh的进程,用kill命令: Wq25,M'  
# kill 27932 2p*!up(  
[1]   Terminated             /usr/prima/sbin/firewall.sh a 4? c~bs  
即将其终止


常州电信/网通机房,100M共享/10M独享/1000M共享/100M独享/电信+网通双线路服务器托管

Tel:0519-89991155 企业QQ:4006023839   5y6s Inc.
[楼 主] | Posted: 2008-01-26 02:12 顶端
deserts


头衔:大客部大客部
该用户目前不在线
级别: 总版主
精华: 0
发帖: 622
威望: 2 点
金钱: 1096 RMB
贡献值: 0 点
在线时间:1766(小时)
注册时间:2006-01-01
最后登录:2026-07-26
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

通过脚本自动屏蔽非法IP

http://www.bornin76.cn/?p=31 ]&X}C{v)G  
/z: mi  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! N{uVh;_  
?$VkMu$2k  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: 5 &0qr$  
sEi9<$~R@0  
sGc.;":  
#! /bin/bash WJN) <+d  
Yh; A)N p  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` E@4/<;eKK  
Pk;YM}  
for i in $SCANIP i:1 @ vo  
do \}_,g  
NUMBER=`echo $i|awk -F= '{print $1}'` aP#/%  
SCANIP=`echo $i|awk -F= '{print $2}'` MfO: BX@$  
echo $NUMBER c7RQ7\  
echo $SCANIP =Q*3\ )7  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] ],Yy)<e.  
then 9dWz3b1[ ]  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP 2OQ\ z;s  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log {(I":rt#  
fi Z58{YCY  
done cQm4q19  
=}.gU WV  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 ;; ;=)'o  
$tej~xZK  
"lSh 4X  
------------------------------------------------------- Ej1 <T,w_  
[bRE=Zr$Ry  
注释: @0d"^  
fH$#vRcq  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过IP伪装了而已。


常州电信/网通机房,100M共享/10M独享/1000M共享/100M独享/电信+网通双线路服务器托管

Tel:0519-89991155 企业QQ:4006023839   5y6s Inc.
[1 楼] | Posted: 2008-01-26 04:39 顶端

常州五颜六色网络技术有限公司 -> 网站建设



Copyright © 2005-2009 5y6s Inc. 苏ICP备05001866号 Powered by PHPWind 5.0.1
Total 0.028718(s) query 5, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息