-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiktstatz.py
executable file
·225 lines (179 loc) · 6.93 KB
/
diktstatz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
#
# DiktStatz.py
# ------------
# A simple Python script that prints some useful statistical information by
# reading a given passwords dictionary.
#
# Coded by: Riccardo Mollo ([email protected])
#
#### TODO:
#### - ???
import argparse
import csv
import os
import re
import signal
import sys
from prettytable import PrettyTable
from termcolor import colored
def signal_handler(s, frame):
if s == 2: # SIGINT
print('You pressed Ctrl+C!')
print('Goodbye!')
sys.exit()
def logo():
print(colored(' _ __ ', 'cyan'))
print(colored('| \\ o | _|_(_ _|_ _ _|_ _ ', 'cyan'))
print(colored('|_/ | |< |___) |_(_| |_ /_', 'cyan'))
print()
print(colored('DiktStatz - Statistics for password dictionaries', 'cyan'))
print(colored('Coded by: Riccardo Mollo', 'cyan'))
print()
def error(message):
print(colored('ERROR!', 'red', attrs = ['reverse', 'bold']) + ' ' + message)
def percent(n1, n2):
return '{0:.2f}%'.format((n1 / n2 * 100))
def count_chars(file):
f = open(file, 'r')
d = f.read().replace('\n', '')
n = len(d)
f.close()
return n
def main():
parser = argparse.ArgumentParser(prog = 'diktstatz.py')
parser.add_argument('-d', '--dictionary', help = 'text file containing a lot of juicy passwords', required = True)
parser.add_argument('-m', '--max-length', help = 'only passwords with length up to this value will be considered', required = False)
parser.add_argument('-o', '--output', help = 'save results\' output as a CSV file', required = False)
args = parser.parse_args()
dict_file = args.dictionary
if not os.path.isfile(dict_file):
error('File "' + dict_file + '" does not exist or is not readable.')
sys.exit(1)
dict_size = count_chars(dict_file)
try:
min_length = len(min(open(dict_file), key = len))
max_length = len(max(open(dict_file), key = len))
except ValueError:
error('File "' + dict_file + '" seems to be empty.')
sys.exit(1)
output = args.output
if args.max_length is not None:
if int(args.max_length) > 0 and int(args.max_length) <= max_length:
max_length = int(args.max_length) + 1
logo()
total = sum(1 for i in open(dict_file, 'r'))
semit = 0
counters = {}
for i in range(max_length):
counters[i] = 0
wn_re = re.compile('[a-zA-Z]+\d+$', re.IGNORECASE) # eg: "Foo123" or "password999" or "Test0"
alnum = 0
alpha = 0
digit = 0
lower = 0
upper = 0
specl = 0
wdnum = 0
space = 0
with open(dict_file) as dictionary:
for password in dictionary:
password = password.rstrip('\n')
l = len(password)
if l <= max_length:
if password.isalnum():
alnum += 1
if password.isalpha():
alpha += 1
if password.isdigit():
digit += 1
if password.islower():
lower += 1
if password.isupper():
upper += 1
if any(not c.isalnum() for c in password):
specl += 1
if wn_re.search(password) is not None:
wdnum += 1
if re.search('\s', password) is not None:
space += 1
try:
counters[l] += 1
semit += 1
except IndexError:
pass
except Exception:
pass
print('Dictionary file: ' + os.path.abspath(dict_file))
print('Dictionary size: ' + str(dict_size) + ' bytes')
print('Min password length: ' + str(min_length - 1))
print('Max password length: ' + str(max_length - 1) + ' characters' + (' (forced by user)' if args.max_length is not None and total != semit else ''))
print('Avg password length: ' + str(round(dict_size / total)) + ' characters')
print('Total passwords: ' + str(total))
print('Considered passwords: ' + str(semit) + ' (' + percent(semit, total) + ')')
print()
if semit == 0:
print('No passwords with length less or equal to ' + str(max_length) + ' characters.')
else:
pwtable = PrettyTable()
chtable = PrettyTable()
if output is not None:
try:
csv_file = open(output, 'w')
writer = csv.writer(csv_file)
except PermissionError:
error('Cannot write file "' + output + '". Permission denied.')
sys.exit(1)
except IsADirectoryError:
error('"' + output + '" is a directory, not a file.')
sys.exit(1)
if args.max_length is None:
pwtable.field_names = ['Length', 'Passwords', 'Total %']
else:
pwtable.field_names = ['Length', 'Passwords', 'Relative %', 'Total %']
if output is not None:
writer.writerow(pwtable.field_names)
pwtable.align['Length'] = 'r'
pwtable.align['Passwords'] = 'r'
if args.max_length is None:
pwtable.align['Total %'] = 'r'
else:
pwtable.align['Relative %'] = 'r'
pwtable.align['Total %'] = 'r'
pwtable.sortby = 'Passwords'
pwtable.reversesort = True
for i in range(max_length):
if counters[i] > 0:
try:
if args.max_length is None:
row = [i, counters[i], percent(counters[i], total)]
else:
row = [i, counters[i], percent(counters[i], semit), percent(counters[i], total)]
pwtable.add_row(row)
if output is not None:
writer.writerow(row)
except KeyError as keyerror:
print(keyerror)
print(pwtable)
print()
chtable.field_names = ['Password type', 'Count', 'Count %']
chtable.align['Password type'] = 'l'
chtable.align['Count'] = 'r'
chtable.align['Count %'] = 'r'
chtable.add_row(['Alphanumeric', alnum, percent(alnum, semit)])
chtable.add_row(['Alphabetic only', alpha, percent(alpha, semit)])
chtable.add_row(['Digits only', digit, percent(digit, semit)])
chtable.add_row(['Lowercase only', lower, percent(lower, semit)])
chtable.add_row(['Uppercase only', upper, percent(upper, semit)])
chtable.add_row(['With spaces', space, percent(space, semit)])
chtable.add_row(['With special chars', specl, percent(specl, semit)])
chtable.add_row(['"WordNumber" format', wdnum, percent(wdnum, semit)])
print(chtable)
if output is not None:
csv_file.close()
print()
print('Output saved to CSV file: ' + output)
print()
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
main()