-
Notifications
You must be signed in to change notification settings - Fork 39
/
lastpass-authenticator-export.py
212 lines (159 loc) · 4.58 KB
/
lastpass-authenticator-export.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
#!/usr/bin/env python3
import requests
import binascii
import hashlib
import base64
import json
import os
import pyotp
import qrcode
import argparse
import getpass
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
VERIFY = True
USER_AGENT = 'lastpass-python/{}'.format('0.3.2')
CLIENT_ID = 'LastPassAuthExport'
def iterations(username):
url = 'https://lastpass.com/iterations.php'
params = {
'email': username
}
headers = {
'user-agent': USER_AGENT
}
r = requests.get(
url = url,
params = params,
verify = VERIFY,
headers = headers
)
try:
iterations = int(r.text)
except ValueError:
iterations = 5000
return iterations
def create_hash(username, password, iteration_count):
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), username.encode('utf-8'), iteration_count, 32)
login_hash = binascii.hexlify(
hashlib.pbkdf2_hmac('sha256', key, password.encode('utf-8'), 1, 32)
)
return key, login_hash
def login(username, password, otp=None):
session = requests.Session()
session.headers = {'user-agent': USER_AGENT}
url = 'https://lastpass.com/login.php'
iteration_count = iterations(username)
key, login_hash = create_hash(username, password, iteration_count)
data = {
'method': 'mobile',
'web': 1,
'xml': 1,
'username': username,
'hash': login_hash,
'iterations': iteration_count,
'imei': CLIENT_ID
}
if otp:
data.update({'otp': otp})
r = session.post(
url = url,
data = data,
verify = VERIFY
)
if not r.text.startswith('<ok'):
print('Login failed!')
print(r.text)
exit(1)
else:
csrf = session.post('https://lastpass.com/getCSRFToken.php', verify=VERIFY).text
return r.cookies.get_dict()['PHPSESSID'], csrf, key
def get_mfa_backup(session, csrf):
url = 'https://lastpass.com/lmiapi/authenticator/backup'
headers = {
'X-CSRF-TOKEN': csrf,
'X-SESSION-ID': session,
'user-agent': USER_AGENT
}
r = requests.get(
url = url,
headers = headers,
verify = VERIFY
)
return r.json()['userData']
def decrypt_user_data(user_data, key):
data_parts = user_data.split('|')
iv = base64.b64decode(data_parts[0].split('!')[1])
ciphertext = base64.b64decode(data_parts[1])
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
plaintext = unpad(
cipher.decrypt(ciphertext),
AES.block_size
)
mfa_data = json.loads(plaintext)
return mfa_data
def write_out(mfa_data):
if not os.path.isdir('export'):
os.makedirs('export')
with open('export/export.json', 'w') as f:
f.write(json.dumps(mfa_data))
table = """
<html>
<head>
<style>
table, th {
border: 1px solid black;
}
td {
text-align: center;
vertical-align: middle;
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Issuer</th>
<th>Account</th>
<th>Secret</th>
<th>QR</th>
</tr>
"""
for account in mfa_data['accounts']:
totp = pyotp.TOTP(account['secret'].replace(' ', ''))
uri = totp.provisioning_uri(
name = account['userName'],
issuer_name = account['issuerName']
)
img = qrcode.make(uri)
img.save(f'export/{account["accountID"]}.png')
table += " <tr>\n"
table += f" <td>{account['issuerName']}</td>\n"
table += f" <td>{account['userName']}</td>\n"
table += f" <td>{account['secret']}</td>\n"
table += f" <td><img src='{account['accountID']}.png' width='200' height='200'></td>\n"
table += f" </tr>\n"
table += """
</table>
</body>
</html>
"""
with open('export/export.html', 'w') as f:
f.write(table)
def get_args():
parser = argparse.ArgumentParser(description='Export LastPass authenticator QR Codes.')
parser.add_argument('-u', '--username', help='LastPass username', required=True)
parser.add_argument('-o', '--otp', help='LastPass OTP', required=False)
return parser.parse_args()
def main():
args = get_args()
username = args.username
otp = args.otp
password = getpass.getpass()
session, csrf, key = login(username, password, otp)
user_data = get_mfa_backup(session, csrf)
mfa_data = decrypt_user_data(user_data, key)
write_out(mfa_data)
if __name__ == '__main__':
main()