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


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

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


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

linux自动屏蔽IP工具

另存为 firewall.sh 给执行的权限 \rpu=*gt  
mp#5V c  
#!/bin/sh D526X0  
# this program is used to check tcp/ip connections &(K* TB|Om  
# and block those ip with excessive connections Gqs8$[o  
W46sKD;\^W  
# my version /ZczfM\  
myver="1.0RC1" 3Ry?{m^  
aAX 8m  
# wake up every 120s if last check found abuse client -Xj+7}4  
wakeup_time_min=120 eq"~b y[Uq  
W>"i0p  
# wake up every 300s if last check found no abuse client 3}H94H)]a  
wakeup_time_max=300 k8h$#@^  
?znSA >  
# rule timeout 3600s cD{[rI E3  
rule_timeout=3600 Jzr(A^vwo  
 ~d }-  
# check port list bNqjjg  
portlist="80" pdE=9l'  
MOj 0"x)  
# max established connection per ip Tj.;\a|d  
max_active_conn=8 D3I;5m`_  
7OT}V}iP  
# iptables chain name Jx 'p\*  
iptables_chain_name="RH-Lokkit-0-50-INPUT" f$P pFSY 4  
4Jw0m#UN1  
# log facility I@[.W!w  
log_facility="local0" N 4Dyec\  
{4#'`Eejj  
# Block policy B=9|g1e  
ipchains_block_policy="DENY" Fm*O&6W\@A  
iptables_block_policy="REJECT" p#3P`I>ZrT  
%[RLc[pB  
# myself aG,N>0k8  
myself=`basename $0` #A?U_32z/2  
oZ!rK/qoA  
mylogger_info() [Q)lJTs  
{ m%m8002  
logger -p $log_facility.info -t $myself $@ 2>/dev/null ]{dg"J  
} V7 c7(G  
i5|!M IY  
mylogger_debug() -p9|l%W  
{ J8(v65  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null {ImZ><xe/  
} J}lBK P:-*  
\Z<' u;  
mylogger_notice() m ?LOd9  
{ A"p7N?|%  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null h@/c76}f6p  
} ,D>$N3;  
N }Z"$4  
dotimeout() )9J&M6LX  
{ Ql!$e&A|l  
mylogger_info "reset firewall when timeout arrives" @ScH"I];uA  
case "$firewall" in 1+v)#Wj  
ipchains) aw {?UvL&  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null ho;Km  
  if [ $? = 0 ] ; then jbGP`b1_  
    mylogger_info "ipchains restarted" H/Y ZwDx,i  
  else rl[&s\[  
    mylogger_notice "ipchains restart failed" 9psD"=/"  
  fi 8E ^yHd4Y  
  ;; 0k0 y'1SL  
iptables) MW6d-  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null rx CSs  
  if [ $? = 0 ] ; then A9!%H6  
    mylogger_info "iptables restarted" Zw` Xg@;xP  
  else X~G!{TT_x6  
    mylogger_notice "iptables restart failed" 2mWW0txil  
  fi /9QI^6& SX  
  ;; 7lqj" o(  
*) 1Y_Cd  
  mylogger_notice "neither ipchains nor iptables" ZC @sUj"  
  ;; XL1v&'HLV  
esac ~8o's`  
m\)z& hv<r  
} rp (nGiI  
PTXy: >]M  
blockclient() _p" nR  
{ a2 e-Q({  
if [ -z "$1" ] || [ -z "$2" ]; then s+8 v7ZJ  
mylogger_notice "blockclient() missing client or port to block" lED-Jo2  
return DJ2EV^D+P  
fi ?kqo~twJ  
local ip port w O;\,zU  
6hv4D`d;o  
ip=$1 SQ9s  
port=$2 9 ,=7Uh#7  
`9}\kn-</8  
case "$firewall" in nw){}g  
ipchains) i Tg?JoE2  
  mylogger_notice "blocking $1 to $2 via ipchains" Hr |De8#f  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` z=J%-Hq>  
if [ -z "$found" ] ; then 0!!b(X(  
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" g} 7FR({b  
mylogger_debug "cmd: $cmd" 9 tvLj5~  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` 2 UU5\ jV6  
if [ $? != 0 ] ; then Mn7nS:  
mylogger_notice "$cmd call failed" -`x$ a&}  
return Fsv:SL+5  
fi f<bc8Lp  
new_block=1 }YH@T]O}  
ever_block=1 P#H|at  
else /^v?Q9=Y  
mylogger_info "$ip already blocked to $port" )xYv$6=  
fi 28J ; 9  
  ;; x;ym_UZ6e  
