zoukankan      html  css  js  c++  java
  • Linux下防御ddos攻击

    导读 Linux服务器在运营过程中可能会受到黑客攻击,常见的攻击方式有SYN,DDOS等。通过更换IP,查找被攻击的站点可能避开攻击,但是中断服务的时间比较长。比较彻底的解决方法是添置硬件防火墙。不过,硬件防火墙价格比较昂贵。可以考虑利用Linux 系统本身提供的防火墙功能来防御。

    SYN攻击是利用TCP/IP协议3次握手的原理,发送大量的建立连接的网络包,但不实际建立连接,最终导致被攻击服务器的网络队列被占满,无法被正常用户访问。 Linux内核提供了若干SYN相关的配置,加大SYN队列长度可以容纳更多等待连接的网络连接数,打开SYN Cookie功能可以阻止部分 SYN攻击,降低重试次数也有一定效果。而DDOS则是通过使网络过载来干扰甚至阻断正常的网络通讯。通过向服务器提交大量请求,使服务器超负荷。阻断某一用户访问服务器阻断某服务与特定系统或个人的通讯。可以通过配置防火墙或者使用脚本工具来防范.

    一、通过sysctl和iptables来防范

    对sysctl参数进行修改

    $ sudo sysctl -a  | grep ipv4 | grep syn

    输出类似下面:

    net.ipv4.tcp_max_syn_backlog = 1024

    net.ipv4.tcp_syncookies = 0

    net.ipv4.tcp_synack_retries = 5

    net.ipv4.tcp_syn_retries = 5

    net.ipv4.tcp_syncookies:是否打开SYN COOKIES的功能,“1”为打开,“2”关闭。

    net.ipv4.tcp_max_syn_backlog:SYN队列的长度,加大队列长度可以容纳更多等待连接的网络连接数。

    net.ipv4.tcp_synack_retries和net.ipv4.tcp_syn_retries:定义SYN重试次数。

    把如下加入到/etc/sysctl.conf即可,之后执行“sysctl -p”!

    net.ipv4.tcp_syncookies = 1

    net.ipv4.tcp_max_syn_backlog = 4096

    net.ipv4.tcp_synack_retries = 2

    net.ipv4.tcp_syn_retries = 2

    提高TCP连接能力

    net.ipv4.tcp_rmem = 32768

    net.ipv4.tcp_wmem = 32768

    net.ipv4.sack=0

    使用iptables命令:

    # netstat -an | grep ":80" | grep ESTABLISHED

    来查看哪些IP可疑~比如:221.238.196.83这个ip连接较多,并很可疑,并不希望它再次与221.238.196.81有连接。可使用命令:

    iptables -A INPUT -s 221.238.196.83 -p tcp -j DROP

    将来自221.238.196.83的包丢弃。
    对于伪造源IP地址的SYN FLOOD攻击。该方法无效
    其他参考
    防止同步包洪水(Sync Flood)

    # iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

    也有人写作

    # iptables -A INPUT -p tcp --syn -m limit --limit 1/s -j ACCEPT

    --limit 1/s 限制syn并发数每秒1次,可以根据自己的需要修改防止各种端口扫描

    # iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT

    Ping洪水攻击(Ping of Death)

    # iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

    二、用DDoS deflate自动屏蔽攻击

    DDoS deflate是一款免费的用来防御和减轻DDoS攻击的脚本。它通过netstat监测跟踪创建大量网络连接的IP地址,
    在检测到某个结点超过预设的限 制时,该程序会通过APF或IPTABLES禁止或阻挡这些IP.
    DDoS deflate官方网站:http://deflate.medialayer.com/
    如何确认是否受到DDOS攻击?
    执行:

    netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

    以下是用VPS测试的结果:

    li88-99:~# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

    1 114.226.9.132

    1 174.129.237.157

    1 58.60.118.142

    1 Address

    1 servers)

    2 118.26.131.78

    3 123.125.1.202

    3 220.248.43.119

    4 117.36.231.253

    4 119.162.46.124

    6 219.140.232.128

    8 220.181.61.31    VPS侦探 http://www.vpser.net/

    2311 67.215.242.196       #(这个看起来像攻击)

    每个IP几个、十几个或几十个连接数都还算比较正常,如果像上面成百上千肯定就不正常了。

    1、安装DDoS deflate

    http://www.inetbase.com/scripts/ddos/install.sh   //下载DDoS deflate

    chmod 0700 install.sh      //添加权限

    ./install.sh         //执行

    2、配置DDoS deflate

    下面是DDoS deflate的默认配置位于/usr/local/ddos/ddos.conf ,内容如下:

    ##### Paths of the script and other files
    PROGDIR="/usr/local/ddos"
    PROG="/usr/local/ddos/ddos.sh"
    IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"  //IP地址白名单
    CRON="/etc/cron.d/ddos.cron"    //定时执行程序
    APF="/etc/apf/apf"
    IPT="/sbin/iptables"
    ##### frequency in minutes for running the script
    ##### Caution: Every time this setting is changed, run the script with --cron
    #####            option so that the new frequency takes effect
    FREQ=1   //检查时间间隔,默认1分钟
    ##### How many connections define a bad IP? Indicate that below.
    NO_OF_CONNECTIONS=150     //最大连接数,超过这个数IP就会被屏蔽,一般默认即可
    ##### APF_BAN=1 (Make sure your APF version is atleast 0.96)
    ##### APF_BAN=0 (Uses iptables for banning ips instead of APF)
    APF_BAN=1        //使用APF还是iptables。推荐使用iptables,将APF_BAN的值改为0即可。
    ##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
    ##### KILL=1 (Recommended setting)
    KILL=1   //是否屏蔽IP,默认即可
    ##### An email is sent to the following address when an IP is banned.
    ##### Blank would suppress sending of mails
    EMAIL_TO="root"   //当IP被屏蔽时给指定邮箱发送邮件,推荐使用,换成自己的邮箱即可
    ##### Number of seconds the banned ip should remain in blacklist.
    BAN_PERIOD=600    //禁用IP时间,默认600秒,可根据情况调整

    3、选项

    /usr/local/ddos/ddos.sh -h    //查看选项

    /usr/local/ddos/ddos.sh -k n  //杀掉连接数大于n的连接。n默认为配置文件的NO_OF_CONNECTIONS

    /usr/local/ddos/ddos.sh -c    //按照配置文件创建一个执行计划

    4、卸载

    http://www.inetbase.com/scripts/ddos/uninstall.ddos

    chmod 0700 uninstall.ddos

    ./uninstall.ddos

    使用以上方法可以缓解一些攻击。

    如果服务器是nginx的话可以按照

    1、打开nginx访问日志

    log_format    access    '$remote_addr - $remote_user [$time_local] "$request" '

    '$status $body_bytes_sent "$http_referer" '

    '"$http_user_agent" $http_x_forwarded_for';  #设置日志格式

    #access_log  /dev/null;

    access_log    /usr/local/nginx/logs/access.log    access;

    2、观察nginx日志

    more /usr/local/nginx/logs/access.log

    查看哪些ip重复很严重。
    (1)agent的特征
    比如:MSIE 5.01
    配置nginx

    location /{

    if ( $http_user_agent ~* "MSIE 5.01" ) {

    #proxy_pass http://www.google.com;
    return 500
    #access_log /home/logs/1.log main;
    }
    }

    将ip加入iptable内

    iptables -A INPUT    -s 202.195.62.113 -j DROP

    如何查看user_agent
        在地址栏输入:

    javascript:alert(navigator.userAgent)

    回车就会弹出当前使用的浏览器的useragent.
    ps:ie和firefox的user—agent

    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"  ( ie6.0)

    Mozilla/4.0 (compatible; MSIE 8.0; ; Trident/4.0; .NET CLR 2.0.50727; CIBA)  (ie8.0)

    Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12

    注意:都含有Windows NT 5.1。

    "Mozilla/4.0 (compatible; MSIE 6.0; Windows 5.1)"(这个应该就是攻击者使用user_agent)

     

    《Linux就该这么学》是一本基于最新Linux系统编写,面向零基础读者的技术书籍。从Linux基础知识讲起,然后渐进式地提高内容难度,详细讲解Linux系统中各种服务的工作原理和配置方式,以匹配真实生产环境对运维人员的要求,突显内容的实用性。想要学习Linux系统的读者可以点击"阅读原文"按钮了解这本书,同时这本书也适合专业的运维人员阅读,作为一本非常有参考价值的工具书!

  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/8599637.html
Copyright © 2011-2022 走看看