-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfirefox_saved_credentials.py
611 lines (494 loc) · 19.6 KB
/
firefox_saved_credentials.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is largely based on firefox_decrypt (https://github.com/unode/firefox_decrypt),
# available under the GPL-3.0 license.
from __future__ import annotations
import ctypes as ct
import json
import logging
import os
import platform
import shutil
import sqlite3
import sys
from base64 import b64decode
from subprocess import run, PIPE
from typing import Optional, Iterator
try:
# Python 3
from subprocess import DEVNULL
except ImportError:
# Python 2
DEVNULL = open(os.devnull, 'w')
try:
# Python 3
from urllib.parse import urlparse
except ImportError:
# Python 2
from urlparse import urlparse
try:
# Python 3
from configparser import ConfigParser
raw_input = input
except ImportError:
# Python 2
from ConfigParser import ConfigParser
LOG: logging.Logger
VERBOSE = False
SYSTEM = platform.system()
SYS64 = sys.maxsize > 2 ** 32
DEFAULT_ENCODING = "utf-8"
PWStore = list[dict[str, str]]
def get_version() -> str:
"""Obtain version information from git if available otherwise use
the internal version number
"""
def internal_version():
return '.'.join(map(str, __version_info__[:3])) + ''.join(__version_info__[3:])
try:
p = run(["git", "describe", "--tags"], stdout=PIPE, stderr=DEVNULL, text=True)
except FileNotFoundError:
return internal_version()
if p.returncode:
return internal_version()
else:
return p.stdout.strip()
__version_info__ = (1, 0, 0, "+git")
__version__: str = get_version()
class NotFoundError(Exception):
"""Exception to handle situations where a credentials file is not found
"""
pass
class Exit(Exception):
"""Exception to allow a clean exit from any point in execution
"""
CLEAN = 0
ERROR = 1
MISSING_PROFILEINI = 2
MISSING_SECRETS = 3
BAD_PROFILEINI = 4
LOCATION_NO_DIRECTORY = 5
BAD_SECRETS = 6
BAD_LOCALE = 7
FAIL_LOCATE_NSS = 10
FAIL_LOAD_NSS = 11
FAIL_INIT_NSS = 12
FAIL_NSS_KEYSLOT = 13
FAIL_SHUTDOWN_NSS = 14
BAD_MASTER_PASSWORD = 15
NEED_MASTER_PASSWORD = 16
PASSSTORE_NOT_INIT = 20
PASSSTORE_MISSING = 21
PASSSTORE_ERROR = 22
READ_GOT_EOF = 30
MISSING_CHOICE = 31
NO_SUCH_PROFILE = 32
UNKNOWN_ERROR = 100
KEYBOARD_INTERRUPT = 102
def __init__(self, exitcode):
self.exitcode = exitcode
def __unicode__(self):
return f"Premature program exit with exit code {self.exitcode}"
class Credentials:
"""Base credentials backend manager
"""
def __init__(self, db):
self.db = db
LOG.debug("Database location: %s", self.db)
if not os.path.isfile(db):
raise NotFoundError(f"ERROR - {db} database not found\n")
LOG.info("Using %s for credentials.", db)
def __iter__(self) -> Iterator[tuple[str, str, str, int]]:
pass
def done(self):
"""Override this method if the credentials subclass needs to do any
action after interaction
"""
pass
class SqliteCredentials(Credentials):
"""SQLite credentials backend manager
"""
def __init__(self, profile):
db = os.path.join(profile, "signons.sqlite")
super(SqliteCredentials, self).__init__(db)
self.conn = sqlite3.connect(db)
self.c = self.conn.cursor()
def __iter__(self) -> Iterator[tuple[str, str, str, int]]:
LOG.debug("Reading password database in SQLite format")
self.c.execute("SELECT hostname, encryptedUsername, encryptedPassword, encType "
"FROM moz_logins")
for i in self.c:
# yields hostname, encryptedUsername, encryptedPassword, encType
yield i
def done(self):
"""Close the sqlite cursor and database connection
"""
super(SqliteCredentials, self).done()
self.c.close()
self.conn.close()
class JsonCredentials(Credentials):
"""JSON credentials backend manager
"""
def __init__(self, profile):
db = os.path.join(profile, "logins.json")
super(JsonCredentials, self).__init__(db)
def __iter__(self) -> Iterator[tuple[str, str, str, int]]:
with open(self.db) as fh:
LOG.debug("Reading password database in JSON format")
data = json.load(fh)
try:
logins = data["logins"]
except Exception:
LOG.error(f"Unrecognized format in {self.db}")
raise Exit(Exit.BAD_SECRETS)
for i in logins:
yield (i["hostname"], i["encryptedUsername"],
i["encryptedPassword"], i["encType"])
def find_nss(locations, nssname) -> ct.CDLL:
"""Locate nss is one of the many possible locations
"""
fail_errors: list[tuple[str, str]] = []
OS = ("Windows", "Darwin")
for loc in locations:
nsslib = os.path.join(loc, nssname)
LOG.debug("Loading NSS library from %s", nsslib)
if SYSTEM in OS:
# On windows in order to find DLLs referenced by nss3.dll
# we need to have those locations on PATH
os.environ["PATH"] = ';'.join([loc, os.environ["PATH"]])
LOG.debug("PATH is now %s", os.environ["PATH"])
# However this doesn't seem to work on all setups and needs to be
# set before starting python so as a workaround we chdir to
# Firefox's nss3.dll/libnss3.dylib location
if loc:
if not os.path.isdir(loc):
# No point in trying to load from paths that don't exist
continue
workdir = os.getcwd()
os.chdir(loc)
try:
nss: ct.CDLL = ct.CDLL(nsslib)
except OSError as e:
fail_errors.append((nsslib, str(e)))
else:
LOG.debug("Loaded NSS library from %s", nsslib)
return nss
finally:
if SYSTEM in OS and loc:
# Restore workdir changed above
os.chdir(workdir)
else:
LOG.error("Couldn't find or load '%s'. This library is essential "
"to interact with your Mozilla profile.", nssname)
LOG.error("If you are seeing this error please perform a system-wide "
"search for '%s' and file a bug report indicating any "
"location found. Thanks!", nssname)
LOG.error("Alternatively you can try launching firefox_decrypt "
"from the location where you found '%s'. "
"That is 'cd' or 'chdir' to that location and run "
"firefox_decrypt from there.", nssname)
LOG.error("Please also include the following on any bug report. "
"Errors seen while searching/loading NSS:")
for target, error in fail_errors:
LOG.error("Error when loading %s was %s", target, error)
raise Exit(Exit.FAIL_LOCATE_NSS)
def load_libnss():
"""Load libnss into python using the CDLL interface
"""
if SYSTEM == "Windows":
nssname = "nss3.dll"
if SYS64:
locations: list[str] = [
"", # Current directory or system lib finder
os.path.expanduser("~\\AppData\\Local\\Mozilla Firefox"),
os.path.expanduser("~\\AppData\\Local\\Mozilla Thunderbird"),
os.path.expanduser("~\\AppData\\Local\\Nightly"),
os.path.expanduser("~\\AppData\\Local\\SeaMonkey"),
os.path.expanduser("~\\AppData\\Local\\Waterfox"),
"C:\\Program Files\\Mozilla Firefox",
"C:\\Program Files\\Mozilla Thunderbird",
"C:\\Program Files\\Nightly",
"C:\\Program Files\\SeaMonkey",
"C:\\Program Files\\Waterfox",
]
else:
locations: list[str] = [
"", # Current directory or system lib finder
"C:\\Program Files (x86)\\Mozilla Firefox",
"C:\\Program Files (x86)\\Mozilla Thunderbird",
"C:\\Program Files (x86)\\Nightly",
"C:\\Program Files (x86)\\SeaMonkey",
"C:\\Program Files (x86)\\Waterfox",
# On windows 32bit these folders can also be 32bit
os.path.expanduser("~\\AppData\\Local\\Mozilla Firefox"),
os.path.expanduser("~\\AppData\\Local\\Mozilla Thunderbird"),
os.path.expanduser("~\\AppData\\Local\\Nightly"),
os.path.expanduser("~\\AppData\\Local\\SeaMonkey"),
os.path.expanduser("~\\AppData\\Local\\Waterfox"),
"C:\\Program Files\\Mozilla Firefox",
"C:\\Program Files\\Mozilla Thunderbird",
"C:\\Program Files\\Nightly",
"C:\\Program Files\\SeaMonkey",
"C:\\Program Files\\Waterfox",
]
# If either of the supported software is in PATH try to use it
software = ["firefox", "thunderbird", "waterfox", "seamonkey"]
for binary in software:
location: Optional[str] = shutil.which(binary)
if location is not None:
nsslocation: str = os.path.join(os.path.dirname(location), nssname)
locations.append(nsslocation)
elif SYSTEM == "Darwin":
nssname = "libnss3.dylib"
locations = (
"", # Current directory or system lib finder
"/usr/local/lib/nss",
"/usr/local/lib",
"/opt/local/lib/nss",
"/sw/lib/firefox",
"/sw/lib/mozilla",
"/usr/local/opt/nss/lib", # nss installed with Brew on Darwin
"/opt/pkg/lib/nss", # installed via pkgsrc
"/Applications/Firefox.app/Contents/MacOS", # default manual install location
"/Applications/Thunderbird.app/Contents/MacOS",
"/Applications/SeaMonkey.app/Contents/MacOS",
"/Applications/Waterfox.app/Contents/MacOS",
)
else:
nssname = "libnss3.so"
if SYS64:
locations = (
"", # Current directory or system lib finder
"/usr/lib64",
"/usr/lib64/nss",
"/usr/lib",
"/usr/lib/nss",
"/usr/local/lib",
"/usr/local/lib/nss",
"/opt/local/lib",
"/opt/local/lib/nss",
os.path.expanduser("~/.nix-profile/lib"),
)
else:
locations = (
"", # Current directory or system lib finder
"/usr/lib",
"/usr/lib/nss",
"/usr/lib32",
"/usr/lib32/nss",
"/usr/lib64",
"/usr/lib64/nss",
"/usr/local/lib",
"/usr/local/lib/nss",
"/opt/local/lib",
"/opt/local/lib/nss",
os.path.expanduser("~/.nix-profile/lib"),
)
# If this succeeds libnss was loaded
return find_nss(locations, nssname)
class c_char_p_fromstr(ct.c_char_p):
"""ctypes char_p override that handles encoding str to bytes"""
def from_param(self):
return self.encode(DEFAULT_ENCODING)
class NSSProxy:
class SECItem(ct.Structure):
"""struct needed to interact with libnss
"""
_fields_ = [
('type', ct.c_uint),
('data', ct.c_char_p), # actually: unsigned char *
('len', ct.c_uint),
]
def decode_data(self):
_bytes = ct.string_at(self.data, self.len)
return _bytes.decode(DEFAULT_ENCODING)
class PK11SlotInfo(ct.Structure):
"""Opaque structure representing a logical PKCS slot
"""
def __init__(self):
# Locate libnss and try loading it
self.libnss = load_libnss()
SlotInfoPtr = ct.POINTER(self.PK11SlotInfo)
SECItemPtr = ct.POINTER(self.SECItem)
self._set_ctypes(ct.c_int, "NSS_Init", c_char_p_fromstr)
self._set_ctypes(ct.c_int, "NSS_Shutdown")
self._set_ctypes(SlotInfoPtr, "PK11_GetInternalKeySlot")
self._set_ctypes(None, "PK11_FreeSlot", SlotInfoPtr)
self._set_ctypes(ct.c_int, "PK11_NeedLogin", SlotInfoPtr)
self._set_ctypes(ct.c_int, "PK11_CheckUserPassword", SlotInfoPtr, c_char_p_fromstr)
self._set_ctypes(ct.c_int, "PK11SDR_Decrypt", SECItemPtr, SECItemPtr, ct.c_void_p)
self._set_ctypes(None, "SECITEM_ZfreeItem", SECItemPtr, ct.c_int)
# for error handling
self._set_ctypes(ct.c_int, "PORT_GetError")
self._set_ctypes(ct.c_char_p, "PR_ErrorToName", ct.c_int)
self._set_ctypes(ct.c_char_p, "PR_ErrorToString", ct.c_int, ct.c_uint32)
def _set_ctypes(self, restype, name, *argtypes):
"""Set input/output types on libnss C functions for automatic type casting
"""
res = getattr(self.libnss, name)
res.argtypes = argtypes
res.restype = restype
# Transparently handle decoding to string when returning a c_char_p
if restype == ct.c_char_p:
def _decode(result, func, *args):
return result.decode(DEFAULT_ENCODING)
res.errcheck = _decode
setattr(self, "_" + name, res)
def initialize(self, profile: str):
# The sql: prefix ensures compatibility with both
# Berkley DB (cert8) and Sqlite (cert9) dbs
profile_path = "sql:" + profile
LOG.debug("Initializing NSS with profile '%s'", profile_path)
err_status: int = self._NSS_Init(profile_path)
LOG.debug("Initializing NSS returned %s", err_status)
if err_status:
self.handle_error(
Exit.FAIL_INIT_NSS,
"Couldn't initialize NSS, maybe '%s' is not a valid profile?",
profile,
)
def shutdown(self):
err_status: int = self._NSS_Shutdown()
if err_status:
self.handle_error(
Exit.FAIL_SHUTDOWN_NSS,
"Couldn't shutdown current NSS profile",
)
def authenticate(self, profile, password):
"""Unlocks the profile if necessary, in which case a password
will prompted to the user.
"""
LOG.debug("Retrieving internal key slot")
keyslot = self._PK11_GetInternalKeySlot()
LOG.debug("Internal key slot %s", keyslot)
if not keyslot:
self.handle_error(
Exit.FAIL_NSS_KEYSLOT,
"Failed to retrieve internal KeySlot",
)
try:
if self._PK11_NeedLogin(keyslot):
LOG.debug("Authenticating with password '%s'", password)
err_status: int = self._PK11_CheckUserPassword(keyslot, password)
LOG.debug("Checking user password returned %s", err_status)
if err_status:
self.handle_error(
Exit.BAD_MASTER_PASSWORD,
"Master password is not correct",
)
else:
LOG.info("No Master Password found - no authentication needed")
finally:
# Avoid leaking PK11KeySlot
self._PK11_FreeSlot(keyslot)
def handle_error(self, exitcode: int, *logerror: Any):
"""If an error happens in libnss, handle it and print some debug information
"""
if logerror:
LOG.error(*logerror)
else:
LOG.debug("Error during a call to NSS library, trying to obtain error info")
code = self._PORT_GetError()
name = self._PR_ErrorToName(code)
name = "NULL" if name is None else name
# 0 is the default language (localization related)
text = self._PR_ErrorToString(code, 0)
LOG.debug("%s: %s", name, text)
raise Exit(exitcode)
def decrypt(self, data64):
data = b64decode(data64)
inp = self.SECItem(0, data, len(data))
out = self.SECItem(0, None, 0)
err_status: int = self._PK11SDR_Decrypt(inp, out, None)
LOG.debug("Decryption of data returned %s", err_status)
try:
if err_status: # -1 means password failed, other status are unknown
self.handle_error(
Exit.NEED_MASTER_PASSWORD,
"Password decryption failed. Passwords protected by a Master Password!",
)
res = out.decode_data()
finally:
# Avoid leaking SECItem
self._SECITEM_ZfreeItem(out, 0)
return res
class MozillaInteraction:
"""
Abstraction interface to Mozilla profile and lib NSS
"""
def __init__(self):
self.profile = None
self.proxy = NSSProxy()
def load_profile(self, profile):
"""Initialize the NSS library and profile
"""
self.profile = profile
self.proxy.initialize(self.profile)
def authenticate(self, password):
"""Authenticate the the current profile is protected by a master password,
prompt the user and unlock the profile.
"""
self.proxy.authenticate(self.profile, password)
def unload_profile(self):
"""Shutdown NSS and deactivate current profile
"""
self.proxy.shutdown()
def decrypt_passwords(self) -> PWStore:
"""Decrypt requested profile using the provided password.
Returns all passwords in a list of dicts
"""
credentials: Credentials = self.obtain_credentials()
LOG.info("Decrypting credentials")
outputs: list[dict[str, str]] = []
url: str
user: str
passw: str
enctype: int
for url, user, passw, enctype in credentials:
# enctype informs if passwords need to be decrypted
if enctype:
try:
LOG.debug("Decrypting username data '%s'", user)
user = self.proxy.decrypt(user)
LOG.debug("Decrypting password data '%s'", passw)
passw = self.proxy.decrypt(passw)
except (TypeError, ValueError) as e:
LOG.warning("Failed to decode username or password for entry from URL %s", url)
LOG.exception(e)
continue
LOG.debug("Decoded username '%s' and password '%s' for website '%s'", user, passw, url)
output = {"url": url, "user": user, "password": passw}
outputs.append(output)
if not outputs:
LOG.warning("No passwords found in selected profile")
# Close credential handles (SQL)
credentials.done()
return outputs
def obtain_credentials(self) -> Credentials:
"""Figure out which of the 2 possible backend credential engines is available
"""
credentials: Credentials
try:
credentials = JsonCredentials(self.profile)
except NotFoundError:
try:
credentials = SqliteCredentials(self.profile)
except NotFoundError:
LOG.error("Couldn't find credentials file (logins.json or signons.sqlite).")
raise Exit(Exit.MISSING_SECRETS)
return credentials
def get_saved_credentials(profile_path, master_password):
global LOG
LOG = logging.getLogger('root')
nss = MozillaInteraction()
nss.load_profile(profile_path)
try:
nss.authenticate(master_password)
result = nss.decrypt_passwords()
for credentials in result:
if credentials.get('username') is None and credentials.get('user') is not None:
credentials['username'] = credentials['user']
del credentials['user']
return result
finally:
nss.unload_profile()