请稍候...
  • 企业级 VPS主机
  • Windows VPS 主机
  • Unmanaged VPS 主机
  • 通配符证书,部署全网SSL证书必备神器
  • 高速稳定独立主机High quality stable server

CSF防火牆的安装与配置

发布时间:2011-07-25 22:07:20 来源: 亚狐科技

偶尔有访客打不开网站。很多时候并不是服务器宕机,而是ip 被防火墙屏蔽。

安装CSF 防火墙通常是我们在服务器上所做的第二件事。防火墙的主要目的是为了帮助完成如下任务:
1 防止暴力破解密码,自动屏蔽连续登陆失败的IP
2 管理网络端口,只开放必要的端口
3 免疫小流量的 DDos 和 CC 攻击。

CSF是一个功能完善的防火墙,并且提供 cPanel 插件,便于管理。当然除了 Web GUI,CSF 也提供基于 CLI 的管理方式。下面介绍下如何在 CentOs 系统安装 CSF:

一、安装依赖包:
yum install perl-libwww-perl perl iptables

二、下载并安装 CSF:
wget http://www.configserver.com/free/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh

三、测试 CSF 是否能正常工作:
perl /etc/csf/csftest.pl

四、裝好了CSF,然後就要修正配置檔開啟/關閉一些埠。

cd /etc/csf
sed -i 's/^TESTING =.*/TESTING = "0"/' csf.conf
sed -i 's/^TCP_IN =.*/TCP_IN = "21,22,9091,51413,30000:35000"/' csf.conf
sed -i 's/^TCP_OUT =.*/TCP_OUT = "1:65535"/' csf.conf
sed -i 's/^UDP_IN =.*/UDP_IN = "20,21,51413"/' csf.conf
service csf restart####### 遭遇CC攻擊解決方法 #######

 

五、如果你使用的LNMP环境,安裝 CSF后,可以选用这个脚本

#!/bin/bash
 
#Collecting list of ip addresses connected to port 80
 
netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1 > /root/iplist
 
#Limit the no of connections
LIMIT=15;
 
for ip in `cat /root/iplist |awk '{print $2}'`;do
 
        if [ `grep $ip /root/iplist | awk '{print $1}'` -gt $LIMIT ]
        then
                echo "15 connection from $ip... `grep $ip /root/iplist | awk '{print $1}'` number of connections... Blocking $ip";
 
                #Blocking the ip ...
 
                CHECK_IF_LOCALIP=0;
                /sbin/ifconfig | grep $ip > /dev/null;
                if [ $? -ne $CHECK_IF_LOCALIP ]
                then {
                        FLAG=0;
                        grep $ip /etc/csf/csf.deny > /dev/null;
                        if [ $? -ne $FLAG ]
                        then
                                iptables -I INPUT -s $ip -j DROP;
                                echo "deny $ip;" >> /usr/local/nginx/conf/vhost/block.conf;
                                /usr/sbin/csf -d $ip;
                                ~/lnmp reload;
                        else
                                echo " Ipaddress $ip is already blocked ";
                        fi
                }
                else
                        echo " Sorry, the ip $ip cannot be blocked since this is a local ip of the server ";
                fi
        fi
done

上一页1下一页
【责任编辑:亚狐科技 (Top) 返回页面顶端