天天看点

Linux下防止ddos攻击(原创)

前言 

虚拟主机服务商在运营过程中可能会受到黑客攻击,常见的攻击方式有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自动屏蔽攻击ip 

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

wget 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、卸载 

wget 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

我发现的 攻击时代理+ddos

搜索ip均为代理地址。日志最后一部分为真实ip(重复很多)。 

如何查看user_agent

在地址栏输入:

回车就会弹出当前使用的浏览器的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). 

参考至:http://hi.baidu.com/niupeiyuan/blog/item/685ca931d1d526b85fdf0e5b.html

           http://baike.baidu.com/view/23271.htm

           http://wenku.baidu.com/view/8d605b1d650e52ea55189821.html

           http://www.hackbase.com/tech/2011-07-12/64479.html

           http://sudone.com/nginx/nginx_ddos.html

           http://www.selfcai.com.cn/?cat=23

本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:[email protected]

作者:czmmiao  文章出处:http://czmmiao.iteye.com/blog/1616837

继续阅读