Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
derv82
GitHub Repository: derv82/wifite2
Path: blob/master/wifite/tools/aircrack.py
412 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from .dependency import Dependency
5
from ..util.process import Process
6
from ..util.input import xrange
7
from ..config import Configuration
8
9
import os
10
import re
11
12
class Aircrack(Dependency):
13
dependency_required = True
14
dependency_name = 'aircrack-ng'
15
dependency_url = 'https://www.aircrack-ng.org/install.html'
16
17
def __init__(self, ivs_file=None):
18
19
self.cracked_file = os.path.abspath(
20
os.path.join(
21
Configuration.temp(), 'wepkey.txt'))
22
23
# Delete previous cracked files
24
if os.path.exists(self.cracked_file):
25
os.remove(self.cracked_file)
26
27
command = [
28
'aircrack-ng',
29
'-a', '1',
30
'-l', self.cracked_file,
31
]
32
if type(ivs_file) is str:
33
ivs_file = [ivs_file]
34
35
command.extend(ivs_file)
36
37
self.pid = Process(command, devnull=True)
38
39
40
def is_running(self):
41
return self.pid.poll() is None
42
43
def is_cracked(self):
44
return os.path.exists(self.cracked_file)
45
46
def stop(self):
47
''' Stops aircrack process '''
48
if self.pid.poll() is None:
49
self.pid.interrupt()
50
51
def get_key_hex_ascii(self):
52
if not self.is_cracked():
53
raise Exception('Cracked file not found')
54
55
with open(self.cracked_file, 'r') as fid:
56
hex_raw = fid.read()
57
58
return self._hex_and_ascii_key(hex_raw)
59
60
@staticmethod
61
def _hex_and_ascii_key(hex_raw):
62
hex_chars = []
63
ascii_key = ''
64
for index in xrange(0, len(hex_raw), 2):
65
byt = hex_raw[index:index+2]
66
hex_chars.append(byt)
67
byt_int = int(byt, 16)
68
if byt_int < 32 or byt_int > 127 or ascii_key is None:
69
ascii_key = None # Not printable
70
else:
71
ascii_key += chr(byt_int)
72
73
hex_key = ':'.join(hex_chars)
74
75
return (hex_key, ascii_key)
76
77
def __del__(self):
78
if os.path.exists(self.cracked_file):
79
os.remove(self.cracked_file)
80
81
82
@staticmethod
83
def crack_handshake(handshake, show_command=False):
84
from ..util.color import Color
85
from ..util.timer import Timer
86
'''Tries to crack a handshake. Returns WPA key if found, otherwise None.'''
87
88
key_file = Configuration.temp('wpakey.txt')
89
command = [
90
'aircrack-ng',
91
'-a', '2',
92
'-w', Configuration.wordlist,
93
'--bssid', handshake.bssid,
94
'-l', key_file,
95
handshake.capfile
96
]
97
if show_command:
98
Color.pl('{+} {D}Running: {W}{P}%s{W}' % ' '.join(command))
99
crack_proc = Process(command)
100
101
# Report progress of cracking
102
aircrack_nums_re = re.compile(r'(\d+)/(\d+) keys tested.*\(([\d.]+)\s+k/s')
103
aircrack_key_re = re.compile(r'Current passphrase:\s*([^\s].*[^\s])\s*$')
104
num_tried = num_total = 0
105
percent = num_kps = 0.0
106
eta_str = 'unknown'
107
current_key = ''
108
while crack_proc.poll() is None:
109
line = crack_proc.pid.stdout.readline()
110
match_nums = aircrack_nums_re.search(line.decode('utf-8'))
111
match_keys = aircrack_key_re.search(line.decode('utf-8'))
112
if match_nums:
113
num_tried = int(match_nums.group(1))
114
num_total = int(match_nums.group(2))
115
num_kps = float(match_nums.group(3))
116
eta_seconds = (num_total - num_tried) / num_kps
117
eta_str = Timer.secs_to_str(eta_seconds)
118
percent = 100.0 * float(num_tried) / float(num_total)
119
elif match_keys:
120
current_key = match_keys.group(1)
121
else:
122
continue
123
124
status = '\r{+} {C}Cracking WPA Handshake: %0.2f%%{W}' % percent
125
status += ' ETA: {C}%s{W}' % eta_str
126
status += ' @ {C}%0.1fkps{W}' % num_kps
127
#status += ' ({C}%d{W}/{C}%d{W} keys)' % (num_tried, num_total)
128
status += ' (current key: {C}%s{W})' % current_key
129
Color.clear_entire_line()
130
Color.p(status)
131
132
Color.pl('')
133
134
# Check crack result
135
if os.path.exists(key_file):
136
with open(key_file, 'r') as fid:
137
key = fid.read().strip()
138
os.remove(key_file)
139
140
return key
141
else:
142
return None
143
144
145
if __name__ == '__main__':
146
(hexkey, asciikey) = Aircrack._hex_and_ascii_key('A1B1C1D1E1')
147
assert hexkey == 'A1:B1:C1:D1:E1', 'hexkey was "%s", expected "A1:B1:C1:D1:E1"' % hexkey
148
assert asciikey is None, 'asciikey was "%s", expected None' % asciikey
149
150
(hexkey, asciikey) = Aircrack._hex_and_ascii_key('6162636465')
151
assert hexkey == '61:62:63:64:65', 'hexkey was "%s", expected "61:62:63:64:65"' % hexkey
152
assert asciikey == 'abcde', 'asciikey was "%s", expected "abcde"' % asciikey
153
154
from time import sleep
155
156
Configuration.initialize(False)
157
158
ivs_file = 'tests/files/wep-crackable.ivs'
159
print('Running aircrack on %s ...' % ivs_file)
160
161
aircrack = Aircrack(ivs_file)
162
while aircrack.is_running():
163
sleep(1)
164
165
assert aircrack.is_cracked(), 'Aircrack should have cracked %s' % ivs_file
166
print('aircrack process completed.')
167
168
(hexkey, asciikey) = aircrack.get_key_hex_ascii()
169
print('aircrack found HEX key: (%s) and ASCII key: (%s)' % (hexkey, asciikey))
170
assert hexkey == '75:6E:63:6C:65', 'hexkey was "%s", expected "75:6E:63:6C:65"' % hexkey
171
assert asciikey == 'uncle', 'asciikey was "%s", expected "uncle"' % asciikey
172
173
Configuration.exit_gracefully(0)
174
175