|
| 1 | +"""Lookup of app secrets in an examiner-supplied iOS keychain. |
| 2 | +
|
| 3 | +iOS keychains are captured separately from the file system extraction, so an |
| 4 | +app that keeps its database key in the keychain cannot find that key inside the |
| 5 | +extraction. The examiner supplies the keychain with --keychain (or the field in |
| 6 | +the GUI) and artifacts read it through here. |
| 7 | +
|
| 8 | +Apps whose keys live in the keychain include Signal and Session (both a 48 byte |
| 9 | +GRDBDatabaseCipherKeySpec that is a 32 byte key followed by a 16 byte salt), |
| 10 | +Snapchat, Trust Wallet and what3words, so the lookup is deliberately generic |
| 11 | +rather than written around one app. |
| 12 | +
|
| 13 | +Supported inputs are the plist keychain dumps produced by extraction tools, |
| 14 | +which hold generic passwords under a 'genp' list. Entries carry an access group |
| 15 | +('agrp') naming the owning app, an account ('acct'), a service ('svce') and the |
| 16 | +secret itself in 'v_Data'. |
| 17 | +""" |
| 18 | +import plistlib |
| 19 | + |
| 20 | +from scripts.context import Context |
| 21 | +from scripts.ilapfuncs import logfunc |
| 22 | + |
| 23 | +GENERIC_PASSWORD_KEY = 'genp' |
| 24 | + |
| 25 | +_cache = {} |
| 26 | + |
| 27 | + |
| 28 | +def _text(entry, field): |
| 29 | + """Keychain string fields are sometimes bytes and sometimes str.""" |
| 30 | + value = entry.get(field) |
| 31 | + if isinstance(value, bytes): |
| 32 | + return value.decode('utf-8', 'ignore').rstrip('\x00') |
| 33 | + return str(value) if value is not None else '' |
| 34 | + |
| 35 | + |
| 36 | +def load_keychain_entries(keychain_path): |
| 37 | + """Return the generic-password entries of a keychain plist, cached per path.""" |
| 38 | + if keychain_path in _cache: |
| 39 | + return _cache[keychain_path] |
| 40 | + |
| 41 | + _cache[keychain_path] = [] |
| 42 | + try: |
| 43 | + with open(keychain_path, 'rb') as keychain_file: |
| 44 | + parsed = plistlib.load(keychain_file) |
| 45 | + except (OSError, ValueError) as error: |
| 46 | + logfunc(f'Keychain: could not read {keychain_path}: {error}') |
| 47 | + return _cache[keychain_path] |
| 48 | + |
| 49 | + if not isinstance(parsed, dict) or GENERIC_PASSWORD_KEY not in parsed: |
| 50 | + logfunc(f'Keychain: {keychain_path} is not a supported keychain dump ' |
| 51 | + '(no generic password entries found)') |
| 52 | + return _cache[keychain_path] |
| 53 | + |
| 54 | + entries = parsed.get(GENERIC_PASSWORD_KEY) or [] |
| 55 | + _cache[keychain_path] = [entry for entry in entries if isinstance(entry, dict)] |
| 56 | + return _cache[keychain_path] |
| 57 | + |
| 58 | + |
| 59 | +def find_keychain_secrets(keychain_path, access_group=None, account=None, service=None): |
| 60 | + """Return the raw secrets matching the given access group, account and service. |
| 61 | +
|
| 62 | + Each filter is optional and matched case-insensitively as a substring, so |
| 63 | + 'whispersystems' matches the full 'TEAMID.org.whispersystems.signal'. |
| 64 | + """ |
| 65 | + matches = [] |
| 66 | + for entry in load_keychain_entries(keychain_path): |
| 67 | + if access_group and access_group.lower() not in _text(entry, 'agrp').lower(): |
| 68 | + continue |
| 69 | + if account and account.lower() not in _text(entry, 'acct').lower(): |
| 70 | + continue |
| 71 | + if service and service.lower() not in _text(entry, 'svce').lower(): |
| 72 | + continue |
| 73 | + secret = entry.get('v_Data') |
| 74 | + if isinstance(secret, (bytes, bytearray)): |
| 75 | + matches.append(bytes(secret)) |
| 76 | + return matches |
| 77 | + |
| 78 | + |
| 79 | +def get_app_secret(access_group, account=None, service=None, expected_length=None): |
| 80 | + """Return one secret for an app from the examiner-supplied keychain, or None. |
| 81 | +
|
| 82 | + Args: |
| 83 | + access_group: substring of the owning app's access group, for example |
| 84 | + 'org.whispersystems.signal'. |
| 85 | + account: substring of the account name, for example |
| 86 | + 'GRDBDatabaseCipherKeySpec'. |
| 87 | + service: substring of the service name, when needed to disambiguate. |
| 88 | + expected_length: if given, only a secret of exactly this many bytes is |
| 89 | + returned, which guards against picking up an unrelated entry. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + The secret as bytes, or None when no keychain was supplied or nothing matched. |
| 93 | + """ |
| 94 | + keychain_path = Context.get_keychain_path() |
| 95 | + if not keychain_path: |
| 96 | + return None |
| 97 | + |
| 98 | + matches = find_keychain_secrets(keychain_path, access_group, account, service) |
| 99 | + if expected_length is not None: |
| 100 | + matches = [secret for secret in matches if len(secret) == expected_length] |
| 101 | + if not matches: |
| 102 | + return None |
| 103 | + if len(matches) > 1: |
| 104 | + logfunc(f'Keychain: {len(matches)} entries matched {access_group}/{account}, ' |
| 105 | + 'using the first') |
| 106 | + return matches[0] |
0 commit comments