iptables) !4cO]wh5  
  mylogger_notice "blocking $1 to $2 via iptables" H)tnxD0)  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` *vqlY[2Ax  
if [ -z "$found" ] ; then [zmx  
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" 22lC^)`TE  
mylogger_debug "cmd: $cmd" <+ 0cQq=2  
`iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` ENW>bS8 e`  
if [ $? != 0 ] ; then Rd7[e^HSN  
mylogger_notice "$cmd call failed" JbN@AX:%  
return 6z3 Yq{1  
fi #Bq.'?c'~  
new_block=1 HPJ\]HV(  
ever_block=1 C;7?TZ&xw  
else l1BbL5#1Q>  
mylogger_info "$ip already blocked to $port" %H OMX{~}#  
fi Esh3 cn4  
  ;; ^~~&[wY  
*) ` QC  
  mylogger_notice "neither ipchains nor iptables" q2v:l SFY  
  ;; 3J t_=!qlo  
esac b=[gK|fu  
} t<S]YA~N'  
bH+x `]{A  
restartservice() ds@X%L;_  
{ H srIw  
local service E+wd9/;  
if [ -z "$1" ] ; then qFEGV+  
mylogger_notice "no port given to see which service to be restart" .Cv0Ze  
return C'8v\C9Ag  
fi m# #( uSh  
I,4t;4;Zk  
case "$1" in VrL==aTYXs  
80) e.pm`%5bO  
  service="httpd" 0Y rdu,c  
  ;; !e~Yp0gX#  
25) sc mn-4j'{  
  service="postfix" F O"8B  
  ;; fQ>=\*b9x^  
110) `J*~B  
  service="courier-pop3d" OO%< ~H  
  ;; a*4"j2j v  
21) 3$Ecq|4J:  
  service="muddleftpd" ^qNh)?V?]I  
  ;; *69 yB  
53)  y!6+jrI  
  service="named" ,:yv T6)p  
  ;; uP,{yna(  
3306) ml7nt 0{  
  service="mysqld" :,.g_@wvG  
  ;; KP)BD;  
esac |||uTfrJ  
if [ ! -z "$service" ] ; then  & t b  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null $`q8-+ {  
if [ $? = 0 ] ; then e|wH5(V  
  mylogger_notice "$service restarted" 6;/>asf  
else \Ol3kx|  
  mylogger_notice "$service restart failed" ?3 :OPP`s  
fi /m,0H)w1  
fi xl5n(~g)p  
} @g|E b}t  
JyPsRpi\  
docheckport() Mwdh]I,#  
{ yQwj [  
mylogger_info "do check port $1" j!r 4p,  
local port last_client count client total_count VdpkE0  
mT57NP  
if [ -z "$1" ] ; then %N )o* H&  
mylogger_notice "docheckport() port not given" #plwK-tPR  
return G~ZDXQ>5CP  
fi 66\0JsT?3  
DM6oMT  
port=$1 fShf4G_w\  
S=O/W(ZB  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` 2,Y8ML<  
if [ $? != 0 ] ; then `Rj<qz^7  
mylogger_notice "netstat call failed" Kb0O auW  
return b5YjhRimS  
fi ZTG*|  
#echo $clientlist 8 ?' ]W\)  
# reset new_block * [*#cMZ   
new_block=0 zypZ3g{vz  
count=0 9vL`|`Vau  
total_count=0 EWN$ILdD  
last_client="" /vP h_1  
for client in $clientlist [!{* )4$6  
do M;W{A)0i1  
#echo "client is $client" / /qTMxn  
if [ -z "$last_client" ] ; then ~Dr/+h:^\  
  count=$((count+1)) [(tgoh/  
  total_count=$((total_count+1)) D8_m_M| P  
  last_client=$client io cr  
else </<_e0  
  if [ "$client" = "$last_client" ] ; then ?[Q;275  
  count=$((count+1)) =HP_IG_  
  total_count=$((total_count+1)) v:CYf_  
  else EA75 D&>I  
  mylogger_debug "$last_client $count connections" BW:&AP@B  
  if [ $count -ge $max_active_conn ] ; then |LKhT4rE  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" -]5dD VSO  
    blockclient $last_client $port df #DKV:  
  fi '2zo  
  count=1 t=S94 ^g  
  total_count=$((total_count+1)) /{7x|ay]  
  last_client=$client +n9&q#ah  
  fi ,{itnKJC  
fi {^9,Dy_D  
done gq[}/E0e  
# check the last client 0$ &Z_oJ  
if [ ! -z "$client" ] ; then :<ujk  
count=$((count+1)) n5UcivyX  
total_count=$((total_count+1)) GfQMdLy\Z  
mylogger_debug "$client $count connections" EPI mh  
if [ $count -ge $max_active_conn ] ; then 30FYq?  
  mylogger_notice "client $client connection $count >= $max_active_conn" ]S,I}NP  
  blockclient $client $port \EYhAx`2  
fi I=f1kr pR  
fi LLgN%!&  
mylogger_info "total connections on port $port: $total_count" BEPDyy  
&#.XLe\y  
if [ $new_block = 1 ] ; then 0g]ABzTn  
restartservice $port Dr(.|)hv[&  
fi l:Ci'=  
} 1ErH \!  
k>VP<Zm13  
docheckall() CN brXN  
{ 6xnJyEQUM  
# reset wakeup_time K+T`'J4  
wakeup_time=$wakeup_time_max Vize0fsD  
for port in $portlist 5?9}^s4  
do S)g5Tu)  
docheckport $port MrOW&7  
if [ $new_block = 1 ] ; then Y z-b~D/=}  
  # set wakeup_time shorter cause we found some abuse client + 6r@HK`,t  
  wakeup_time=$wakeup_time_min XFK$p^qu  
