python写一个防御DDos的脚本(请安好环境否则无法实验)

起因:

居然有ddos脚本,怎么可以没防御ddos的脚本!

开始:

1.请执行 install.py安装好DDos-defalte,会在root目录下多出这个文件夹

python写一个防御DDos的脚本(请安好环境否则无法实验)

代码:

python写一个防御DDos的脚本(请安好环境否则无法实验)

2.然后执行fyddos.py

python写一个防御DDos的脚本(请安好环境否则无法实验)

第一个让你设置IP白名单

第二个让你设置一个ip超过100个连接数,自动封掉

第三个封ip,自动发送邮件,设置好你的邮件

默认封600秒

4.然后输入这条命令

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

  就可以查询谁DDos你了

DDos-defalte
DDOS defalte是一款免费的用来防御和减轻DDoS攻击。它通过netstat监测跟踪创建大量网络连接的IP地址,在检测到某个结点超过预设的限制时,该程序会通过APF或iptables禁止或阻挡这些IP. #该版对原版进行了错误修复,而且进行了本地化不需要安装的时候下载文件。 #安装步骤 下载 ddos-defalte.tar.gz
tar zxfv ddos-defalte.tar.gz
cd ddos-defalte
./install.sh
安装完毕。
配置文件位于 /usr/local/ddos/ddos.conf
ip白名单文件位于 /usr/local/ddos/ignore.ip.list ,格式:一行一个ip
配置完毕之后执行命令 ddos 可以看到一行行的输出,每行:第一个是连接数 第二个是对应ip
/usr/local/sbin/ddos命令是脚本/usr/local/ddos/ddos.sh的一个软链接
安装完毕。 然后只需要计划任务周执 ddos 命令即可。ddos每执行一次都会检测配置里面的设置监测系统网络连接情况封ip。
比如root帐号下,执crontab -e 可以这样执行计划任务 */1 * * * * /usr/local/ddos/ddos.sh >/dev/null 2>&1
命令ddos有一些带参数的用法,可以通过ddos -h 了解用法。
配置文件/usr/local/ddos/ddos.conf的一些简要说明: PROGDIR="/usr/local/ddos"
PROG="/usr/local/ddos/ddos.sh"
IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list" #ip的白名单
APF="/etc/apf/apf"
IPT="/sbin/iptables" ##### How many connections define a bad IP? Indicate that below.
NO_OF_CONNECTIONS=100 #一个ip超过100个连接数,自动封掉 ##### 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=0 #这里我使用iptables封ip ##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
##### KILL=1 (Recommended setting)
KILL=1 ##### An email is sent to the following address when an IP is banned.
##### Blank would suppress sending of mails ,设置为 root 则不发邮件
EMAIL_TO="root" #封ip,自动发送邮件 ##### Number of seconds the banned ip should remain in blacklist.
BAN_PERIOD=600

  所有代码:

import os
import time
def fyddos():
IGNORE_IP_LIST = "/usr/local/ddos/ignore.ip.list"
PGOPDIR="/usr/local/ddos/ddos.conf"
PROG = "/usr/local/ddos/ddos.sh"
if os.path.exists(PROG):
print '[+]The execution file exists to continue the program.'
else:
print '[-]The execution file does not exist, the closing procedure.'
exit()
if os.path.exists(PGOPDIR):
print '[+]The configuration file exists to start the execution of the program.'
else:
print '[-]There is no exit procedure for the configuration file.'
exit()
if os.path.exists(IGNORE_IP_LIST):
print '[+]The IP white list file exists to continue the program.'
else:
print '[-]IP white list file does not exist, end the program.'
exit() time.sleep(1)
print '[+]Start configuring the IP white list.'
IPlist=raw_input('Please enter your white list IP, and if not, enter q to enter the next step:')
if IPlist=='q':
try:
import dy1
except Exception , g:
print '[-]Catch the error cause:',g
print '[+]It has been added to it.'
time.sleep(3)
try:
import dy2
except Exception , r:
print '[-]Catch the error cause:',r
time.sleep(3)
try:
import dy3
except Exception , p:
print '[-]Catch the error cause:',p
print '[+]Query information,Please input: netstat -ntu | awk ''{print $5}'' | cut -d: -f1 | sort | uniq-c | sort -n command view.'
isd=os.system('ddos')
sda=os.system('service iptables status')
print isd
print sda else:
list=open('/usr/local/ddos/ignore.ip.list','w')
list.write(IPlist+"\n")
print '[+]It has been added to it:',IPlist
time.sleep(3)
try:
import dy1
except Exception , w:
print('[-]Catch the error cause:',w)
time.sleep(3)
try:
import dy2
except Exception , d:
print '[-]Catch the error cause:',d
time.sleep(3)
try:
import dy3
except Exception ,z:
print '[-]Catch the error cause:',z
print '[+]Query information,Please input: netstat -ntu | awk ''{print $5}'' | cut -d: -f1 | sort | uniq-c | sort -n command view.'
isds = os.system('ddos')
sdas= os.system('service iptables status')
print isds
print sdas fyddos()

  

