-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2fa
200 lines (162 loc) · 5.9 KB
/
2fa
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
#!/usr/bin/python3
# temp env
secret_dir = '~'
# imports
import argparse
import configparser
import sys
import base64
import pyotp
import pyperclip
import time
import math
from pathlib import Path
from cryptography.fernet import Fernet
# check dir (env)
if secret_dir == '~':
secret_dir = str(Path.home()) + '/.2fa'
else:
secret_dir = secret_dir + '/.2fa'
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
add_parser = subparsers.add_parser('add')
add_parser.set_defaults(func=add)
add_parser.add_argument('name', type=str)
add_parser.add_argument('secret', type=str)
get_parser = subparsers.add_parser('get')
get_parser.set_defaults(func=get)
get_parser.add_argument('name', type=str)
get_parser.add_argument('-c', '--copy', action='store_true')
ls_parser = subparsers.add_parser('ls')
ls_parser.set_defaults(func=ls)
delt_parser = subparsers.add_parser('delete')
delt_parser.set_defaults(func=delt)
delt_parser.add_argument('name', type=str)
args = parser.parse_args()
args.func(args)
def add(args):
config = configparser.ConfigParser()
config.read(secret_dir)
# make sure secrets file exists
my_file_dir = Path(secret_dir)
if not (my_file_dir.is_file()):
my_file_dir.touch()
config['Secrets'] = {}
if not config.has_section('Secrets'):
config['Secrets'] = {}
# encrypt secret
passwd_input = input('Please enter your encryption key (save this, or keep it equal for all secrets! keep it consistent!): ')
key = base64.urlsafe_b64encode(passwd_input.encode('utf-8').ljust(32)[:32])
passwd = Fernet(key)
encrypted_secret = passwd.encrypt(args.secret.encode('utf-8')).decode('utf-8')
# write secret to file
config['Secrets'][args.name] = encrypted_secret
with open(my_file_dir, 'w') as configfile:
config.write(configfile)
print(f"The key `{args.name}` has sucessfully been stored. Use `2fa get {args.name}` to retrieve your OTP.")
def get(args):
config = configparser.ConfigParser()
config.read(secret_dir)
my_file_dir = Path(secret_dir)
if not (my_file_dir.is_file()):
print('No secrets file found. Create a secret first.')
sys.exit(1)
if not config.has_section('Secrets'):
print('No secrets found. Create a secret first.')
sys.exit(1)
# decrypt secret
# todo: catch errors for invalid key
try:
secret = config['Secrets'][args.name]
passwd_input = input('Please enter your decryption key: ')
print("\033[A \033[A")
key = base64.urlsafe_b64encode(passwd_input.encode('utf-8').ljust(32)[:32])
passwd = Fernet(key)
decrypted_secret = passwd.decrypt(secret.encode('utf-8')).decode('utf-8')
except:
print('Invalid key. Please try again.')
sys.exit(1)
# generate totp, get time remaining
totp_obj = pyotp.TOTP(decrypted_secret)
try:
totp_code = totp_obj.now()
except Exception as e:
print(f"The secret provided was invalid. Please try readding the secret.")
sys.exit(1)
# check if valid in 5 seconds
valid_thru_five = totp_obj.verify(otp=totp_code, for_time=time.time() + 5)
if (valid_thru_five == False):
print('You will receive your TOTP in 5 seconds.')
for i in range(6):
time.sleep(1)
print(f'{i}..',end=" ",flush=True)
print('\n')
totp_code = totp_obj.now()
# one time binary search for time remaining
min = 0
max = 60
count = 0
done = False
while not done and count <= 60:
checkAmount = (min + max) / 2
valid = totp_obj.verify(otp=totp_code, for_time=time.time() + checkAmount)
if not valid:
max = checkAmount
else:
min = checkAmount
count += 1
min = math.ceil(min)
# final result
print(f"{totp_code} | {min}s")
# copy to system clipboard
if args.copy == True: pyperclip.copy(totp_code)
def ls(args):
config = configparser.ConfigParser()
config.read(secret_dir)
my_file_dir = Path(secret_dir)
if not (my_file_dir.is_file()):
print('No secrets file found. Create a secret first.')
sys.exit(1)
if not config.has_section('Secrets'):
print('No secrets found. Create a secret first.')
sys.exit(1)
count = 1
print('Id ┃ Secret')
print('━━━━╋━━━━━━━━━━━━━━')
for key in config['Secrets']:
length = len(str(count))
charsToAdd = 3 - length
sbcount = count # build string but keep global scope count separate
for i in range(charsToAdd):
sbcount = str(sbcount) + ' '
print(f"{sbcount} ┃ {key}")
count += 1
def delt(args):
config = configparser.ConfigParser()
config.read(secret_dir)
my_file_dir = Path(secret_dir)
if not (my_file_dir.is_file()):
print('No secrets file found. Create a secret first.')
sys.exit(1)
if not config.has_section('Secrets'):
print('No secrets found. Create a secret first.')
sys.exit(1)
# decrypt, confirm password/'elevation'
try:
secret = config['Secrets'][args.name]
passwd_input = input('Please enter your decryption key: ')
print("\033[A \033[A")
key = base64.urlsafe_b64encode(passwd_input.encode('utf-8').ljust(32)[:32])
passwd = Fernet(key)
decrypted_secret = passwd.decrypt(secret.encode('utf-8')).decode('utf-8')
except:
print('Invalid key. Please try again.')
sys.exit(1)
# delete secret
del config['Secrets'][args.name]
print(f"The key `{args.name}` has been deleted.")
with open(my_file_dir, 'w') as configfile:
config.write(configfile)
if __name__ == '__main__':
main()