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


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

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


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

linux自动屏蔽IP工具

另存为 firewall.sh 给执行的权限 ~Tbj =f  
WE]e m >  
#!/bin/sh RrU BpqA  
# this program is used to check tcp/ip connections N7E$G{TT  
# and block those ip with excessive connections ljl^ GFo  
ARcB'z\r  
# my version ]Q]W5WDe:  
myver="1.0RC1" "ju6XdZ o  
][}0#'/mV  
# wake up every 120s if last check found abuse client iq( )8nxi  
wakeup_time_min=120 rZ8`sIWQt  
W9NX=gE4  
# wake up every 300s if last check found no abuse client dy_:- 2S  
wakeup_time_max=300 %p%%~ewmx  
n; {76Q  
# rule timeout 3600s LL:_L<  
rule_timeout=3600 4apL4E"r  
8M!:N(a  
# check port list <b,~:9*?  
portlist="80" [eTSZjIN7  
8WnwQ%;m?  
# max established connection per ip ]U"94S U:)  
max_active_conn=8 1GzAG;UUo6  
-GxaV #{  
# iptables chain name Hh+  2mkg  
iptables_chain_name="RH-Lokkit-0-50-INPUT" c/sC&i;%O  
p5G?N(l  
# log facility K6R.@BMN  
log_facility="local0" p, #o<W  
J{Q|mD=  
# Block policy % >=!p  
ipchains_block_policy="DENY" ZHa"isl$e  
iptables_block_policy="REJECT" E>K!Vrh-L  
{ ;2PL^i  
# myself hIT+gnhh  
myself=`basename $0` i{`:(F5*  
0)gdB'9V_  
mylogger_info() RCpR3iC2  
{ ~sh`r{0  
logger -p $log_facility.info -t $myself $@ 2>/dev/null 'tH_p  
} c24dSNJg,  
r8RoE`/T  
mylogger_debug() F'21jy&  
{ <J`0  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null %lGl,me H  
}  tVN  
YpVD2.jy  
mylogger_notice() bE !GJZ  
{  R[D{|K@"  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null 9dx/hFA  
} <eWf<  
Hx?;fl'G%  
dotimeout() e w$ B)W  
{ #V~me  
mylogger_info "reset firewall when timeout arrives" 0Qf,@^zL*  
case "$firewall" in ?67Y-\}   
ipchains) GjvOM y  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null I&x= ;   
  if [ $? = 0 ] ; then at,XB.}Z]  
    mylogger_info "ipchains restarted" 8;JWK3Gv  
  else hcsP2 0s  
    mylogger_notice "ipchains restart failed" A P?R"%  
  fi YJT&{jYi  
  ;; iP ->S\  
iptables) . YAT:;L  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null  mn"G_I  
  if [ $? = 0 ] ; then 2*laAB  
    mylogger_info "iptables restarted" f<d`B]$(  
  else z]_wjYn Z  
    mylogger_notice "iptables restart failed" *av<E  
  fi f@wquG'  
  ;; )Q&(f/LT  
*) ct Z uA+  
  mylogger_notice "neither ipchains nor iptables" FpmM63$VN[  
  ;; 3Ei#q+7  
esac _)3|f<E_t)  
Q4#m\KK;i9  
} -P$PAg5"2  
NX*Q  F+  
blockclient() ZB= E}]v6  
{ 3w*R&  
if [ -z "$1" ] || [ -z "$2" ]; then Q%`@0#"]Sv  
mylogger_notice "blockclient() missing client or port to block" X:"i4i[}{9  
return [6Izlh+D  
fi SaO}e  
local ip port ]g3JZF-  
@1roe G  
ip=$1 nfbR P t  
port=$2 ,q`\\d  
I3L<[-ZE  
case "$firewall" in 5{X<y#vAC0  
ipchains) *w`sM%]Rq  
  mylogger_notice "blocking $1 to $2 via ipchains" vm8eZG|  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` &jJL"gq"  
if [ -z "$found" ] ; then ^}=,g  
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" \';gvr|  
mylogger_debug "cmd: $cmd" dFB]~QEK  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` & '`g#N  
if [ $? != 0 ] ; then eu-*?]&Di  
mylogger_notice "$cmd call failed" %YscBG  
return )qw&%sO +  
fi )m T<MkP  
new_block=1 v@L;x [Q  
ever_block=1 6<QQ@5_  
else QA `sx  
mylogger_info "$ip already blocked to $port" j</: WRA`]  
fi %ntRG !  
  ;; T.BW H2gRP  
