This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathpass_phrase.py
executable file
·419 lines (334 loc) · 13.8 KB
/
pass_phrase.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python
# encoding: utf-8
import random
import optparse
import sys
import re
import os
import math
import datetime
import string
__LICENSE__ = """
The MIT License (MIT)
Copyright (c) 2012 Aaron Bassett, http://aaronbassett.com
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
# random.SystemRandom() should be cryptographically secure
try:
rng = random.SystemRandom
except AttributeError:
sys.stderr.write("WARNING: System does not support cryptographically "
"secure random number generator or you are using Python "
"version < 2.4.\n"
"Continuing with less-secure generator.\n")
rng = random.Random
# Python 3 compatibility
if sys.version[0] == "3":
raw_input = input
def validate_options(options, args):
"""
Given a set of command line options, performs various validation checks
"""
if options.num <= 0:
sys.stderr.write("Little point running the script if you "
"don't generate even a single passphrase.\n")
sys.exit(1)
if options.max_length < options.min_length:
sys.stderr.write("The maximum length of a word can not be "
"lesser then minimum length.\n"
"Check the specified settings.\n")
sys.exit(1)
if len(args) >= 1:
parser.error("Too many arguments.")
for word_type in ["adjectives", "nouns", "verbs"]:
wordfile = getattr(options, word_type, None)
if wordfile is not None:
if not os.path.exists(os.path.abspath(wordfile)):
sys.stderr.write("Could not open the specified {0} word file.\n".format(word_type))
sys.exit(1)
else:
common_word_file_locations = ["{0}.txt", "~/.pass-phrase/{0}.txt"]
for loc in common_word_file_locations:
wordfile = loc.format(word_type)
if os.path.exists(wordfile):
setattr(options, word_type, wordfile)
break
if getattr(options, word_type, None) is None:
sys.stderr.write("Could not find {0} word file, or word file does not exist.\n".format(word_type))
sys.exit(1)
def leet(word):
geek_letters = {
"a": ["4", "@"],
"b": ["8",],
"c": ["(",],
"e": ["3",],
"f": ["ph", "pH"],
"g": ["9", "6"],
"h": ["#",],
"i": ["1", "!", "|"],
"l": ["!", "|"],
"o": ["0", "()"],
"q": ["kw",],
"s": ["5", "$"],
"t": ["7",],
"x": ["><",],
"y": ["j",],
"z": ["2",]
}
geek_word = ""
for letter in word:
l = letter.lower()
if l in geek_letters:
# swap out the letter *most* (80%) of the time
if rng().randint(1,5) % 5 != 0:
letter = rng().choice(geek_letters[l])
else:
# uppercase it *some* (10%) of the time
if rng().randint(1,10) % 10 != 0:
letter = letter.upper()
geek_word += letter
# if last letter is an S swap it out half the time
if word[-1:].lower() == "s" and rng().randint(1,2) % 2 == 0:
geek_word = geek_word[:-1] + "zz"
return geek_word
def mini_leet(word):
geek_letters = {
"a": "4",
"b": "8",
"e": "3",
"g": "6",
"i": "1",
"o": "0",
"s": "5",
"t": "7",
"z": "2",
}
geek_word = ""
for letter in word:
l = letter.lower()
if l in geek_letters:
letter = geek_letters[l]
geek_word += letter
return geek_word
def generate_wordlist(wordfile=None,
min_length=0,
max_length=20,
valid_chars='.',
make_leet=False,
make_mini_leet=False):
"""
Generate a word list from either a kwarg wordfile, or a system default
valid_chars is a regular expression match condition (default - all chars)
"""
words = []
regexp = re.compile("^%s{%i,%i}$" % (valid_chars, min_length, max_length))
# At this point wordfile is set
wordfile = os.path.expanduser(wordfile) # just to be sure
wlf = open(wordfile)
for line in wlf:
thisword = line.strip()
if regexp.match(thisword) is not None:
if make_mini_leet:
thisword = mini_leet(thisword)
elif make_leet:
thisword = leet(thisword)
words.append(thisword)
wlf.close()
if len(words) < 1:
sys.stderr.write("Could not get enough words!\n")
sys.stderr.write("This could be a result of either {0} being too small,\n".format(wordfile))
sys.stderr.write("or your settings too strict.\n")
sys.exit(1)
return words
def craking_time(seconds):
minute = 60
hour = minute * 60
day = hour * 24
week = day * 7
if seconds < 60:
return "less than a minute"
elif seconds < 60 * 5:
return "less than 5 minutes"
elif seconds < 60 * 10:
return "less than 10 minutes"
elif seconds < 60 * 60:
return "less than an hour"
elif seconds < 60 * 60 * 24:
hours, r = divmod(seconds, 60 * 60)
return "about %i hours" % hours
elif seconds < 60 * 60 * 24 * 14:
days, r = divmod(seconds, 60 * 60 * 24)
return "about %i days" % days
elif seconds < 60 * 60 * 24 * 7 * 8:
weeks, r = divmod(seconds, 60 * 60 * 24 * 7)
return "about %i weeks" % weeks
elif seconds < 60 * 60 * 24 * 365 * 2:
months, r = divmod(seconds, 60 * 60 * 24 * 7 * 4)
return "about %i months" % months
else:
years, r = divmod(seconds, 60 * 60 * 24 * 365)
return "about %i years" % years
def verbose_reports(**kwargs):
"""
Report entropy metrics based on word list size"
"""
options = kwargs.pop("options")
f = {}
for word_type in ["adjectives", "nouns", "verbs"]:
print("The supplied {word_type} list is located at {loc}.".format(
word_type=word_type,
loc=os.path.abspath(getattr(options, word_type))
))
words = kwargs[word_type]
f[word_type] = {}
f[word_type]["length"] = len(words)
f[word_type]["bits"] = math.log(f[word_type]["length"], 2)
if (int(f[word_type]["bits"]) == f[word_type]["bits"]):
print("Your %s word list contains %i words, or 2^%i words."
% (word_type, f[word_type]["length"], f[word_type]["bits"]))
else:
print("Your %s word list contains %i words, or 2^%0.2f words."
% (word_type, f[word_type]["length"], f[word_type]["bits"]))
entropy = f["adjectives"]["bits"] +\
f["nouns"]["bits"] +\
f["verbs"]["bits"] +\
f["adjectives"]["bits"] +\
f["nouns"]["bits"]
print("A passphrase from this list will have roughly "
"%i (%0.2f + %0.2f + %0.2f + %0.2f + %0.2f) bits of entropy, " % (
entropy,
f["adjectives"]["bits"],
f["nouns"]["bits"],
f["verbs"]["bits"],
f["adjectives"]["bits"],
f["nouns"]["bits"]
))
combinations = math.pow(2, int(entropy)) / 1000
time_taken = craking_time(combinations)
print "Estimated time to crack this passphrase (at 1,000 guesses per second): %s\n" % time_taken
def generate_passphrase(adjectives, nouns, verbs, separator):
return "{0}{s}{1}{s}{2}{s}{3}{s}{4}".format(
rng().choice(adjectives),
rng().choice(nouns),
rng().choice(verbs),
rng().choice(adjectives),
rng().choice(nouns),
s=separator
)
def passphrase(adjectives, nouns, verbs, separator, num=1,
uppercase=False, lowercase=False, capitalise=False):
"""
Returns a random pass-phrase made up of
adjective noun verb adjective noun
I find this basic structure easier to
remember than XKCD style purely random words
"""
phrases = []
for i in range(0, num):
phrase = generate_passphrase(adjectives, nouns, verbs, separator)
if capitalise:
phrase = string.capwords(phrase)
phrases.append(phrase)
all_phrases = "\n".join(phrases)
if uppercase:
all_phrases = all_phrases.upper()
elif lowercase:
all_phrases = all_phrases.lower()
return all_phrases
if __name__ == "__main__":
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage)
parser.add_option("--adjectives", dest="adjectives",
default=None,
help="List of valid adjectives for passphrase")
parser.add_option("--nouns", dest="nouns",
default=None,
help="List of valid nouns for passphrase")
parser.add_option("--verbs", dest="verbs",
default=None,
help="List of valid verbs for passphrase")
parser.add_option("-s", "--separator", dest="separator",
default=' ',
help="Separator to add between words")
parser.add_option("-n", "--num", dest="num",
default=1, type="int",
help="Number of passphrases to generate")
parser.add_option("--min", dest="min_length",
default=0, type="int",
help="Minimum length of a valid word to use in passphrase")
parser.add_option("--max", dest="max_length",
default=20, type="int",
help="Maximum length of a valid word to use in passphrase")
parser.add_option("--valid_chars", dest="valid_chars",
default='.',
help="Valid chars, using regexp style (e.g. '[a-z]')")
parser.add_option("-U", "--uppercase", dest="uppercase",
default=False, action="store_true",
help="Force passphrase into uppercase")
parser.add_option("-L", "--lowercase", dest="lowercase",
default=False, action="store_true",
help="Force passphrase into lowercase")
parser.add_option("-C", "--capitalise", "--capitalize", dest="capitalise",
default=False, action="store_true",
help="Force passphrase to capitalise each word")
parser.add_option("--l337", dest="make_leet",
default=False, action="store_true",
help="7#izz R3@l|j !$ 4941Nst 7#3 w#()|e 5P|R!7 0pH t#3 7#|N6.")
parser.add_option("--l337ish", dest="make_mini_leet",
default=False, action="store_true",
help="A l337 version which is easier to remember.")
parser.add_option("-V", "--verbose", dest="verbose",
default=False, action="store_true",
help="Report various metrics for given options")
(options, args) = parser.parse_args()
validate_options(options, args)
# Generate word lists
adjectives = generate_wordlist(wordfile=options.adjectives,
min_length=options.min_length,
max_length=options.max_length,
valid_chars=options.valid_chars,
make_mini_leet=options.make_mini_leet,
make_leet=options.make_leet)
nouns = generate_wordlist(wordfile=options.nouns,
min_length=options.min_length,
max_length=options.max_length,
valid_chars=options.valid_chars,
make_mini_leet=options.make_mini_leet,
make_leet=options.make_leet)
verbs = generate_wordlist(wordfile=options.verbs,
min_length=options.min_length,
max_length=options.max_length,
valid_chars=options.valid_chars,
make_mini_leet=options.make_mini_leet,
make_leet=options.make_leet)
if options.verbose:
verbose_reports(adjectives=adjectives,
nouns=nouns,
verbs=verbs,
options=options)
print(passphrase(
adjectives,
nouns,
verbs,
options.separator,
num=int(options.num),
uppercase=options.uppercase,
lowercase=options.lowercase,
capitalise=options.capitalise
)
)