-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswords.py
440 lines (374 loc) · 13.7 KB
/
passwords.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/local/bin/python3
# make sure we are using the right version
from sys import version
if version[0]!='3':
print("python 2 is deprecated, please use python 3")
exit()
# imports
from subprocess import run, check_output as call
from urllib.request import urlopen
import json; import base64; from sys import argv
from getpass import getpass
from os import system,remove,urandom
from os.path import expanduser,realpath
from random import randint as rand
if len(argv)==2 and argv[1]=='-install': pass
else:
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
try: import pyperclip
except: pass
fileName = expanduser('~')+'/.passwordmanager.enc'
saltLen = 16
# url of passwords.py script on github
url = 'https://raw.githubusercontent.com/mathewdblewis/passwordManager/master/passwords.py'
# utilities
def helpstr(state):
if state == 'viewEntry':
print("p = copy password to clipboard")
print("u = copy username to clipboard")
print("v = print password")
print("w = copy website to clipboard")
print("o = open website")
print("e = edit entry")
print("d = delete entry")
print("press enter to return to the main menu")
if state == 'main':
print("a = add entry")
print("c = change master password")
print("x = to quit this program")
print("press enter to search")
def Print(s,clear=True):
if clear: system('clear')
else: print("")
print('*'*len(s)+'****\n'+'* '+s+' *\n'+'*'*len(s)+'****\n')
def copy(data):
try:
run("pbcopy", universal_newlines=True, input=data)
return
except: pass
try:
pyperclip.copy(data)
return
except: pass
print("The copy to clipboard feature is unavailable")
print("you can print your password to console with")
def randstr(l,s):
nums = "1234567890"
lets = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"
syms = "~!@#$%^&*()_+-={}|[]<>?,./"
used = ""
if 'n' in s: used += nums
if 'l' in s: used += lets
if 's' in s: used += syms
if used == "": raise Exception('not enough character types chosen')
return "".join([used[rand(0,len(used)-1)] for _ in range(l)])
def save(data,final=False):
if final: start = 'c' # file starts with c because the file is "closed"
else: start = 'o' # file starts with o because the file is "open"
salt = urandom(saltLen)
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),length=32,salt=salt,iterations=100000,backend=default_backend())
cipher = Fernet(base64.urlsafe_b64encode(kdf.derive(data['password'].encode('utf-8'))))
towrite = start.encode('utf-8')+salt+cipher.encrypt(json.dumps(data).encode('utf-8'))
open(fileName,'wb').write(towrite)
# states
def load(empty):
file,plainText,data = "","",{}
try:
file = open(fileName,'rb').read()
if file[0] == ord('o'): # if the file is open
Print("WARNING")
print("Your password manager either apears to already be open")
print("or you are recovering your previous session after improperly quiting")
x = input("Press 'c' to continue or anything else to quit: ")
if 'c' != x: return ('exitState',)
file = file[1:]
salt,file = file[1:saltLen+1],file[saltLen+1:]
except: return ('setup',) # create new password manager
Print("PASSWORD MANAGER")
while True:
print("\nEnter your master password here. You can also press enter to exit")
password = getpass("or d to delete the password file and create a new password manager: ")
if password == '': return ('exitState',)
elif password == 'd':
print("Are you sure you want to delete the password file? This cannot be undone.")
if getpass("Enter y if yes: ")=='y':
remove(fileName)
return ('setup',)
else: continue
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),length=32,salt=salt,iterations=100000,backend=default_backend())
cipher = Fernet(base64.urlsafe_b64encode(kdf.derive(password.encode('utf-8'))))
try:
plainText = cipher.decrypt(file)
break
except: print('\nWrong password')
try: data = json.loads(plainText)
except:
print('ERROR: the file has been corrupted')
return ('exitState',)
save(data)
return ('search',data)
def setup(empty):
Print('WELCOME TO PASSWORD MANAGER!')
while True:
password = getpass('To start, first enter your master password or press enter to quit: ')
if password == "": return ('exitState',)
if len(password)<10:
print("your password must be at least 10 characters long.")
continue
if password == getpass('Reenter your master password: '): break
print("Your passwords don't match, try again")
data = {'password':password,'entries':{}}
save(data)
print('You will now be taken to the main page, from there enter h for help')
input('Enter anything to continue: ')
return ('main',data)
def search(params):
data,B = params[0],False
Print('SEARCH')
print('Press enter twice consecutively to return to main menu')
print('or enter the name of an entry to search for it')
while True:
x = input(': ')
if x=='':
if B: return ('main',data)
print('\n'.join(sorted([s for s in data['entries']])))
B = True
else:
B = False
if x[-1]==' ' and x.strip() in data['entries']: return ('viewEntry',data,x.strip())
else:
S = sorted([s for s in data['entries'] if s[:len(x)]==x])
if len(S)==0 and len(x)!=0: print('No entries starting with "' + x + '" found')
elif len(S)==0 and len(x)==0:
print('Your password manager is empty!')
print('Go to the main menue and enter "a" to add an entry.')
elif len(S)==1: return ('viewEntry',data,S[0])
else: print('\n'.join(S))
def main(params):
data = params[0]
Print('PASSWORD MANAGER')
print('(Enter "h" for help)')
while True:
x = input(': ')
if x=='a': return ('addEntry',data)
elif x=='': return ('search',data)
elif x=='c': return ('changeMasterPassword',data)
elif x=='x':
save(data,final=True)
return ('exitState',)
elif x=='h': helpstr('main')
else: print('Unknown option "' + x + '"')
def changeMasterPassword(params):
Print("CHANGE MASTER PASSWORD")
data = params[0]
while True:
print("\nPress Enter to return to the main page")
password = getpass('Enter new password: ')
if password == "": return ('exitState',)
if len(password)<10:
print("your password must be at least 10 characters long.")
continue
if password == '': return ('main',data)
elif password!=getpass('Re-enter new password: '): print("passwords don't match, try again")
else:
data['password'] = password
break
save(data)
input("The new password has been saved, press enter to continue: ")
return ('main',data)
def addEntry(params):
data = params[0]
Print('ADD ENTRY')
print("Provide the following: service name, username, website, notes, password")
print("'notes' may be multilined, to stop writting to 'notes' press enter on an empty line")
while True:
service = input("\nEntry name: ").strip()
if service in data['entries']: print('this entry already exists')
elif service == '': print("the entry can't have an empty string for its name")
else: break
username = input("username: ")
website = input("website: ")
if website[:7] not in ['https:/','http://','']: website = 'https://' + website
print("enter your notes below:")
notes = ""
while True:
nextline = input()
notes += nextline+'\n'
if nextline == '': break
notes = notes[:-1]
print("\nTo generate a password, enter the desired length (at least 10, at most 99)")
print("followed by some subset of the characters 'n','l','s'")
print("Including n will generate a password with numbers")
print("and similarly for 'l' and letters and 's' for symbols")
print("For example, '30 ns' will result in a password")
print("of length 30 with only numbers and symbols")
print("If a number is not provided or not enough symbols are used")
print("this setting will default to '30 nls'")
x = input("To use a custom password, simply press enter: ")
if x == '':
while True:
password = getpass("password: ")
if password == '': print("you must provide a password")
elif password == getpass("reenter password: "): break
else: print("passwords don't match, try again")
else:
l = 30
try: l = int(x[:2])
except: pass
try: password = randstr(l,x)
except: password = randstr(l,'snl')
data['entries'][service] = {'username':username,'website':website,'notes':notes,'password':password}
save(data)
input('Your entry has been saved, press enter to continue: ')
return ('viewEntry',data,service)
def viewEntry(params):
data,serviceName = params
entry = data['entries'][serviceName]
Print(serviceName)
print('(Enter "h" for help)\n')
if entry['username']!='': print('Username:\t' , entry['username'])
if entry['website']!='': print('Website:\t' , entry['website'])
if entry['notes']!='':
print('Notes:')
print(entry['notes'])
while True:
x = input(': ')
if x=='p': copy(entry['password'])
elif x=='v': print(entry['password'])
elif x=='u': copy(entry['username'])
elif x=='w': copy(entry['website'])
elif x=='o':
if entry['website']!='': system('open ' + entry['website'])
else: print("no website for this entry, option unavailable")
elif x=='e': return ('editEntry',data,serviceName)
elif x=='': return ('main',data)
elif x=='h': helpstr('viewEntry')
elif x=='d': return ('deleteEntry',data,serviceName)
else: print('Unknown option "' + x + '"')
def deleteEntry(params):
data,serviceName = params
print("Are you sure you want to delete this entry?")
i = input('If yes press "y", press anything else to exit: ')
if i=='y':
del data['entries'][serviceName]
save(data)
return ('search',data)
return ('viewEntry',data,serviceName)
def editEntry(params):
data,serviceName = params
Print('EDIT ENTRY')
print("When prompted to modify a field either enter the new value")
print("or press enter to leave it unchanged")
print("'notes' may be multilined, to stop writting to 'notes' press enter on an empty line")
while True:
service = input("\nEntry name: ").strip()
if service in data['entries']: print('this entry already exists')
else: break
username = input("username: ")
website = input("website: ")
if website[:7] not in ['https:/','http://','']: website = 'https://' + website
print("enter your notes below:")
notes = ""
while True:
nextline = input()
notes += nextline+'\n'
if nextline == '': break
notes = notes[:-1]
x = input("To use a custom password or keep your old password, press enter: ")
if x == '':
while True:
password = getpass("password: ")
if password == '': break
elif password == getpass("reenter password: "): break
else: print("passwords don't match, try again")
else:
l = 30
try: l = int(x[:2])
except: pass
try: password = randstr(l,x)
except: password = randstr(l,'snl')
if service != '':
data['entries'][service] = data['entries'][serviceName]
del data['entries'][serviceName]
serviceName = service
service = serviceName
temp = {'username':username,'website':website,'notes':notes,'password':password}
for x in temp:
if temp[x] == '': temp[x] = data['entries'][service][x]
data['entries'][service] = temp
save(data)
input('Your entry has been saved, press enter to continue: ')
return ('viewEntry',data,service)
def install():
# get dependencies
Print("INSTALLING DEPENDENCIES",clear=False)
try: system("pip3 install cryptography")
except:
print("could not install dependency 'cryptography'")
exit(1)
try: system("pip3 install pyperclip")
except: pass
# get file
file = ""
try: file = urlopen(url).read().decode()
except:
print("the source code could not be found")
exit(1)
file = '#!'+call('which python3',shell=True).decode()+'\n'+'\n'.join(file.split('\n')[1:])
# get path
Print("INSTALLING PASSWORD MANAGER",clear=False)
default,paths = "",call('echo $PATH',shell=True).decode().split(':')
for path in paths:
try: open(path+'/passwords','w')
except: continue
default = path+'/passwords'
break
path = ""
while True:
print('Give the full path of where you would like to put this program.')
print('You must provide an absolute path; ',end='')
if default!='': print('the path "' + default + '" is recommended.')
else: print(".")
if default!='': print('Press enter now to use the default or ')
path = input('type out your prefered path here: ')
if path == '': path = default
if path[0] != '/':
print("ERROR: you must provide an absolute path")
continue
try: open(path,'w')
except:
print("ERROR: this file cannot be written to, give a different path")
continue
break
# install program
open(path,'w').write(file)
call('chmod 700 ' + path, shell=True)
# prompt the user
Print("ALL DONE!",clear=False)
print("passwords has been installed!")
print("To run the CLI, enter '" + path.split('/')[-1] + "' below")
exit()
if __name__ == '__main__':
devmode = len(argv) == 2 and argv[1] == '-devmode'
if len(argv) == 2 and argv[1] == '-install': install()
state = ('load',)
try:
while True:
if state[0] == 'exitState': exit()
elif state[0] == 'load': state = load(state[1:])
elif state[0] == 'search': state = search(state[1:])
elif state[0] == 'setup': state = setup(state[1:])
elif state[0] == 'viewEntry': state = viewEntry(state[1:])
elif state[0] == 'addEntry': state = addEntry(state[1:])
elif state[0] == 'editEntry': state = editEntry(state[1:])
elif state[0] == 'deleteEntry': state = deleteEntry(state[1:])
elif state[0] == 'main': state = main(state[1:])
elif state[0] == 'changeMasterPassword': state = changeMasterPassword(state[1:])
else: raise Exception('unknown state: "' + state[0] + '"')
except Exception as e:
if devmode: raise e
else: print("SOFTWARE ERROR\nPlease notify the developer")
exit(1)