Python a simple anti-attack script

Source: Internet
Author: User

Learn python, write a simple prevention attack script, feel bad, Mark to stay after the improvement.

#!/bin/env python#-*- coding:utf-8 -*-import sqlite3import commandsimport  Timeimport logginglog_file= '/var/log/ddoskill.log ' logging.basicconfig (level=logging.info,format= '% ( asctime) s % (filename) s[line:% (Lineno) d]% (levelname) s % (message) s ', datefmt= '%a,%d %b %y  %h:%m:%s ', filename=log_file,filemode= ' a ') exclude_list = [' 192.168.1.56 ', ' 192.168.1.200 ', ' 192.168.1.100 ', ' 192.168.1.300 ', ' 127.0.0.1 ']cx = sqlite3.connect ('/tmp/ddoskill.db ') #查看系统防火墙是否开启 ( Status_4,output_4)  = commands.getstatusoutput ("Service iptables status") if status_4  != 0:    logging.error ("iptables is closed!")     exit (#取出数据库中已有IP存入ip列表中ip_list)  = []out_list =  cx.execute ("Select ip from ddos"). Fetchall () I = 0while i < len (out_list):     ip_list.append (str (out_list[i][0))     i+=1# the IP that is too large and does not exist in the database is forbidden command_1= "netstat -n|awk  '/^tcp/{print $5} ' |cut  -d: -f1|sort|uniq -c "Output_1 = commands.getoutput (command_1) Length = len ( Output_1.split (' \ n ')) x = 0while x < length:    num =  output_1.split (' \ n ') [X].split () [0]    ip = str (' Output_1.split (' \ n ') [x]. Split () [1])     if int (num)  >= 100 and ip not in  ip_list and IP not in exclude_list:         logging.warning ("Write the  %s  into the database and disable access in Iptable!") &NBSP;%&NBSP;IP)         command_3 =  "iptables -i  input -s  "+ip+"  -j drop "        output_3 =  commands.getoutput (command_3)         cX.execute ("Insert into ddos (IP)  values (?)", (IP,))     x+=1# Delete data from the list 5 hours ago, and also delete iptable corresponding entry for ip  in ip_list:    select_com = "select  time from ddos where ip= '%s ' " % ip    otime = str ( Cx.execute (select_com). Fetchone () [0])     intv = time.time ()  -  Time.mktime (Time.strptime (otime, '%y-%m-%d %h:%m:%s '))     if intv/60/60 >  5:        logging.warning ("Remove from Iptables and database:%s" &NBSP;%&NBSP;IP)         command_2 =  "Iptables -d input -s   "+ip+"  -j drop "        output_2 =  Commands.getoutput (command_2)         delete_com =  "Delete  from ddos where ip='%s ' " % ip        cx.execute (delete_com) cx.commit () Cx.close ( )


Python a simple anti-attack script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.