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


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

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


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

linux自动屏蔽IP工具

另存为 firewall.sh 给执行的权限 '$5.{o`s*1  
<A"T_Rk  
#!/bin/sh EhEUkZE3 )  
# this program is used to check tcp/ip connections `yjHLg  
# and block those ip with excessive connections '@Zau\xC  
-ZTe#@J  
# my version P@ gVzx)M  
myver="1.0RC1" #cG7h( !  
mv7><C  
# wake up every 120s if last check found abuse client s>=DfE-;"  
wakeup_time_min=120 9H@I<`qGC  
tx$kD2  
# wake up every 300s if last check found no abuse client 7Ao9MF-  
wakeup_time_max=300 hdL/zW7]  
QEKRAPw  
# rule timeout 3600s #cB=] (N  
rule_timeout=3600 uFseO9F.2  
e):jQite   
# check port list ZLsfF =/G  
portlist="80" /.Yf& 2X\  
 iV h^;  
# max established connection per ip \;al@yC=T  
max_active_conn=8 mR3 )$!  
:}QBrd  
# iptables chain name $XBn:0U  
iptables_chain_name="RH-Lokkit-0-50-INPUT" ]V|rOtxb  
H .F-mm  
# log facility y v6V1gK  
log_facility="local0" ~}uv4;0l]  
Bw;gl^:UG  
# Block policy 6f"jl  
ipchains_block_policy="DENY" Q5[x2 s_d  
iptables_block_policy="REJECT" M^q< qS>d  
nz{ ;]U1  
# myself #w%d  
myself=`basename $0` B,Pbm|U1  
r]e1a\)r  
mylogger_info() 75@!j[QL<  
{ oXC|q-(C  
logger -p $log_facility.info -t $myself $@ 2>/dev/null 1D *oXE9Ig  
} 9((BOq  
Xk9mJ]31LC  
mylogger_debug() i D6f/|g  
{ Nk.m$  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null ^Rr!YnEN  
} 2C6o?*RjyY  
v'Y0|9c  
mylogger_notice() :n#8/'%1  
{ ;(&$Iw9X  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null WqX$;' }h  
} 0+-"9pED>E  
ae%Bl[  
dotimeout() Yfbo=yk  
{ \ltbiDP2  
mylogger_info "reset firewall when timeout arrives" ~Q+E""  
case "$firewall" in '99rXw  
ipchains) d}RU-uiW  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null F"-u8in`  
  if [ $? = 0 ] ; then {[ |je ]3v  
    mylogger_info "ipchains restarted" >PY Lk{q  
  else ?\Bm>p% +  
    mylogger_notice "ipchains restart failed" MG}rvzn@  
  fi D`Cy]j  
  ;; Ub,5~I+`  
iptables) n 3eLIA{  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null <r>1W~bp.q  
  if [ $? = 0 ] ; then `{oFdvL~)  
    mylogger_info "iptables restarted" s![Di  
  else whW% c8  
    mylogger_notice "iptables restart failed" Jkx_5kk/\  
  fi .5^7Jwh  
  ;; B>;`$-  
*) . ),Fdrg  
  mylogger_notice "neither ipchains nor iptables" MaS-*;BY,  
  ;; <eh<4_<qF  
esac ;RW0Dn)Q  
RM3"8J  
} a5k![sw\  
W9rmAQjn  
blockclient() Ja7yq{j  
{ vNDf1B5z  
if [ -z "$1" ] || [ -z "$2" ]; then ,%T sfB  
mylogger_notice "blockclient() missing client or port to block" Xk(p:^ R  
return :^En\YcU  
fi pwg\b  
local ip port 8x`E UJ  
sTu]C +A  
ip=$1 ="('  #o  
port=$2 Z [YSE T  
vn x+1T  
case "$firewall" in `]I p`_{  
ipchains) {O!B8a    
  mylogger_notice "blocking $1 to $2 via ipchains" Tg@:mw5  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` SUb:0GUa  
if [ -z "$found" ] ; then !y&<IT(\4  
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" 0i>>CvAl}  
mylogger_debug "cmd: $cmd" }vEMG-sxX  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` &9ERlZ(A  
if [ $? != 0 ] ; then c=f;3N  
mylogger_notice "$cmd call failed" 2Ah B)8bG  
return cS%;JV>C  
fi >ra)4huZ  
new_block=1 ^4C djMF-E  
ever_block=1 he|.Ow  
else 0V+v)\4FE  
mylogger_info "$ip already blocked to $port" }_oQg_-7e  
fi 0"l`M5-KP  
  ;; &<EixDi4q  
iptables) VKcVwq  
  mylogger_notice "blocking $1 to $2 via iptables" )C$pjjo/`  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` lvH} 8 lJ  
