Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
derv82
GitHub Repository: derv82/wifite2
Path: blob/master/wifite/model/result.py
412 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from ..util.color import Color
5
from ..config import Configuration
6
7
import os
8
import time
9
from json import loads, dumps
10
11
class CrackResult(object):
12
''' Abstract class containing results from a crack session '''
13
14
# File to save cracks to, in PWD
15
cracked_file = Configuration.cracked_file
16
17
def __init__(self):
18
self.date = int(time.time())
19
self.readable_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.date))
20
21
def dump(self):
22
raise Exception('Unimplemented method: dump()')
23
24
def to_dict(self):
25
raise Exception('Unimplemented method: to_dict()')
26
27
def print_single_line(self, longest_essid):
28
raise Exception('Unimplemented method: print_single_line()')
29
30
def print_single_line_prefix(self, longest_essid):
31
essid = self.essid if self.essid else 'N/A'
32
Color.p('{W} ')
33
Color.p('{C}%s{W}' % essid.ljust(longest_essid))
34
Color.p(' ')
35
Color.p('{GR}%s{W}' % self.bssid.ljust(17))
36
Color.p(' ')
37
Color.p('{D}%s{W}' % self.readable_date.ljust(19))
38
Color.p(' ')
39
40
def save(self):
41
''' Adds this crack result to the cracked file and saves it. '''
42
name = CrackResult.cracked_file
43
saved_results = []
44
if os.path.exists(name):
45
with open(name, 'r') as fid:
46
text = fid.read()
47
try:
48
saved_results = loads(text)
49
except Exception as e:
50
Color.pl('{!} error while loading %s: %s' % (name, str(e)))
51
52
# Check for duplicates
53
this_dict = self.to_dict()
54
this_dict.pop('date')
55
for entry in saved_results:
56
this_dict['date'] = entry.get('date')
57
if entry == this_dict:
58
# Skip if we already saved this BSSID+ESSID+TYPE+KEY
59
Color.pl('{+} {C}%s{O} already exists in {G}%s{O}, skipping.' % (
60
self.essid, Configuration.cracked_file))
61
return
62
63
saved_results.append(self.to_dict())
64
with open(name, 'w') as fid:
65
fid.write(dumps(saved_results, indent=2))
66
Color.pl('{+} saved crack result to {C}%s{W} ({G}%d total{W})'
67
% (name, len(saved_results)))
68
69
@classmethod
70
def display(cls):
71
''' Show cracked targets from cracked file '''
72
name = cls.cracked_file
73
if not os.path.exists(name):
74
Color.pl('{!} {O}file {C}%s{O} not found{W}' % name)
75
return
76
77
with open(name, 'r') as fid:
78
cracked_targets = loads(fid.read())
79
80
if len(cracked_targets) == 0:
81
Color.pl('{!} {R}no results found in {O}%s{W}' % name)
82
return
83
84
Color.pl('\n{+} Displaying {G}%d{W} cracked target(s) from {C}%s{W}\n' % (
85
len(cracked_targets), name))
86
87
results = sorted([cls.load(item) for item in cracked_targets], key=lambda x: x.date, reverse=True)
88
longest_essid = max([len(result.essid or 'ESSID') for result in results])
89
90
# Header
91
Color.p('{D} ')
92
Color.p('ESSID'.ljust(longest_essid))
93
Color.p(' ')
94
Color.p('BSSID'.ljust(17))
95
Color.p(' ')
96
Color.p('DATE'.ljust(19))
97
Color.p(' ')
98
Color.p('TYPE'.ljust(5))
99
Color.p(' ')
100
Color.p('KEY')
101
Color.pl('{D}')
102
Color.p(' ' + '-' * (longest_essid + 17 + 19 + 5 + 11 + 12))
103
Color.pl('{W}')
104
# Results
105
for result in results:
106
result.print_single_line(longest_essid)
107
Color.pl('')
108
109
110
@classmethod
111
def load_all(cls):
112
if not os.path.exists(cls.cracked_file): return []
113
with open(cls.cracked_file, 'r') as json_file:
114
json = loads(json_file.read())
115
return json
116
117
@staticmethod
118
def load(json):
119
''' Returns an instance of the appropriate object given a json instance '''
120
if json['type'] == 'WPA':
121
from .wpa_result import CrackResultWPA
122
result = CrackResultWPA(json['bssid'],
123
json['essid'],
124
json['handshake_file'],
125
json['key'])
126
elif json['type'] == 'WEP':
127
from .wep_result import CrackResultWEP
128
result = CrackResultWEP(json['bssid'],
129
json['essid'],
130
json['hex_key'],
131
json['ascii_key'])
132
133
elif json['type'] == 'WPS':
134
from .wps_result import CrackResultWPS
135
result = CrackResultWPS(json['bssid'],
136
json['essid'],
137
json['pin'],
138
json['psk'])
139
140
elif json['type'] == 'PMKID':
141
from .pmkid_result import CrackResultPMKID
142
result = CrackResultPMKID(json['bssid'],
143
json['essid'],
144
json['pmkid_file'],
145
json['key'])
146
result.date = json['date']
147
result.readable_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(result.date))
148
return result
149
150
if __name__ == '__main__':
151
# Deserialize WPA object
152
Color.pl('\nCracked WPA:')
153
json = loads('{"bssid": "AA:BB:CC:DD:EE:FF", "essid": "Test Router", "key": "Key", "date": 1433402428, "handshake_file": "hs/capfile.cap", "type": "WPA"}')
154
obj = CrackResult.load(json)
155
obj.dump()
156
157
# Deserialize WEP object
158
Color.pl('\nCracked WEP:')
159
json = loads('{"bssid": "AA:BB:CC:DD:EE:FF", "hex_key": "00:01:02:03:04", "ascii_key": "abcde", "essid": "Test Router", "date": 1433402915, "type": "WEP"}')
160
obj = CrackResult.load(json)
161
obj.dump()
162
163
# Deserialize WPS object
164
Color.pl('\nCracked WPS:')
165
json = loads('{"psk": "the psk", "bssid": "AA:BB:CC:DD:EE:FF", "pin": "01234567", "essid": "Test Router", "date": 1433403278, "type": "WPS"}')
166
obj = CrackResult.load(json)
167
obj.dump()
168
169