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


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

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


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

linux自动屏蔽IP工具

另存为 firewall.sh 给执行的权限 *Cb(4h-  
^2??]R&Q  
#!/bin/sh O.$<Bf9  
# this program is used to check tcp/ip connections !J71[4t  
# and block those ip with excessive connections Sc0ZT/Lm  
F@I_sGCcb  
# my version q@!:<Ra,){  
myver="1.0RC1" ZE\t{s0  
,H%\+yn{  
# wake up every 120s if last check found abuse client /d=$,q1  
wakeup_time_min=120 E+2y-B)E  
r"Bf@va  
# wake up every 300s if last check found no abuse client *@ o3{0[Z  
wakeup_time_max=300 WIGb7}egR  
<7&b|f$CL  
# rule timeout 3600s UB1/FM4~  
rule_timeout=3600 m,KY_1%M  
oD8-I^  
# check port list wTW"1M  
portlist="80" E.6\(^g  
aj,ZM,Ad  
# max established connection per ip MQ+ek4  
max_active_conn=8 }Q=Zqlvz  
96.Wfx  
# iptables chain name 4[r a  
iptables_chain_name="RH-Lokkit-0-50-INPUT" JU@$(  
}cT}G;L'-  
# log facility R3PhKdQ"  
log_facility="local0" >H1d9y +Z  
} :mI6zsNj  
# Block policy J$Q-1fjj  
ipchains_block_policy="DENY" ah92<'ix  
iptables_block_policy="REJECT" F"=MU8  
Wg%-m%7O  
# myself %)D7Dr  
myself=`basename $0` f3>/6 C  
-AT@M1K7%  
mylogger_info() =/;_7|ssd  
{ B`F82_O  
logger -p $log_facility.info -t $myself $@ 2>/dev/null D'h2 DP!  
} ="DgrH  
3(:mRb}  
mylogger_debug() C\^K6,m5  
{ /v{[Z &z  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null o zYI/b^  
}  o,yvi  
[ q&J"dt  
mylogger_notice() pJz8e&wyLM  
{ L\ %_<2  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null `<zb  
} i!3KG|V  
!r*;R\!n2  
dotimeout() Q*.FUV&;  
{ Q /c WV  
mylogger_info "reset firewall when timeout arrives" _6!/}Fm  
case "$firewall" in (NdgF+'=  
ipchains) <6C9R>  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null ?+5{HFx  
  if [ $? = 0 ] ; then rcx;3Vne  
    mylogger_info "ipchains restarted" P|4E1O  
  else ^Nw]'e3  
    mylogger_notice "ipchains restart failed" N[bN"'U/1  
  fi rR@n> Xx  
  ;; *\KMkx  
iptables) w3qf7{b  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null I#t9aR+&  
  if [ $? = 0 ] ; then 'c0'P%[5A  
    mylogger_info "iptables restarted" ^70.g?(f[  
  else .k`*$1?73x  
    mylogger_notice "iptables restart failed" `B\KS*Gya#  
  fi 9c{%m4  
  ;; V-;nj,.mY  
*) (4ci=*3=  
  mylogger_notice "neither ipchains nor iptables" a^c ,=X3  
  ;; AKKU-5 B9c  
esac cm@oun  
e4q k>Cw  
} h0 GdFWN  
wYQ1Z  
blockclient() 9` C iE  
{ KPjqw{gR_R  
if [ -z "$1" ] || [ -z "$2" ]; then e0N=2i?I#z  
mylogger_notice "blockclient() missing client or port to block" #~URLN  
return M-Z6TL  
fi y:|.m@ j1  
local ip port x|v[Dxf]  
H]i+o6  
ip=$1 s/G5wRl<  
port=$2 h`EH~W0:z  
0^!,[oh6*  
case "$firewall" in Ag>>B9  
ipchains) 1Fvv/Tj  
  mylogger_notice "blocking $1 to $2 via ipchains" Nx4DC  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` &FJU%tFA  
if [ -z "$found" ] ; then ZaRr2Z:!  
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" t|#NMRz  
mylogger_debug "cmd: $cmd" qN1e{T8u  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` _yxe2[TD  
if [ $? != 0 ] ; then XgiI6-B~  
mylogger_notice "$cmd call failed" Q_dXRBv=n  
return KF:]4`$  
fi Pa +AF  
new_block=1 f $.\o  
ever_block=1 ~b>nCP8q  
else  '{j\0  
mylogger_info "$ip already blocked to $port" ]s*[Lib  
fi uFrJ:l+  
  ;; :\1rQT  
iptables) uGP[l`f|FQ  
  mylogger_notice "blocking $1 to $2 via iptables" Qe @A5#  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` Ersr\ZB  
if [ -z "$found" ] ; then fw:7Q7 qo  
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" ?b*/ddIs  
mylogger_debug "cmd: $cmd"  r21?c|IP  
`iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` fXF=F,!t  
if [ $? != 0 ] ; then =9DhO7I'  
mylogger_notice "$cmd call failed" ?;:9 W  
return DJ 1XN pm  
fi 4hsPbUx9  
new_block=1 NS Mjr_  
ever_block=1 OQytgXED  
else g33<qYxP  
mylogger_info "$ip already blocked to $port" bK_0NrXP  
fi =1 {H Sf  
  ;; $I)Tk`=  
*) gD,YQ%aq  
  mylogger_notice "neither ipchains nor iptables" m6bWmGn GC  
  ;; hV3,^#9o  
esac S\! a"0$  
} 8Ehy9<  
L?3VyBE  
restartservice() W\I$`gyC/  
{ T8,k7 7  
local service D:YN_J"kV  
if [ -z "$1" ] ; then -vv   
mylogger_notice "no port given to see which service to be restart" \ &eY)^vw  
return '+)6#/*  
fi kdK*MUB  
S_(&UeTC  
case "$1" in Qv&T E3  
80) q*HAIw[<y  
  service="httpd" S2koXg(  
  ;; 6obQ9L c  
25) LFE p  
  service="postfix" E0sbU<11  
  ;; +G5'kYzJ  
110) RtW4 n:c  
  service="courier-pop3d" 2. StG(Y!  
  ;; Q;XXgX#l  
21) #[no~&E  
  service="muddleftpd"  )v${&H  
  ;; ,DE(5iDS  
53) yQu vW$  
  service="named" hWe}' L-  
  ;; i0ILb/LS  
3306) i{<8 hLO  
  service="mysqld" dj? G.-  
  ;; YH6snC$u  
esac G i$  
if [ ! -z "$service" ] ; then s ;48v  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null E2>im>p  
if [ $? = 0 ] ; then ILNE 4n  
  mylogger_notice "$service restarted" -Bl/ 4p  
else  UP\8w#~  
  mylogger_notice "$service restart failed" w+Ad$4Pf"  
fi )$MS 0[?  
fi #o"tMh!f  
} w(aUEWYL  
wh9L(0  
docheckport() j6`6+W=S(  
{ ZQ@3P7T  
mylogger_info "do check port $1" #g,H("Qy({  
local port last_client count client total_count pMOD\J:l,  
"_t4F4z  
if [ -z "$1" ] ; then cvxIp#FbW  
mylogger_notice "docheckport() port not given" `$H7KIG  
return TV? ^c?{5  
fi VIetcs  
,bxz]S1W  
port=$1 VZ;@S3TS  
%j%%Rn  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` V!mWn|lf  
if [ $? != 0 ] ; then OO$|9`a  
mylogger_notice "netstat call failed" 0xv\D0  
return I I&<  
fi gEE9/\>%-  
#echo $clientlist B .El a  
# reset new_block AYt*'Zeg!s  
new_block=0 CK=ARh#|  
count=0 0 "4J"q]&  
total_count=0 Il|GCj*N  
last_client="" Rb?~ Rs\  
for client in $clientlist @#A!w;bz  
do g2A"1w<-AH  
#echo "client is $client" jBS'g{y-!  
if [ -z "$last_client" ] ; then (p^S~Ax  
  count=$((count+1)) u9BjgK(M  
  total_count=$((total_count+1)) s~=g*99H  
  last_client=$client S3QaYq"v  
else rJX\6{V!_  
  if [ "$client" = "$last_client" ] ; then TKd6MZhT  
  count=$((count+1)) * ]>])ms)  
  total_count=$((total_count+1)) |ebvx ?\  
  else 5 1"8Py  
  mylogger_debug "$last_client $count connections" %oR>Uo  
  if [ $count -ge $max_active_conn ] ; then u"\=^F  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" #sS9vv7i  
    blockclient $last_client $port K0LbZMn,/  
  fi y>)MAzz~\  
  count=1 4? v,wq  
  total_count=$((total_count+1)) V +.Q0$~F5  
  last_client=$client &lUNy L  
  fi Et.j1M|g  
fi  J+lGh9G  
done ZzT&$J7]`{  
# check the last client )Y~xIj >  
if [ ! -z "$client" ] ; then -IbbPuRq  
count=$((count+1)) O <>#>[  
total_count=$((total_count+1)) v[smQO  
mylogger_debug "$client $count connections" _!%M %  
if [ $count -ge $max_active_conn ] ; then qv$!\T  
  mylogger_notice "client $client connection $count >= $max_active_conn" A*Rn<{U  
  blockclient $client $port 7pP+5&*  
fi # 3. \j"b  
fi h07eE g  
mylogger_info "total connections on port $port: $total_count" UP]1(S?  
o(zTNk5d  
if [ $new_block = 1 ] ; then .$P|^Zx,  
restartservice $port `\ R{5TU  
fi 0d";Hh:  
} _; 7fraqX  
0 7\02f  
docheckall() 1|]xo3j"'  
{ -Uri|^t  
# reset wakeup_time hF.6}28U1  
wakeup_time=$wakeup_time_max DFc [z"[  
for port in $portlist Hit Ac8  
do ewym 1}o  
docheckport $port `.[ 8$  
if [ $new_block = 1 ] ; then `R0Y+#$8h  
  # set wakeup_time shorter cause we found some abuse client >D~w}z/fk  
  wakeup_time=$wakeup_time_min gApz:K[l  
fi r@yD8D \  
done S/fW/W*/}  
} {>h97}P  
>(3'Tnu  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then lV)G@l[1  
firewall="ipchains" ,Bf(r  
fi a,cC!   
'?8Tx&}U8  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then >XnO&hW  
firewall="iptables" 8U=A{{0p  
fi B2(,~^39  
LABNj{=D!  
if [ -z "$firewall" ] ; then ]u0Jd#@  
echo "Error: This machine does not have ipchains or iptables firewall support" 1eD.:_t4  
exit 1 @)b^^Fp  
fi r.<JDdj  
/>F.Nsujy  
mylogger_info "firewall.sh v$myver ValueOf.com starting" T>F9Hs  W  
mylogger_info "Firewall is:       $firewall"  D%gGRA  
mylogger_info "Port protected:     $portlist" N5!&~~  
mylogger_info "Max connection per ip: $max_active_conn" >)3VbO  
mylogger_info "Min time to check:   $wakeup_time_min""s" |!}wF}iLc)  
mylogger_info "Max time to check:   $wakeup_time_max""s" F~R7~ZE  
mylogger_info "Timeout circle:     $rule_timeout""s" OD|1c6+X  
mylogger_info "Output is logged to:   $log_facility" w~LU\Ct  
%Rarr  
# if new ip blocked at this check run? u5F}(+4r  
new_block=0 x5F@ad 9  
# if new ip blocked at this timeout run? aS/`A  
ever_block=0 L;yEz[#xaT  
# reset wakeup_time 0H+c4IW  
wakeup_time=$wakeup_time_max P7r4ePtLk{  
ET+'Pj3  
lasttime=`date +%s` %w:'!X><  
L$hc,  
while [ 1 ] =fWdk\Wv  
do |_nC6 ;  
curtime=`date +%s` < T4(H[9B  
timediff=$((curtime-lasttime)) yoU2AMH2D^  
#echo "timediff: $timediff" @ef$b?wg  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then b{pg!/N4  
lasttime=$curtime jb 3.W  
ever_block=0 L|J~9FM  
dotimeout F[]&1  
fi [#Fg\2bq_y  
docheckall rS,j;8D-  
mylogger_info "sleep for $wakeup_time""s" \JU{xQMB  
sleep $wakeup_time J7n5Ps\M  
done g ;LVECk  
=ybGb7?  
)S Q('vwg  
yQwVQUW8B  
1. 说明 PkJcd->  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, u-39r^`5  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 Ut_mr b+W  
该程序可以同时保护多个端口 9A+M|;O  
j{ "[Ec  
2. 安装 J#(,0h  
tar zxf firewall-1.0b.tar.gz [TEcg^  
cd firewall-1.0b 4|F#gK5E  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh ;dOs0/UM&  
Y }/c N\  
3. 配置 =)*JbwQ   
主要配置项目如下: *]2R.u  
# 最小检查周期,缺省为120秒 x< S\D&  
wakeup_time_min=120 /x/4Ne D  
-ek1$y9)  
# 最大检查周期,缺省为300秒 e`AUYli"  
wakeup_time_max=600 4,zvFH*AH  
H!. ZH(asY  
# 重设防火墙状态的时间,缺省为3600秒 >fbo r'|  
rule_timeout=3600 4Cd#sQ  
\gd6Yx^[  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) Vrg3{@$  
# 一般的网络攻击都是针对80和25,又以80居多 B0"0_n7-  
portlist="80 25" z^3Q.4Qc6^  
FT4l$g7"  
# 每个ip可占用的最大活动(Established)连接数 *`1bc'umM;  
max_active_conn=8 |K)p]i+  
wI>h%y-%!  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 ZMe}M!V  
# 如果用的是ipchains,可以忽略此项 v~aLTI  
iptables_chain_name="RH-Lokkit-0-50-INPUT" |i u2&p >  
P{Lg{I_w.B  
# 日志输出目标 k{{hZ/om  
log_facility="local0" lZvS0JS  
"8?TSm8  
**** 关于检查周期 **** Y-= /,   
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 D>Ua#<52q  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 o"\{OX  
 -tMA  
**** ipchains vs iptables **** *7_@7=W,  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 NFV_+{X\  
/etc/sysconfig/iptables都没有检测到,则报错退出。 VAg68 EbnF  
EZ `}*Yr d  
**** 日志输出 **** ([tG y  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf )\"I*Jwir  
中加入一条: =oPng= :  
local0.*                 /var/log/firewall.log oz uIwzi7N  
然后重新启动syslog E^82==R  
/etc/init.d/syslog restart 58PKx5`D  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 HGb.656r  
5)8 .  
4. 运行 &<@%{h@ =  
/usr/prima/sbin/firewall.sh & {<r`5  
`;e^2  
范例输出: "&}mAWT%If  
*** firewall.sh v1.0b ValueOf.com*** [*t U}9  
Firewall is:       ipchains 1MpX] j8C#  
Port protected:     80 25 cT I,1U  
Max connection per ip: 8 n-W?Z'H{r  
Min time to check:   120s -C=]n<ak  
Max time to check:   300s uZ-`fcCjD  
Timeout circle:     3600s dw3'T4TC?  
Output is logged to:   local0 +*<K"H|,  
ThbP;CzI#  
察看/var/log/firewall.log,可以看到: We|-5  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 bIq-1 Y(  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 x)d2G 6x  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 nwhm[AaNs  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 B JU*`Tx  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 aam1tm#Q  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 [O [FCn  
:C > J-zY  
5. 停止 s !NisF  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 {rs6"X^  
# ps auxww|grep firewall.sh O3!Ouh&  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh ocq2  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh UP5%C;  
第一行即firewall.sh的进程,用kill命令: xu`d`!Tx  
# kill 27932 'T6B_9GQ8  
[1]   Terminated             /usr/prima/sbin/firewall.sh ~>.awu+o|  
即将其终止


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

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


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

通过脚本自动屏蔽非法IP

http://www.bornin76.cn/?p=31 5qeS|]^`  
|=h)efo}  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! AT'_0> x8  
fjK]m.w  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: 7}iewtdy,  
5LhJ8$W  
=J[[>H'<d  
#! /bin/bash I  W] 841  
w'zO(6 `  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` u`H@Q&(^wa  
O( 5L2G  
for i in $SCANIP MTFVnoZMQ_  
do n\8[G [M  
NUMBER=`echo $i|awk -F= '{print $1}'` #&`WMLl+8  
SCANIP=`echo $i|awk -F= '{print $2}'` ^1FZ`2u;  
echo $NUMBER ? /|@ #&  
echo $SCANIP g]P LW3  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] WNlSve)]ie  
then J^+$L"K  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP >-@{vyoOy  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log b]qfcV  
fi N mjBJ_G  
done 5z(>4d!  
E.Q} \E  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 .Zo9^0`C  
ka9v2tE\  
<"|<)BGeI  
------------------------------------------------------- "a`0w9Mm}  
s:Ql](/B#  
注释: MzW$Sl&:  
ZWe$(?  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过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.018780(s) query 5, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息