iptables) 6%_nZvRv  
  mylogger_notice "blocking $1 to $2 via iptables" K+3=tk]W9u  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` V b?oJhR  
if [ -z "$found" ] ; then \~$#1D1f  
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" &j"?\f?  
mylogger_debug "cmd: $cmd" oD .Cs'  
`iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` 583|blL  
if [ $? != 0 ] ; then 1>h]{%I  
mylogger_notice "$cmd call failed" z Rr*7G  
return ]{@-HTt  
fi K w ]=  
new_block=1 Wh*uaad7  
ever_block=1 @I?=<Riu  
else n{mfn *r.  
mylogger_info "$ip already blocked to $port" ?Z/ V~,  
fi eD6fpe\ (  
  ;; !%c\N8<>GD  
*) oUU1+F-  
  mylogger_notice "neither ipchains nor iptables" |@d\S[~^G  
  ;; &V/Mmm T  
esac M .mfw#*  
} eeB{c.#  
-[.[>&`/  
restartservice() 4|?;TE5  
{ 3a'<*v<xw  
local service 4,DeHJjAlE  
if [ -z "$1" ] ; then X'iWJ8  
mylogger_notice "no port given to see which service to be restart" &tj!*k'  
return FQ\h4` >B  
fi Y76gJ[y jn  
N(yz k_~  
case "$1" in gg2( 5FPP  
80) sfl<qD+?  
  service="httpd" 6Z"X}L,*  
  ;; zdam^o  
25) Z *x'+X  
  service="postfix" 4j^ @wV'  
  ;; r9?Mw06Wc5  
110) Jln:`!#fDf  
  service="courier-pop3d" TB31- ()  
  ;; 3ckclO\|>  
21) 493*{  
  service="muddleftpd" hM! a_'  
  ;; V !~wj  
53) 3Jn ;}  
  service="named" f _:A0  
  ;; )boE/4  
3306) 'V>-QD%1  
  service="mysqld" }x ,S%M-  
  ;; n+R7D.<q!!  
