-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
372 lines (292 loc) · 11.6 KB
/
manager.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
#!/usr/bin/env python2.7
from __future__ import print_function
from base64 import b64encode, b64decode
import os.path
from sys import exit
from random import SystemRandom
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Random import get_random_bytes
from Crypto.Protocol.KDF import scrypt
from json import loads, dumps
from getpass import getpass
import pyperclip
"""
Simple implementation of an encrypted password store.
Author: Szymon Bialkowski
Date: 15/01/2018
"""
# Read in all user information
def readAllPasswords():
userMessage = "Enter in emails, passwords and notes. Type 'exit' on service if you're done."
serviceName = ""
userEmail = ""
userPassword = ""
userNotes = ""
credentials = {}
print(userMessage)
while True:
serviceName = raw_input("\nService:\t")
if serviceName == 'exit':
break
userEmail = raw_input("Email:\t\t")
userPassword = generatePassword(int(raw_input("Pass Length:\t")))
userNotes = raw_input("Notes:\t\t")
credentials[serviceName] = {"email": userEmail,
"password": userPassword,
"notes": userNotes
}
return credentials
# Simple function which returns input for overwriting vault
# For readability purposes
def overwriteVaultQuestion():
message = "Vault already exists. "
message += "Are you sure you want to overwrite it? (y/n)\n"
return raw_input(message)
# Read password from user
def readAndReturnPassword():
while True:
while True:
password = raw_input("\nPlease enter in a strong master password.\n")
if not 0 < len(password) < 256:
print("Password length has to be between 0-256")
else:
return password
# Generates random password using urandom
def generatePassword(length=30):
characterSet = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
randomGenerator = SystemRandom()
password = [ randomGenerator.choice(characterSet) for i in range(length) ]
return ''.join(password)
# Create new vault
def createVault():
print("\nCreating new vault...")
if os.path.isfile("PasswordVault"):
check = overwriteVaultQuestion()
if check != 'y':
return None
password = readAndReturnPassword()
print("\nCreating vault...")
credentials = readAllPasswords()
try:
with open("PasswordVault", "w") as vault:
# All necessary arguments
salt = get_random_bytes(32)
N = 262144
r = 8
p = 1
keyLenght = 32
nonce = get_random_bytes(32)
# Perform scrypt key stretching
password = scrypt(password, salt, keyLenght, N, r, p)
aes = AES.new(password, AES.MODE_GCM, nonce=nonce)
credentials = dumps(credentials)
credentials, tag = aes.encrypt_and_digest(credentials)
[vault.write(x) for x in (salt, nonce, tag, credentials)]
print("Vault successfully created.")
except Exception:
print("Unable to create vault.")
def openVault():
vaultMessage = """\nVault Menu.
1: View.
2: Add.
3: Update.
4: Delete.
5: Encrypt and close vault.
6: Discard changes and close vault."""
password = getpass("Please enter in your vault password: ")
with open("PasswordVault", "r") as vault:
try:
keyLenght = 32
salt, nonce, tag, ciphertext = [ vault.read(x) for x in (32, 32, 16, -1) ]
password = scrypt(password, salt, keyLenght, 262144, 8, 1)
aes = AES.new(password, AES.MODE_GCM, nonce=nonce)
credentials = aes.decrypt_and_verify(ciphertext, tag)
credentials = loads(credentials)
except ValueError:
print("Invalid password or vault has been tampered with.")
raw_input()
return None
while True:
try:
print(vaultMessage)
userInput = int(raw_input("Choice: "))
if userInput == 1:
viewVault(credentials)
elif userInput == 2:
serviceName = raw_input("\nService:\t")
userEmail = raw_input("Email:\t\t")
userPassword = generatePassword(int(raw_input("Pass Length:\t")))
userNotes = raw_input("Notes:\t\t")
credentials[serviceName] = {"email": userEmail,
"password": userPassword,
"notes": userNotes
}
elif userInput == 3:
updateVault(credentials)
elif userInput == 4:
try:
service = raw_input("\nPlease enter service name: ")
if raw_input("Are you sure you want to delete {}? (y/n)\n".format(service)) == 'y':
del credentials[service]
print("Service deleted successfully.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 5:
with open("PasswordVault", "w") as vault:
nonce = get_random_bytes(32)
aes = AES.new(password, AES.MODE_GCM, nonce=nonce)
credentials = dumps(credentials)
credentials, tag = aes.encrypt_and_digest(credentials)
[vault.write(x) for x in (salt, aes.nonce, tag, credentials)]
print("Vault successfully saved.")
raw_input()
break
elif userInput == 6:
break
except Exception as e:
print("Invalid input. Please try again.")
def viewVault(credentials):
viewMessage = """\nView Menu.
1: Copy service password into clipboard.
2: Copy service email into clipboard.
3: Print service password. (CLEARTEXT!)
4: Print service email and notes.
5: Print all services.
6: Back to vault menu."""
userInput = 0
while True:
print(viewMessage)
try:
userInput = int(raw_input("Choice: "))
if userInput == 1:
try:
service = raw_input("\nPlease enter service name: ")
pyperclip.copy(credentials[service]['password'])
print("Password successfully copied to clipboard.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 2:
try:
service = raw_input("\nPlease enter service name: ")
pyperclip.copy(credentials[service]['email'])
print("Email successfully copied to clipboard.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 3:
try:
service = raw_input("\nPlease enter service name: ")
print(credentials[service]['password'])
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 4:
try:
service = raw_input("\nPlease enter service name: ")
print("Email: {}\nNotes: {}".format(credentials[service]['email'], credentials[service]['notes']))
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 5:
print("\n{}".format('\n'.join(credentials)))
raw_input()
elif userInput == 6:
break
except ValueError:
print("Please enter a valid number.")
def updateVault(credentials):
updateMessage = """\nUpdate Menu.
1: Update service Email.
2: Generate new service Password.
3: Update service notes.
4: Concatenate to service notes.
5: Return to vault menu."""
userInput = 0
while True:
print(updateMessage)
try:
userInput = int(raw_input("Choice: "))
if userInput == 1:
try:
service = raw_input("\nPlease enter service name: ")
credentials[service]['email'] = raw_input("\nPlease enter new email.")
print("Email changed successfully.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 2:
try:
service = raw_input("\nPlease enter service name: ")
credentials[service]['password'] = generatePassword(int(raw_input("\nPassword length: ")))
print("Password changed successfully.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 3:
try:
service = raw_input("\nPlease enter service name: ")
credentials[service]['notes'] = raw_input("\nNew {} service notes: ".format(service))
print("Notes changed successfully.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 4:
try:
service = raw_input("\nPlease enter service name: ")
credentials[service]['notes'] += raw_input("\nConcatenate to {} service notes: ".format(service))
print("Notes concatenated successfully.")
raw_input()
except KeyError:
print("\"{}\" service does not exist.".format(service))
raw_input()
elif userInput == 5:
break
except ValueError:
print("Please enter a valid number.")
def main():
welcomeMessage = """\nPassword Manager.
Please choose one of the following options.
1: Create new vault.
2: Open existing vault.
3: Exit.
"""
userInput = 0
print(welcomeMessage)
while True:
try:
userInput = int(raw_input("Choice: "))
if userInput == 1:
createVault()
print(welcomeMessage)
elif userInput == 2:
openVault()
print(welcomeMessage)
elif userInput == 3:
print("Thank you for using my Password Manager.")
exit()
else:
print(welcomeMessage)
print("\nInvalid option. Please try again.")
except ValueError as e:
print(welcomeMessage)
print("\nPlease enter a number.")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\nThank you for using my Password Manager.")
try:
exit(0)
except SystemExit:
os._exit(0)