-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeeper.py
More file actions
165 lines (124 loc) · 6.03 KB
/
keeper.py
File metadata and controls
165 lines (124 loc) · 6.03 KB
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
from Crypto.Cipher import AES
from Crypto.Util.number import bytes_to_long, long_to_bytes
from hashlib import sha256
from sys import argv
from keeper_api import KeeperAPI
from json import dumps
import secrets
import coloredlogs
import logging
class Keeper():
ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&?@_'
USERNAME_LENGTH = 12
LABEL_LENGTH = 32
def __init__(self, master_key, contract_address, infura_provider, private_key):
self.master_key = sha256(bytes(master_key, 'utf-8')).digest()
self.contract = KeeperAPI('smart contracts/Keeper.sol', contract_address, infura_provider, private_key)
def oracle(self, msg, nonce=None):
aes = AES.new(self.master_key, AES.MODE_CTR, nonce=nonce)
nonce = aes.nonce
return aes.encrypt(msg), nonce
def generatePassword(self, username=None):
password = ''.join([secrets.choice(self.ALPHABET) for _ in range(12)])
target = password + username
encrypted_target, nonce = self.oracle(target.encode('utf-8'))
return bytes_to_long(encrypted_target + nonce)
def uploadPassword(self, target, label):
encrypted_label, nonce = self.oracle(label.encode('utf-8'))
label = bytes_to_long(encrypted_label + nonce)
if self.contract.addPassword(label, target):
logging.info('[+] Password Uploaded Succesfully')
else:
logging.error('[-] Upload Failed')
def getLabels(self):
labels = {}
for i in range(self.contract.getLabelsLength()):
encrypted_label = long_to_bytes(self.contract.getLabel(i))
label = self.oracle(encrypted_label[:-8], encrypted_label[-8:])[0]
labels[label.decode('utf-8')] = (encrypted_label, i)
return labels
def getPassword(self, label):
labels = self.getLabels()
if label not in labels.keys():
logging.error('[-] Label does not exist in the smart contract')
return None
encrypted_target = long_to_bytes(self.contract.getTarget(bytes_to_long(labels[label][0])))
target, nonce = self.oracle(encrypted_target[:-8], encrypted_target[-8:])
password = target[:12].decode('utf-8')
username = target[12:].decode('utf-8')
logging.info(dumps({'label': label, 'password': password, 'username': username}, indent=4))
def removePassword(self, label):
labels = self.getLabels()
if label not in labels.keys():
logging.error('[-] Label does not exist in the smart contract')
return None
label_index = labels[label][1]
assert self.contract.getLabel(label_index) == bytes_to_long(labels[label][0])
if self.contract.removeLabel(label_index):
logging.info('[+] Password Removed Succesfully')
else:
logging.error('[-] Operation Failed')
def main(self):
while True:
logging.info('\n[~] which operation would you like to perform?\n')
logging.info('[1] Generate a new Password')
logging.info('[2] Get All Stored Labels')
logging.info('[3] Remove a Password')
logging.info('[4] Retreive a Password')
logging.info('[5] Exit\n')
try:
choice = int(input('[~] choice: '))
assert choice in range(1, 6)
except (AssertionError, TypeError):
logging.error('[-] Choice must be 1, 2, 3, 4 or 5\n')
continue
if choice == 1:
while True:
try:
label = input(f'[~] Label(Must be between 1 and {self.LABEL_LENGTH} characters long): ')
assert len(label) in range(1, self.LABEL_LENGTH + 1)
break
except AssertionError:
logging.error(f'[-] Label Must be between 1 and {self.LABEL_LENGTH} characters long')
while True:
try:
username = input(f'[~] Username({self.USERNAME_LENGTH} characters max, hit enter to leave empty): ')
assert len(username) <= self.USERNAME_LENGTH
break
except AssertionError:
logging.error(f'[-] Username Must be {self.USERNAME_LENGTH} characters max')
if label not in self.getLabels().keys():
target = self.generatePassword(username)
self.uploadPassword(target, label)
else:
logging.error('[-] Label already exists')
elif choice == 2:
labels = self.getLabels()
logging.info(f'Available Labels: {dumps(list(labels.keys()), indent=4)}')
elif choice == 3:
while True:
try:
label = input(f'[~] Label(Must be between 1 and {self.LABEL_LENGTH} characters long): ')
assert len(label) in range(1, self.LABEL_LENGTH + 1)
break
except AssertionError:
logging.error(f'[-] Label Must be between 1 and {self.LABEL_LENGTH} characters long')
self.removePassword(label)
elif choice == 4:
while True:
try:
label = input(f'[~] Label(Must be between 1 and {self.LABEL_LENGTH} characters long): ')
assert len(label) in range(1, self.LABEL_LENGTH + 1)
break
except AssertionError:
logging.error(f'[-] Label Must be between 1 and {self.LABEL_LENGTH} characters long')
self.getPassword(label)
else:
break
if __name__ == '__main__':
coloredlogs.install(level='info', fmt='%(msg)s')
if len(argv) < 5:
logging.error('[-] Usage: python keeper.py <master key> <contract address> <infura provider> <private key>')
exit(0)
keeper = Keeper(*argv[1:])
keeper.main()