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


常州五颜六色网络技术有限公司 -> 网站建设 -> 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 给执行的权限 y iO!ZT  
N;4bEcWjp  
#!/bin/sh @&?E3?5ll  
# this program is used to check tcp/ip connections q;Tdqv!Ju  
# and block those ip with excessive connections w^z5O6   
V[ju7\>$Z  
# my version uP, iGA  
myver="1.0RC1" <|Srbs+  
5;'(^z-bL  
# wake up every 120s if last check found abuse client 01=nS?  
wakeup_time_min=120 |y9(qcKn$  
@~k4,dJ  
# wake up every 300s if last check found no abuse client %"-bG'Yc  
wakeup_time_max=300 %O6r  
"Vh3hnS~  
# rule timeout 3600s "uCO?hv0  
rule_timeout=3600 [wU e"{  
Hb}O/G$a*  
# check port list RO+ jVY~H-  
portlist="80" PAng(tubl  
'41'Gn  
# max established connection per ip |w5m2Z  
max_active_conn=8 Zt_~Zxn3  
&!P' M  
# iptables chain name H9;0$Y(e-  
iptables_chain_name="RH-Lokkit-0-50-INPUT" [|".j#ZlK  
U!d|5W.{Q  
# log facility c wNJ{S+  
log_facility="local0" ~c,CngeL0  
QB uX#bDV  
# Block policy u9nJ;:  
ipchains_block_policy="DENY" [)+wke9  
iptables_block_policy="REJECT" ?f}?I`S,  
3r VfBz  
# myself s$%t2UaV  
myself=`basename $0` JDJ"D\85  
(> W \Nf  
mylogger_info() z$gtGrU  
{ qk~m\U8r  
logger -p $log_facility.info -t $myself $@ 2>/dev/null 80zpR U"  
} _ amP:h  
l`uI K.  
mylogger_debug() P_4E<"eK  
{ b wM?DY  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null He,, bq  
} 78 w  
l A%FS]vh  
mylogger_notice() Jd^Lnp6?  
{ SJ91(K  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null JK`$/l|7  
} M^*\ $K%  
2eHVl.C5  
dotimeout() (dZ]j){  
{ /ej[oR  
mylogger_info "reset firewall when timeout arrives" s`ZP2"`f  
case "$firewall" in 06O_!"GD}  
ipchains) "u]&~$  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null )Gavjj&uJ  
  if [ $? = 0 ] ; then KF6C=,Yc%  
    mylogger_info "ipchains restarted" NT0n [o^  
  else ?2l `%l5(  
    mylogger_notice "ipchains restart failed" ,jEc4ih4  
  fi AT)b/ycC  
  ;; aZ$5"  
iptables) ?m5E Xe  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null N2k<W?wQ  
  if [ $? = 0 ] ; then UOsK(mB  
    mylogger_info "iptables restarted" R)BXN~dQ  
  else a _+?#m  
    mylogger_notice "iptables restart failed" A+!,{G  
  fi g0bYO!gC r  
  ;; 0Dna+V/jI  
*) l_/(J)|a  
  mylogger_notice "neither ipchains nor iptables" mE`qA*=?  
  ;; -&sY*(:n_  
esac ;:j1FOj  
w6Nn x5Ay  
} n8F~!|lQ0  
4")`}T  
blockclient() tnaFbmp  
{ Vn4wk>b}$2  
if [ -z "$1" ] || [ -z "$2" ]; then >xabn*Kq  
mylogger_notice "blockclient() missing client or port to block" ,bXZ<RY $  
return &G\mcstX  
fi w+>+hq  
local ip port gT_KOO0n  
~<f[7dBv  
ip=$1 UPU$SZAIx  
port=$2 HK8sn1j  
_yv#v_Z  
case "$firewall" in >i,_qe?V:w  
ipchains) <- !1`@l>  
  mylogger_notice "blocking $1 to $2 via ipchains" X8Q'*  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` }a||@unr  
if [ -z "$found" ] ; then d]] z )  
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" YND}P9 h  
mylogger_debug "cmd: $cmd" g w([08  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` 4}h}`KZZ  
if [ $? != 0 ] ; then WJOoDS!i  
mylogger_notice "$cmd call failed" \]V:>=ry>  
return ^;RK-)  
fi & "&s,  
new_block=1 &bW,N  
ever_block=1 l,7& z  
else Bz/Vzc(  
mylogger_info "$ip already blocked to $port" Wr%ov6:  
fi a)JXxst  
  ;; d fj23+  
iptables) <bKtAf  
  mylogger_notice "blocking $1 to $2 via iptables" eo0-aHs  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` yV!4Im.>  
if [ -z "$found" ] ; then js<d"m*  
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" CaJ-oy8  
mylogger_debug "cmd: $cmd" .J)TIc__|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` iYSt l  
if [ $? != 0 ] ; then 'dwT&v]@  
mylogger_notice "$cmd call failed" s$R /!,c  
return ^HSxE  
fi fY!?rZ)$  
new_block=1 ]nhh|q9r{  
ever_block=1 b_31 \  
else "CT}34l  
mylogger_info "$ip already blocked to $port" /I`TN5~  
fi r%PWv0z_c  
  ;; xol%\$|  
*) zuvPV{ X  
  mylogger_notice "neither ipchains nor iptables" fjRVYOG#  
  ;; G)Gp}4gV}  
esac {]HiTpn  
} &kg^g%%  
nje7?Vz  
restartservice() rwF$aR>9  
{ c{=Sy;i@  
local service =?RI`}vw_H  
if [ -z "$1" ] ; then ^[?y 2A:  
mylogger_notice "no port given to see which service to be restart" C}8#yAS9M  
return %Rk|B`ST  
fi GcVQz[E  
4D?h}U /  
case "$1" in 4\;zz8 5E  
80) I=0c\ U}  
  service="httpd" 3(BL  
  ;; /Q1*Vh4  
25) rnIj pc F  
  service="postfix" ")Fd'&58  
  ;; B.8B1MFm  
110) V?{d<Ng~J  
  service="courier-pop3d" ,h*gd^i  
  ;; W'./p"2g  
21) ?2<) Jw  
  service="muddleftpd" Tv``\<   
  ;; ;+ hh|NiQ  
53) Cj0r2^`  
  service="named" KdEvu?  
  ;; !M~: #k  