conf=input('Please enter an IP number more than how many connections will be blocked:')
data = ''
with open('/usr/local/ddos/ddos.conf', 'r+') as f:
for line in f.readlines():
if (line.find('NO_OF_CONNECTIONS') == 0):
line = 'NO_OF_CONNECTIONS={}'.format(conf) + '\n'
data += line with open('/usr/local/ddos/ddos.conf', 'r+') as f:
f.writelines(data)

  

lv=raw_input('Use APF or iptables to seal IP. It is recommended to use iptables to change the value of APF_BAN to 0:')
data=''
with open('/usr/local/ddos/ddos.conf','r+') as f:
for line in f.readlines():
if(line.find('APF_BAN')==0):
line='APF_BAN={}'.format(lv)+'\n'
data+=line with open('/usr/local/ddos/ddos.conf','r+') as f:
f.writelines(data)

  原理:通过DDos-defalte的设置来帮助我们防御DDos(如果愿意请自行加设置代码)

一篇有关DDos-defalte的文章:http://blog.csdn.net/fly2749/article/details/51354696

ddos-defalte的Github下载地址:https://github.com/snail007/ddos-defalte

我的Github:https://github.com/422926799/python

上一篇:将图片的二进制字节字符串在HTML页面以图片形式输出


下一篇:HTML页面中显示HTML标签

</a></p></div></article></div><section id="related-posts" class="widget-box"><h3>相关文章</h3><div class="widget-content"><ul><li class="other-news"><span>08-18</span><a href="/manong/747701.html" title="python写一个防御DDos的脚本(请安好环境否则无法实验)"><i class="icon-angle-right"></i>python写一个防御DDos的脚本(请安好环境否则无法实验)</a></li></ul><div class="clear"></div></div></section></div><aside class="span4 sidebar-right hide-sidebar"><div id="posts-list-widget-9" class="widget-box widget widget-posts"><div class="widget-title"><span class="icon"><i class="icon-list"></i></span><h3>编程语言最新文章</h3></div><div class="widget-content"><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/179022.html" title="印象最深的一个bug——使用uinapp做混合开发静态图片在安卓端不显示">印象最深的一个bug——使用uinapp做混合开发静态图片在安卓端不显示</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/403861.html" title="工作当中我所解决的一个线上问题">工作当中我所解决的一个线上问题</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/585078.html" title="干货分享:蚂蚁金服前端框架和工程化实践">干货分享:蚂蚁金服前端框架和工程化实践</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/39833.html" title="技术创业者如何绘制战略“一张图”">技术创业者如何绘制战略“一张图”</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/139119.html" title="【阿里Java技术进阶】官方钉群直播大全(持续更新)">【阿里Java技术进阶】官方钉群直播大全(持续更新)</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/172.html" title="成为*博主的秘诀是什么?《乘风者周刊》专访“处女座程序猿”牛亚运">成为*博主的秘诀是什么?《乘风者周刊》专访“处女座程序猿”牛亚运</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/270262.html" title="十一放假干什么 - 看书啊。 Java 书籍推荐">十一放假干什么 - 看书啊。 Java 书籍推荐</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/53968.html" title="(更新完结)阿里珍贵技术资料免费下载">(更新完结)阿里珍贵技术资料免费下载</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/522935.html" title="如何让用户回放视频更清晰?——对话在线音乐教育平台Finger CTO林荣坤">如何让用户回放视频更清晰?——对话在线音乐教育平台Finger CTO林荣坤</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/497880.html" title="Docker 镜像加速器">Docker 镜像加速器</a></div></li><div class="clear"></div></div></div></aside></div></div></div><footer id="footer" class="row-fluid" role="contentinfo" itemscope=""><div class="span12 footer-nav"><ul><li><a href="/zuixin/">更多编程技术文章</a></li><li><a href="/sitemap.xml">网站地图</a></li></ul></div><div class="span12 footer-info"><p>本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮告之,本站会尽快删除。<span class="email-span"></span></p><p><a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备2022053706号</a></p></div></footer><div class="returnTop" title="" style="display: none;"><span class="s"></span><span class="b"></span>" </div><script type="text/javascript" src="/statics/553668/js/all.js"></script></body></html>