esac h{qgEIk&  
if [ ! -z "$service" ] ; then yB!dp;gM{  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null m]&SNz=  
if [ $? = 0 ] ; then 7d\QB (~  
  mylogger_notice "$service restarted" ;kKyksxlD  
else 4<v&S2Yq  
  mylogger_notice "$service restart failed" qe\5m.k  
fi NHE18_v5  
fi  ~$J2g   
} ~.|_RdN  
U $UIN#  
docheckport() 5:?! =<=  
{ 2`-Bs  
mylogger_info "do check port $1" 2Khv>#l  
local port last_client count client total_count -Qe'YBy:  
d_ P` qA  
if [ -z "$1" ] ; then nr3==21Om4  
mylogger_notice "docheckport() port not given" H,NF;QPPC  
return QsW/X0YBv  
fi "Y.y:Vv;  
GN>@ZdVG}#  
port=$1 Ts x>&WC  
"gwSJ~:ds  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` `0R./|bv\I  
if [ $? != 0 ] ; then i-&yH  
mylogger_notice "netstat call failed" Xnh8e  
return @%SQFu@FJ  
fi 6H|S;K+  
#echo $clientlist fXB0j;A  
# reset new_block % u6Sr5A[s  
new_block=0 B7%U_F|m  
count=0 LgYq.>Nl9  
total_count=0 1YMh1+1  
last_client="" =\d?'dII:  
for client in $clientlist l;V173W=&  
do &.ACd+Cd  
#echo "client is $client" mA}"a<0  
if [ -z "$last_client" ] ; then ctV,Q3' Z  
  count=$((count+1)) H[|~/0?K  
  total_count=$((total_count+1)) L=h'Qgk%  
  last_client=$client hzC>~Ub5  
else t:c.LFrF  
  if [ "$client" = "$last_client" ] ; then @|)Z"m7  
  count=$((count+1)) 8$|=P!7EO  
  total_count=$((total_count+1)) 'H! XUtFs"  
  else =":,.Ttq41  
  mylogger_debug "$last_client $count connections" >i O!*&Y>  
  if [ $count -ge $max_active_conn ] ; then :i7;w%B  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" GH xp7H  
    blockclient $last_client $port yppo6HGD  
  fi 5M_H NWi4  
  count=1 ,Lt[\_  
  total_count=$((total_count+1)) a}u Sm/S  
  last_client=$client Ca3~/K rM  
  fi =rK+eG#,  
fi v9UD%@tZ  
done Q5_o/wk  
# check the last client M#[{>6>iE  
if [ ! -z "$client" ] ; then E.>4C[O  
count=$((count+1)) yCX?!E;La  
total_count=$((total_count+1)) 8JD,u  
mylogger_debug "$client $count connections" VD\=`r)nT  
if [ $count -ge $max_active_conn ] ; then A+)`ZTuO  
  mylogger_notice "client $client connection $count >= $max_active_conn" zv"Z DRW  
  blockclient $client $port I`p;F!s  
fi 5 u0HI  
fi ;({W#Wa  
mylogger_info "total connections on port $port: $total_count" 1!gbTeVlY  
! v0LBe4  
if [ $new_block = 1 ] ; then C>w|a  
restartservice $port )>- =R5ZV  
fi eGHaY4|  
} O1mKe%'|  
xZv#Es%#  
docheckall() _ORvo{[:  
{ '08=yqy4N  
# reset wakeup_time @{O`E^}-D  
wakeup_time=$wakeup_time_max :4%k9BGAj"  
for port in $portlist # 4PVVu<  
do ( ICd}  
docheckport $port \} :PLCKT  
if [ $new_block = 1 ] ; then ,nB5/Lx  
  # set wakeup_time shorter cause we found some abuse client q%?in+l  
  wakeup_time=$wakeup_time_min 3;s\OW`  
fi w: Kl6"c  
done NPe%F+X  
} EJNU761  
w=J3=T@TD  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then ;VO:ph4Aj  
firewall="ipchains" b`O'1r\Y;  
fi r? E)obE  
8q}q{8  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then $@"g^,n  
firewall="iptables" RuV GG)  
fi -I,$_  
'u |c  
if [ -z "$firewall" ] ; then "mvt>X  
echo "Error: This machine does not have ipchains or iptables firewall support" DG:Z=LuJr  
exit 1 Q|?L*Pq2I  
fi (S\[Y9  
SmSH2m-  
mylogger_info "firewall.sh v$myver ValueOf.com starting" 'Xq| Kf (  
mylogger_info "Firewall is:       $firewall"  DwE[D]7o  
mylogger_info "Port protected:     $portlist" !58@pLJw  
mylogger_info "Max connection per ip: $max_active_conn" ^N{h3b8  
mylogger_info "Min time to check:   $wakeup_time_min""s" 54R#W:t  
mylogger_info "Max time to check:   $wakeup_time_max""s" '=8d?aeF  
mylogger_info "Timeout circle:     $rule_timeout""s" MJ [m  
mylogger_info "Output is logged to:   $log_facility" 41?HY{&2  
B; h"lv  
# if new ip blocked at this check run? 9c,'k#k  
new_block=0 Jgd'1'FOs  
# if new ip blocked at this timeout run? V_}"+&W9  
ever_block=0 |AU~_{H  
# reset wakeup_time RF53Jyt  
wakeup_time=$wakeup_time_max ryUQU^v  
peuZ&yK+"  
lasttime=`date +%s` [H^z-6x:0  
9FYUo  
while [ 1 ] :L@?2),  
do n QZwC  
curtime=`date +%s` 9YQb &  
timediff=$((curtime-lasttime)) Z|j>gq  
#echo "timediff: $timediff" ^-Kf']hU  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then jaMjZp;{ (  
lasttime=$curtime dx{bB%?Y\=  
ever_block=0 sF?TmBQ*  
dotimeout j0S# >t  
fi u jq=F  
docheckall {9;CNsd  
mylogger_info "sleep for $wakeup_time""s" dIa+K?INX  
sleep $wakeup_time (X1e5j>Ru  
done Ou!2 [oe@M  
xH(lm2kvT  
}c`"_L  
AFDq}*2Qb  
1. 说明 YYl4"l  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, kmsb hYM)  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 &5spTMw8  
该程序可以同时保护多个端口 st3l2Q  
QP^Cx=  
2. 安装 otl0J Ht*+  
tar zxf firewall-1.0b.tar.gz % X+:o]T  
cd firewall-1.0b %C'?@,7C  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh W*4-.*U8a  
6C^ D#.S  
3. 配置 FLCexlv^  
主要配置项目如下: FQ2  
# 最小检查周期,缺省为120秒 /W30~y  
wakeup_time_min=120 6BHXp# #z  
vNY{j7l/W  
# 最大检查周期,缺省为300秒 bv9]\qC]T<  
wakeup_time_max=600 }6}l7x  
gQelD6c  
# 重设防火墙状态的时间,缺省为3600秒 D/B8tf+V  
rule_timeout=3600 y8Z_Itlf  
]^n7  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) R'`qKc  
# 一般的网络攻击都是针对80和25,又以80居多 "f2$w  
portlist="80 25" Nk 8B_{   
mPK:R^RjG&  
# 每个ip可占用的最大活动(Established)连接数 A5RN5`}  
max_active_conn=8 W$;,CU.v  
V`d,qn)i  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 \0I_<  
# 如果用的是ipchains,可以忽略此项 Y>T-af49  
iptables_chain_name="RH-Lokkit-0-50-INPUT" GPv1fearl  
T_ <@..C  
# 日志输出目标 D~iz+{Q4  
log_facility="local0" $q{!5-e  
syj0.JD  
**** 关于检查周期 **** :$|HNeDO  
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 ;_I8^?d  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 M,,bf[p$  
EhvX)s  
**** ipchains vs iptables **** E\EsWb  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 aEWWP]  
/etc/sysconfig/iptables都没有检测到,则报错退出。 A#<?4&  
) 7@ `ut  
**** 日志输出 **** ~?Pw& K2  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf /NFj(+&g+  
中加入一条: $G+@_'  
local0.*                 /var/log/firewall.log ,HV(l+k {|  
然后重新启动syslog IdYt\^@>  
/etc/init.d/syslog restart xn(kKB.  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 U&OJXJd j  
g2ixx+`?|:  
4. 运行 ]^7@}Ce_  
/usr/prima/sbin/firewall.sh &  `25yE/  
Vwf$JdK%&l  
范例输出: <2x^slx)?  
*** firewall.sh v1.0b ValueOf.com*** *#|&JIEsi  
Firewall is:       ipchains ((;!<5-`s  
Port protected:     80 25 @n /nH?L  
Max connection per ip: 8 tw/dD +  
Min time to check:   120s k%#EEMh  
Max time to check:   300s lgy <?LI\  
Timeout circle:     3600s 8/cX]J  
Output is logged to:   local0 AeM^73t  
MKk\ u9  
察看/var/log/firewall.log,可以看到: RSRS wkC  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 \?-<4Bc@  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 yx[/|nZDC4  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 Mb}QD~=M  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 8/#A!Ww]  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 I$G['` XX/  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 ^uc=f2=>,  
`/+>a8  
5. 停止 ^z*t%<@[Q  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 ebhXak[w  
# ps auxww|grep firewall.sh khxnlry  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh jB2[(  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh  K5h  
第一行即firewall.sh的进程,用kill命令: <n$'voR7]  
# kill 27932 T'9'G M  
[1]   Terminated             /usr/prima/sbin/firewall.sh RtS+<^2a;  
即将其终止


常州电信/网通机房,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-01-28
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

通过脚本自动屏蔽非法IP

http://www.bornin76.cn/?p=31 s eJ^s@H5l  
f| g g  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! )4;`^]F  
jebx40TA3  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: _aeBauD  
'J|_2*  
6Kz,{F@  
#! /bin/bash \~wMfP8  
G3v5KmT  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` \_fv7Fdp{  
5!9zI+S|=`  
for i in $SCANIP _o~ nr]zx  
do  !}$$:  
NUMBER=`echo $i|awk -F= '{print $1}'` , R|BG  
SCANIP=`echo $i|awk -F= '{print $2}'` vZ Lf  
echo $NUMBER > Nr#O  
echo $SCANIP +"VP-s0  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] =g|FT  
then Z?z.?a r  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP 9nbLg5P  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log 5kXYeP3:  
fi WX0tgXl  
done Rx}Gz $   
0ZO2#>gh$  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 YK~%xo  
nFs(?Rv*  
xkR0  
------------------------------------------------------- >F&47Yn  
)\^-2[;  
注释: ~u+9J}  
~e.L.,4QZ8  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过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.022283(s) query 7, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息