3306) ziM{2Fs>  
  service="mysqld" Yk Ku4f  
  ;; I{IB>j}8  
esac 1w>[&#7  
if [ ! -z "$service" ] ; then q<09]i  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null KXPCkNIN!  
if [ $? = 0 ] ; then ?{J1&;j*  
  mylogger_notice "$service restarted" ZdW+=;/#  
else 7Jb&~{DVk  
  mylogger_notice "$service restart failed" db )2>  
fi 6qZQ20h  
fi ;j0.#P:a  
} {\$S585  
eXl?f_9  
docheckport() e@hPb$7  
{ X_PzK'#m  
mylogger_info "do check port $1" |.; N_i  
local port last_client count client total_count +wQ5m8E  
A+/Lt>+AS  
if [ -z "$1" ] ; then xM_#FxJb  
mylogger_notice "docheckport() port not given" r'i9 9 ~  
return kHK<~srB  
fi U`*we43  
ir^d7CV,   
port=$1 Dk\%,[4(  
H;=yR]E  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` v=^^Mr"Z^  
if [ $? != 0 ] ; then T#&1q]P1F  
mylogger_notice "netstat call failed" uNf'Zeo  
return za6 hyd^  
fi +aL6 $  
#echo $clientlist |mhKD#:  
# reset new_block wW TuEM  
new_block=0 z{R Mb  
count=0 IiKU =^~w  
total_count=0 Xp;'Wa"@  
last_client="" @!$xSH  
for client in $clientlist +^:uPW^U  
do 7gtaI3   
#echo "client is $client" &'\-M6GW  
if [ -z "$last_client" ] ; then 41R~.?  
  count=$((count+1)) \1R*M  
  total_count=$((total_count+1)) hj= n;,a9  
  last_client=$client DI{Qs[  
else j\#)'>"  
  if [ "$client" = "$last_client" ] ; then W&A^.% 2l  
  count=$((count+1)) S?DMeZ{ :  
  total_count=$((total_count+1)) u}'m7|)8  
  else )m-(-I  
  mylogger_debug "$last_client $count connections" o<|u4r={s  
  if [ $count -ge $max_active_conn ] ; then ddYb=L+_b  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" RCkmxO;b&  
    blockclient $last_client $port EhW@iYL  
  fi TPJuS)TU9  
  count=1 $y)tcVc  
  total_count=$((total_count+1)) BAqwYWdS  
  last_client=$client 9aID&b +  
  fi rl"yE=  
fi JpQV7}$  
done #,j m3M qj  
# check the last client 'fb&3  
if [ ! -z "$client" ] ; then 7CT446  
count=$((count+1)) V2Z^W^  
total_count=$((total_count+1)) a/d8_(0  
mylogger_debug "$client $count connections" ylmVmHmc  
if [ $count -ge $max_active_conn ] ; then 4YMUkwh  
  mylogger_notice "client $client connection $count >= $max_active_conn" rp-.\Hl/a  
  blockclient $client $port ?AQR\)P  
fi NS4W!o;"  
fi :2L-Nf  
mylogger_info "total connections on port $port: $total_count" K>+c2;t;  
B3u:D"t  
if [ $new_block = 1 ] ; then 3k+46Wp  
restartservice $port S?H qrf7<  
fi Oi!uJofW  
} c }7Rt|`c  
:as2fO$?  
docheckall() F-?s8RD  
{ 9(9\kQj{C  
# reset wakeup_time 62OZj%CXN  
wakeup_time=$wakeup_time_max |A u+^#:;  
for port in $portlist 2K(zYv54  
do k5Df9 7\s  
docheckport $port Gy[m4n~Z5  
if [ $new_block = 1 ] ; then BG20R=p  
  # set wakeup_time shorter cause we found some abuse client gM#]o QOGE  
  wakeup_time=$wakeup_time_min oGZ%w4T  
fi x_KJCU  
done 7gbu7"Qc  
} %,5_]bGvb  
[lAZ)6E~=  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then <bOi}  
firewall="ipchains" sZP A(N?  
fi e/!xyd  
1A\N$9Dls  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then +dSO? Y]  
firewall="iptables" -Fs<{^E3j  
fi S_56!  
w+3-j  
if [ -z "$firewall" ] ; then m&8 'O\$  
echo "Error: This machine does not have ipchains or iptables firewall support" niJtgK:H^  
exit 1 3N5b3F  
fi TW}nO|qw  
3rw<#t;v  
mylogger_info "firewall.sh v$myver ValueOf.com starting" x.~AvJ  
mylogger_info "Firewall is:       $firewall" x\R 8W8M  
mylogger_info "Port protected:     $portlist" =L&}&pT  
mylogger_info "Max connection per ip: $max_active_conn"  - }9a%  
mylogger_info "Min time to check:   $wakeup_time_min""s" kls 6Dk#  
mylogger_info "Max time to check:   $wakeup_time_max""s" NvqIYW  
mylogger_info "Timeout circle:     $rule_timeout""s" y)e8pPDG  
mylogger_info "Output is logged to:   $log_facility" %d#h<e|,.  
$ ,9A?'  
# if new ip blocked at this check run? XIJW$CY  
new_block=0 _)? 59  
# if new ip blocked at this timeout run? MYVgi{  
ever_block=0 V 0M&D,  
# reset wakeup_time VuWBWb?0Q  
wakeup_time=$wakeup_time_max 7dbGUbT  
(6#, $Ze   
lasttime=`date +%s` -\V! f6Q  
78fFAN`  
while [ 1 ] T@)|0M  
do .Do(iYO.L  
curtime=`date +%s` EAPjQA-B?  
timediff=$((curtime-lasttime)) LW '3m5  
#echo "timediff: $timediff" oW \k%Vj  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then oDBv5  
lasttime=$curtime GLE/ 1  
ever_block=0 /_l$h_{DH  
dotimeout bs9X4n5  
fi 'NYW`,  
docheckall 6I!B>V#U+  
mylogger_info "sleep for $wakeup_time""s" p]E\!/  
sleep $wakeup_time bc}BQ|Q  
done O; #qG/b1  
r(6$. zx  
d$T856  
qb 1JE[2F  
1. 说明 > t~2  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, J,Rp&tavt:  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 ;\48Q;  
该程序可以同时保护多个端口 gHmy?+)  
9T;DFUM  
2. 安装 { d|lN:B  
tar zxf firewall-1.0b.tar.gz H05xt$J  
cd firewall-1.0b V3v/h V:  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh @h)Z8so  
3EO:Uk5<   
3. 配置 _A]=45cn ~  
主要配置项目如下: 9L7jYy=A#  
# 最小检查周期,缺省为120秒 .6HHUy  
wakeup_time_min=120 n>k1 D  
_oAWj]~rO  
# 最大检查周期,缺省为300秒 Bh ,GQHJ  
wakeup_time_max=600 n&\DJzW\#  
Cr>YpWm  
# 重设防火墙状态的时间,缺省为3600秒 *w5xC5*  
rule_timeout=3600 y)uxj-G  
o_R_  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) )PCh;P0C  
# 一般的网络攻击都是针对80和25,又以80居多 !Y^3%B%  
portlist="80 25" nXM[#~  
b`& :`  
# 每个ip可占用的最大活动(Established)连接数 MN<uIqG  
max_active_conn=8 :J;&Z{  
vugGMP;D(  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 |] cFsB#G  
# 如果用的是ipchains,可以忽略此项 iTc q=  
iptables_chain_name="RH-Lokkit-0-50-INPUT" GD@|X wK){  
,%d?gi"&  
# 日志输出目标 O/Mx $Q3re  
log_facility="local0" V)jF]u~g  
fjh,e  
**** 关于检查周期 **** <*[D30<  
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 \V %l.P4>e  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 'Rq2x-72}  
U#g ,XJ  
**** ipchains vs iptables **** cxz\1Vphd  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 RfQ*`^D  
/etc/sysconfig/iptables都没有检测到,则报错退出。 LXS)(-&  
{66P-4Ev(  
**** 日志输出 **** H>Q X?>j  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf WwF2Ry^a  
中加入一条: UH`cWVLpr  
local0.*                 /var/log/firewall.log A@ZsL  
然后重新启动syslog t~M0_TnXlP  
/etc/init.d/syslog restart E-v^eMWX  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 ~nRbb;M  
CZDWEM}   
4. 运行 ")i_{C,b^  
/usr/prima/sbin/firewall.sh & ]PQ6 em  
&f-x+ y  
范例输出: TpRI+*\  
*** firewall.sh v1.0b ValueOf.com*** ,A[NcFdCB  
Firewall is:       ipchains >~*}9y0$  
Port protected:     80 25 j2s{rQQ  
Max connection per ip: 8  _j2q  
Min time to check:   120s K;*B$2Z#k  
Max time to check:   300s go?}M]c%7  
Timeout circle:     3600s n8Rsle`a  
Output is logged to:   local0 h~5gHx/ a  
,:xses*7  
察看/var/log/firewall.log,可以看到: l,ic-Y1  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 h3j`X'  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 $_O;yz  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 .o)  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 2#X>^LH  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 an)Z.x  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 F7\nG}#s  
juEH$7N !  
5. 停止 IgEVz^W?h  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 " nLWvV1  
# ps auxww|grep firewall.sh E=]$nE]b  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh %afz{a5  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh -IS$1  
第一行即firewall.sh的进程,用kill命令: A'suZpL  
# kill 27932 7ZFd;-  
[1]   Terminated             /usr/prima/sbin/firewall.sh y4,t=Gq7^  
即将其终止


常州电信/网通机房,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 I~25}(IDZ"  
_Q}vPSJviC  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! ,<fs+oi  
#z9@x}p5g  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: )gNVJ  
Y {2L[5_1  
.p-T >  
#! /bin/bash B) s%B'  
^Kn:T` vB  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` qoOq47F  
=p&uQ6.i+  
for i in $SCANIP 6Wc.iomx8  
do E~ 1"Nh  
NUMBER=`echo $i|awk -F= '{print $1}'` J;Veza  
SCANIP=`echo $i|awk -F= '{print $2}'` m]/s R3yF  
echo $NUMBER AP(%m';  
echo $SCANIP 9l}G{u9a  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] SHN'$f0Mb  
then ^ 4{"h  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP 8J8@0  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log io*iA<@Gx  
fi O8 5)^  
done o7v,:e:  
P30|TU+B  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 CF/8d6}Vf  
xoN?[  
`r*bG=  
------------------------------------------------------- ]McDN[h:  
zC_@wMWB  
注释: 'SnB7Y  
1g_D kv|D  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过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.019392(s) query 5, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息
51La