在开发直播系统源码时,怎样才能防止ddos攻击

实验环境:
如果 1 分钟内,一个 IP 地址对我们服务器访问 xx次以上,就认为发生 DDOS,使用
iptables 把这个 IP 地址自动屏蔽掉。

检测是否有 DDOS 攻击
netstat -ntu|awk ‘{print $5}’|cut -d: -f1|sort |uniq -c|sort -rn|grep -vE ‘192.168|127.0’
如果发现某个 IP 连接数据上百的链接,说明就有 DDOS 攻击,可以使用ddos deflate来防止

下面开始安装 DDos deflate
wget http://www.inetbase.com/scripts/ddos/install.sh

chmod 700 install.sh(给权限)

./install.sh

Installing DOS-Deflate 0.6
Downloading source files.........done
Creating cron to run script every minute.....(Default setting).....done
Installation has completed.
Config file is at /usr/local/ddos/ddos.conf
Please send in your comments and/or suggestions to [email protected]
###################################################
###############################################
# "Artistic License" #
# #
# Preamble #
# #
# The intent of this document is to state the conditions under which a #
# Package may be copied, such that the Copyright Holder maintains some #
q 输入 q 退出


修改配置文件

vim /usr/local/ddos/ddos.conf
配置文件中的注释如下:
##### 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 即可。
改:19 APF_BAN=1
为:19 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 protected] #当 IP 被屏蔽时给指定邮箱发送邮件报警,换成自己的邮箱即可
##### Number of seconds the banned ip should remain in blacklist.
BAN_PERIOD=600 #禁用 IP 时间,默认 600 秒,可根据情况调整

用户可根据给默认配置文件加上的注释提示内容,修改配置文件。
注:安装后,不需要手动运行任何软件,因为有 crontab 计划任务,每过一分钟,会行自动执行一次。检查是否有不正常的访问量

下面模拟下ddos
ab -n 1000 -c 10 http://公网IP/index.html
查看结果,发现已成功拦截

iptables -L –n

Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all --   xxxx  0.0.0.0/0   (xxxx代表所被禁止的IP)

以上就是在开发直播系统源码时,防止ddos攻击的大概流程,感兴趣可继续关注,后续会不断更新。
声明:本文由作者原创,转载请注明出处及原文链接。

发布了150 篇原创文章 · 获赞 65 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/yb1314111/article/details/103999994
今日推荐