-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_password_gpg.py
280 lines (255 loc) · 9.38 KB
/
store_password_gpg.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
#!/usr/bin/env python3
# Copyright 2010 Karl A. Magdsick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Site configuration
DEFAULT_EMAIL_DOMAIN = 'gmail.com'
GPG_PATH = 'gpg'
MIN_BITS=40
import argparse
import bz2
import json
import hashlib
from math import log, ceil
import os
import random
import sys
import string
import time
ALPHABET_32 = string.digits + 'abcdefghjkmnpqrstvwxyz'
ALPHABET_36 = string.digits + string.ascii_lowercase
ALPHABET_62 = string.ascii_letters + string.digits
ALPHABET_64 = ALPHABET_62 + '_-'
ALPHABET_68 = ALPHABET_64 + '.!?/'
ALPHABET_71 = ALPHABET_64 + '#@$?.!/'
ALPHABET_84 = ALPHABET_62 + string.punctuation
ALL_ALPHABETS = [string.digits, string.ascii_lowercase, string.ascii_letters, ALPHABET_32, ALPHABET_36, ALPHABET_62, ALPHABET_64, ALPHABET_68,
ALPHABET_71, ALPHABET_84]
DEFAULT_ALPHABETS = (ALPHABET_62, ALPHABET_71, ALPHABET_71, ALPHABET_71, ALPHABET_84)
def lazy_len(s):
return s.__len__()
def base_dir():
if os.name == 'posix':
return os.path.join(os.getenv('HOME'), 'Documents', 'Passwords')
else:
return os.path.join(os.getenv('CSIDL_MYDOCUMENTS'), 'Passwords')
def get_bits(n, generator = os.urandom):
'''Returns a long integer suitable for generating a password with at least n bits of entropy'''
byte_count = (n+128+7)//8 # get 128 extra bits (rounded up) to reduce bias when taking modulo a non-power of two
data = generator(byte_count)
result = 0
for x in data:
result = 256 * result + x
return result
def create_password(bits, alphabet = ALPHABET_71, generator = os.urandom):
alphabet[0] # Force lazy values
alphabet_size = len(alphabet)
length = ceil(log(1<<bits) / log(alphabet_size))
bits = ceil(log(alphabet_size**length) / log(2))
seed = get_bits(bits, generator)
if type(alphabet) == str:
result = ''
while len(result) < length:
result += alphabet[seed % alphabet_size]
seed //= alphabet_size
return result
else:
result = []
while len(result) < length:
result.append(alphabet[seed % alphabet_size])
seed //= alphabet_size
return ' '.join(result)
def defaults():
config_path = os.path.join(base_dir(),'config.json')
if os.path.exists(config_path):
with open(config_path) as f:
config = json.load(f)
else:
config = {}
if not 'email' in config:
config['email'] = '{0}@{1}'.format(os.getenv('USER'), DEFAULT_EMAIL_DOMAIN)
if not 'user' in config:
config['user'] = config['email']
if not 'keys' in config:
config['keys'] = []
if 'key' in config:
config['keys'].append(config['key'])
del config['key']
if len(config['keys']) == 0:
config['keys'].append(config['email'])
if not 'alphabet' in config:
config['alphabet'] = None
if not 'bits' in config:
config['bits'] = 96
if not 'wordlist' in config:
config['wordlist'] = 'wordlist.txt.bz2'
# wordlist in the config doesn't make sense as a path relative to cwd specified in the config
# so a relative path in the config is relative to base_dir()
if not os.path.isabs(config['wordlist']):
config['wordlist'] = os.path.join(base_dir(), config['wordlist'])
return config
class LazyLength:
def __init__(self, future_list, min_len):
self.__min_len = min_len
self.__future = future_list
def __eq__(self, v):
if self.__min_len > v:
return False
if self.__future != None:
self.__min_len = len(self.__future.result())
self.__future = None
return self.__min_len == v
def __gt__(self, v):
if self.__min_len > v:
return True
if self.__future != None:
self.__min_len = len(self.__future.result())
self.__future = None
return self.__min_len > v
def __ge__(self, v):
if self.__min_len >= v:
return True
if self.__future != None:
self.__min_len = len(self.__future.result())
self.__future = None
return self.__min_len >= v
def __lt__(self, v):
if self.__min_len >= v:
return False
if self.__future != None:
self.__min_len = len(self.__future.result())
self.__future = None
return self.__min_len < v
def __le__(self, v):
if self.__min_len > v:
return False
if self.__future != None:
self.__min_len = len(self.__future.result())
self.__future = None
return self.__min_len <= v
class WordList:
'''Lazily loads a compressed list of words from disk.'''
def __init__(self, path):
assert os.path.exists(path)
self.__path = path
self.__words = None
def __getitem__(self, index):
return self.result()[index]
def __len__(self):
if self.__words == None:
return LazyLength(self, 100)
else:
return len(self.__words)
def __iter__(self):
return iter(self.result())
def result(self):
if self.__words == None:
print('Trying to load ' + self.__path)
with bz2.BZ2File(self.__path) as wordlist:
self.__words = [line.decode('utf8').strip() for line in wordlist.readlines()]
self.__path = None
return self.__words
def try_load_wordlist(path):
if os.path.exists(path):
words = WordList(path)
ALL_ALPHABETS.append(words)
ALL_ALPHABETS.append(words)
ALL_ALPHABETS.append(words) # Make pass-phrases thee times as likely
def shuffle_alphabets(alphabet_len, loop):
if alphabet_len != None and alphabet_len > 0:
if alphabet_len < 100:
result = [x for x in list(ALL_ALPHABETS) if lazy_len(x) == alphabet_len]
else:
result = [x for x in list(ALL_ALPHABETS) if lazy_len(x) >= alphabet_len]
if len(result) == 0:
raise ValueError('No known alphabets of length {0}'.format(alphabet_len))
elif loop:
result = list(ALL_ALPHABETS)
else:
result = list(DEFAULT_ALPHABETS)
random.shuffle(result)
return result
if __name__ == '__main__':
config = defaults()
parser = argparse.ArgumentParser(description='Generate a secure password')
parser.add_argument('domain')
parser.add_argument('--user', default=config['user'])
parser.add_argument('--email', default=config['email'])
parser.add_argument('--key', action='append', dest='keys', help='GPG key to use to store random passwords', default=config['keys'])
parser.add_argument('--alphabet', type=int, help='Number of characters in the alphabet to be used', default=config['alphabet'])
parser.add_argument('--wordlist', help='Path to a bzip2 compressed list of words, one word per line', default=config['wordlist'])
parser.add_argument('--bits', type=int, help='password securty level, in bits of entropy', default=config['bits'])
parser.add_argument('--loop', action='store_true', help='loop forever generating passwords until interrupted')
parser.add_argument('--verbose', action='store_true', help='verbose output')
parser.add_argument('--note', action='append', dest='notes', help='Note to be added to encrypted data')
args = parser.parse_args()
if args.bits < MIN_BITS:
parser.print_help()
print('\n\nPasswords must be at least {0} bits strong\n'.format(max(64, MIN_BITS))) # lie about the limit
sys.exit(1)
gpg_file = os.path.join(base_dir(), args.domain + '.gpg')
try_load_wordlist(args.wordlist)
alphabets = shuffle_alphabets(args.alphabet, args.loop)
if args.domain == '':
if args.loop:
try:
while True:
for alphabet in alphabets:
print(create_password(args.bits, alphabet))
time.sleep(0.75)
except KeyboardInterrupt:
sys.exit(0)
parser.print_help()
print('\nNo domain specified\n')
sys.exit(1)
if not os.path.exists(base_dir()):
raise NotADirectoryError('Missing folder {0}'.format(base_dir()))
if args.user == None:
args.user = args.email
if not args.keys:
args.keys = [args.email]
if os.path.exists(gpg_file):
print('ALREADY HAVE PASSWORD FOR '+args.domain)
time.sleep(0.1)
else:
if args.loop:
try:
while True:
for alphabet in alphabets:
password = create_password(args.bits, alphabet)
print(password)
time.sleep(2)
except KeyboardInterrupt:
print('\n')
else:
password = create_password(args.bits, alphabets[0])
# Always include domain at the top of the file to prevent domain-switching attack
# whene an attacker who can't break the encryption switches files and tricks the
# user into using the password for a valuable site on a non-valuable site controlled
# by the attacker.
msg = 'domain: {0}\nusername: {1}\npassword: {2}'.format(args.domain, args.user, password)
if args.email != args.user:
msg += '\nemail: ' + args.email
if args.notes:
msg += '\nNotes:\n {0}'.format('\n\n '.join(args.notes))
cmd = '{0} -e -r "{1}" --output "{2}"'.format( GPG_PATH, '" -r "'.join(args.keys), gpg_file)
if args.verbose:
print(cmd)
pipe = os.popen(cmd, 'w')
print(msg, file=pipe) # Use print to get DOS line endings on Windows
pipe.close()
cmd = '{0} -d "{1}"'.format(GPG_PATH, gpg_file)
if args.verbose:
print(cmd)
time.sleep(1)
os.system(cmd)