fi IA8kq =W  
done 2U~oWg2P  
} s)/i_Oe$\  
Qg9{<0{u  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then webT  
firewall="ipchains" T^A:pL1  
fi 0pkU1t~9  
Qt>yRt  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then oVC~RKA*  
firewall="iptables" K3 ]hUe #  
fi m} Yf6:cr  
OY"{XnPZ  
if [ -z "$firewall" ] ; then ",Q\A I  
echo "Error: This machine does not have ipchains or iptables firewall support" Grjm9tbX}  
exit 1 #+Vvf  
fi uY "88|  
PFEi=}Y@((  
mylogger_info "firewall.sh v$myver ValueOf.com starting" IUu[`\b=  
mylogger_info "Firewall is:       $firewall" : O//A6 v  
mylogger_info "Port protected:     $portlist" /}M@ @W  
mylogger_info "Max connection per ip: $max_active_conn" ,T&B.'cq  
mylogger_info "Min time to check:   $wakeup_time_min""s" ,qvz:a  
mylogger_info "Max time to check:   $wakeup_time_max""s" [eWB vAiW  
mylogger_info "Timeout circle:     $rule_timeout""s" _v2FXm   
mylogger_info "Output is logged to:   $log_facility" [HNGTde&  
]^Q`CiKd  
# if new ip blocked at this check run? "F%cn@l  
new_block=0 e?Cbl'  
# if new ip blocked at this timeout run? zD)2af  
ever_block=0 n$b/@hp$z  
# reset wakeup_time ?"sk"{  
wakeup_time=$wakeup_time_max dnNc,l&g  
5jYRIvM[Q~  
lasttime=`date +%s` ^'FY!^dE  
xQJdt $]U@  
while [ 1 ] <[a9"G 7  
do z),l&7  
curtime=`date +%s` vQIoj31  
timediff=$((curtime-lasttime)) #Va@4<4r  
#echo "timediff: $timediff" k# &y  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then 40q8,M  
lasttime=$curtime ^PWZ1.T  
ever_block=0 &:{| nDT_2  
dotimeout _Thc\{aV#  
fi <'B`b  
docheckall wetkmd  
mylogger_info "sleep for $wakeup_time""s" (6JD<pBm  
sleep $wakeup_time Ye1P5+W(  
done $9ON 3>  
TXy*-<#vR  
R8HA X  
p]pFZ";70  
1. 说明 v3aiX  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, `CHgTkv  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 x28Bz*O  
该程序可以同时保护多个端口 9prG@  
o*dhks[  
2. 安装 ];Z_S`JR  
tar zxf firewall-1.0b.tar.gz rtUd L,Hx  
cd firewall-1.0b !)&-\!M>  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh 'l<kY\I!%  
flmcY7ZV  
3. 配置 orL7y&w(v:  
主要配置项目如下: ,cbP yg  
# 最小检查周期,缺省为120秒 +  ^~n09  
wakeup_time_min=120 r%xNfTa  
Q>z0?%B  
# 最大检查周期,缺省为300秒 %`g qV9a  
wakeup_time_max=600 OGae]O<  
4V[+6EV  
# 重设防火墙状态的时间,缺省为3600秒 Wc+ e>*  
rule_timeout=3600 U>:p`@  
@Yt[%tOF+  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) ,',fO?Qv'  
# 一般的网络攻击都是针对80和25,又以80居多 ^TB%| yZ _  
portlist="80 25" eQYW>z'%,  
t,RR\ S  
# 每个ip可占用的最大活动(Established)连接数 67f#Z&r2k  
max_active_conn=8 /S|Pq!4<  
eBBh/=Zc  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 a0.)zgWr  
# 如果用的是ipchains,可以忽略此项 9fe~Q%x=u  
iptables_chain_name="RH-Lokkit-0-50-INPUT" B\N,%vsx#U  
znu?x|mV  
# 日志输出目标 y+X%qTB  
log_facility="local0" ^>]p4Q3 6  
u6|7P<HUfb  
**** 关于检查周期 **** sd#a_  
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 ,wnF]K 2D0  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 <;_X=s`f,  
VE{t]>*-u  
**** ipchains vs iptables **** =SLJkw&w6  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 q \0>SG  
/etc/sysconfig/iptables都没有检测到,则报错退出。 IWKQU/l!  
Z=wLNmH  
**** 日志输出 **** }X}fX#[  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf Soq 'B?>  
中加入一条: h5B'w  
local0.*                 /var/log/firewall.log yPuT%H&i  
然后重新启动syslog _%"/I96'  
/etc/init.d/syslog restart v')T^b F@  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 r=s2wjk  
Z?1.Y7Npr  
4. 运行 opdi5 e)jK  
/usr/prima/sbin/firewall.sh & .y[=0K:  
Hz+edM UL  
范例输出: +d Ig&}Tr  
*** firewall.sh v1.0b ValueOf.com*** J Wof<D,  
Firewall is:       ipchains XCQ =`3f  
Port protected:     80 25 <C]s\ "o-`  
Max connection per ip: 8 \$GlB+ iCx  
Min time to check:   120s u9sffX5x[J  
Max time to check:   300s m$0T"`AP`  
Timeout circle:     3600s c00a;=ji  
Output is logged to:   local0 *G2p;n=2  
WVp14Z?k  
察看/var/log/firewall.log,可以看到: >*k3D&  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 So,EPB+  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 `3\5&Bf  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 rS8/_'  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 Ws.F=kS>h  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 (4x`/  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 1kvBQ1+  
U $e-e/  
5. 停止 )t&j0`Yq  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 R\ <HR9 r  
# ps auxww|grep firewall.sh .@.O*n#K  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh K'Bq@6@C g  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh D3|y|Dr  
第一行即firewall.sh的进程,用kill命令: 68W&qzw.[r  
# kill 27932 ~bg?V0  
[1]   Terminated             /usr/prima/sbin/firewall.sh 7S(5\9  
即将其终止


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

通过脚本自动屏蔽非法IP

http://www.bornin76.cn/?p=31 36iDiT_  
Esx"nex  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! E "9`  
%+gK5aVab  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: 2E7vuFH4c  
K X0{dizZ  
W5a7HkM  
#! /bin/bash 55LW[Pc  
rx[l7F q  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` wN}@%D-[v  
)w/ #T  
for i in $SCANIP <cig^B{nX  
do 0o-. m  
NUMBER=`echo $i|awk -F= '{print $1}'` oJ4OVfknD  
SCANIP=`echo $i|awk -F= '{print $2}'` ^W8kt  
echo $NUMBER ZhvZe/  
echo $SCANIP ) oypl+y  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] IZ2(F,{o  
then WZh%iuI{C  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP L[s7q0 F`l  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log "2m (*+  
fi zv.R~lMtY  
done @}{lp'8FYi  
~9ZW~z'  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 &d,!^9  
u$M,&Om  
Sj(5xa[  
------------------------------------------------------- ]wxjd l  
./l^Iz&0  
注释: =%=lq0GF0  
@yImR+^.7  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过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.018598(s) query 5, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息