if [ -z "$found" ] ; then i|xC# hV  
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" a(AYY<g  
mylogger_debug "cmd: $cmd" g'NR \<6A  
`iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` MxqIB(5k  
if [ $? != 0 ] ; then Prr z>  
mylogger_notice "$cmd call failed" c#Qlr{ES  
return {$I1(DYN  
fi @CS%=tE}U  
new_block=1 ?>NX}~2cf  
ever_block=1 U U3o (Yq  
else p@eW*tE  
mylogger_info "$ip already blocked to $port" mM'uRhO+  
fi i.gagb  
  ;; 9?uU%9r5P  
*) P}w0=  
  mylogger_notice "neither ipchains nor iptables" MaZVGrcC  
  ;; ,MUgw w!.  
esac I7+yu>  
} :464~tHI[`  
A#gy[.Bb  
restartservice() !1#=j;N`  
{ q! WiX|P  
local service jP.dQj^j&  
if [ -z "$1" ] ; then _opB,,G  
mylogger_notice "no port given to see which service to be restart" #Eqx E o;  
return Bqw/\Lxwlf  
fi WH<\f |xR  
(8(P12l  
case "$1" in `We?j7O  
80) MADt$_  
  service="httpd" +A1xqOB  
  ;; \"P$*y4Le  
25) j07b!j:"\}  
  service="postfix" cHJ4[x=  
  ;; |Ew&.fgz  
110) !T.yv5ge'  
  service="courier-pop3d" tcD5"ALJ  
  ;; :/6u*HwZh  
21) Vq S1n  
  service="muddleftpd" %1 rN6A!%  
  ;; eIbz`|%3  
53) >[<f\BN|  
  service="named" ,#hNHFa'JH  
  ;; HG3iK  
3306) nMOXy\&mI  
  service="mysqld" UW&K\P  
  ;; c LfPSA  
esac Dk}txw}#  
if [ ! -z "$service" ] ; then 6>[J^k%~w)  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null G5UNW<P2C  
if [ $? = 0 ] ; then %V92q0XW  
  mylogger_notice "$service restarted" )jMk ~;'r  
else +XAM2uN5_.  
  mylogger_notice "$service restart failed" RA}Y$}^#'  
