• 简单配置vps,防ddos攻击


    防人之心不可无。 网上总有些无聊或者有意的人。不多说了。上干货,配置vps apf防小流量ddos攻击。

    对于大流量的ddos攻击, 需要机房的硬件防火墙,vps内部可能也扛不住。

    1. 安装 DDoS deflate

    DDoS deflate的原理是通过netstat命令找出 发出过量连接的单个IP,并使用iptables防火墙将这些IP进行拒绝。由于iptables防火墙拒绝IP的连接远比从Apache层面上来得高效,因此iptables便成了运行在Apache前端的“过滤器”。同样的,DDoS deflate也可以设置采用APF(高级防火墙)进行IP阻止。

    wget http://www.inetbase.com/scripts/ddos/install.sh
    chmod +x install.sh
    ./install.sh
    主要功能与配置

    1、可以设置IP白名单,在 /usr/local/ddos/ignore.ip.list 中设置即可;

    2、主要配置文件位于 /usr/local/ddos/ddos.conf ,打开此文件,根据提示进行简单的编辑即可;

    3、DDoS deflate可以在阻止某一IP后,隔一段预置的时候自动对其解封;

    4、可以在配置文件中设置多长时间检查一次网络连接情况;

    5、当阻止IP后,可以设置Email提醒

    简单配置一下:

    FREQ=1 #检测的频率为1分钟 
    NO_OF_CONNECTIONS=100 #当单个IP超过100个连接请求时判定为DDOS
    
    APF_BAN=1 #如果打算使用APF阻止IP,则设置为1(需要预先安装APF);如果使用iptables,则设置为0; 
    
    KILL=1 #是否阻止 
    
    EMAIL_TO="webmaster@firstVM.com" #接收邮件 
    
    BAN_PERIOD=600 #阻止时长,10分钟

    2. 安装配置apf。

    APF(Advanced Policy Firewall)是 Rf-x Networks 出品的Linux环境下的软件防火墙,被大部分Linux服务器管理员所采用,使用iptables的规则,易于理解及使用。

    适合对iptables不是很熟悉的人使用,因为它的安装配置比较简单,但是功能还是非常强大的。

    脚本安装:

    root@linux:/home/zhangy# wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz  
    root@linux:/home/zhangy# tar -xvzf apf-current.tar.gz  
    root@linux:/home/zhangy# cd apf-9.7-1  
    root@linux:/home/zhangy/apf-9.7-1# ./install.sh  

    ubuntu 可以快速安装:

    sudo aptitude install apf-firewall

    配置:

    vi /etc/apf/conf.apf

    往后翻页,找到:

    # Configure inbound (ingress) accepted services. This is an optional
    # feature; services and customized entries may be made directly to an ip's
    # virtual net file located in the vnet/ directory. Format is comma separated
    # and underscore separator for ranges.
    #
    # Example:
    # IG_TCP_CPORTS="21,22,25,53,80,443,110,143,6000_7000"
    # IG_UDP_CPORTS="20,21,53,123"
    # IG_ICMP_TYPES="3,5,11,0,30,8"
    
    # Common inbound (ingress) TCP ports
    IG_TCP_CPORTS="22"

    默认只有22端口开放。 我们先不管。 访问以下80端口的网站试试。 发现竟然可以访问。 为什么规则没有起作用。

    继续查看配置文件。 找啊找。

    这一行引起了我的注意:

    # Untrusted Network interface(s); all traffic on defined interface will be
    # subject to all firewall rules. This should be your internet exposed
    # interfaces. Only one interface is accepted for each value.
    IFACE_IN="eth0"

    突然想到, 会不会是监听端口的问题。

    我们知道, 如果是真实服务器或者是 xen虚拟化的vps, 其网卡是 eth*。  例如:

    ifconfig

    image

    但是, 我这台vps是openvz虚拟化的。 它的网卡一般是 vnet* 的。 比如:

    image

    于是改上面的配置文件:

    # Untrusted Network interface(s); all traffic on defined interface will be
    # subject to all firewall rules. This should be your internet exposed
    # interfaces. Only one interface is accepted for each value.
    IFACE_IN="venet0"

    重启 apf:

    apf -r

    提示说找不到 ip_tables 模块。

    ip_tables

    apf(4677): {glob} unable to load iptables module (ip_tables), aborting.

    image

    于是搜索:  ubuntu  apf  找到这篇文章

    http://davidwinter.me/articles/2011/06/05/install-apf-on-ubuntu-11-04/

    大致意思是说, 在ubuntu中, iptables默认被编译进了内核, 而不是以模块方式运行的。apf默认是使用模块方式调用iptables。 所以要修改apf的配置:

    SET_MONOKERN="1"

    然后重启 apf

    然后看到一长串的日志:

    image

    看样子是成功了。

    试一下, 果然80端口不能访问了。

    只有22还在。

    回到配置文件, 我们把端口开放:

    vi /etc/apf/conf.apf

    找到:

    # Configure inbound (ingress) accepted services. This is an optional
    # feature; services and customized entries may be made directly to an ip's
    # virtual net file located in the vnet/ directory. Format is comma separated
    # and underscore separator for ranges.
    #
    # Example:
    # IG_TCP_CPORTS="21,22,25,53,80,443,110,143,6000_7000"
    # IG_UDP_CPORTS="20,21,53,123"
    # IG_ICMP_TYPES="3,5,11,0,30,8"
    
    # Common inbound (ingress) TCP ports
    IG_TCP_CPORTS="22,80,443"

    保存,重启:  apf –r

    再访问以下,成功了。

    最后, 关闭apf的调试模式,正式上线:

    找到:

    # !!! Do not leave set to (1) !!!
    # When set to enabled; 5 minute cronjob is set to stop the firewall. Set
    # this off (0) when firewall is determined to be operating as desired.
    DEVEL_MODE="1"

    改成 0:

    # !!! Do not leave set to (1) !!!
    # When set to enabled; 5 minute cronjob is set to stop the firewall. Set
    # this off (0) when firewall is determined to be operating as desired.
    DEVEL_MODE="0"

    欢迎大家访问我的个人独立博客: http://blog.byneil.com

  • 相关阅读:
    SQL注入详解
    Nginx跨域及Https配置
    GET请求和POST请求的request和response的中文乱码问题
    创建Maven工程
    Maven环境变量配置
    Cookie&Session会话技术
    Maven库站
    20191002思维导图工具MindManager 000 033
    20190930-02 Redis持久化方式一:RDB及修改RDB的默认持久化策略 000 032
    Tomcat配置HTTPS方式生成安全证书
  • 原文地址:https://www.cnblogs.com/shuidao/p/3538702.html
Copyright © 2020-2023  润新知