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


常州五颜六色网络技术有限公司 -> 网站建设 -> 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 给执行的权限 ;{0%Vp{  
98vn"=3  
#!/bin/sh e488}h6#m  
# this program is used to check tcp/ip connections *@W B aN+  
# and block those ip with excessive connections hE'>8{  
]@CXUa,>a  
# my version *o<|^,R  
myver="1.0RC1" zXkq2\GHA  
J6@RIia  
# wake up every 120s if last check found abuse client (^]3l%Ed  
wakeup_time_min=120 ^KV:.up6  
u]]mbER*t#  
# wake up every 300s if last check found no abuse client n;>r  
wakeup_time_max=300 mqY=N~/O  
}^*`&Lh  
# rule timeout 3600s _*xY>?Aq  
rule_timeout=3600 MFipXE!  
f{|n/j;n=C  
# check port list J8[aVG  
portlist="80" >A}ra^gU  
}|RL6p-/'  
# max established connection per ip q3|SZoN  
max_active_conn=8 xFA`sAucr  
j_b/66JyN  
# iptables chain name 7>EMr}f C  
iptables_chain_name="RH-Lokkit-0-50-INPUT" %~0]o@LW7  
(.b!kfC  
# log facility @[Wf!8_  
log_facility="local0" ${97G#  
V3/OKI\o  
# Block policy AU;Iif6  
ipchains_block_policy="DENY"  gA19f  
iptables_block_policy="REJECT" dE 3M    
upq3)t_  
# myself SXV2Y-  
myself=`basename $0` s,M]f,T  
6 I43a1[s  
mylogger_info() P7 5@Yu(  
{ dd4^4X`j  
logger -p $log_facility.info -t $myself $@ 2>/dev/null m=w #l>!  
} =~q$k  
$]a*ZHd;2&  
mylogger_debug() $M1;d1e6'  
{ T6\d]  
logger -p $log_facility.debug -t $myself $@ 2>/dev/null p#>,{  
} 8boiJku`  
HLqDI lL  
mylogger_notice() +77j2W_0  
{ LDDt=HEY4  
logger -p $log_facility.notice -t $myself $@ 2>/dev/null $6d5W=u$H  
} 1([?EfC  
ayN[y  
dotimeout() 2l~qzT-  
{ = jTC+0u  
mylogger_info "reset firewall when timeout arrives"  o%$R`;  
case "$firewall" in )0=H)k0  
ipchains) EIOP+9zP  
  /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null jTqE V (  
  if [ $? = 0 ] ; then 6dRvx;d  
    mylogger_info "ipchains restarted" - P4X@s_;  
  else >ffQ264g=i  
    mylogger_notice "ipchains restart failed" pO2XQYhrY  
  fi M2A_T.F=H  
  ;;  e )]  
iptables) - I~\  
  /etc/init.d/iptables restart 1>/dev/null 2>/dev/null \{o<-S;h  
  if [ $? = 0 ] ; then E .CG  
    mylogger_info "iptables restarted" +,eF(VS!  
  else "[CR5q9Pr  
    mylogger_notice "iptables restart failed" W}WDj:  
  fi &p55Cg@e)  
  ;; %'Z`425a  
*) E/^N   
  mylogger_notice "neither ipchains nor iptables" O 2+taB  
  ;; /WIH#M  
esac 4^ZbT  
Ag;Ybk[  
} .Z@iz5  
gdg "g6b  
blockclient()  \ns} M3  
{ )J 8mn*  
if [ -z "$1" ] || [ -z "$2" ]; then sLc,Dx"+  
mylogger_notice "blockclient() missing client or port to block" `F_R J.g*p  
return perhR!#J  
fi p R ! m  
local ip port k: z)Sw  
U(hIT9  
ip=$1 !fJy7Y  
port=$2 x}uDW   
s6Il3K f  
case "$firewall" in !_-Uwg  
ipchains) Mwa Rwk ;  
  mylogger_notice "blocking $1 to $2 via ipchains" D=M'g}l  
found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` _CizU0S  
if [ -z "$found" ] ; then )k81   
cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" l=<},_]{  
mylogger_debug "cmd: $cmd" w3"%d~/[x  
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` .RWq!Z=)3  
if [ $? != 0 ] ; then $]4o!Z  
mylogger_notice "$cmd call failed" 5m?9O7Pg  
return -` 1)yhS  
fi 5YXMnYt9  
new_block=1 )4n]n:FjN  
ever_block=1 >j&k:  
else *~d<]U5h  
mylogger_info "$ip already blocked to $port" cea e~  
fi :#=B wdC  
  ;; I:YE6${k!  
iptables) dlMjy$/T  
  mylogger_notice "blocking $1 to $2 via iptables" '_" S/X +v  
