deserts
大客部
级别: 总版主
精华:
0
发帖: 607
威望: 2 点
金钱: 1061 RMB
贡献值: 0 点
在线时间:1761(小时)
注册时间:2006-01-01
最后登录:2025-02-11
|
linux自动屏蔽IP工具
另存为 firewall.sh 给执行的权限 QpZCU] 5.3=2/ #!/bin/sh +XP9=U*g # this program is used to check tcp/ip connections $"[5
]{'J # and block those ip with excessive connections mW&hUPRx >j$y@"+ # my version zIT)Hs5 myver="1.0RC1" (764-iv( dFVx*{6 # wake up every 120s if last check found abuse client g<(\#F}/ wakeup_time_min=120 {\
62c;. o/~Rf1 # wake up every 300s if last check found no abuse client @zGz8IF wakeup_time_max=300 {usv*Cm (@&+?A"6` # rule timeout 3600s D@
4sq^|2 rule_timeout=3600 2{;~Bgd > t~2 # check port list u]uZc~T portlist="80"
UOYhz. eOUv#F # max established connection per ip 9T;DFUM max_active_conn=8 '74*-yd H05xt$J # iptables chain name \;bDDTM iptables_chain_name="RH-Lokkit-0-50-INPUT" -4!S?rHwd+ /|#";QsPN # log facility tx_h1[qi log_facility="local0"
'LW~_\ 9[,+4&wX7 # Block policy 1}ER+;If ipchains_block_policy="DENY" g
?OC-zw iptables_block_policy="REJECT" }>$3B5} -a\[`JHi # myself h}&1
7M myself=`basename $0` Tx:S{n7& EAZLo; mylogger_info() fb|%)A= { y*H rv logger -p $log_facility.info -t $myself $@ 2>/dev/null iOXsj }
Lm1
- /Xf_b.ZM& mylogger_debug() JrNqS[c/ { *iiyU}x logger -p $log_facility.debug -t $myself $@ 2>/dev/null LVe[N-K } ocPM zq- G7SmlFn? mylogger_notice() K'U8ft*_ { P&sYS<9q logger -p $log_facility.notice -t $myself $@ 2>/dev/null [DD#YL\P } j
eWI<ms ^E]y >Y dotimeout() nxhn|v { >SHP,><H/ mylogger_info "reset firewall when timeout arrives" ~%?LFR' case "$firewall" in m5
l,Lxj ipchains) ,t@B]ll /etc/init.d/ipchains restart 1>/dev/null 2>/dev/null @&9,0x if [ $? = 0 ] ; then wu~?P` mylogger_info "ipchains restarted" g]Z@_ else WPDi)UX mylogger_notice "ipchains restart failed" K;l
xPM] fi pSdtAv ;; sUz,F8G iptables) cP^c}e*;NS /etc/init.d/iptables restart 1>/dev/null 2>/dev/null qQp;i{X if [ $? = 0 ] ; then P2fiK mylogger_info "iptables restarted" J936o3F_ else EI8KKo * mylogger_notice "iptables restart failed" aSt:G*a" fi O&evv8 6L ;; X86r`} *) ~S~4pK mylogger_notice "neither ipchains nor iptables" qCy
SL lp0 ;; Kb~i9x& esac GQ6~Si2 ,m[#<}xXA } KJi8LM N)
'|l0x0 blockclient() ?/}-
&A" { !|~yf3 if [ -z "$1" ] || [ -z "$2" ]; then p9[gG\ mylogger_notice "blockclient() missing client or port to block" 4P?R "Lk return $_O;yz fi .o) local ip port D-Q54"^3 U*\1d ip=$1 F7\nG}#s port=$2 >6gduD!6I gg@Ew4L& case "$firewall" in " nLWvV1 ipchains) E=]$nE]b mylogger_notice "blocking $1 to $2 via ipchains" %afz{a5 found=`ipchains -nL | egrep "^$ipchains_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+\->[[:space:]]+$port"` KJW^pAj$B if [ -z "$found" ] ; then ;R7+6 cmd="ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null" VEx
) mylogger_debug "cmd: $cmd" Q1RUmIe_& `ipchains -I input 1 -p tcp -s $ip -d 0/0 $port -j $ipchains_block_policy 1>/dev/null 2>/dev/null` ZJV;&[$[ if [ $? != 0 ] ; then G\|P3j mylogger_notice "$cmd call failed" #YK=e&da return T1c&3 fi b&!x.+d-z new_block=1 P.3kcZ ever_block=1 EO\- J-nM else 3+8{Y mylogger_info "$ip already blocked to $port" y6&o+;I$[ fi r
TK)jxklX ;; *R8P brN iptables) R]N"P:wf@ mylogger_notice "blocking $1 to $2 via iptables" ]Uul~T found=`iptables -nL | egrep "^$iptables_block_policy.*[[:space:]]+$ip[[:space:]]+.*[[:space:]]+dpt:$port[[:space:]]+"` {i)FDdDGD if [ -z "$found" ] ; then })C}'!+] 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" A>rN.XW mylogger_debug "cmd: $cmd" G
!U
`8R `iptables -I $iptables_chain_name 1 -p tcp -m tcp -s $ip --dport $port -j $iptables_block_policy 1>/dev/null 2>/dev/null` 'h>CgR^NM1 if [ $? != 0 ] ; then Oe&gTXo mylogger_notice "$cmd call failed" b"eG8 return $|K-wN[ fi rGQY new_block=1 CPJ%<+4%b ever_block=1 bl[2VM7P else G`z=qaj mylogger_info "$ip already blocked to $port" &'c&B0j fi 1(qL),F; ;; 5)i+x- *) zZiga q" mylogger_notice "neither ipchains nor iptables" T_oL/x_; ;; R!2E`^{Wl esac B1 xlWdm } U\6Ee-1#_ +z+u=)I restartservice() 34t[]v|LD { >Slu?{l' local service O[R
if [ -z "$1" ] ; then [ip}f4K mylogger_notice "no port given to see which service to be restart" |qtZb}"| return Ns.{$'ll fi ZC9S0Z qKNHhXi case "$1" in FIpJ>E"n 80) }PzHtA,V service="httpd" DhT8Kh{ ;; #z9@x}p5g 25) ]9~6lx3/ service="postfix" VX e7b ;; P*kC>lvSv 110) )`}4rD^b service="courier-pop3d" td|O#R ;; w s7LDY&( 21) YH{FTVOt{C service="muddleftpd" .%_=(C<E ;; 4?l:.\fB: 53) K b
z|h,< service="named" y-o54e$4Cq ;; FyA0" 3306) }?[a>.]u service="mysqld" 90!67Ap`x ;; ?xtP\~ esac V)cL=4G if [ ! -z "$service" ] ; then k4YW;6<C+ /etc/init.d/$service restart 1>/dev/null 2>/dev/null qfyuq] if [ $? = 0 ] ; then B q7Qbj mylogger_notice "$service restarted" !k8j8v& else dU#}Tk mylogger_notice "$service restart failed" -h{|u{t fi n<Z1i) fi fk(h*L|sI } VZr AZV^c &#<>fT_ docheckport() w>6cc#>q { 2Z*^)ZQB mylogger_info "do check port $1" 6>F]Z)]} local port last_client count client total_count N3?hu} 7/6%92T/B if [ -z "$1" ] ; then JI|MR#_u mylogger_notice "docheckport() port not given" 2q%vd=T return ZGQz@H5 fi vXeI)vFK _#TbOfu port=$1 ^_JByBD [PL]!\NJ clientlist=`netstat -an --tcp| grep ESTABLISHED | awk "{ if ( index(\\$4,\":$port\") ) print \\$5}" | awk -F ':' '{print $1}'|sort` qBqh>Wo if [ $? != 0 ] ; then yPVK>em5 mylogger_notice "netstat call failed" a_o99lP return K1
m'20U fi ?7R&=B1g #echo $clientlist
D:Fi/JY~ # reset new_block */A ~lR| new_block=0 ,nz3S5~ count=0 Kp%:\s,lO total_count=0 NLF
6O9 last_client="" fkJElO-F for client in $clientlist [.c'22R6 do n$y@a?al #echo "client is $client" }<g-0&GLm if [ -z "$last_client" ] ; then @]X5g8h count=$((count+1)) b]@@x;v$@ total_count=$((total_count+1)) b]Z>P{ j last_client=$client p
_q
]Rt else >PoVK{&y if [ "$client" = "$last_client" ] ; then RzN9pAe count=$((count+1)) fm&pxQjg total_count=$((total_count+1)) Z$:iq else *1CZRfWI mylogger_debug "$last_client $count connections" R>dd#`r" if [ $count -ge $max_active_conn ] ; then ^=7XA894 mylogger_notice "client $last_client connection $count >= $max_active_conn" NAh^2X blockclient $last_client $port 22r$Ri_> fi _68{
{. count=1 (I\qTf
N4 total_count=$((total_count+1)) d{
*e0 last_client=$client DBk]2W|i fi `F4gal^ ^ fi #D|n6[Y'.t done .l +yK-BZ # check the last client C] >?YR4 if [ ! -z "$client" ] ; then %)p?&_ count=$((count+1))
-mY,nMDb total_count=$((total_count+1)) aYws{Vii mylogger_debug "$client $count connections" Ji7<UJ30x if [ $count -ge $max_active_conn ] ; then QI*<MF,1 mylogger_notice "client $client connection $count >= $max_active_conn" Sgq?r-Q. blockclient $client $port i:\|G^h fi +wxDK A_ fi #8@o%%Fd mylogger_info "total connections on port $port: $total_count" a<CACWsN.T B\Uocn
if [ $new_block = 1 ] ; then V2LvE.Kj restartservice $port YuFR*W;$ fi MZ"|Jn } tZan1C%p> Hp5.jor(k docheckall() {.o@XP,. { z4{:X Da # reset wakeup_time ug^esB wakeup_time=$wakeup_time_max '^%kTNn for port in $portlist /_</m?&.U& do tR(nD UHV5 docheckport $port r$W%d[pB
if [ $new_block = 1 ] ; then %jn)=;\ # set wakeup_time shorter cause we found some abuse client v"-K-AQjB wakeup_time=$wakeup_time_min P7\?WN$p fi \IZY\WU}2 done _
o.j({S } ;b!qt-;.< #p2`9o if [ -z "$firewall" ] && [ -f /etc/sysconfig/ipchains ] ; then GQ2/3kt firewall="ipchains" H{hzw&dZ<P fi _$i)bJ E
YA=f
U if [ -z "$firewall" ] && [ -f /etc/sysconfig/iptables ] ; then 8p]9A,Uq& firewall="iptables" oI-,6G} fi sO{TGk]* qHnX) if [ -z "$firewall" ] ; then #mkf2Z=t- echo "Error: This machine does not have ipchains or iptables firewall support" Q89fXi0Ivb exit 1 sE&1ZJ]7 fi -5JN`
)]kxLf# mylogger_info "firewall.sh v$myver ValueOf.com starting" ,B'fOJ.2
mylogger_info "Firewall is: $firewall" |}b~YHTs mylogger_info "Port protected: $portlist" aa"3
Io mylogger_info "Max connection per ip: $max_active_conn" pQ>|dH+. mylogger_info "Min time to check: $wakeup_time_min""s" U7Oa
13Qz mylogger_info "Max time to check: $wakeup_time_max""s" fbD,\ rjT mylogger_info "Timeout circle: $rule_timeout""s" 4AMe>s mylogger_info "Output is logged to: $log_facility" S=\cF,Zs
x#gZC1$Y # if new ip blocked at this check run? hfw+n
< new_block=0 t<o7 S:a" # if new ip blocked at this timeout run? CK1A$$gnz ever_block=0 HLOrDlj7 # reset wakeup_time "$pbK: wakeup_time=$wakeup_time_max 4}s'xMT! S`$%C=a. lasttime=`date +%s` 2vh }:A_ @<`V q while [ 1 ] N`?|~g3 do NGJst_ curtime=`date +%s` eL~3CAV{ timediff=$((curtime-lasttime)) -+Ot'^ #echo "timediff: $timediff" 8&M<?oe if [ $timediff -ge $rule_timeout ] && [ $ever_block = 1 ] ; then eh>
|m>JY lasttime=$curtime {Wi*B( ever_block=0 ptrwZ8' dotimeout ^RO<r}Bu fi `hdff0 docheckall /_
})7I52 mylogger_info "sleep for $wakeup_time""s"
H(gY= sleep $wakeup_time oyr b.lu/ done BGxwPJd K1^7v}P xT]t3'y|- g'b|[ q 1. 说明 ('7$K firewall.sh是一个shell脚本程序,每隔一段时间检查tcp连接的统计信息,如果来自某个ip对某个端口的活动连接超过规定的最大数量, w%s];EE 则自动将该IP对该端口的访问屏蔽,并重新启动相应的服务。再每隔一段时间,会重设防火墙到初始状态。 dW%t ph 该程序可以同时保护多个端口 1Acs0`3 LWm1j:0 2. 安装 2|=hF9
tar zxf firewall-1.0b.tar.gz B}[f
]8jrM cd firewall-1.0b \
a,}1FS install -m 700 firewall.sh /usr/prima/sbin/firewall.sh >
mX6;6FF cM3B5Lp 3. 配置
`YC7+`q 主要配置项目如下: @fYVlHT%E # 最小检查周期,缺省为120秒 Y
(bB7tR wakeup_time_min=120 2H}y1bkW h?p_jI # 最大检查周期,缺省为300秒 l=
p_ wakeup_time_max=600 E`I(x&_ \OtreYi # 重设防火墙状态的时间,缺省为3600秒 ,c|
MB rule_timeout=3600 J)Y`G4l2@ y;Cs#eo # 保护的端口列表,缺省为80和25,支持的其他端口包括21(ftp), 110(pop3), 53(named), 3306(mysql) Y4HN1 # 一般的网络攻击都是针对80和25,又以80居多 RW+u5Y portlist="80 25" $uDgBZA\ myR{}G # 每个ip可占用的最大活动(Established)连接数 'k[qx} max_active_conn=8 lbda/Zx _
\X ,a5Un # iptables防火墙规则链名称,必须和/etc/sysconfig/iptables中一致 k\.9iI'6 # 如果用的是ipchains,可以忽略此项 Rtz~:v% iptables_chain_name="RH-Lokkit-0-50-INPUT" < ,0D|O,Y o& FOp' # 日志输出目标 HvG %## log_facility="local0"
Pd~=:4 1*u]v{JJ( **** 关于检查周期 **** qIQ=OY=6 程序定义了两个检查周期,如果上次检查中屏蔽了某个IP,则程序会更频繁地检查连接情况,反之则等待更长时间。通过检查周期
I
U" 的动态调整,可以有效调度在遭受攻击和正常状态下程序的运行次数。 )1 T2u 'Gl~P><e **** ipchains vs iptables **** \L(cFjLIl 目前该程序支持ipchains和iptables两种软件防火墙,使用何种是由程序启动时自动检测的。如果/etc/sysconfig/ipchains和 ,~naKd.ZY /etc/sysconfig/iptables都没有检测到,则报错退出。 8xO H_$?b **** 日志输出 ****
9y*] {IY 程序的输出信息记录在系统日志中,目标是local0。如果没有特殊配置,可以在/var/log/messages中看到。建议在/etc/syslog.conf =|z:wlOs 中加入一条: 71R,R, local0.* /var/log/firewall.log ][S q^5` 然后重新启动syslog nUP, Yd /etc/init.d/syslog restart _J!^iJ 这样,可以将firewall.sh输出的日志单独记到文件/var/log/firewall.log里。 dR]-R/1| >NBc-DX^ 4. 运行 />S^`KSTM /usr/prima/sbin/firewall.sh & CK7([>2 rpRyB9 范例输出: 5U~KYy^v *** firewall.sh v1.0b ValueOf.com*** ?z9!=A%<V~ Firewall is: ipchains RhE~Rwbx Port protected: 80 25 `HRL .uX Max connection per ip: 8 rQimQ|+ Min time to check: 120s 2xLtJR4L Max time to check: 300s U9:)qvMXe Timeout circle: 3600s ki'$P.v{$w Output is logged to: local0 l)[|wPf (I}owr5: 察看/var/log/firewall.log,可以看到: <t"T'\3 Oct 16 14:08:55 server firewall.sh: do check port 80 // 检查80端口 OS,!`8cw Oct 16 14:08:55 server firewall.sh: 192.168.0.60 2 connections // 有两个来自192.168.0.60的连接 3@V?L:J Oct 16 14:08:55 server firewall.sh: total connections on port 80: 2 // 80端口总共2个连接 $yASWz Oct 16 14:08:55 server firewall.sh: do check port 25 // 检查25端口 S/`#6 Oct 16 14:08:55 server firewall.sh: total connections on port 25: 0 // 25端口没有连接 4{uQ}ea Oct 16 14:08:55 server firewall.sh: sleep for 300s // 等待300秒 C(XV
YND3 05LkLB 5. 停止 K<J,n!zc 先用ps命令察看firewall.sh进程的进程号,然后用kill命令将其终止,如 YcobK#c # ps auxww|grep firewall.sh WoZU} T- root 27932 0.0 0.5 2312 1060 pts/2 S 12:38 0:00 /bin/sh /usr/prima/sbin/firewall.sh RK!9(^Ja root 27967 0.0 0.3 1732 592 pts/2 S 12:39 0:00 grep firewall.sh kg^0%-F
第一行即firewall.sh的进程,用kill命令: |0i{z(B # kill 27932 7
XxZF43 [1] Terminated /usr/prima/sbin/firewall.sh -iR2UE@M 即将其终止
|
常州电信/网通机房,100M共享/10M独享/1000M共享/100M独享/电信+网通双线路服务器托管
Tel:0519-89991155 企业QQ:4006023839 5y6s Inc.
|
[楼 主]
|
Posted: 2008-01-26 02:12 |
| |