python寫一個防護DDos的腳本(請安好環境不然沒法實驗)

原由:python

竟然有ddos腳本,怎麼能夠沒防護ddos的腳本!git

開始:github

1.請執行 install.py安裝好DDos-defalte,會在root目錄下多出這個文件夾網絡

代碼:.net

2.而後執行fyddos.pyorm

第一個讓你設置IP白名單blog

第二個讓你設置一個ip超過100個鏈接數,自動封掉 crontab

第三個封ip,自動發送郵件,設置好你的郵件ip

默認封600秒rem

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

相關文章
相關標籤/搜索