found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` -5@hU8B'a  
if [ -z "$found" ] ; then J4?SC+\  
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" VI)hA ^ S  
mylogger_debug "cmd: $cmd" Z*.rv t  
`iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` jV#1d8qm  
if [ $? != 0 ] ; then /`7G7pQ+  
mylogger_notice "$cmd call failed" [o.#$(   
return y_T%xWK 5  
fi jgBJs^JgYG  
new_block=1 a4",BDx  
ever_block=1 wYh]3  
else w } r mYQ  
mylogger_info "$ip already blocked to $port" kWgrsN+Z  
fi !ozHS_  
  ;; Jjh=zxR>  
*) 0kaMYV?  
  mylogger_notice "neither ipchains nor iptables" H:o=gP60]  
  ;; L tK, _j  
esac $^W|@et{ ]  
} TIno"tc3  
|j4;XaG)  
restartservice() unyU|B  
{ ,N8SP 'R  
local service ;B;wU.Y"  
if [ -z "$1" ] ; then -c %'f&P  
mylogger_notice "no port given to see which service to be restart" cq8JpSB(  
return Jv~^hN2  
fi /=KEM gI?  
AdRK)L  
case "$1" in &Vg)/t;  
80) 8/-hODoT_  
  service="httpd"  r^,"OM]  
  ;; 'w_Qs~6~{  
25) C%"aj^u  
  service="postfix" 66scBi_d  
  ;; ^-(DokdBn  
110) KR>o 2  
  service="courier-pop3d" P5v;o9B&  
  ;; \ F)}brPc  