fi alyA#zao|  
fi wpOM~!9R  
} \o5/, C  
FB O_B  
docheckport() >viLvDng  
{ X + B=?|M  
mylogger_info "do check port $1" 2lxA/.f  
local port last_client count client total_count C+0BV~7J<<  
>t4<2|!(M  
if [ -z "$1" ] ; then :~~\{fm  
mylogger_notice "docheckport() port not given" B Wzo|isv  
return $~r=I[5' (  
fi 1(/rg  
ki]i[cdk  
port=$1 ,DCrh k  
KXcE@q9  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` cB2~W%H  
if [ $? != 0 ] ; then 6g"qwW Zp  
mylogger_notice "netstat call failed" )NlxW5  
return K?=g IC:  
fi -\g@s@5  
#echo $clientlist ]GzfU'fOn|  
# reset new_block #$JY &!M  
new_block=0 =@.5J'!  
count=0 df9$k0Fx  
total_count=0 $3(E0\#O  
last_client="" vH{JLN2  
for client in $clientlist +P=I4-?eX  
do )"s(;kU!  
#echo "client is $client" O_Z   
if [ -z "$last_client" ] ; then %Pk@`t(3  
  count=$((count+1)) NJ(H$tB@  
  total_count=$((total_count+1)) CjU?3Ag  
  last_client=$client |]OI)w*  
else vVH*\&H\T  
  if [ "$client" = "$last_client" ] ; then rv %^2h<&  
  count=$((count+1)) I(+%`{Wv  
  total_count=$((total_count+1)) %F]:nk`  
  else `^ uX`M/  
  mylogger_debug "$last_client $count connections" qa5 T(:8  
  if [ $count -ge $max_active_conn ] ; then #mc6;TRZO  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" vFsl]|<;8  
    blockclient $last_client $port  t/a  
  fi ,kE"M1W  
  count=1 ;V4f6[<]'z  
  total_count=$((total_count+1)) +'9eo%3O  
  last_client=$client ]}BT'fky#  
  fi f_ UwIP  
fi P6;Cohfh  
done ,%,.c^-  
# check the last client u'aWvN y+  
if [ ! -z "$client" ] ; then r<-@.$lf  
count=$((count+1)) /` 4B-Y4M4  
total_count=$((total_count+1)) [HQ Bx`3TS  
mylogger_debug "$client $count connections" ?a` $Y>?h  
if [ $count -ge $max_active_conn ] ; then iPt{v5}]  
  mylogger_notice "client $client connection $count >= $max_active_conn" S{c;n*xf  
  blockclient $client $port 3H@29TrJ+  
fi 6#1:2ZHKG  
fi `rI[   
mylogger_info "total connections on port $port: $total_count" 3ypf_]<  
rqYx\i?  
if [ $new_block = 1 ] ; then x|<89o L  
restartservice $port 'A9U[|  
fi %B-m- =gz  
} Zcxj.F(,  
1IV R4:a  
docheckall() g+M& _n  
{ R%^AW2   
# reset wakeup_time }A'Ro/n  
wakeup_time=$wakeup_time_max V2 _I=]p_  
for port in $portlist b0R{cj=<[  
do + +G %~)S:  
docheckport $port XpibI3:<  
if [ $new_block = 1 ] ; then RT+_e  
  # set wakeup_time shorter cause we found some abuse client ELQc: t -2  
  wakeup_time=$wakeup_time_min cp<jwcc!  
fi +K03yphZr  
done LjMhPzCp  
} }?XNA.Wz  
MyJG2C#R  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then >PMLjXK  
firewall="ipchains" 9V( esveq  
fi Ba /^CS  
o!ycVY$yW  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then 'ai!6[|SD  
firewall="iptables" ~7Jj\@68  
fi 3WQ"3^G  
Wg ?P"  
if [ -z "$firewall" ] ; then t`y*oRy  
echo "Error: This machine does not have ipchains or iptables firewall support" JypXQC}~  
exit 1 N"q C-h  
fi 6`l7saHXE  
T&PLvyBL  
mylogger_info "firewall.sh v$myver ValueOf.com starting" {r2fIj~V  
mylogger_info "Firewall is:       $firewall" a#G]5T Z  
mylogger_info "Port protected:     $portlist" njN]0l{p  
mylogger_info "Max connection per ip: $max_active_conn" %:WM]dc  
mylogger_info "Min time to check:   $wakeup_time_min""s" <UMT:`h1MZ  
mylogger_info "Max time to check:   $wakeup_time_max""s" {- ?8r>  
mylogger_info "Timeout circle:     $rule_timeout""s" 8x9$6HO  
mylogger_info "Output is logged to:   $log_facility" e=%6\&q  
]~A<Q{  
# if new ip blocked at this check run? Z 2x%  
new_block=0 XTOZ]H*^  
# if new ip blocked at this timeout run? b R;Zc  
ever_block=0 `DPR >dd@  
# reset wakeup_time $ZOKB9QccC  
wakeup_time=$wakeup_time_max 1KtPq,  
8 K/o/  
lasttime=`date +%s` :&oUI&(o  
) "o+wSI1  
while [ 1 ] |rbl sL2?Z  
do +#d}3^_]  
curtime=`date +%s` ttK`*Ng  
timediff=$((curtime-lasttime)) r\-25F<e5  
#echo "timediff: $timediff" r 7mg>3  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then &PAgab2$  
lasttime=$curtime 1xkU;no  
ever_block=0 9C{\=?e;  
dotimeout *KXg;777  
fi qWzzUM1=  
docheckall {vJ)!'Eh  
mylogger_info "sleep for $wakeup_time""s" {&Bpf K;`)  
sleep $wakeup_time ,OQ!lI_`R  
done weOzs]uc  
%YaUc{.%  
;^,2 QsM  
]/+qM)F  
1. 说明 Z-{!Z;T)z  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, /I' n]  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 }bv0~}G4  
该程序可以同时保护多个端口 yt&eY6Xp  
6rnehv!p  
2. 安装 |-zwl8E  
tar zxf firewall-1.0b.tar.gz #&1Y!kbdd  
cd firewall-1.0b %}=$HwN)  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh 9 @yP;{Q  
bmAgB}Ior  
3. 配置 {I |k@  
主要配置项目如下: v}IP%84  
# 最小检查周期,缺省为120秒 G5Ci"0  
wakeup_time_min=120 bH41#B  
y0qE::/H$  
# 最大检查周期,缺省为300秒 NEO~|B*oDU  
wakeup_time_max=600 S iw9_c  
LoG@(g&)  
# 重设防火墙状态的时间,缺省为3600秒 F.=u Jdl.!  
rule_timeout=3600 qD\%8l.]Z  
co3H=#2a  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql)  &1Fcwj  
# 一般的网络攻击都是针对80和25,又以80居多 7atYWz~yG  
portlist="80 25" !9"R4~4  
P:k(=CzZ@J  
# 每个ip可占用的最大活动(Established)连接数 +^+wS`Y  
max_active_conn=8 #|XEBOmsQ  
SEchF"KJQF  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 W7A'5  
# 如果用的是ipchains,可以忽略此项 fnnwe2aso  
iptables_chain_name="RH-Lokkit-0-50-INPUT" T *t$   
7IJb$af:;  
# 日志输出目标 29ft!R>[  
log_facility="local0" ,:\2Lf  
qd|*vE  
**** 关于检查周期 **** >q &ouVE  
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 3R#<9 O  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 ,f?#i%EF&  
z3*G(,  
**** ipchains vs iptables **** C6"!'6 W  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 K[sM)_I  
/etc/sysconfig/iptables都没有检测到,则报错退出。 T  %a]3  
P)ZSxU  
**** 日志输出 **** aJ)5DlfLR  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf M<$l&%<`G  
中加入一条: ,Vs:Lle  
local0.*                 /var/log/firewall.log Ee|+uQ981>  
然后重新启动syslog ^LU[{HZV  
/etc/init.d/syslog restart +~fu-%,k  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 'c$9[|x  
#8z\i2I  
4. 运行 `f'q/  
/usr/prima/sbin/firewall.sh & 3N*Shzusbt  
#P9VX5Tg  
范例输出: Awh"SU Oh0  
*** firewall.sh v1.0b ValueOf.com*** % 9D@W*Z  
Firewall is:       ipchains I@S<D"af  
Port protected:     80 25 \ QMSka>  
Max connection per ip: 8 blN1Q%m6  
Min time to check:   120s .4Ny4CMHZ  
Max time to check:   300s _uu:)%  
Timeout circle:     3600s R]Yhuo9,&n  
Output is logged to:   local0 z>0"T2W y  
@iP6 N  
察看/var/log/firewall.log,可以看到: ZkIQ-;wx  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 y^A $bTQq  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 $($SQZK&  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 j&Aq^aI  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 >eTlew<5  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 <M7* N .  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 0@Ijk(|  
<di_2hN  
5. 停止 9>#|~P&FE  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 ;gMh]$|"  
# ps auxww|grep firewall.sh }gW}Vr <  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh uf^:3{1  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh V3oAZ34)  
第一行即firewall.sh的进程,用kill命令: eI}VHBAz  
# kill 27932 ]2(c$R  
[1]   Terminated             /usr/prima/sbin/firewall.sh ifs *-f  
即将其终止


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

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


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

通过脚本自动屏蔽非法IP

http://www.bornin76.cn/?p=31 0tm_}L$g=b  
mGpkM?Y"  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! [^d6cMEOlc  
Q`p}X&^a  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: *o e0=  
},EUcVXk  
,gL)~6!A  
#! /bin/bash S,Wl)\  
b!5tFX;J  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` gvc' $9%  
*fvI.cKiGP  
for i in $SCANIP ^ ,Y~M_=  
do ?V5Pt s  
NUMBER=`echo $i|awk -F= '{print $1}'` w] 5U  
SCANIP=`echo $i|awk -F= '{print $2}'` dy6F+V\DG  
echo $NUMBER M,_^hm7  
echo $SCANIP JM+sHHs  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] <7FP"YU  
then 34m']n  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP m['v3m:  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log  q(&^9"  
fi cEdz;kbUM  
done Yn$>QS 4  
>xU72l#5  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 gB{]yA"('  
TM/|K|_  
&w;^m/zP3  
------------------------------------------------------- 5}X<(q(  
@ \6nXf  
注释: *fQ ?A|l!x  
A4#3O5kij  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过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.023243(s) query 7, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息