deserts
大客部
级别: 总版主
精华:
0
发帖: 607
威望: 2 点
金钱: 1061 RMB
贡献值: 0 点
在线时间:1761(小时)
注册时间:2006-01-01
最后登录:2025-02-11
|
linux自动屏蔽IP工具
另存为 firewall.sh 给执行的权限 &JVe
-. s,\!@[N #!/bin/sh 6(f[<V!r # this program is used to check tcp/ip connections {()8 Wr # and block those ip with excessive connections oQBfDD0 5#!pwjt~7 # my version ]/XNfb myver="1.0RC1" MW &iNioX SuMK=^>% # wake up every 120s if last check found abuse client ]6v6&YV wakeup_time_min=120 9?:SxI;v {*bXO8vi(( # wake up every 300s if last check found no abuse client n9B1NM5 \ wakeup_time_max=300 rQC{"hS1 U~azI(1"W # rule timeout 3600s hR
Y*WL rule_timeout=3600 lY
yt8H o}mD1q0yE # check port list H1N_ portlist="80" \::<]
eNY$N_P # max established connection per ip &Y;z[+(P max_active_conn=8 &]iX
>m. qiV#T+\ # iptables chain name ZY-W~p1:G iptables_chain_name="RH-Lokkit-0-50-INPUT" x3xBl_t ,kfUlv= # log facility f7mP4[+dS log_facility="local0" `{s:lf ;>{BK, # Block policy c~[L;_ ipchains_block_policy="DENY" ':lADUt iptables_block_policy="REJECT" R@-rc|FunJ % zHsh # myself WB
Lfxr myself=`basename $0` by,"Orpwq; f8'D{OP"G mylogger_info() c&z@HEzV7 { _ #288`bU logger -p $log_facility.info -t $myself $@ 2>/dev/null C
vfm ,BL } H@GiHej 6|+I~zJ88 mylogger_debug() *6=2UJcJ { `O ?61YUQH logger -p $log_facility.debug -t $myself $@ 2>/dev/null fT9$0:eO } ~ \z7$9Q }a6tG mylogger_notice() J=|PZ2" { @eu4W^W logger -p $log_facility.notice -t $myself $@ 2>/dev/null |{udd~oE& } GZ(
W64 o5)lTVQ~~ dotimeout() 60(j[d-$p { E-\Wo3 mylogger_info "reset firewall when timeout arrives" _0p8FhNt case "$firewall" in [Zc8tE2oN ipchains) Ze_4MwCW /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null &g%9$*gmT if [ $? = 0 ] ; then aGY R:jR$ mylogger_info "ipchains restarted" LldZ"%P else +j
Z,vKr mylogger_notice "ipchains restart failed" J3`a}LyDf fi I(!i"b9 ;; =?hGa;/rb iptables) %X)w$}WH /etc/init.d/iptables restart 1>/dev/null 2>/dev/null ^ z!g3 if [ $? = 0 ] ; then c&4EO| mylogger_info "iptables restarted" =Ji+GJ<,9 else 3fA.DK[4[ mylogger_notice "iptables restart failed" eW%Cef fi mAO$gHQ ;; .=@xTJh *) !7)` g i mylogger_notice "neither ipchains nor iptables" x -CTMKX ;; S~L;oX?(! esac 3azyqpwU$ vl1`s
^}R } oY)xXx |7XP
u blockclient() 7T7
A[A\ { ,_+Gb if [ -z "$1" ] || [ -z "$2" ]; then 4G ?Cu,$ mylogger_notice "blockclient() missing client or port to block" (tG8HwV- return tNk.|} fi 0Ncx':]5 local ip port 5B:%##Ug5 VPC7Dh%. ip=$1 C_5o&O8Bc port=$2 +)J;4B 1:Sq?=& case "$firewall" in GvB;o^Wd ipchains) \t=ls
mylogger_notice "blocking $1 to $2 via ipchains" `eo$o! found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` ,_wpYTl*X if [ -z "$found" ] ; then +Oxw?`I$ cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" -!bfxbP mylogger_debug "cmd: $cmd" b7uxCH]Z
`ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` LdH23\ if [ $? != 0 ] ; then s@$SM,tnn mylogger_notice "$cmd call failed" 4lp90sa return xU6rZCqE fi S'
<X) new_block=1 uUIjntSF( ever_block=1 k00&+C else {9c_T!c mylogger_info "$ip already blocked to $port" N{}o*
K fi
C
CDO8 ;; &_1x-@oI2: iptables) ~@H9h<T mylogger_notice "blocking $1 to $2 via iptables" &=.SbS found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` ^D]y<@01 if [ -z "$found" ] ; then 6uu49x_^L4 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" hpBn_ mylogger_debug "cmd: $cmd" Z~{0x#?4% `iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` qmF+@R&^i if [ $? != 0 ] ; then
=7vbcAJ\ mylogger_notice "$cmd call failed" Rq[VP# return CubBD+hl* fi JTS<n4<a new_block=1 D>6vI ever_block=1 "t>W
M else NGp^/PZX0 mylogger_info "$ip already blocked to $port" /I@`B2 fi aSMSuX8 ;; A4IPd *) OlcWptM$ mylogger_notice "neither ipchains nor iptables" F!MxC ;; 5X}OUn8 esac d$<1Ma} } 'Im&&uSkr &B@qb?UE1 restartservice() "*oN~&flc { J?[}h&otQ local service 2`cVi"U if [ -z "$1" ] ; then rT!9{uK mylogger_notice "no port given to see which service to be restart" e=4k|8G return Jh`6@d fi vy{YGT A,f%0
eQR case "$1" in 9
?(P?H 80) 2\L}Ka|v service="httpd" O/{W:hJjd ;; *h Bo,
25) L}.V`
v{zc service="postfix" [.*o<
KP ;; -"X}
)N2 110) !#Hca service="courier-pop3d" cwKOE?! ;; &Vi0.o
21) }hX"A!0 service="muddleftpd" wA>bLPTw ;; %Q[+bN[/ 53) >6cENe_@t service="named" 6P:H` ;; G9uWn%
5r 3306) oO)KhA?
y service="mysqld" D$[/|%3 ;; 4!r>
^a esac dwt<s[k if [ ! -z "$service" ] ; then -hP-w>
/etc/init.d/$service restart 1>/dev/null 2>/dev/null bUi@4S if [ $? = 0 ] ; then Q`X5W mylogger_notice "$service restarted" k<3_!?3 else #O.-/&Z mylogger_notice "$service restart failed" fMFlY%@t fi sP NAG
fi jn;b{*Lf } 5bzYTK&- 4)3!n*I docheckport() 4';]fmf@[i { 'D4KaM.d mylogger_info "do check port $1" i#~1|2 local port last_client count client total_count Z0e+CEzq !u#o"e<qh if [ -z "$1" ] ; then {M?!nS6t mylogger_notice "docheckport() port not given" pPG@_9qf return =c8U:\0 fi uGQCW\!"4 N2_=^s7 port=$1 EN%Xs578 8&=+Mw clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` ~j\/3;^s
if [ $? != 0 ] ; then lC1X9Op mylogger_notice "netstat call failed" GSW{h[Op return ~^"cq
S( fi [21tT/ #echo $clientlist 9&W\BQ # reset new_block S@7A) new_block=0 ii*Ty!Sa count=0 *
qG=p` total_count=0 &fj&UBA last_client="" ^D
B0C for client in $clientlist /:e|B;P`k do %p2C5z? #echo "client is $client" 0{PK]qp7 if [ -z "$last_client" ] ; then _uHyE }d count=$((count+1)) fINM$ 6 total_count=$((total_count+1)) ~9oS~fP?I last_client=$client IFPywL{K else TL7-uH if [ "$client" = "$last_client" ] ; then VUF7-C* count=$((count+1)) J7QlGm,= total_count=$((total_count+1)) 1}8e@`G0.] else I/WnF"yP mylogger_debug "$last_client $count connections" [1nI%/</> if [ $count -ge $max_active_conn ] ; then ooZ7HTP| mylogger_notice "client $last_client connection $count >= $max_active_conn" 2z[Pw0#V blockclient $last_client $port $LRFG( fi TTE#7\K~B count=1 g'Xl>q total_count=$((total_count+1)) @/0aj last_client=$client K.z}%a fi Tz*5;y%4 fi uEi!P2zN
done sa#"@j) # check the last client @ ~sp:l if [ ! -z "$client" ] ; then y
ph count=$((count+1)) #^v5Eo total_count=$((total_count+1)) VS).!;>z mylogger_debug "$client $count connections" akqXh 9g if [ $count -ge $max_active_conn ] ; then tcX7Ua(I` mylogger_notice "client $client connection $count >= $max_active_conn" "Z{^i3gN blockclient $client $port THua?,oyW fi }dXL= ul fi lpp'.HTP mylogger_info "total connections on port $port: $total_count" -%N (X8 f(_qcgXp if [ $new_block = 1 ] ; then U50X`J restartservice $port uyA9`~p=# fi gvU6p[D } 8p211MQ< 9/%|#b-z docheckall() iK#{#ebAoW { cu"%>>,, # reset wakeup_time PLY7qMw wakeup_time=$wakeup_time_max E+2y-B)E for port in $portlist r"Bf@va do -12v/an]L7 docheckport $port WIGb7}egR if [ $new_block = 1 ] ; then <7&b|f$CL # set wakeup_time shorter cause we found some abuse client UB1/FM4~ wakeup_time=$wakeup_time_min "Dk:r/ fi M`<D Z<:< done wLO/2V}/ } gky_]7Av 5a`}DTB[Co if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then H xlw1(zS firewall="ipchains" b#m47yTW9< fi f/B--jq Z~|J"2. if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then 9N29dp>g{{ firewall="iptables" 3q>"#+R.t fi NLpD,q{ e>^R 8qM? if [ -z "$firewall" ] ; then }:mI6zsNj echo "Error: This machine does not have ipchains or iptables firewall support" ?MYD}`Cv exit 1 HA%ye"(y8 fi W% [5~N
#J!?
:(m: mylogger_info "firewall.sh v$myver ValueOf.com starting" Rv-o__C
! mylogger_info "Firewall is: $firewall" #r:`bQ0; mylogger_info "Port protected: $portlist" $ZU(bEUOG
mylogger_info "Max connection per ip: $max_active_conn" zi
,Rk. mylogger_info "Min time to check: $wakeup_time_min""s" *LRGfk+h mylogger_info "Max time to check: $wakeup_time_max""s" MUrY>FYgx mylogger_info "Timeout circle: $rule_timeout""s" b{rmxtx mylogger_info "Output is logged to: $log_facility" ^ztf:'l@C o^?{j*)g # if new ip blocked at this check run? K*9b `% new_block=0
%\cC]<> # if new ip blocked at this timeout run? {Y[D!W2y ever_block=0 VO Qt{v{1| # reset wakeup_time .y/b$|d, wakeup_time=$wakeup_time_max h+UscdUl )US/bC!M$ lasttime=`date +%s` }dUC^04 _kHpM:;. while [ 1 ] M9#QS`G do =!^iiHF curtime=`date +%s` 3ms/v:\ timediff=$((curtime-lasttime)) \z9?rvT: #echo "timediff: $timediff" mM*yv if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then k+[KD>;1 lasttime=$curtime -ZP&zOsDr ever_block=0 u;Eu<jU1 dotimeout U.U.\ fi KeI:/2 docheckall e:LZs0 mylogger_info "sleep for $wakeup_time""s" 1gm/{w6O sleep $wakeup_time {@5WeWlz~ done TRLeZ0EC $|(|Qzi% sBtG}Mo) LGROEn<*d 1. 说明 )O@^H firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, \'+{X(] 则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 I"sobZ` 该程序可以同时保护多个端口 R}wwC[{ #A/J^Ko 2. 安装 ~,!hE&LE~ tar zxf firewall-1.0b.tar.gz UD<^r]'x cd firewall-1.0b W(a'^
#xe install -m 700 firewall.sh /usr/prima/sbin/firewall.sh 2${,%8"0s ;D"P9b]9$ 3. 配置 1gShV ]2 主要配置项目如下: y'!p>/%v # 最小检查周期,缺省为120秒 O\?5#. wakeup_time_min=120 _`@Xy!Ye jaO#><f # 最大检查周期,缺省为300秒 K~Au?\{
wakeup_time_max=600 J;=aIiN]R M,\|V3s # 重设防火墙状态的时间,缺省为3600秒 W|H4i;u rule_timeout=3600 NkA6Cp[Q,1 TtwJ,&b # 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) Og$eQS # 一般的网络攻击都是针对80和25,又以80居多 e(n2+S#N portlist="80 25" >b4YbLkI# =>:% n # 每个ip可占用的最大活动(Established)连接数 +A'q#~yILa max_active_conn=8 |,a%z-l ;ad9{":J#B # iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 GwMUIevO_ # 如果用的是ipchains,可以忽略此项 Y1yXB).AH8 iptables_chain_name="RH-Lokkit-0-50-INPUT" MOp=9d+N~ 3)C6OF>7
# 日志输出目标 dEQReD log_facility="local0" KM_)7?` 0i`v:Lq% **** 关于检查周期 **** 2pxWv
)0 程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期 $LxG>db 的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 ~51kiQW %;=IMMK **** ipchains vs iptables **** Jm]]>K8.3V 目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 f
gK2.;> /etc/sysconfig/iptables都没有检测到,则报错退出。 d6t)gG*5 -~(0O **** 日志输出 **** 7zJrT5 程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf R<y Nv 中加入一条: q19k<BqR local0.* /var/log/firewall.log _q$0lqq~u 然后重新启动syslog #|{BGVp /etc/init.d/syslog restart .*RB~c
t 这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 4hsPbUx9 ;I!+lx3[ 4. 运行 OQytgXED /usr/prima/sbin/firewall.sh & g33<qYxP +X* F<6mZ 范例输出: l#2r.q^$| *** firewall.sh v1.0b ValueOf.com*** o ;[C(OS Firewall is: ipchains sW]yuu!/ Port protected: 80 25 ]/aRc=Gn Max connection per ip: 8 ;_?zB NW Min time to check: 120s WX%h4)z* Max time to check: 300s S F&M
(=w< Timeout circle: 3600s ESC Output is logged to: local0 =R8f)UQYx W;3
R; 察看/var/log/firewall.log,可以看到: "jl1.Ah Oct 16 14:08:55 server firewall.sh: do check port 80 // 检查80端口 @K,2mhE~h Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections // 有两个来自192.168.0.60的连接 5z
9r S< Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2 // 80端口总共2个连接 '`M#UuU Oct 16 14:08:55 server firewall.sh: do check port 25 // 检查25端口 DHh+%|e Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0 // 25端口没有连接 S_(&UeTC Oct 16 14:08:55 server firewall.sh: sleep for 300s // 等待300秒 Qv&T E3 q*HAIw[<y 5. 停止 X)FL[RO%q 先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 kN'|,eKH4 # ps auxww|grep firewall.sh w"fCI13 root 27932 0.0 0.5 2312 1060 pts/2 S 12:38 0:00 /bin/sh /usr/prima/sbin/firewall.sh a6;gBoV root 27967 0.0 0.3 1732 592 pts/2 S 12:39 0:00 grep firewall.sh 2^zg0!z 第一行即firewall.sh的进程,用kill命令: RtW4n:c # kill 27932 sl~b\j [1] Terminated /usr/prima/sbin/firewall.sh ^K7q<X, 即将其终止
|
常州电信/网通机房,100M共享/10M独享/1000M共享/100M独享/电信+网通双线路服务器托管
Tel:0519-89991155 企业QQ:4006023839 5y6s Inc.
|
[楼 主]
|
Posted: 2008-01-26 02:12 |
| |