21) _[N*k"  
  service="muddleftpd" [!`5kI  
  ;; 'v5q/l  
53) 18> v\Hi<  
  service="named" {X-a6OQj  
  ;; !'>,37()  
3306) 5`q#~fJ2  
  service="mysqld" p,7?rI\N  
  ;; 2h1P!4W85  
esac "lL/OmG  
if [ ! -z "$service" ] ; then 1+qP7 3a^  
/etc/init.d/$service restart 1>/dev/null 2>/dev/null CM)V^k*  
if [ $? = 0 ] ; then Ka$lNL3<j  
  mylogger_notice "$service restarted" [ZS.6{vr  
else D{JwZL@7k2  
  mylogger_notice "$service restart failed" ~Jlq.S'  
fi I 1Yr{(ho  
fi @IOl0db  
} D0h6j0r 5  
T75N0/teS  
docheckport() |Gr@Mi5  
{  T NF  
mylogger_info "do check port $1" :)%cL8Nz]$  
local port last_client count client total_count $ SZIJe"K  
#It!D5A  
if [ -z "$1" ] ; then *pyC<4W  
mylogger_notice "docheckport() port not given" .p(r|5(b  
return t;_1/ mt  
fi o8S P#ET"n  
2m9qg-W  
port=$1 /buj(/q^#  
$9Gra#  
clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` Z 4c^6v  
if [ $? != 0 ] ; then 3;R`_#t+  
mylogger_notice "netstat call failed" $)'LbOe  
return BRv#`  
fi xM/WS':V  
#echo $clientlist c)c_Qv  
# reset new_block kH= qJ3Z  
new_block=0 }&%&0$%  
count=0 FA;B :O@:'  
total_count=0 KVoM\ttP  
last_client="" 1+Vei<H$  
for client in $clientlist #r]GnC,  
do  !>Q{co'  
#echo "client is $client" <80M$a g  
if [ -z "$last_client" ] ; then jJ?G7Q5 l  
  count=$((count+1)) ac8su0  
  total_count=$((total_count+1)) <)oxs ]<  
  last_client=$client =.,XJIw&  
else Me^L%%: @  
  if [ "$client" = "$last_client" ] ; then Y>K8^GS  
  count=$((count+1)) ,.&D{ $1W  
  total_count=$((total_count+1)) fqn;,!D?9  
  else $=Tq<W*c  
  mylogger_debug "$last_client $count connections" rEs!gG NN  
  if [ $count -ge $max_active_conn ] ; then t`1~5#?Du(  
    mylogger_notice "client $last_client connection $count >= $max_active_conn" Qd}h:U^  
    blockclient $last_client $port ryTtGx%a  
  fi uFaT~ 4  
  count=1 Vb?_RE_H  
  total_count=$((total_count+1)) .GFKy  
  last_client=$client Ex{;&UWm  
  fi )F6p+i="  
fi -U@ycx|r  
done ?y^ ix+ M  
# check the last client f1t?<=3Ek<  
if [ ! -z "$client" ] ; then 3GZrVhU?m  
count=$((count+1)) a(x#6  
total_count=$((total_count+1)) Vtk}>I@%  
mylogger_debug "$client $count connections" ^k!u  
if [ $count -ge $max_active_conn ] ; then Z FIgKWZ'  
  mylogger_notice "client $client connection $count >= $max_active_conn" {tnhP^C3>  
  blockclient $client $port pFEU^]V3*  
fi yI's=Iu`  
fi 6Q`7>l.|?  
mylogger_info "total connections on port $port: $total_count" 83Fmu/(  
X&,a=#C^  
if [ $new_block = 1 ] ; then pwV{ @h!  
restartservice $port N0fmC*1-  
fi j7C&&G q  
}  @T{I;8S  
9tB:1n}  
docheckall() 1(4IcIR5T;  
{ ))uki*UNK  
# reset wakeup_time }k6gO0z  
wakeup_time=$wakeup_time_max 3L;&MG=  
for port in $portlist </qli-fXB}  
do " R-Pe\W  
docheckport $port ~Yl%{1  
if [ $new_block = 1 ] ; then wdEQB-dA  
  # set wakeup_time shorter cause we found some abuse client :UDe\zcd "  
  wakeup_time=$wakeup_time_min pg4j PuCM  
fi >\ PNKpn{  
done |z.Ov&d4)(  
} C?PgC~y)  
{I QCA-AI  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then M"z3F! -j  
firewall="ipchains" CGCSfoS9f  
fi gK- $y9]~+  
I#](mRJ6  
if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then |dk9/xdX  
firewall="iptables" 2(NN QU@Uz  
fi  c|~f[  
__,}/|K2  
if [ -z "$firewall" ] ; then O:te;l Q K  
echo "Error: This machine does not have ipchains or iptables firewall support" `Z~\&r=  
exit 1 REKv&^FLN  
fi !$.h[z^  
|PW.CV0,  
mylogger_info "firewall.sh v$myver ValueOf.com starting" F7qQrE5bl  
mylogger_info "Firewall is:       $firewall" N^B y#Z  
mylogger_info "Port protected:     $portlist" #wZBWTj.  
mylogger_info "Max connection per ip: $max_active_conn" 3']=w@~ O[  
mylogger_info "Min time to check:   $wakeup_time_min""s" aG/L'we R  
mylogger_info "Max time to check:   $wakeup_time_max""s" NW@guhK.  
mylogger_info "Timeout circle:     $rule_timeout""s" X4:SH> U!  
mylogger_info "Output is logged to:   $log_facility" +#0,2 wR#  
gIaPS0Q  
# if new ip blocked at this check run? ,daZ K xT  
new_block=0 b>"=kN/  
# if new ip blocked at this timeout run? tD !$!\`O  
ever_block=0 lhhp6-r  
# reset wakeup_time .#$D\cwV  
wakeup_time=$wakeup_time_max z2.ZxL"*  
yW)X asn  
lasttime=`date +%s` b py576GwA  
M4(`o^n  
while [ 1 ]  Gu P1  
do ImVHX~ qHJ  
curtime=`date +%s` ia}V8i  
timediff=$((curtime-lasttime)) #9) D.d|5  
#echo "timediff: $timediff" YXWlg%s  
if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then KF4}cM=.5  
lasttime=$curtime t=$Hv  
ever_block=0 rq>Om MQ67  
dotimeout wX*F'r"z  
fi ' Zmslijf  
docheckall IHlTp0?  
mylogger_info "sleep for $wakeup_time""s" \a}W{e=FNT  
sleep $wakeup_time P1f@?R&t+  
done >k{KwFB^S  
EZvf\s>LT  
N N;'QiE  
79JU   
1. 说明 Pwh0Se5Z  
firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, ~)\E&c  
则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 4]$$ar)  
该程序可以同时保护多个端口 ?H2{R:  
^7 oXJu=  
2. 安装 +`)4jx)r/  
tar zxf firewall-1.0b.tar.gz a9CK4Kg  
cd firewall-1.0b NlnmeTLO5  
install -m 700 firewall.sh /usr/prima/sbin/firewall.sh 56 raZC  
O~WT$  
3. 配置 5, <:|/r  
主要配置项目如下: L93KsI  
# 最小检查周期,缺省为120秒 Px<*n '~}  
wakeup_time_min=120 ]VU a $$  
K!MIA  
# 最大检查周期,缺省为300秒 *j0kb"#  
wakeup_time_max=600 ^Q'^9M2)  
jK{)gO  
# 重设防火墙状态的时间,缺省为3600秒 3Y}X7-|)Z  
rule_timeout=3600 *75?%l  
n 7Bua  
# 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) j\Z/R1RcW  
# 一般的网络攻击都是针对80和25,又以80居多 )j[rm   
portlist="80 25" !}gC0dJ  
8"\g?/  
# 每个ip可占用的最大活动(Established)连接数 eaI!}#>R +  
max_active_conn=8 FB-_a  
E9L)dMZSpj  
# iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 ) OAd[u<  
# 如果用的是ipchains,可以忽略此项 T!T6M6?  
iptables_chain_name="RH-Lokkit-0-50-INPUT" :$`"M#vMX  
wlM"Zt  
# 日志输出目标 ]i3 1@O  
log_facility="local0" ];.H]TIc6  
'7!b#if  
**** 关于检查周期 **** BihXYux*  
程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 $ 'B0ZL  
的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 xt%-<%s%f  
O+Zt*jN;  
**** ipchains vs iptables **** #N=!O/Y  
目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 AQ>8]`e`  
/etc/sysconfig/iptables都没有检测到,则报错退出。 m{/?6h 1  
ng-g \&-  
**** 日志输出 **** `|1#Vuk  
程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf >&|C E2'  
中加入一条: u'o."J^&'  
local0.*                 /var/log/firewall.log }4!R 2c  
然后重新启动syslog E6{|zF/3'  
/etc/init.d/syslog restart ^MyuD?va  
这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 1CkBfK  
/e^q>>z  
4. 运行 =AhXEu^  
/usr/prima/sbin/firewall.sh & Z\{"/( Hi  
".9 b}}  
范例输出: kvzGI>H:  
*** firewall.sh v1.0b ValueOf.com*** A8?uCkG  
Firewall is:       ipchains VpDNp (2  
Port protected:     80 25 < p<J;@  
Max connection per ip: 8 2L#$WuM~^  
Min time to check:   120s zFy0Sz F  
Max time to check:   300s u? a*bW  
Timeout circle:     3600s y#'|=0vTvP  
Output is logged to:   local0 LV4]YC  
|U*wMYC  
察看/var/log/firewall.log,可以看到: ]]\\Y|0  
Oct 16 14:08:55 server firewall.sh: do check port 80                   // 检查80端口 STDT]3.  
Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections             // 有两个来自192.168.0.60的连接 9o|=n'o  
Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2         // 80端口总共2个连接 ZGUhje!  
Oct 16 14:08:55 server firewall.sh: do check port 25                   // 检查25端口 JG%y_ Qy?K  
Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0         // 25端口没有连接 b~khb!]  
Oct 16 14:08:55 server firewall.sh: sleep for 300s                     // 等待300秒 rkF]Q_'`t;  
?mdgY1  
5. 停止 qNI2+<u)j  
先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 S]kY'(V(*  
# ps auxww|grep firewall.sh [FHSFr E,5  
root   27932 0.0 0.5 2312 1060 pts/2   S   12:38   0:00 /bin/sh /usr/prima/sbin/firewall.sh H"_ZqEg  
root   27967 0.0 0.3 1732 592 pts/2   S   12:39   0:00 grep firewall.sh v(+9&  
第一行即firewall.sh的进程,用kill命令: }<y-`WB  
# kill 27932 {&_1/  
[1]   Terminated             /usr/prima/sbin/firewall.sh |!VSed#FSn  
即将其终止


常州电信/网通机房,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 MV"aO@  
a8K"Z-LlQ  
最近很是奇怪,我查看我的服务器日志,居然发现有来自全世界[1]的很多人在锲而不舍的试图猜解我的系统密码(遗憾的是还没人可以成功入侵)。我是穷尽我吃奶的智商也想不通,就这么一个破机器(无屏的IBM T23,开博说明里就已经明确说了),上面只是跑了一个可有可无的Blog程序而已,咋就这么多人感兴趣?莫不是都把我这里当成了入侵中央银行的系统入口?我倒是希望这是那个入口哦! mT8")J|2  
F<wwuCbF  
研究了一下,觉得通过对日志文件进行判断,识别出扫描者的IP地址,然后再对其进行处理,这样也许是一种比较不错的简单的解决办法。经过实践,证明这是可行的。脚本代码如下: '5V2{k$4U  
XP Nk#"  
h(aF>a\Z  
#! /bin/bash nK*$P +[R  
;M '?k8L  
SCANIP=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'` PxENLQ3a=  
RC5b'+E&#  
for i in $SCANIP {}ks[%,_\  
do =y" lX{}G  
NUMBER=`echo $i|awk -F= '{print $1}'` B|#*I[4`w@  
SCANIP=`echo $i|awk -F= '{print $2}'` MqXN,n+`k  
echo $NUMBER #8.%YG  
echo $SCANIP .VF4?~+M-  
if [ $NUMBER -gt 10 ] && [ -z "`iptables -vnL INPUT|grep $SCANIP`" ] n,eO6X 4  
then q=8I0E&q  
iptables -I INPUT -s $SCANIP -m state --state NEW,RELATED,ESTABLISHED -j DROP IZ /Md@C  
echo "`date` $SCANIP($NUMBER)" >> /var/log/scanip.log OA#AiQUR  
fi E7E>w#T5  
done X5kIM\  
D@T>z;  
这个世界终于清静了!有遇到类似情况的朋友可以一试,我的系统是Linux,防火墙是用的Iptables。 e-Z+)4fH  
%H\i}}PTe  
w]w>yD>$  
------------------------------------------------------- >U@7xeK  
/I~iUND"G  
注释: V[>MKB(  
(hTe53d<S?  
有美国、冰岛、日本、韩国、印度、挪威、唐山、内蒙古、广州等等,不过也许只是同一个人通过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.021109(s) query 7, Gzip enabled
会员言论不代表本站立场 本站法律顾问:北京汇泽律师事务所 韩律师
QQ:点击这里给我发消息 /点击这里给我发消息 /点击这里给我发消息