diff --git a/.gitignore b/.gitignore index 80f465e..1ee68db 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ debian/tmp/ *.bin *.log +backups/ +firmware/*.xpfwext .idea/* .pybuild/ __pycache__ diff --git a/dbus_service/dbus-service b/dbus_service/dbus-service index cf29fae..e01b843 100755 --- a/dbus_service/dbus-service +++ b/dbus_service/dbus-service @@ -18,14 +18,11 @@ import dbus.mainloop.glib import dbus.service import yaml from gi.repository import GLib -try: - from gi.repository import GLibUnix -except ImportError: # PyGObject versions before the split namespace - GLibUnix = None from usb import core as usb_core from validitysensor import init from validitysensor.db import subtype_to_string, db, SidIdentity, User +from validitysensor.glib_compat import unix_signal_add from validitysensor.init_data_dir import PYTHON_VALIDITY_DATA_DIR, init_data_dir from validitysensor.operation import OperationBusy, OperationController from validitysensor.registration import RetryingRegistrar @@ -366,7 +363,9 @@ def main(): logging.info('Caught signal %d. Stopping...' % x) loop.quit() - signal_add = GLibUnix.signal_add if GLibUnix else GLib.unix_signal_add + # g_unix_signal_add moved between GLib and GLibUnix across PyGObject + # releases; the helper picks whichever binding this build provides. + signal_add = unix_signal_add() signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, die, signal.SIGINT) signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM, die, signal.SIGTERM) try: diff --git a/debian/python3-validity.udev b/debian/python3-validity.udev index b2c0e3c..21e37da 100644 --- a/debian/python3-validity.udev +++ b/debian/python3-validity.udev @@ -7,6 +7,7 @@ ATTRS{idVendor}=="06cb", ATTRS{idProduct}=="009a", GOTO="python_validity_match" ATTRS{idVendor}=="138a", ATTRS{idProduct}=="00ab", GOTO="python_validity_match" ATTRS{idVendor}=="06cb", ATTRS{idProduct}=="00b7", GOTO="python_validity_match" ATTRS{idVendor}=="06cb", ATTRS{idProduct}=="00cb", GOTO="python_validity_match" +ATTRS{idVendor}=="06cb", ATTRS{idProduct}=="00bb", GOTO="python_validity_match" GOTO="python_validity_end" diff --git a/docs/00bb-no-wipe.md b/docs/00bb-no-wipe.md new file mode 100644 index 0000000..2351e51 --- /dev/null +++ b/docs/00bb-no-wipe.md @@ -0,0 +1,123 @@ +# Reusing an existing Windows Hello pairing (no wipe) + +This is the long version of the "No-wipe path" section in `../README.md`. +Everything here is read-only with respect to the sensor; the only writes are +dump files under `/tmp/opencode/`. + +## Why it can work at all + +`checkpair.py` reports `PAIRED_TO_OTHER_HOST` when the pairing blob in +partition 1 (the cert-store) is encrypted with a different host secret than +the DMI default. That does **not** mean the pairing is unusable: Windows also +encrypted it with *some* machine-derived secret, so the pairing is potentially +recoverable if that secret can be found offline. + +Only one pairing slot exists on the sensor. Finding the Windows key means Linux +and Windows Hello can share the pairing instead of replacing it. + +## Candidate sources + +```bash +# offline: brute-force plausible host keys against the cert-store HMAC +sudo python3 tools/hwkey_probe.py # DMI candidates +sudo bash tools/find_windows_guid.sh # read-only NTFS mount +sudo python3 tools/hwkey_probe.py --guid-file /tmp/opencode/windows-machine-guid.reg +sudo bash tools/dump_windows_pairing.sh # driver's Pairing* values +sudo python3 tools/hwkey_probe.py --raw-file /tmp/opencode/windows-pairing.txt +sudo python3 tools/hwkey_probe.py --read-part4 # sensor's own record +``` + +`find_windows_guid.sh` needs no packages beyond `python3`; it parses the +SOFTWARE hive itself with `tools/scan_hive.py`. The HP driver DLL +(`synaWudfBioUsb116SGX.dll` from SP138431) reads +`HKLM\SOFTWARE\Microsoft\Cryptography\MachineGuid`, so that value is worth +trying — but on the reference machine it did **not** reproduce the pairing +HMAC. + +If the Windows partition cannot be mounted (BitLocker / hibernated Windows), +boot Windows once and run: + +``` +reg query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid +``` + +then pass the value with `--machine-guid`. + +`dump_windows_pairing.sh` is the same idea for the Synaptics driver state: the +UMDF driver keeps `PairingData`, `PairingContext` and `UnpairingContext` in the +device's registry key (`HKLM\SYSTEM\...\Enum\USB\VID_06CB&PID_00BB\...`), plus +`fwextId` and `MachineGuid` in SOFTWARE. The script mounts read-only, reads +those five values with `scan_hive.py` (one pass per hive), and writes +`Name=hex` lines to `/tmp/opencode/windows-pairing.txt` (mode 0600); string +values (`REG_SZ`) appear as `Name=text`. + +`hwkey_probe.py --raw-file` then tries the whole value, its first/last 32 +bytes, and every 32-byte window inside it — and string values as both ASCII and +UTF-16LE. If `PairingData` is the 32-byte key Windows used, this finds it in +seconds. + +The same run also writes `/tmp/opencode/windows-pairing-names.txt`, a survey of +every value name matching `pair|gwk|fwext|validity|synaptics|hostkey` +(`scan_hive.py --list-names`). If none of the dumped values matches, that list +shows whether the driver uses a different name. Re-run the dump with +`--name ` (repeatable) to add such a value to the dump — its size and, if +printable, its text are shown, and its bytes become candidates: + +```bash +sudo bash tools/dump_windows_pairing.sh --name Synaptics_01 --name AppPairingId +sudo python3 tools/hwkey_probe.py --raw-file /tmp/opencode/windows-pairing.txt +``` + +## Using a recovered key + +`hwkey_probe.py` is read-only and offline by default. If it prints +`MATCH FOUND`, verify the live sensor without writing anything: + +```bash +sudo PYTHON_VALIDITY_HW_KEY= python3 tools/checkpair.py +``` + +If that says `ALREADY_PAIRED`, install the driver as usual and make the +override persistent with a systemd drop-in: + +```bash +sudo systemctl edit python3-validity.service +# in the editor: +[Service] +Environment=PYTHON_VALIDITY_HW_KEY= + +sudo systemctl daemon-reload +sudo systemctl restart python3-validity.service +``` + +Status then: Linux works, **and** Windows Hello keeps working, because the +pairing is only reused, never replaced. Only one pairing still exists, so this +is sharing, not duplicating. + +## What was ruled out on the reference machine + +Reference unit: HP Spectre x360 13-ap0xxx, `06cb:00bb`, sensor +`57K0 FM-3439-004`. If no candidate matches, the secret is probably TPM-sealed +and not derivable from Linux; the wipe in `../README.md` is then the only +option. Ruled out: + +- DMI values (`product_name`, `product_serial`, board/BIOS strings) — 17 forms. +- Windows `MachineGuid` as ASCII, UTF-16LE and GUID text. +- Sensor partition 4, all 335872 bytes (1 GUID, 3 strings, every 32-byte + window). +- The driver's own registry state: `PairingData`, `PairingContext`, + `UnpairingContext` and `fwextId` are **absent**; only `MachineGuid` exists. +- `Synaptics_01` and `AppPairingId` from the name survey — the former is the + HID match pattern `HID\VID_06CB&PID_*` (one copy per key), the latter a + 16-byte app-pairing token. +- Every full registry dump of those values fed back as raw candidate bytes. + +A key that survives all of that is not stored in anything Linux can read — +TPM-sealed to Windows, or held by the sensor in a slot the host key unlocks. + +## Background + +- Recipe for this exact machine: + +- Clean-slate / d51 background: + diff --git a/firmware/README.md b/firmware/README.md new file mode 100644 index 0000000..d363f29 --- /dev/null +++ b/firmware/README.md @@ -0,0 +1,28 @@ +# Firmware is not redistributed here + +`06cb:00bb` needs the HP xpfwext payload named `6_07f_hp_mis_qm.xpfwext` for a +factory-reset or wiped sensor. That file is proprietary HP firmware, so it is +**not** part of this repository. + +You do not have to fetch it by hand: `tools/flash00bb.py` and +`validity-sensors-firmware` download the pinned HP SoftPaq **SP138431** +(`sp138431.exe`, SHA512 +`b9a268773ac948a4b6bfaa7a5762c58ab482aa47ea321a429ebc8dba3fcdd17ffe750d189791d8e0325b21e95161db5f751c61d7714d7460ee0a406055060a8f`), +verify that hash, and extract `6_07f_hp_mis_qm.xpfwext` with `cabextract`. +The file is cached outside the repo, in +`/var/lib/python-validity/firmware/`. + +`--phase preflight` caches the firmware **before** anything is wiped, so a +wiped sensor can always be re-provisioned offline from that cache. + +If your region uses a different SoftPaq, pass it explicitly: + +```bash +sudo python3 tools/flash00bb.py --softpaq /path/to/spXXXXXX.exe --phase preflight +``` + +Joentja's original report for this machine used SP106161, which carries the +same payload. A firmware signature mismatch (`044f`) usually means the wrong +SoftPaq for your region. + +See `../README.md` for the full procedure. diff --git a/install-00bb.sh b/install-00bb.sh new file mode 100755 index 0000000..9e236cd --- /dev/null +++ b/install-00bb.sh @@ -0,0 +1,257 @@ +#!/usr/bin/env bash +# +# Install the patched python-validity (06cb:00bb support) on this machine. +# +# sudo ./install-00bb.sh install the driver, udev rule and service +# sudo ./install-00bb.sh --check only verify prerequisites +# sudo ./install-00bb.sh --uninstall remove the installed driver files +# +# This script never touches PAM configuration, never removes packages and never +# reflashes the sensor. Package-level changes (open-fprintd / fprintd-clients) +# are printed as commands for you to run. +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PREFIX=/usr +SITE_PACKAGES=/usr/lib/python3/dist-packages +UNIT=/etc/systemd/system/python3-validity.service +UDEV_RULE=/etc/udev/rules.d/60-python3-validity.rules +DBUS_CONF=/usr/share/dbus-1/system.d/io.github.uunicorn.Fprint.conf +CONFIG_DIR=/etc/python-validity +STATE_DIR=/var/lib/python-validity +MODE=install + +for arg in "$@"; do + case "$arg" in + --check) MODE=check ;; + --uninstall) MODE=uninstall ;; + -h|--help) sed -n '2,11p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; + *) echo "unknown argument: $arg" >&2; exit 2 ;; + esac +done + +say() { printf '%s\n' "$*"; } +ok() { printf ' [ok] %s\n' "$*"; } +bad() { printf ' [!!] %s\n' "$*"; } +note() { printf ' [--] %s\n' "$*"; } + +# dpkg -s succeeds even for packages in the "rc" (removed, configs left) state, +# which made a previously removed fprintd look installed. +pkg_installed() { + [ "$(dpkg-query -W -f='${db:Status-Abbrev}' "$1" 2>/dev/null)" = "ii " ] +} + +need_root() { + if [ "$(id -u)" -ne 0 ]; then + echo "This step needs root: sudo $0 $*" >&2 + exit 1 + fi +} + +check_python_modules() { + local missing=() + for module in usb cryptography yaml dbus gi; do + if ! python3 - "$module" 2>/dev/null <<'PY' +import importlib, sys +importlib.import_module(sys.argv[1]) +PY + then + missing+=("$module") + fi + done + if [ ${#missing[@]} -gt 0 ]; then + bad "missing Python modules: ${missing[*]}" + say " sudo apt install python3-usb python3-cryptography python3-yaml python3-dbus python3-gi" + return 1 + fi + ok "python modules: usb, cryptography, yaml, dbus, gi" +} + +check_extractor() { + if command -v cabextract >/dev/null || command -v innoextract >/dev/null; then + ok "SoftPaq extractor: $(command -v cabextract || command -v innoextract)" + else + bad "neither cabextract nor innoextract is installed" + say " sudo apt install cabextract" + return 1 + fi +} + +check_backend() { + local rc=0 + if pkg_installed open-fprintd; then + ok "open-fprintd installed ($(dpkg-query -W -f='${Version}' open-fprintd))" + else + bad "open-fprintd is not installed" + say " sudo add-apt-repository ppa:uunicorn/open-fprintd" + say " sudo apt update" + say " sudo apt install open-fprintd fprintd-clients" + rc=1 + fi + if pkg_installed fprintd; then + bad "the old 'fprintd' package is still installed and conflicts with open-fprintd" + say " sudo apt install open-fprintd fprintd-clients # replaces it" + rc=1 + else + ok "old fprintd package absent" + fi + if [ -e /usr/lib/x86_64-linux-gnu/security/pam_fprintd.so ] \ + || [ -e /lib/x86_64-linux-gnu/security/pam_fprintd.so ]; then + ok "pam_fprintd.so present" + else + bad "pam_fprintd.so missing; install fprintd-clients" + rc=1 + fi + # The module being installed does not mean PAM uses it: the profile ships + # disabled, so common-auth has no pam_fprintd line until it is enabled. + if grep -q pam_fprintd /etc/pam.d/common-auth 2>/dev/null; then + ok "PAM uses pam_fprintd (screen unlock / sudo offer a finger)" + else + note "PAM does not use pam_fprintd yet; enable it with:" + say " sudo pam-auth-update --enable fprintd" + say " (not done by this script, which never edits PAM)" + fi + return $rc +} + +do_check() { + local rc=0 + say "Prerequisite check" + check_python_modules || rc=1 + check_extractor || rc=1 + check_backend || rc=1 + if [ $rc -ne 0 ]; then + say "" + say "Fix the [!!] items above, then re-run: sudo $0" + return 1 + fi + say "" + say "All prerequisites satisfied." +} + +do_install() { + need_root "$@" + do_check || exit 1 + + say "" + say "Installing the patched python-validity package" + cd "$HERE" + # --single-version-externally-managed is what makes this use the Debian + # layout instead of falling back to easy_install. The fallback installs an + # .egg and silently SKIPS data_files, so /usr/lib/python-validity/dbus-service + # (the service's ExecStart) was never created and the daemon died with + # status=203/EXEC. + python3 setup.py install --prefix="$PREFIX" --install-layout=deb \ + --single-version-externally-managed --record=/tmp/00bb-installed.txt \ + >/tmp/00bb-setup.log 2>&1 || { + bad "setup.py install failed; see /tmp/00bb-setup.log" + tail -20 /tmp/00bb-setup.log + exit 1 + } + ok "validitysensor installed in $SITE_PACKAGES" + + # Remove the artefacts of an older easy_install-style run so the plain + # package tree wins and the .pth does not point at a deleted egg. + for egg in "$SITE_PACKAGES"/python_validity-*.egg; do + [ -e "$egg" ] || continue + rm -rf "$egg" + ok "removed stale egg $(basename "$egg")" + done + if [ -f "$SITE_PACKAGES/easy-install.pth" ]; then + sed -i '/python_validity-.*\.egg/d' "$SITE_PACKAGES/easy-install.pth" + fi + + for path in "$SITE_PACKAGES/validitysensor/__init__.py" \ + $PREFIX/lib/python-validity/dbus-service \ + $PREFIX/share/dbus-1/system.d/io.github.uunicorn.Fprint.conf; do + if [ ! -e "$path" ]; then + bad "installation incomplete: $path is missing" + exit 1 + fi + done + # The service imports the installed copy, so prove it resolves outside this + # checkout (a leftover egg or a stale PYTHONPATH could otherwise mask it). + if ! (cd / && python3 -c 'import validitysensor' 2>/dev/null); then + bad "the installed validitysensor package is not importable from /" + exit 1 + fi + ok "driver files complete (dbus-service, D-Bus policy, module)" + + rm -rf "$HERE/build" "$HERE/python_validity.egg-info" + + say "" + say "Installing udev rule and systemd unit" + install -d -m 0755 /etc/udev/rules.d /etc/systemd/system "$CONFIG_DIR" + install -m 0644 "$HERE/debian/python3-validity.udev" "$UDEV_RULE" + install -m 0644 "$HERE/debian/python3-validity.service" "$UNIT" + ok "$UDEV_RULE" + ok "$UNIT" + + if [ ! -e "$CONFIG_DIR/dbus-service.yaml" ]; then + install -m 0600 "$HERE/etc/python-validity/dbus-service.yaml" "$CONFIG_DIR/dbus-service.yaml" + ok "$CONFIG_DIR/dbus-service.yaml (new)" + else + ok "$CONFIG_DIR/dbus-service.yaml (kept existing)" + fi + + install -d -m 0700 "$STATE_DIR" + + systemctl daemon-reload + udevadm control --reload-rules + # Match what the .deb does (enable, do not start) and clear the failed state + # left by any earlier run, then let the udev add event start the daemon. + systemctl enable python3-validity.service >/dev/null 2>&1 || true + systemctl reset-failed python3-validity.service 2>/dev/null || true + udevadm trigger --attr-match=idVendor=06cb --attr-match=idProduct=00bb || true + ok "udev cache and systemd reloaded (service enabled)" + + say "" + say "Done. Next steps:" + say " 1. sudo python3 $HERE/tools/probe00bb.py" + say " (read-only; confirms identity, backs up the old cert-store)" + say " 2. sudo python3 $HERE/tools/flash00bb.py --phase preflight" + say " (read-only; caches the pinned HP firmware before anything is wiped)" + say " 3. sudo python3 $HERE/tools/flash00bb.py --phase all" + say " (DESTRUCTIVE: destroys the Windows Hello pairing on the sensor)" + say " 4. sudo pam-auth-update --enable fprintd" + say " (lets the login screen and sudo actually offer a finger)" + say " 5. fprintd-enroll -f right-index-finger \"\$USER\"" + say " 6. lock the screen and log in with your finger; then test 'sudo -k; sudo true'" +} + +do_uninstall() { + need_root "$@" + say "Removing the patched python-validity driver files" + systemctl disable --now python3-validity.service 2>/dev/null || true + systemctl stop python3-validity.service 2>/dev/null || true + + rm -f "$UNIT" "$UDEV_RULE" "$DBUS_CONF" + rm -rf /usr/lib/python-validity /usr/share/python-validity + rm -rf "$SITE_PACKAGES/validitysensor" "$SITE_PACKAGES"/python_validity-*.egg-info + rm -rf "$SITE_PACKAGES"/python_validity-*.egg + if [ -f "$SITE_PACKAGES/easy-install.pth" ]; then + sed -i '/python_validity-.*\.egg/d' "$SITE_PACKAGES/easy-install.pth" + fi + rm -f /usr/bin/validity-led-dance /usr/bin/validity-sensors-firmware + + systemctl daemon-reload + udevadm control --reload-rules + + ok "driver files removed" + say "" + say "Kept on purpose:" + say " $CONFIG_DIR (D-Bus service config)" + say " $STATE_DIR (calibration data, cached xpfwext)" + say "" + say "To restore stock fprintd afterwards:" + say " sudo apt install fprintd libpam-fprintd" + say "" + say "The sensor itself stays paired to this machine; re-pairing it with" + say "Windows Hello would break the Linux pairing again." +} + +case "$MODE" in + check) do_check ;; + install) do_install "$@" ;; + uninstall) do_uninstall "$@" ;; +esac diff --git a/tests/test_debian_packaging.py b/tests/test_debian_packaging.py index 52c0586..b1088bb 100644 --- a/tests/test_debian_packaging.py +++ b/tests/test_debian_packaging.py @@ -28,6 +28,26 @@ def test_matched_hardware_still_starts_without_manual_configuration(self): self.assertIn('start python3-validity.service', rules) self.assertIn('LABEL="python_validity_match"', rules) + def test_unit_execstart_is_shipped_as_a_data_file(self): + setup_py = (ROOT / 'setup.py').read_text() + unit = (ROOT / 'debian/python3-validity.service').read_text() + + self.assertIn( + "('lib/python-validity/', ['dbus_service/dbus-service'])", + setup_py, + ) + self.assertIn('ExecStart=/usr/lib/python-validity/dbus-service', unit) + + def test_installer_uses_the_debian_layout_not_easy_install(self): + script = (ROOT / 'install-00bb.sh').read_text() + + # Plain `setup.py install` can fall back to easy_install, which + # silently skips data_files: no dbus-service, so the unit died with + # status=203/EXEC while the script still reported success. + self.assertIn('--single-version-externally-managed', script) + self.assertIn('--install-layout=deb', script) + self.assertIn('lib/python-validity/dbus-service', script) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_glib_compat.py b/tests/test_glib_compat.py new file mode 100644 index 0000000..08ed2c0 --- /dev/null +++ b/tests/test_glib_compat.py @@ -0,0 +1,72 @@ +import sys +import types +import unittest +from unittest import mock + +from validitysensor.glib_compat import unix_signal_add + + +def fake_gi(glib, glib_unix=None, require_error=None): + """Build the sys.modules entries that ``from gi.repository import ...`` hits.""" + repository = types.SimpleNamespace(GLib=glib) + if glib_unix is not None: + repository.GLibUnix = glib_unix + + def require_version(*args): + if require_error is not None: + raise require_error + + gi = types.SimpleNamespace(repository=repository, require_version=require_version) + modules = {'gi': gi, 'gi.repository': repository} + if glib_unix is not None: + modules['gi.repository.GLibUnix'] = glib_unix + return modules + + +class UnixSignalAddTests(unittest.TestCase): + def test_prefers_glib_unix_signal_add(self): + sentinel = object() + glib = types.SimpleNamespace(unix_signal_add=sentinel) + + with mock.patch.dict(sys.modules, fake_gi(glib)): + self.assertIs(unix_signal_add(), sentinel) + + def test_falls_back_to_glibunix_signal_add(self): + sentinel = object() + glib = types.SimpleNamespace() + glib_unix = types.SimpleNamespace(signal_add=sentinel) + + with mock.patch.dict(sys.modules, fake_gi(glib, glib_unix)): + self.assertIs(unix_signal_add(), sentinel) + + def test_signal_add_full_is_wrapped_with_user_data(self): + calls = [] + glib = types.SimpleNamespace() + glib_unix = types.SimpleNamespace( + signal_add_full=lambda *args: calls.append(args) or 42) + + with mock.patch.dict(sys.modules, fake_gi(glib, glib_unix)): + add = unix_signal_add() + result = add(1, 2, 'handler', 'user-data') + + self.assertEqual(result, 42) + self.assertEqual(calls, [(1, 2, 'handler', 'user-data')]) + + def test_missing_binding_is_an_explicit_error(self): + glib = types.SimpleNamespace() + + with mock.patch.dict(sys.modules, fake_gi(glib)): + with self.assertRaises(RuntimeError): + unix_signal_add() + + def test_require_version_failure_is_not_fatal(self): + glib = types.SimpleNamespace() + modules = fake_gi(glib, require_error=ValueError('namespace unavailable')) + + with mock.patch.dict(sys.modules, modules): + with self.assertRaises(RuntimeError): + unix_signal_add() + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_host_key_override.py b/tests/test_host_key_override.py new file mode 100644 index 0000000..dc20b7e --- /dev/null +++ b/tests/test_host_key_override.py @@ -0,0 +1,54 @@ +import os +import unittest +from unittest.mock import patch + +from validitysensor.tls import (Tls, gwk_sign_hardcoded, password_hardcoded, + prf) + + +def expected_key(hw_key: bytes) -> bytes: + return prf(password_hardcoded, b'GWK' + hw_key, 0x20) + + +class HostKeyOverrideTests(unittest.TestCase): + def make_tls(self): + # usb is only used for commands, not for key derivation + return Tls(None) + + def test_default_uses_dmi_strings_with_nul_terminators(self): + tls = self.make_tls() + with patch.dict(os.environ, {}, clear=False): + os.environ.pop('PYTHON_VALIDITY_HW_KEY', None) + tls.set_hwkey('Laptop', 'SERIAL') + self.assertEqual(tls.psk_encryption_key, + expected_key(b'Laptop\0SERIAL\0')) + + def test_validation_key_uses_the_encryption_key(self): + tls = self.make_tls() + with patch.dict(os.environ, {}, clear=False): + os.environ.pop('PYTHON_VALIDITY_HW_KEY', None) + tls.set_hwkey('Laptop', 'SERIAL') + self.assertEqual( + tls.psk_validation_key, + prf(tls.psk_encryption_key, + b'GWK_SIGN' + gwk_sign_hardcoded, 0x20)) + + def test_env_override_is_used_as_raw_bytes(self): + tls = self.make_tls() + with patch.dict(os.environ, + {'PYTHON_VALIDITY_HW_KEY': b'abc'.hex()}): + tls.set_hwkey('Laptop', 'SERIAL') + self.assertEqual(tls.psk_encryption_key, + expected_key(b'abc')) + + def test_env_override_wins_over_dmi(self): + tls = self.make_tls() + with patch.dict(os.environ, + {'PYTHON_VALIDITY_HW_KEY': b'other'.hex()}): + tls.set_hwkey('Laptop', 'SERIAL') + self.assertNotEqual(tls.psk_encryption_key, + expected_key(b'Laptop\0SERIAL\0')) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_hwkey_probe.py b/tests/test_hwkey_probe.py new file mode 100644 index 0000000..240121f --- /dev/null +++ b/tests/test_hwkey_probe.py @@ -0,0 +1,188 @@ +import importlib.util +import io +import tempfile +import unittest +from contextlib import redirect_stderr +from pathlib import Path + +TOOLS = Path(__file__).resolve().parent.parent / 'tools' + + +def load_hwkey_probe(): + spec = importlib.util.spec_from_file_location( + 'hwkey_probe', TOOLS / 'hwkey_probe.py') + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +hwkey_probe = load_hwkey_probe() + + +class RawVariantsTests(unittest.TestCase): + def keys(self, label, blob): + return [key for _, key in hwkey_probe.raw_variants(label, blob)] + + def test_exact_32_byte_blob_is_a_candidate(self): + blob = bytes(range(32)) + variants = hwkey_probe.raw_variants('PairingData', blob) + self.assertEqual(variants[0], ('PairingData', blob)) + + def test_windows_expose_every_offset(self): + blob = b'\x01\x02\x03\x04' + bytes(range(32)) + b'\xaa' + keys = self.keys('x', blob) + self.assertIn(blob[4:36], keys) + self.assertIn(blob[:32], keys) + self.assertIn(blob[-32:], keys) + # every 32-byte window inside the blob is covered + for offset in range(0, len(blob) - 32 + 1): + self.assertIn(blob[offset:offset + 32], keys) + + def test_duplicates_are_removed(self): + blob = b'\0' * 96 + keys = self.keys('zero', blob) + self.assertEqual(len(keys), len(set(keys))) + + def test_large_blob_is_not_used_whole(self): + blob = b'\x11' * 8192 + keys = self.keys('big', blob) + self.assertNotIn(blob, keys) + self.assertIn(blob[:32], keys) + + def test_huge_blob_is_scanned_only_at_the_edges(self): + blob = bytearray(204800) + blob[0x100:0x120] = bytes(range(32)) + blob[-0x800:-0x800 + 32] = bytes(range(32, 64)) + blob[0x20000:0x20020] = b'\xf0' * 32 + variants = hwkey_probe.raw_variants('huge', bytes(blob)) + self.assertLess(len(variants), 0x10000 + 0x1000 + 10) + keys = [key for _, key in variants] + self.assertIn(bytes(blob[:32]), keys) + self.assertIn(bytes(blob[-32:]), keys) + self.assertIn(bytes(blob[0x100:0x120]), keys) # near the start + self.assertIn(bytes(blob[-0x800:-0x800 + 32]), keys) # near the end + self.assertNotIn(b'\xf0' * 32, keys) # middle skipped + + def test_trailing_nul_stripped_form_is_added(self): + blob = b'Laptop\0SERIAL\0' + self.assertIn(b'Laptop\0SERIAL', self.keys('dmi', blob)) + + def test_empty_blob_yields_nothing(self): + self.assertEqual(hwkey_probe.raw_variants('empty', b''), []) + + +class LoadRawBlobsTests(unittest.TestCase): + def test_raw_hex_is_decoded(self): + blobs = hwkey_probe.load_raw_blobs(['0011aaff', '00 11 aa ff'], []) + self.assertEqual([label for label, _ in blobs], + ['raw-hex[0]', 'raw-hex[1]']) + self.assertEqual(blobs[0][1], b'\x00\x11\xaa\xff') + + def test_invalid_hex_is_warned_about_and_skipped(self): + err = io.StringIO() + with redirect_stderr(err): + blobs = hwkey_probe.load_raw_blobs(['xyz', 'abc'], []) + self.assertEqual(blobs, []) + self.assertIn('not valid hex', err.getvalue()) + + def test_name_equals_hex_dump_file(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'windows-pairing.txt' + path.write_text( + 'PairingData=deadbeef\n' + 'PairingContext = 00010203 (type 3)\n' + 'not a value line\n', + encoding='utf-8') + blobs = hwkey_probe.load_raw_blobs([], [path]) + labels = [label for label, _ in blobs] + self.assertIn('PairingData=windows-pairing.txt', labels) + self.assertIn('PairingContext=windows-pairing.txt', labels) + self.assertIn(b'\xde\xad\xbe\xef', + [blob for label, blob in blobs if 'PairingData' in label]) + + def test_guid_text_is_accepted_as_ascii_and_utf16(self): + guid = '0123abcd-4567-89ef-0123-456789abcdef' + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'windows-pairing.txt' + path.write_text('MachineGuid=%s\n' % guid, encoding='utf-8') + blobs = hwkey_probe.load_raw_blobs([], [path]) + blobs = dict(blobs) + self.assertEqual(blobs['MachineGuid=windows-pairing.txt ascii'], + guid.encode()) + self.assertEqual(blobs['MachineGuid=windows-pairing.txt utf16le'], + guid.encode('utf-16le')) + + def test_plain_string_value_is_accepted_as_ascii_and_utf16(self): + # scan_hive decodes REG_SZ, so a device path arrives as plain text. + device = 'USB\\VID_06CB&PID_00BB\\0123456789AB' + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'windows-pairing.txt' + path.write_text('Synaptics_01=%s (type 1)\n' % device, + encoding='utf-8') + blobs = hwkey_probe.load_raw_blobs([], [path]) + blobs = dict(blobs) + self.assertEqual(blobs['Synaptics_01=windows-pairing.txt ascii'], + device.encode()) + self.assertEqual(blobs['Synaptics_01=windows-pairing.txt utf16le'], + device.encode('utf-16le')) + + def test_overlong_string_value_is_not_a_candidate(self): + prose = 'x' * (hwkey_probe.TEXT_CANDIDATE_MAX + 1) + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'windows-pairing.txt' + path.write_text('Description=%s\n' % prose, encoding='utf-8') + blobs = hwkey_probe.load_raw_blobs([], [path]) + # No ascii/utf16 pairs for the prose; only the whole-file fallback. + self.assertEqual([label for label, _ in blobs], + ['raw-file[windows-pairing.txt]']) + + def test_binary_file_is_read_whole(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'pairing.bin' + path.write_bytes(b'\x00\xff\xfe\x80') + blobs = hwkey_probe.load_raw_blobs([], [path]) + self.assertEqual(blobs, [('raw-file[pairing.bin]', b'\x00\xff\xfe\x80')]) + + def test_text_file_without_hex_pairs_is_read_whole(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'notes.txt' + path.write_text('nothing useful here\n', encoding='utf-8') + blobs = hwkey_probe.load_raw_blobs([], [path]) + self.assertEqual(blobs, + [('raw-file[notes.txt]', b'nothing useful here\n')]) + + def test_missing_file_is_warned_about(self): + err = io.StringIO() + with redirect_stderr(err): + blobs = hwkey_probe.load_raw_blobs( + [], [Path('/nonexistent/pairing.bin')]) + self.assertEqual(blobs, []) + self.assertIn('cannot read', err.getvalue()) + + +class BuildCandidatesTests(unittest.TestCase): + DMI = {'product_name': 'Laptop', 'product_serial': 'SERIAL'} + + def build(self, **kwargs): + return hwkey_probe.build_candidates( + dict(self.DMI), [], kwargs.get('explicit', []), + kwargs.get('scanned', []), kwargs.get('raw', [])) + + def test_default_hw_key_layout_is_a_candidate(self): + keys = [key for _, key in self.build()] + self.assertIn(b'Laptop\0SERIAL\0', keys) + + def test_raw_blob_is_a_candidate(self): + blob = bytes(range(48)) + keys = [key for _, key in self.build(raw=[('PairingData', blob)])] + self.assertIn(blob, keys) + self.assertIn(blob[8:40], keys) + + def test_raw_blob_combinations_are_bounded(self): + blob = bytes(range(64)) + candidates = self.build(raw=[('PairingData', blob)]) + self.assertLess(len(candidates), 5000) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_scan_hive.py b/tests/test_scan_hive.py new file mode 100644 index 0000000..ce55315 --- /dev/null +++ b/tests/test_scan_hive.py @@ -0,0 +1,301 @@ +import importlib.util +import io +import struct +import tempfile +import unittest +from contextlib import redirect_stderr, redirect_stdout +from pathlib import Path + +TOOLS = Path(__file__).resolve().parent.parent / 'tools' + + +def load_scan_hive(): + spec = importlib.util.spec_from_file_location( + 'scan_hive', TOOLS / 'scan_hive.py') + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +scan_hive = load_scan_hive() + +REG_SZ = 1 +REG_BINARY = 3 +REG_DWORD = 4 + + +def align8(size): + return (size + 7) & ~7 + + +class HiveBuilder: + """Minimal but structurally valid regf/hbin writer for tests. + + Offsets in the registry are relative to the start of the hive-bins area + (i.e. 0x1000 in the file), which is what ``scan_hive`` assumes. + """ + + def __init__(self): + self.bins = bytearray() + self.cells = [] + + def _alloc(self, payload, allocated=True): + size = align8(len(payload) + 4) + offset = 0x20 + len(self.bins) + sign = -size if allocated else size + self.bins += struct.pack(' three db segments + hive = HiveBuilder() + hive.add_big_value('ProductPolicy', payload) + hive.add_value('MachineGuid', REG_SZ, + GUID.encode('utf-16le') + b'\0\0') + data = hive.build() + values = scan_hive.find_values(data, 'ProductPolicy') + self.assertEqual(values[0][1], payload) + self.assertEqual(scan_hive.find_values(data, 'MachineGuid')[0][1], + GUID) + + def test_big_data_value_not_confused_with_db_header(self): + hive = HiveBuilder() + hive.add_value('SmallBinary', REG_BINARY, b'db\x00\x00\0\0\0\0\0\0') + values = scan_hive.find_values(hive.build(), 'SmallBinary') + self.assertEqual(values[0][1], b'db\x00\x00\0\0\0\0\0\0') + + def test_list_value_names_matches_regex(self): + names = scan_hive.list_value_names(guid_hive(), '(?i)value') + self.assertEqual([name for name, _ in names], + ['BinaryValue', 'DwordValue']) + shapes = dict(names) + self.assertIn((REG_BINARY, 40), shapes['BinaryValue']) + + def test_list_value_names_no_match(self): + self.assertEqual(scan_hive.list_value_names(guid_hive(), 'zzz'), []) + + def test_cli_list_names(self): + with tempfile.TemporaryDirectory() as tmp: + hive = Path(tmp) / 'SYSTEM' + hive.write_bytes(guid_hive()) + buf = io.StringIO() + with redirect_stdout(buf): + rc = scan_hive.main([str(hive), '--list-names', '(?i)machine']) + self.assertEqual(rc, 0) + self.assertIn('MachineGuid', buf.getvalue()) + self.assertIn('MachineGuidBackup', buf.getvalue()) + + def test_cli_list_names_no_match_exits_1(self): + with tempfile.TemporaryDirectory() as tmp: + hive = Path(tmp) / 'SYSTEM' + hive.write_bytes(guid_hive()) + with redirect_stdout(io.StringIO()), \ + redirect_stderr(io.StringIO()): + self.assertEqual( + scan_hive.main([str(hive), '--list-names', 'zzz']), 1) + + def test_cli_writes_value_file(self): + with tempfile.TemporaryDirectory() as tmp: + hive = Path(tmp) / 'SOFTWARE' + hive.write_bytes(guid_hive()) + out = Path(tmp) / 'guid.txt' + buf = io.StringIO() + with redirect_stdout(buf): + rc = scan_hive.main( + [str(hive), '--name', 'MachineGuid', '--out', str(out)]) + self.assertEqual(rc, 0) + self.assertIn(GUID, buf.getvalue()) + self.assertIn('MachineGuid=' + GUID, out.read_text()) + + def test_cli_accepts_repeated_names(self): + with tempfile.TemporaryDirectory() as tmp: + hive = Path(tmp) / 'SYSTEM' + hive.write_bytes(guid_hive()) + out = Path(tmp) / 'values.txt' + with redirect_stdout(io.StringIO()): + rc = scan_hive.main( + [str(hive), '--name', 'MachineGuid', + '--name', 'BinaryValue', + '--name', 'Missing', '--out', str(out)]) + self.assertEqual(rc, 0) + text = out.read_text() + self.assertIn('MachineGuid=' + GUID, text) + self.assertIn('BinaryValue=' + bytes(range(40)).hex(), text) + self.assertNotIn('Missing=', text) + + def test_cli_missing_value_exits_1(self): + with tempfile.TemporaryDirectory() as tmp: + hive = Path(tmp) / 'SOFTWARE' + hive.write_bytes(guid_hive()) + with redirect_stdout(io.StringIO()), \ + redirect_stderr(io.StringIO()): + self.assertEqual( + scan_hive.main([str(hive), '--name', 'Missing']), 1) + + def test_cli_unreadable_file_exits_2(self): + with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): + self.assertEqual( + scan_hive.main(['/nonexistent/SOFTWARE', '--name', + 'MachineGuid']), 2) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/checkpair.py b/tools/checkpair.py new file mode 100644 index 0000000..f41b491 --- /dev/null +++ b/tools/checkpair.py @@ -0,0 +1,365 @@ +#!/usr/bin/env python3 +"""Read-only: is this 06cb:00bb sensor already paired to THIS machine? + +Background +---------- +The sensor keeps exactly one ECDH/TLS pairing. Its cert-store (flash +partition 1) holds the private key, encrypted with a key derived from this +host's DMI strings (``product_name`` + ``product_serial``) -- see +``validitysensor/tls.py``. ``handle_priv()`` verifies that blob with an HMAC +before it is used. + +That gives a definitive, **non-destructive** test: + +* HMAC verifies -> the pairing already belongs to this machine. No wipe is + needed; just install the driver and enroll. +* HMAC mismatch -> the pairing belongs to another host (Windows Hello). The + only way to Linux support is the re-provisioning in ``flash00bb.py``. +* no block 0x0004 -> the sensor is unprovisioned (clean slate); provision it. + +Results: ``ALREADY_PAIRED`` / ``LOCAL_KEY_OK`` (paired), +``PAIRED_TO_OTHER_HOST``, ``NO_PAIRING``, ``CERTSTORE_CORRUPT``, ``CERTSTORE_UNREADABLE``, +``TLS_REJECTED``, ``PARSE_ERROR``. Exit code 0 only for the first two. + +What this sends (hardware mode) +------------------------------- +1. the standard init sequence (``01``, ``19``, ``43 02``, init blob) -- the + same bytes the python-validity daemon sends on every start; +2. ``40 01`` reads of flash partition 1 (cert-store), twice: it is saved and + analysed; +3. a TLS 1.2 handshake (``44`` client hello / key exchange / finished) and, if + it succeeds, one encrypted ``3e`` (flash info) + ``75`` (identity) read; +4. ``05 02 00`` reboot, the normal daemon exit path. + +Nothing is written, erased or re-paired. No ``4f``, no ``50``, no reset blob. + +Offline mode +------------ +``--certstore PATH`` skips the USB device entirely and analyses a previously +saved cert-store backup (e.g. the one written by ``probe00bb.py``). Useful as +a dry run before touching hardware, and it needs no sensor access -- only +read permission on the file (root, since the backup is 0600). + +Run: + sudo python3 tools/checkpair.py + sudo python3 tools/checkpair.py --certstore backups/00bb-certstore-*.bin +""" + +import argparse +import hashlib +import json +import os +import sys +import time +from pathlib import Path +from struct import unpack + +REPO = Path(__file__).resolve().parent.parent +PROJECT = REPO +sys.path.insert(0, str(REPO)) + +from validitysensor import blobs # noqa: E402 +from validitysensor.tls import tls # noqa: E402 + +VID, PID = 0x06CB, 0x00BB + + +def status_of(rsp: bytes) -> int: + return unpack('= 2 else -1 + + +def send(label: str, data: bytes) -> bytes: + from validitysensor.usb import usb + + rsp = usb.cmd(data) + print(' %-24s -> status 0x%04x (%d bytes)' + % (label, status_of(rsp), len(rsp))) + return rsp + + +def init_sequence(): + """Exactly what the daemon sends on start (usb.send_init()).""" + if status_of(send('rom info (01)', b'\x01')) != 0: + raise RuntimeError('ROM info rejected') + if status_of(send('19', b'\x19')) != 0: + raise RuntimeError('19 rejected') + fw = send('fwext info (43 02)', b'\x43\x02') + if status_of(send('init blob', blobs.init_hardcoded())) != 0: + raise RuntimeError('init blob rejected') + if status_of(fw) != 0: + send('init blob (clean slate)', blobs.init_hardcoded_clean_slate()) + return fw + + +def analyze_certstore(certstore: bytes, report: dict) -> bool: + """Print the block table and verify the private-key HMAC. + + Returns True if the blob is bound to this machine's DMI key. Sets + ``report['result']`` on failure, returns False. + """ + report['certstore_sha256'] = hashlib.sha256(certstore).hexdigest() + print(' %d bytes, sha256 %s' + % (len(certstore), report['certstore_sha256'])) + + walk = certstore + seen = set() + while len(walk) >= 0x24: + hdr, walk = walk[:4], walk[4:] + hs, walk = walk[:0x20], walk[0x20:] + block_id, size = unpack(' Path | None: + path = outdir / name + try: + path.write_text(json.dumps(report, indent=2) + '\n') + os.chmod(path, 0o600) + except OSError as error: + print('\ncould not write report %s: %s' % (path, error), + file=sys.stderr) + return None + print('\nreport: %s' % path) + return path + + +def run_offline(certstore_path: Path) -> int: + report = {'mode': 'offline', 'certstore': str(certstore_path), + 'stamp': time.strftime('%Y%m%d-%H%M%S'), + 'result': None, 'certstore_sha256': None, 'blocks': []} + + print('== host key derivation') + try: + apply_host_key() + except RuntimeError as error: + print('ERROR: %s' % error, file=sys.stderr) + return 2 + + print('== cert-store %s' % certstore_path) + try: + certstore = certstore_path.read_bytes() + except OSError as error: + print('ERROR: cannot read %s: %s' % (certstore_path, error), + file=sys.stderr) + print(' the probe wrote it 0600, so run this with sudo.', + file=sys.stderr) + return 2 + + outdir = PROJECT / 'backups' + outdir.mkdir(parents=True, exist_ok=True) + + print('== blocks + HMAC') + paired = analyze_certstore(certstore, report) + if paired: + print() + print('RESULT: LOCAL_KEY_OK') + print(' The private-key blob in this backup belongs to THIS machine.') + print(' If the live sensor still holds the same cert-store, no wipe') + print(' is needed. Confirm with: sudo python3 tools/checkpair.py') + write_report(outdir, '00bb-checkpair-offline-%s.json' % report['stamp'], + report) + return 0 if paired else 1 + + +def run_hardware() -> int: + import usb.core as ucore + + from validitysensor.flash import get_flash_info, read_flash + from validitysensor.sensor import (RebootException, identify_sensor, + reboot) + from validitysensor.usb import usb + + dev = ucore.find(idVendor=VID, idProduct=PID) + if dev is None: + print('ERROR: 06cb:00bb not found on the USB bus.', file=sys.stderr) + return 2 + + serial = (dev.serial_number or 'unknown').replace('/', '_') + stamp = time.strftime('%Y%m%d-%H%M%S') + outdir = PROJECT / 'backups' + outdir.mkdir(parents=True, exist_ok=True) + os.chmod(outdir, 0o700) + + print('== 0. open 06cb:00bb') + print(' serial %s, bus %d address %d' + % (dev.serial_number, dev.bus, dev.address)) + try: + apply_host_key() + except RuntimeError as error: + print('ERROR: %s' % error, file=sys.stderr) + return 2 + + report = {'mode': 'hardware', 'serial': dev.serial_number, 'stamp': stamp, + 'result': None, 'certstore_sha256': None, 'blocks': []} + tls.reset() + usb.open_dev(dev) + + try: + print('== 1. init sequence (same as the daemon)') + fw = init_sequence() + report['fwext_reported'] = status_of(fw) == 0 + + print('== 2. read cert-store (partition 1)') + try: + certstore = read_flash(1, 0, 0x1000) + except Exception as error: # noqa: BLE001 + report['result'] = 'CERTSTORE_UNREADABLE' + print() + print('RESULT: CERTSTORE_UNREADABLE: %r' % error) + print(' The sensor has no readable partition 1 (unprovisioned /') + print(' clean slate). Provision it with:') + print(' sudo python3 tools/flash00bb.py --phase all') + return 1 + path = outdir / ('00bb-certstore-%s-%s.bin' % (serial, stamp)) + path.write_bytes(certstore) + os.chmod(path, 0o600) + print(' saved %s' % path) + + print('== 3. inspect blocks and verify the HMAC against this host') + if not analyze_certstore(certstore, report): + return 1 + + print('== 4. TLS 1.2 handshake with the existing pairing') + try: + tls.open() + except Exception as error: # noqa: BLE001 + report['result'] = 'TLS_REJECTED' + print() + print('RESULT: TLS_REJECTED: %r' % error) + print(' The local key verified, but the sensor refused the session.') + return 1 + + print(' handshake OK') + + print('== 5. encrypted reads (proves the channel works)') + info = get_flash_info() + print(' flash %s, %d bytes, %d partition(s)' + % (info.ic.name, info.ic.size, len(info.partitions))) + ident = identify_sensor() + print(' sensor type=0x%04x name=%r' % (ident.type, ident.name)) + + report['result'] = 'ALREADY_PAIRED' + print() + print('RESULT: ALREADY_PAIRED') + print(' This sensor is already paired to this machine.') + print(' Do NOT wipe it. Install the driver and enroll:') + print(' sudo ./install-00bb.sh') + print(' sudo -u "$USER" fprintd-enroll -f right-index-finger "$USER"') + finally: + print('== 6. reboot sensor (normal daemon exit path)') + try: + reboot() + except RebootException: + pass + except Exception as error: # noqa: BLE001 + print(' reboot failed (usually harmless): %s' % error) + usb.dev = None + + write_report(outdir, '00bb-checkpair-%s-%s.json' % (serial, stamp), + report) + + return 0 if report['result'] == 'ALREADY_PAIRED' else 1 + + +def main(argv=None) -> int: + parser = argparse.ArgumentParser( + description='Read-only check: is the 06cb:00bb sensor paired to ' + 'this machine?') + parser.add_argument('--certstore', metavar='PATH', type=Path, + help='analyse a saved cert-store backup offline ' + '(no USB access)') + args = parser.parse_args(argv) + + if args.certstore is not None: + return run_offline(args.certstore) + + if os.geteuid() != 0: + print('ERROR: run as root (needed for the USB node and for ' + '/sys/class/dmi/id/product_serial).', file=sys.stderr) + return 2 + + return run_hardware() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/dump_windows_pairing.sh b/tools/dump_windows_pairing.sh new file mode 100755 index 0000000..6cf9519 --- /dev/null +++ b/tools/dump_windows_pairing.sh @@ -0,0 +1,233 @@ +#!/usr/bin/env bash +# Read-only: dump the Windows registry values that may hold the pairing key. +# +# The Synaptics UMDF driver keeps its per-device state in the device registry +# key (HKLM\SYSTEM\...\Enum\USB\VID_06CB&PID_00BB\...\Device Parameters): +# values "PairingData", "PairingContext" and "UnpairingContext". The firmware +# extension id ("fwextId") lives under HKLM\SOFTWARE\Validity. +# +# One of those values may be the 32-byte host key (GWK) that Windows used when +# it paired this sensor. If it is, tools/hwkey_probe.py can try it and the +# existing Windows Hello pairing can be reused without a wipe. +# +# This script only mounts the Windows volumes read-only and reads the hive +# files with tools/scan_hive.py. It never writes to them. +# +# Usage: +# sudo bash tools/dump_windows_pairing.sh [--name NAME]... [OUTFILE] +# +# --name NAME adds a registry value from the survey (a name we did not guess) +# to the dump; repeatable. Its size and, when printable, its text are shown, +# and its bytes are handed to hwkey_probe as another candidate. +# +# Default OUTFILE: /tmp/opencode/windows-pairing.txt +# Feed it to: +# sudo python3 tools/hwkey_probe.py --raw-file /tmp/opencode/windows-pairing.txt + +set -uo pipefail + +script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +scan="$script_dir/scan_hive.py" + +die() { echo "ERROR: $*" >&2; exit 2; } + +OUT="" +EXTRA=() +while [ "$#" -gt 0 ]; do + case "$1" in + --name) + [ "$#" -ge 2 ] || die "--name needs a value" + EXTRA+=("$2"); shift 2 ;; + --name=*) + EXTRA+=("${1#*=}"); shift ;; + -h|--help) + sed -n '2,25p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; + -*) + die "unknown option $1 (try --help)" ;; + *) + [ -z "$OUT" ] || die "only one OUTFILE may be given" + OUT="$1"; shift ;; + esac +done + +OUT="${OUT:-/tmp/opencode/windows-pairing.txt}" +NAMES="${OUT%.txt}-names.txt" + +VALUES=(PairingData PairingContext UnpairingContext fwextId MachineGuid) +# Extras may repeat a default name; keep the list unique so the hive is +# scanned once per value. +for extra in "${EXTRA[@]:-}"; do + [ -n "$extra" ] || continue + skip="" + for known in "${VALUES[@]}"; do + [ "$extra" = "$known" ] && skip=1 + done + [ -n "$skip" ] || VALUES+=("$extra") +done +# Name survey: catches a key stored under a name we did not guess. +SURVEY_RE='(?i)pair|gwk|fwext|validity|synaptics|hostkey|host_key' + +# stdin: "Name=hex" lines from scan_hive.py; prints size and printable text. +# scan_hive writes REG_SZ/REG_EXPAND_SZ values as decoded text and everything +# else as hex, so both forms are handled here. +# The program is kept in a variable because a heredoc would steal stdin from +# the pipe that feeds this function. +SHOW_VALUES_PY=$(cat <<'PY' +import sys + +rows = [] # (name, rendered, count), first-seen order +index = {} # rendered line -> position in rows + + +def render(name, value): + try: + raw = bytes.fromhex(value.strip()) + except ValueError: + # A decoded string value, e.g. MachineGuid or a device path. + return ' %-16s %4d chars "%s"' % (name, len(value), value), 'chars:"%s"' % value + text = '' + for encoding in ('utf-16-le', 'ascii'): + try: + decoded = raw.decode(encoding) + except UnicodeDecodeError: + continue + if decoded and all(32 <= ord(ch) < 127 for ch in decoded): + text = decoded + break + suffix = ' "%s"' % text if text else '' + return (' %-16s %4d bytes%s' % (name, len(raw), suffix), + 'bytes:"%s"' % raw.hex()) + + +for line in sys.stdin: + line = line.strip() + if not line or '=' not in line: + continue + name, value = line.split('=', 1) + rendered, key = render(name.strip(), value) + position = index.get(key) + if position is None: + index[key] = len(rows) + rows.append([rendered, 1]) + else: + rows[position][1] += 1 + +# One REG_SZ can appear in hundreds of keys ("HID\VID_06CB&PID_*" does); +# show it once with a multiplicity instead of flooding the terminal. +for rendered, count in rows: + if count > 1: + rendered += ' (x%d)' % count + print(rendered) +PY +) + +show_values() { python3 -c "$SHOW_VALUES_PY"; } + +[ "$(id -u)" -eq 0 ] || die "run with sudo" +command -v lsblk >/dev/null 2>&1 || die "lsblk not found (util-linux)" +command -v python3 >/dev/null 2>&1 || die "python3 not found" +[ -f "$scan" ] || die "missing $scan" + +mkdir -p "$(dirname "$OUT")" || die "cannot create $(dirname "$OUT")" +mkdir -p /tmp/opencode || die "cannot create /tmp/opencode" + +work=$(mktemp -d /tmp/opencode/winhive.XXXXXX) || die "mktemp failed" + +declare -a mounts=() +cleanup() { + for m in "${mounts[@]:-}"; do + [ -n "$m" ] && umount "$m" 2>/dev/null + done + rm -rf "$work" +} +trap cleanup EXIT +trap 'exit 130' INT TERM + +mapfile -t parts < <(lsblk -rno NAME,FSTYPE | awk '$2 ~ /^ntfs/ {print "/dev/" $1}') +if [ "${#parts[@]}" -eq 0 ]; then + die "no NTFS partition found (BitLocker-encrypted volume?)" +fi + +: > "$OUT" +: > "$NAMES" +found=0 + +for dev in "${parts[@]}"; do + mnt="$work/$(basename "$dev")" + mkdir -p "$mnt" + + if ! mount -t ntfs3 -o ro "$dev" "$mnt" 2>/dev/null; then + if ! mount -t ntfs-3g -o ro "$dev" "$mnt" 2>/dev/null; then + echo "-- $dev: cannot mount read-only (BitLocker / hibernated / dirty)" + rmdir "$mnt" 2>/dev/null + continue + fi + fi + mounts+=("$mnt") + + hives=$(find "$mnt" -maxdepth 5 \ + \( -ipath '*/Windows/System32/config/SYSTEM' \ + -o -ipath '*/Windows/System32/config/SOFTWARE' \) \ + ! -ipath '*/Windows.old/*' \ + -print 2>/dev/null) + if [ -z "$hives" ]; then + echo "-- $dev: mounted, no Windows registry hives" + continue + fi + + while IFS= read -r hive; do + [ -n "$hive" ] || continue + echo "-- $dev: $hive" + # One pass per hive: scan_hive accepts repeated --name. + args=() + for name in "${VALUES[@]}"; do + args+=(--name "$name") + done + before=$(wc -l < "$OUT") + if python3 "$scan" "$hive" "${args[@]}" --out "$OUT" >/dev/null 2>&1; then + tail -n +"$((before + 1))" "$OUT" | show_values + found=1 + fi + + survey=$(mktemp "$work/names.XXXXXX") || continue + if python3 "$scan" "$hive" --list-names "$SURVEY_RE" > "$survey" 2>/dev/null; then + echo " name survey:" + sed "s/^/ /" "$survey" + echo "-- $hive" >> "$NAMES" + cat "$survey" >> "$NAMES" + fi + rm -f "$survey" + done <<< "$hives" +done + +if [ "$found" -eq 0 ]; then + rm -f "$OUT" + echo + echo "No pairing-related registry value could be read." + echo "Options:" + echo " * Windows may be hibernated (fast startup) -> boot it, shut down" + echo " fully, then retry." + echo " * if the volume is BitLocker-encrypted, unlock it in Windows first." + echo " * from Windows, export the values with (cmd as admin):" + echo " reg query \"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_06CB&PID_00BB\" /s /v PairingData" + echo " reg query \"HKLM\\SOFTWARE\\Validity\" /s" + exit 1 +fi + +if [ -s "$NAMES" ]; then + chmod 600 "$NAMES" +else + rm -f "$NAMES" +fi + +chmod 600 "$OUT" +echo +echo "Wrote $OUT" +[ -f "$NAMES" ] && echo "Name survey: $NAMES" +echo "Now run:" +echo " sudo python3 $script_dir/hwkey_probe.py --raw-file $OUT" +echo +echo "If no match is found, look at the name survey: a driver value with a" +echo "different name might hold the key. Re-run with that name to add it:" +echo " sudo bash $script_dir/dump_windows_pairing.sh --name $OUT" +echo " sudo python3 $script_dir/hwkey_probe.py --raw-file $OUT" diff --git a/tools/find_windows_guid.sh b/tools/find_windows_guid.sh new file mode 100755 index 0000000..bdd0e67 --- /dev/null +++ b/tools/find_windows_guid.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# Read-only: pull the Windows MachineGuid out of a Windows installation. +# +# Mounts every NTFS partition read-only, locates the Windows SOFTWARE registry +# hive, and reads HKLM\SOFTWARE\Microsoft\Cryptography\MachineGuid directly +# with tools/scan_hive.py (no hivex / chntpw needed). Nothing is written to +# the Windows volumes; the output is a small text file that +# tools/hwkey_probe.py can consume with --guid-file. +# +# Usage: +# sudo bash tools/find_windows_guid.sh [OUTFILE] +# +# Default OUTFILE: /tmp/opencode/windows-machine-guid.reg + +set -uo pipefail + +OUT="${1:-/tmp/opencode/windows-machine-guid.reg}" +script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +scan="$script_dir/scan_hive.py" + +die() { echo "ERROR: $*" >&2; exit 2; } + +[ "$(id -u)" -eq 0 ] || die "run with sudo" +command -v lsblk >/dev/null 2>&1 || die "lsblk not found (util-linux)" +command -v python3 >/dev/null 2>&1 || die "python3 not found" +[ -f "$scan" ] || die "missing $scan" + +mkdir -p "$(dirname "$OUT")" || die "cannot create $(dirname "$OUT")" +mkdir -p /tmp/opencode || die "cannot create /tmp/opencode" + +work=$(mktemp -d /tmp/opencode/winhive.XXXXXX) || die "mktemp failed" + +declare -a mounts=() +cleanup() { + for m in "${mounts[@]:-}"; do + [ -n "$m" ] && umount "$m" 2>/dev/null + done + rm -rf "$work" +} +trap cleanup EXIT INT TERM + +mapfile -t parts < <(lsblk -rno NAME,FSTYPE | awk '$2 ~ /^ntfs/ {print "/dev/" $1}') +if [ "${#parts[@]}" -eq 0 ]; then + die "no NTFS partition found (BitLocker-encrypted volume?)" +fi + +: > "$OUT" +found=0 + +for dev in "${parts[@]}"; do + mnt="$work/$(basename "$dev")" + mkdir -p "$mnt" + + if ! mount -t ntfs3 -o ro "$dev" "$mnt" 2>/dev/null; then + if ! mount -t ntfs-3g -o ro "$dev" "$mnt" 2>/dev/null; then + echo "-- $dev: cannot mount read-only (BitLocker / hibernated / dirty)" + rmdir "$mnt" 2>/dev/null + continue + fi + fi + mounts+=("$mnt") + + hive=$(find "$mnt" -maxdepth 5 \ + -ipath '*/Windows/System32/config/SOFTWARE' \ + -print -quit 2>/dev/null) + if [ -z "$hive" ]; then + echo "-- $dev: mounted, no Windows SOFTWARE hive" + continue + fi + echo "-- $dev: $hive" + + if python3 "$scan" "$hive" --name MachineGuid --out "$OUT"; then + found=1 + else + rc=$? + if [ "$rc" -eq 2 ]; then + echo " hive could not be parsed (unexpected format?)" + else + echo " no MachineGuid value in this hive" + fi + fi +done + +guids=$(sed -n 's/^MachineGuid=//p' "$OUT" 2>/dev/null | sort -u) +if [ -z "$guids" ]; then + rm -f "$OUT" + echo + echo "No MachineGuid could be read." + echo "Options:" + echo " * boot Windows once and run (cmd):" + echo " reg query HKLM\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid" + echo " then boot Linux and run:" + echo " sudo python3 $script_dir/hwkey_probe.py --machine-guid " + echo " * if the volume is BitLocker-encrypted, unlock it in Windows first" + echo " (or use 'dislocker', which needs the recovery key)." + exit 1 +fi + +chmod 600 "$OUT" +echo +echo "MachineGuid:" +echo "$guids" | sed 's/^/ /' +echo +echo "Wrote $OUT" +echo "Now run:" +echo " sudo python3 $script_dir/hwkey_probe.py --guid-file $OUT" diff --git a/tools/flash00bb.py b/tools/flash00bb.py new file mode 100755 index 0000000..d1a428a --- /dev/null +++ b/tools/flash00bb.py @@ -0,0 +1,735 @@ +#!/usr/bin/env python3 +"""Re-provision the Synaptics 06cb:00bb sensor for Linux (python-validity). + +This is the *destructive* half of the 06cb:00bb support, adapted from the +working recipe reported by Joentja on uunicorn/python-validity#283 (exact same +HP Spectre x360 13-ap0xxx, sensor type 0x969, hw id 0x88, W25Q80B flash). + +What it does in `--phase all` +------------------------------ +1. preflight : identify the sensor, back up the cleartext TLS cert-store + (partition 1), and make sure the HP xpfwext is cached. +2. wipe : send the 0x969-family reset blob (11973 bytes) + commit + reboot. + The chip comes back with **zero partitions**. +3. provision : re-create the five partitions (4f), do the ECDH pairing (50), + open TLS, erase the fresh partitions and write a cert-store + bound to *this* machine's DMI (product_name + product_serial). + This mirrors validitysensor.init_flash() exactly. + firmware : in a fresh TLS session, re-upload the HP xpfwext if the wipe + removed it (mirrors validitysensor.upload_fwext()). +4. verify : reboot, read the new cert-store back, and complete a TLS 1.2 + handshake against the sensor. + +Consequences +------------ +* The existing Windows Hello pairing on the sensor is destroyed. +* Fingerprint templates stored on the sensor (Windows Hello) are destroyed. +* Windows Hello must be re-enrolled from Windows afterwards (which would, in + turn, invalidate the Linux pairing again -- the sensor keeps one pairing). +* Nothing on your Linux disks is touched. + +Usage +----- + sudo python3 tools/flash00bb.py # safe preflight only + sudo python3 tools/flash00bb.py --phase wipe # DESTRUCTIVE, asks for serial + sudo python3 tools/flash00bb.py --phase provision # format+pair, then firmware + sudo python3 tools/flash00bb.py --phase firmware # recover a missing fwext + sudo python3 tools/flash00bb.py --phase verify + sudo python3 tools/flash00bb.py --phase all # everything, asks first + +Use --phase all unless you need to stop between steps. +""" + +import argparse +import hashlib +import os +import shutil +import subprocess +import sys +import time +from pathlib import Path +from struct import unpack + +REPO = Path(__file__).resolve().parent.parent +PROJECT = REPO +sys.path.insert(0, str(REPO)) + +import usb.core as ucore # noqa: E402 +from cryptography.hazmat.backends import default_backend # noqa: E402 +from cryptography.hazmat.primitives.asymmetric import ec # noqa: E402 + +from validitysensor import blobs # noqa: E402 + + +def blob(name: str) -> bytes: + """Return a firmware blob from validitysensor.blobs. + + blobs.py exposes ``init_hardcoded`` etc. as zero-argument thunks that + *replace the module attribute with the bytes* on first use, so the same + name is callable once and a plain bytes object afterwards. Reading the + module attribute directly (as this tool used to) works exactly once per + process, which broke the wipe phase after preflight had already run. + """ + value = getattr(blobs, name) + return value() if callable(value) else value +from validitysensor.flash import ( # noqa: E402 + call_cleanups, erase_flash, get_flash_info, get_fw_info, read_flash, write_flash, + write_flash_all, write_fw_signature, +) +from validitysensor.init_flash import ( # noqa: E402 + encrypt_key, flash_layout_hardcoded, partition_flash, partition_signature, +) +from validitysensor.init_data_dir import ( # noqa: E402 + PYTHON_VALIDITY_FIRMWARE_DIR, init_data_dir, +) +from validitysensor.sensor import ( # noqa: E402 + RebootException, RomInfo, identify_sensor, + read_hw_reg32, reboot, write_hw_reg32, +) +from validitysensor.tls import tls # noqa: E402 +from validitysensor.usb import usb # noqa: E402 + +VID, PID = 0x06CB, 0x00BB +FWTYPE = '6_07f_hp_mis_qm.xpfwext' +FW_CACHE = Path(PYTHON_VALIDITY_FIRMWARE_DIR) / FWTYPE +STAMP = time.strftime('%Y%m%d-%H%M%S') +BACKUP_DIR = PROJECT / 'backups' +LOG_PATH = BACKUP_DIR / ('00bb-flash-%s.log' % STAMP) + +_log_file = None + + +def log(msg: str = ''): + print(msg, flush=True) + if _log_file is not None: + _log_file.write(msg + '\n') + _log_file.flush() + + +def status_of(rsp: bytes) -> int: + return unpack('= 2 else -1 + + +class Abort(Exception): + pass + + +def systemctl(*args: str): + """Run ``systemctl ``; used for simple start/stop/mask calls.""" + subprocess.run(['systemctl', *args], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False) + + +COMPETING_UNITS = ('fprintd.service', 'open-fprintd.service', 'python3-validity.service') +_was_active = {} + + +def stop_competing_services(): + global _was_active + for unit in COMPETING_UNITS: + result = subprocess.run(['systemctl', 'is-active', '--quiet', unit], check=False) + _was_active[unit] = result.returncode == 0 + systemctl('stop', unit) + # Keep udev from restarting the daemon while this script owns the sensor. + systemctl('mask', '--runtime', 'python3-validity.service') + + +def restart_services(): + systemctl('unmask', '--runtime', 'python3-validity.service') + # Only bring back what was running before, and only if it is installed. + for unit in reversed(COMPETING_UNITS): + if not _was_active.get(unit): + continue + result = subprocess.run(['systemctl', 'start', unit], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + check=False) + if result.returncode != 0: + log(' note: could not restart %s' % unit) + + +def release_device(): + if usb.dev is not None: + try: + usb.dev.reset() + except Exception: # noqa: BLE001 + pass + usb.dev = None + + +def wait_for_device(timeout: float = 60.0): + log(' waiting for 06cb:00bb ...') + deadline = time.time() + timeout + last_error = None + while time.time() < deadline: + dev = ucore.find(idVendor=VID, idProduct=PID) + if dev is not None: + time.sleep(1.0) # let the USB stack settle after re-enumeration + try: + tls.reset() + usb.open_dev(dev) + log(' device back at bus %d address %d' + % (usb.usb_dev().bus, usb.usb_dev().address)) + return + except Exception as error: # noqa: BLE001 + last_error = error + time.sleep(0.5) + raise Abort('sensor did not come back within %ds (%s). ' + 'Unplug/replug power or reboot, then continue with the next phase.' + % (timeout, last_error)) + + +def send(label: str, data: bytes) -> bytes: + rsp = usb.cmd(data) + log(' %-22s -> status 0x%04x (%d bytes)' % (label, status_of(rsp), len(rsp))) + return rsp + + +def send_init_sequence(): + log(' sending 01 / 19 / 43 02 / init blob') + if status_of(send('rom info (01)', b'\x01')) != 0: + raise Abort('ROM info rejected') + send('19', b'\x19') + fw_rsp = send('fwext info (43 02)', b'\x43\x02') + init_status = status_of(send('init blob', blob('init_hardcoded'))) + if init_status != 0: + log(' init blob rejected -> trying clean-slate variant') + if status_of(send('init blob (clean)', blob('init_hardcoded_clean_slate'))) != 0: + raise Abort('both init blobs were rejected') + elif status_of(fw_rsp) != 0: + # Same rule as Usb.send_init(): 4302 reports b004 when the fwext + # partition is empty (zero-partition / freshly wiped sensor), and the + # clean-slate init blob must follow. + log(' no firmware reported (clean slate) -> sending clean-slate init blob') + if status_of(send('init blob (clean)', blob('init_hardcoded_clean_slate'))) != 0: + log(' WARNING: clean-slate init blob rejected; continuing') + return fw_rsp + + +def open_sensor(): + wait_for_device() + + +def assert_target_device(force: bool): + """Refuse to wipe anything that is not the known 0x969 HP sensor.""" + ident = identify_sensor() + log(' identity: type=0x%04x hw-id=0x%02x name=%r' + % (ident.type, ident.version, ident.name)) + matches = ident.type == 0x969 and 'FM-3439' in ident.name + if not matches: + msg = ('unexpected sensor identity (type 0x%04x, name %r); expected ' + 'type 0x969 "57K0 FM-3439-xxx". Refusing to wipe.' + % (ident.type, ident.name)) + if not force: + raise Abort(msg + ' Re-run with --force only if you are certain.') + log(' WARNING: ' + msg + ' (continuing because of --force)') + if ident.version != 0x88: + log(' note: hw id 0x%02x (the reference unit is 0x88)' % ident.version) + return ident + + +def backup_certstore(info): + ids = [p.id for p in info.partitions] + if 1 not in ids: + log(' partition 1 not present; nothing to back up') + return None + data = read_flash(1, 0, 0x1000) + BACKUP_DIR.mkdir(parents=True, exist_ok=True) + path = BACKUP_DIR / ('00bb-certstore-before-wipe-%s.bin' % STAMP) + path.write_bytes(data) + os.chmod(path, 0o600) + digest = hashlib.sha256(data).hexdigest() + log(' backup: %s (%d bytes, sha256 %s)' % (path, len(data), digest)) + log(' note: fingerprint templates in partition 4 cannot be read without ' + 'the old pairing; they are lost by design.') + return path + + +def extract_softpaq(archive, destination): + """Unpack an HP SoftPaq (CAB self-extractor) and install the xpfwext.""" + import tempfile + init_data_dir() + with tempfile.TemporaryDirectory(prefix='00bb-fw-') as tmp: + if shutil.which('cabextract'): + cmd = ['cabextract', '-q', '-d', tmp, archive] + elif shutil.which('innoextract'): + cmd = ['innoextract', '--output-dir', tmp, '--include', FWTYPE, + '--collisions', 'overwrite', archive] + else: + raise Abort('need cabextract or innoextract: sudo apt install cabextract') + log(' extracting: %s' % ' '.join(cmd)) + result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + found = None + for root, _dirs, files in os.walk(tmp): + if FWTYPE in files: + found = Path(root) / FWTYPE + break + if result.returncode != 0 or found is None: + raise Abort('could not extract %s from %s' % (FWTYPE, archive)) + shutil.copyfile(found, destination) + os.chmod(destination, 0o600) + + +def _target_dev_type(): + from validitysensor.usb import SupportedDevices + return SupportedDevices.DEV_BB + + +def download_softpaq(destination): + """Fetch and unpack the pinned HP SoftPaq without touching the sensor. + + Deliberately does not shell out to ``validity-sensors-firmware``: that + script re-enumerates the USB bus to detect the device and would collide + with the handle this tool already owns. + """ + import importlib.machinery + import importlib.util + import tempfile + + from validitysensor.firmware_tables import FIRMWARE_URIS + + dev_type = _target_dev_type() + uri = FIRMWARE_URIS[dev_type]['driver'] + sha512 = FIRMWARE_URIS[dev_type]['sha512'] + log(' downloading %s' % uri) + log(' expected sha512 %s' % sha512) + + spec = importlib.util.spec_from_file_location( + 'validity_sensors_firmware', REPO / 'bin' / 'validity-sensors-firmware', + loader=importlib.machinery.SourceFileLoader( + 'validity_sensors_firmware', str(REPO / 'bin' / 'validity-sensors-firmware'))) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + + with tempfile.TemporaryDirectory(prefix='00bb-fw-') as tmp: + path = module.download_and_extract_fw(dev_type, tmp, fwuri=uri) + shutil.copyfile(path, destination) + os.chmod(destination, 0o600) + + +def prepare_firmware(softpaq=None): + """Make sure the xpfwext is cached *before* we wipe anything.""" + init_data_dir() + if softpaq: + source = Path(softpaq).expanduser() + if not source.is_file(): + raise Abort('--softpaq %s does not exist' % source) + extract_softpaq(str(source), FW_CACHE) + + if FW_CACHE.is_file() and FW_CACHE.stat().st_size > 0: + log(' firmware cached: %s (%d bytes)' % (FW_CACHE, FW_CACHE.stat().st_size)) + return FW_CACHE + + log(' firmware not cached: %s' % FW_CACHE) + if not (shutil.which('cabextract') or shutil.which('innoextract')): + raise Abort('need cabextract or innoextract to unpack the HP SoftPaq.\n' + ' sudo apt install cabextract\n' + ' then re-run. (Nothing was wiped.)') + try: + download_softpaq(FW_CACHE) + except Abort: + raise + except Exception as error: # noqa: BLE001 + raise Abort('could not obtain %s from the pinned HP SoftPaq (%r).\n' + ' Download the HP fingerprint SoftPaq that contains ' + '%s\n (SP138431, or SP106161 as used by Joentja) and ' + 'pass\n --softpaq /path/to/spXXXXXX.exe (or --firmware ' + '/path/to/%s).\n Nothing was wiped.' + % (FWTYPE, error, FWTYPE, FWTYPE)) + if not FW_CACHE.is_file(): + raise Abort('firmware extraction produced no %s. Nothing was wiped.' % FWTYPE) + log(' firmware ready: %s (%d bytes)' % (FW_CACHE, FW_CACHE.stat().st_size)) + return FW_CACHE + + +def phase_preflight(args): + log('== PREFLIGHT (read-only; no flash writes)') + open_sensor() + try: + send_init_sequence() + info = get_flash_info() + log(' flash: %s, %d bytes, %d partition(s)' + % (info.ic.name, info.ic.size, len(info.partitions))) + for p in info.partitions: + log(' %r' % p) + assert_target_device(args.force) + backup_certstore(info) + rom = RomInfo.get() + log(' ROM: %r' % rom) + fwi = get_fw_info(2) + if fwi is None: + log(' fwext: not reported (will be uploaded during provisioning)') + else: + log(' fwext: %d.%d buildtime %d, %d modules' + % (fwi.major, fwi.minor, fwi.buildtime, len(fwi.modules))) + prepare_firmware(args.softpaq) + finally: + try: + reboot() + except RebootException: + pass + except Exception: # noqa: BLE001 + pass + release_device() + log(' preflight OK. Nothing was modified.') + + +def phase_wipe(args): + log('== WIPE (DESTRUCTIVE: writes the 0x969 reset blob, erases the flash layout)') + open_sensor() + already_blank = False + try: + send_init_sequence() + info = get_flash_info() + if len(info.partitions) == 0: + log(' flash already has zero partitions; nothing to wipe.') + already_blank = True + else: + ident = assert_target_device(args.force) + backup_certstore(info) + prepare_firmware(args.softpaq) + if not args.yes: + expected = usb.usb_dev().serial_number or 'ERASE' + print() + print('This will destroy the Windows Hello pairing and every') + print('fingerprint template stored on the sensor 06cb:00bb.') + answer = input('Type the sensor serial (%s) to continue: ' + % expected).strip() + if answer != expected: + raise Abort('confirmation did not match; nothing was wiped.') + + rom = RomInfo.get() + log(' ROM before wipe: %r' % rom) + log(' sending 0x969 reset blob (%d bytes) ...' % len(blob('reset_blob'))) + status = status_of(usb.cmd(blob('reset_blob'))) + log(' reset blob -> status 0x%04x' % status) + if status != 0: + raise Abort('the sensor rejected the reset blob (status 0x%04x).\n' + ' Nothing was changed. Do NOT continue with other ' + 'reset blobs.' % status) + log(' sending commit (10 00 * 0x61) ...') + status = status_of(usb.cmd(b'\x10' + b'\x00' * 0x61)) + log(' commit -> status 0x%04x' % status) + if status != 0: + raise Abort('commit rejected (status 0x%04x).' % status) + log(' rebooting sensor into the unprovisioned state') + try: + reboot() + except RebootException: + pass + log(' reset accepted for %r' % (ident,)) + finally: + release_device() + + if not already_blank: + wait_for_device() + log(' checking flash state ...') + send_init_sequence() + info = get_flash_info() + log(' partitions after wipe: %d' % len(info.partitions)) + if len(info.partitions) != 0: + raise Abort('the reset blob was accepted but %d partitions are still ' + 'present. Stop here and report this on issue #283.' + % len(info.partitions)) + try: + reboot() + except RebootException: + pass + release_device() + log(' wipe complete. Next: --phase provision') + + +def phase_provision(args): + """Format + pair, following validitysensor.init_flash(). + + The library runs this same sequence whenever the daemon starts on a + zero-partition sensor, so keeping the tool identical to it means the + provisioned state is exactly what the daemon expects. The one deliberate + difference is the 0x969 register preflight from #283, inserted between the + reset blob and 4f. Firmware is uploaded in a separate TLS session + afterwards (upload_fwext() does the same on a normal daemon start). + """ + log('== PROVISION (re-create partitions, new local pairing)') + open_sensor() + try: + send_init_sequence() + info = get_flash_info() + if len(info.partitions) == 0: + log(' flash is unprovisioned: %s, %d bytes' + % (info.ic.name, info.ic.size)) + + log(' sending reset blob (factory path) ...') + status = status_of(usb.cmd(blob('reset_blob'))) + log(' reset blob -> status 0x%04x' % status) + if status != 0: + raise Abort('reset blob rejected (0x%04x); nothing was changed' % status) + + # Register preflight, as reported for this exact unit on #283: + # without it the 0x969 silicon refuses the 4f partition command. + log(' register preflight (0x8000205c = 7) ...') + write_hw_reg32(0x8000205C, 7) + value = read_hw_reg32(0x80002080) + log(' 0x80002080 = 0x%08x' % value) + if value not in (2, 3): + raise Abort('unexpected register value 0x%08x (expected 2 or 3); ' + 'refusing to format.' % value) + # Do NOT send identify_sensor() (0x75) or call_cleanups() (0x1a) + # here. The factory path has no pending commit, and the sensor + # leaves the reset/factory state on 0x1a, after which 4f is + # rejected with 0x0401 ("invalid command in this state"). The + # working sequence on #283 is: reset_blob -> reg preflight -> 4f. + + backend = default_backend() + skey = ec.generate_private_key(ec.SECP256R1(), backend) + snums = skey.private_numbers() + client_private = snums.private_value + client_public = snums.public_numbers + + log(' sending 4f (partition table + server cert) ...') + partition_flash(info, flash_layout_hardcoded, + partition_signature, client_public) + log(' partitions created') + + RomInfo.get() + log(' sending 50 (ECDH) ...') + try: + rsp = usb.cmd(b'\x50') + finally: + call_cleanups() + if status_of(rsp) != 0: + raise Abort('ECDH (50) failed: status 0x%04x' % status_of(rsp)) + payload = rsp[2:] + length, = unpack(' int: + global _log_file + parser = argparse.ArgumentParser( + description='Re-provision the Synaptics 06cb:00bb fingerprint sensor', + formatter_class=argparse.RawDescriptionHelpFormatter, + epilog=__doc__) + parser.add_argument('--phase', default='preflight', + choices=['preflight', 'wipe', 'provision', 'firmware', + 'verify', 'all']) + parser.add_argument('--firmware', help='path to an extracted %s' % FWTYPE) + parser.add_argument('--softpaq', help='HP SoftPaq .exe to extract %s from ' + '(used instead of the pinned download)' % FWTYPE) + parser.add_argument('--yes', action='store_true', + help='skip the interactive confirmation (dangerous)') + parser.add_argument('--force', action='store_true', + help='allow wiping a sensor whose identity is not the ' + 'known 0x969 / FM-3439-xxx') + args = parser.parse_args() + + if os.geteuid() != 0: + print('ERROR: must run as root (USB device node is root-only).', file=sys.stderr) + return 2 + + BACKUP_DIR.mkdir(parents=True, exist_ok=True) + os.chmod(BACKUP_DIR, 0o700) + _log_file = open(LOG_PATH, 'a') + os.chmod(LOG_PATH, 0o600) + + if args.firmware: + source = Path(args.firmware).expanduser() + if not source.is_file(): + print('ERROR: --firmware %s does not exist' % source, file=sys.stderr) + return 2 + init_data_dir() + shutil.copyfile(source, FW_CACHE) + os.chmod(FW_CACHE, 0o600) + + log('python-validity 00bb re-provisioning, phase=%s, %s' % (args.phase, STAMP)) + log('log file: %s' % LOG_PATH) + + stop_competing_services() + try: + if args.phase in ('preflight', 'all'): + phase_preflight(args) + if args.phase in ('wipe', 'all'): + phase_wipe(args) + if args.phase in ('provision', 'all'): + phase_provision(args) + if args.phase == 'firmware': + phase_firmware(args) + if args.phase in ('verify', 'all'): + phase_verify(args) + except Abort as error: + log('\nSTOPPED: %s' % error) + log('The device may be left in a partially provisioned state; the log is ' + 'in %s.' % LOG_PATH) + return 1 + except KeyboardInterrupt: + log('\ninterrupted') + return 130 + except Exception as error: # noqa: BLE001 + import traceback + log('\nUNEXPECTED ERROR: %r' % error) + log(traceback.format_exc()) + log('Log: %s' % LOG_PATH) + return 1 + finally: + if _log_file is not None: + _log_file.close() + _log_file = None + restart_services() + + log('\nDONE.') + if args.phase in ('provision', 'firmware', 'verify', 'all'): + log('Next: enroll a finger with fprintd-enroll') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/hwkey_probe.py b/tools/hwkey_probe.py new file mode 100644 index 0000000..d8ba8ef --- /dev/null +++ b/tools/hwkey_probe.py @@ -0,0 +1,731 @@ +#!/usr/bin/env python3 +"""Try to recover the host key that encrypts the sensor's pairing blob. + +Why +--- +When this sensor was paired by Windows Hello, Windows generated a P-256 +keypair and stored only the *encrypted* private key on the sensor (flash +partition 1, cert-store block 0x0004). The encryption/HMAC key is derived +from a host secret: + + psk_enc = PRF(password_hardcoded, b'GWK' + hw_key) + psk_val = PRF(psk_enc, b'GWK_SIGN' + gwk_sign_hardcoded) + +``python-validity`` uses ``hw_key = product_name + '\\0' + product_serial + +'\\0'`` (ASCII). ``tools/checkpair.py`` proved that Windows used something +else on this machine. If we can find the secret Windows used, we can reuse +the existing pairing: no wipe, Windows Hello keeps working, Linux works too. + +This tool is **read-only and offline**. It takes the cert-store backup, +computes the HMAC of block 0x0004 for a large set of candidate ``hw_key`` +values, and reports the first one that verifies. No sensor access, no +writes. + +Candidate sources +----------------- +* every readable ``/sys/class/dmi/id/*`` value (name, serial, UUID, board, + chassis, BIOS ...), in ASCII and UTF-16LE, with and without NUL terminators; +* GUIDs passed with ``--machine-guid`` / found in ``--guid-file`` / + ``--part4`` (Windows registry exports, partition-4 dumps), including the + Windows little-endian raw layout; +* free-form values with ``--value`` (e.g. a hostname); +* raw binary blobs with ``--raw-hex`` / ``--raw-file`` -- e.g. the + ``PairingData`` value dumped from the Windows device registry, including + 32-byte windows at every offset inside a larger blob; +* all sensible concatenations of the above. + +A match is a 256-bit HMAC - false positives are not a concern. + +Usage +----- + sudo python3 tools/hwkey_probe.py + sudo python3 tools/hwkey_probe.py --machine-guid 12345678-1234-...-... + sudo python3 tools/hwkey_probe.py --guid-file /tmp/cryptography.reg + sudo python3 tools/hwkey_probe.py --raw-file /tmp/opencode/windows-pairing.txt + sudo python3 tools/hwkey_probe.py --read-part4 # needs the sensor + sudo python3 tools/hwkey_probe.py --part4 backups/00bb-part4-*.bin + +Getting the Windows device-registry pairing values (read-only) +-------------------------------------------------------------- + sudo bash tools/dump_windows_pairing.sh + sudo python3 tools/hwkey_probe.py --raw-file /tmp/opencode/windows-pairing.txt + +Getting the Windows MachineGuid (read-only, Windows not needed) +--------------------------------------------------------------- + sudo bash tools/find_windows_guid.sh + sudo python3 tools/hwkey_probe.py --guid-file /tmp/opencode/windows-machine-guid.reg + +The script mounts every NTFS partition read-only, reads +HKLM\\SOFTWARE\\Microsoft\\Cryptography\\MachineGuid straight out of the +SOFTWARE hive with tools/scan_hive.py (no hivex / chntpw needed), and umounts +again. If the volume cannot be mounted (BitLocker, hibernated Windows), boot +Windows once and run: + + reg query HKLM\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid + +then pass the value with ``--machine-guid``. + +Exit codes: 0 match found, 1 no match, 2 precondition missing. +""" + +import argparse +import hashlib +import hmac +import json +import os +import re +import sys +import time +from pathlib import Path +from struct import unpack + +REPO = Path(__file__).resolve().parent.parent +PROJECT = REPO +sys.path.insert(0, str(REPO)) + +from validitysensor.tls import gwk_sign_hardcoded, password_hardcoded, prf # noqa: E402 + +GUID_PATTERN = r'[0-9A-Fa-f]{8}-(?:[0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}' +GUID_TEXT_RE = re.compile(GUID_PATTERN) +GUID_BYTES_RE = re.compile(GUID_PATTERN.encode()) +PRINTABLE_RE = re.compile(rb'[ -~]{6,64}') + +DMI_FILES = ( + 'product_name', 'product_serial', 'product_uuid', 'product_sku', + 'product_family', 'product_version', + 'sys_vendor', 'board_name', 'board_serial', 'board_vendor', + 'board_version', 'board_asset_tag', + 'chassis_serial', 'chassis_vendor', 'chassis_version', + 'bios_vendor', 'bios_version', 'bios_date', +) + +# Seeds/labels tried in front of hw_key. The first one is what +# python-validity uses; the others are cheap to test. +GWK_LABELS = (b'GWK', b'GWK\0', b'GWK\0\0') + + +# -------------------------------------------------------------------------- +# cert-store handling +# -------------------------------------------------------------------------- + +def parse_blocks(certstore: bytes): + walk = certstore + blocks = {} + while len(walk) >= 0x24: + hdr, walk = walk[:4], walk[4:] + hs, walk = walk[:0x20], walk[0x20:] + block_id, size = unpack(' bytes: + psk_enc = prf(password_hardcoded, label + hw_key, 0x20) + psk_val = prf(psk_enc, b'GWK_SIGN' + gwk_sign_hardcoded, 0x20) + return hmac.new(psk_val, c, hashlib.sha256).digest() + + +# -------------------------------------------------------------------------- +# candidate generation +# -------------------------------------------------------------------------- + +def read_dmi_values(): + values = {} + for name in DMI_FILES: + try: + text = Path('/sys/class/dmi/id/' + name).read_text().strip('\0\r\n\t ') + except OSError: + continue + if text and not text.startswith('<'): + values[name] = text + return values + + +def guid_variants(guid: str): + """All plausible byte encodings of one GUID string.""" + g = guid.strip().strip('{}') + if not GUID_TEXT_RE.fullmatch(g): + return [] + out = [] + for f in {g.lower(), g.upper()}: + for s in (f, '{%s}' % f): + out.append(('ascii:%s' % s, s.encode())) + out.append(('ascii-nul:%s' % s, s.encode() + b'\0')) + out.append(('utf16:%s' % s, s.encode('utf-16le'))) + out.append(('utf16-nul:%s' % s, s.encode('utf-16le') + b'\0\0')) + raw = bytes.fromhex(g.replace('-', '')) + le = raw[0:4][::-1] + raw[4:6][::-1] + raw[6:8][::-1] + raw[8:] + out.append(('raw-be:%s' % g, raw)) + out.append(('raw-le:%s' % g, le)) + out.append(('raw-le-nul:%s' % g, le + b'\0')) + # "hex without dashes" is a common registry/UEFI representation + compact = g.replace('-', '') + for f in {compact.lower(), compact.upper()}: + out.append(('compact:%s' % f, f.encode())) + out.append(('compact-nul:%s' % f, f.encode() + b'\0')) + out.append(('compact-utf16:%s' % f, f.encode('utf-16le'))) + out.append(('compact-utf16-nul:%s' % f, f.encode('utf-16le') + b'\0\0')) + return out + + +def text_variants(label: str, text: str): + out = [] + for enc in ('ascii', 'utf-16le'): + try: + raw = text.encode(enc) + except UnicodeEncodeError: + continue + suffix = b'\0' if enc == 'ascii' else b'\0\0' + out.append(('%s[%s]' % (label, enc), raw)) + out.append(('%s[%s]+nul' % (label, enc), raw + suffix)) + return out + + +def raw_variants(label: str, blob: bytes): + """Plausible hw_key views of a raw registry blob. + + The GWK is 32 bytes. A registry value may hold it bare, or wrapped in a + small header (type/size), so also try every 32-byte window. Digests and + NUL-stripped forms are cheap extra shots. + """ + out = [] + if not blob: + return out + if len(blob) <= 4096: + out.append((label, blob)) + if len(blob) > 32: + out.append(('%s first32' % label, blob[:32])) + out.append(('%s last32' % label, blob[-32:])) + # A registry blob holding the key is small. For very large values, + # scan the start and the end instead of every offset. + if len(blob) <= 0x10000: + offsets = range(0, len(blob) - 32 + 1) + else: + offsets = list(range(0, 0x10000)) + \ + list(range(len(blob) - 0x1000, len(blob) - 32 + 1)) + for offset in offsets: + out.append(('%s window@%#x' % (label, offset), + blob[offset:offset + 32])) + stripped = blob.rstrip(b'\0') + if stripped and stripped != blob: + out.append(('%s strip-nul' % label, stripped)) + seen = set() + unique = [] + for desc, key in out: + if key not in seen: + seen.add(key) + unique.append((desc, key)) + return unique + + +def build_candidates(dmi: dict, guids, explicit_values, scanned_values, + raw_values=()): + """Return list of (description, hw_key bytes), de-duplicated. + + ``explicit_values`` come from ``--value`` (trusted, combined freely); + ``scanned_values`` come from a partition dump (noisy, only combined with + the DMI core); ``raw_values`` are (label, bytes) pairs from ``--raw-hex`` + / ``--raw-file`` (trusted, like ``--value``). + """ + singles = [] # (desc, key) + for name, text in sorted(dmi.items()): + variants = text_variants('dmi.%s=%r' % (name, text), text) + singles += variants + if name in ('product_name', 'product_serial', 'product_uuid'): + # Windows might normalise case; cheap to test for the core fields + for desc, key in variants: + if '[ascii' in desc: + singles.append((desc + ' lower', key.lower())) + singles.append((desc + ' upper', key.upper())) + if name in ('product_name', 'product_serial'): + # SMBIOS strings are sometimes space padded to a fixed width + for width in (32, 64): + if len(text) < width: + padded = text.ljust(width).encode('ascii', 'ignore') + singles.append(('dmi.%s padded%d' % (name, width), padded)) + singles.append(('dmi.%s padded%d+nul' % (name, width), + padded + b'\0')) + for guid in guids: + singles += guid_variants(guid) + for i, value in enumerate(explicit_values): + singles += text_variants('value[%d]=%r' % (i, value), value) + for i, value in enumerate(scanned_values): + singles += text_variants('scan[%d]=%r' % (i, value), value) + raw_atoms = [] + for label, blob in raw_values: + if not blob: + continue + variants = raw_variants(label, blob) + singles += variants + raw_atoms += variants + + # de-duplicate by bytes, keep the first (most descriptive) label + seen = set() + atoms = [] + for desc, key in singles: + if key and key not in seen: + seen.add(key) + atoms.append((desc, key)) + + # The Windows driver may hash the identifier before using it as the GWK + # input; test the common digests as standalone candidates. + hashed = [] + for desc, key in atoms: + hashed.append(('sha256(%s)' % desc, hashlib.sha256(key).digest())) + hashed.append(('sha1(%s)' % desc, hashlib.sha1(key).digest())) + hashed.append(('md5(%s)' % desc, hashlib.md5(key).digest())) + + candidates = list(atoms) + hashed + + # Atoms built from --value / partition-4 strings are noisy; pair them + # with the DMI core but not with each other. + core = [(d, k) for d, k in atoms + if any(('dmi.%s=' % n) in d for n in + ('product_name', 'product_serial', 'product_uuid')) + or d.startswith('guid')] + explicit = [(d, k) for d, k in atoms if d.startswith('value[')] + scanned = [(d, k) for d, k in atoms if d.startswith('scan[')] + + separators = (b'', b'\0', b'\0\0') + + # Raw blobs expand to hundreds of windows; combining all of them with the + # DMI core would multiply that by ~100 for almost no gain. The key is + # expected to be a self-contained 32-byte value, so only combine the + # leading candidates of each blob. + raw_combine = [item for item in raw_atoms if len(item[1]) <= 128][:24] + + def combine(list_a, list_b): + for desc_a, key_a in list_a: + for desc_b, key_b in list_b: + for sep in separators: + candidates.append(('%s + %r + %s' + % (desc_a, sep, desc_b), + key_a + sep + key_b)) + + combine(core, core) + combine(explicit, core) + combine(core, explicit) + combine(explicit, explicit) + combine(scanned, core) + combine(raw_combine, core) + combine(core, raw_combine) + combine(raw_combine, explicit) + + # 3-part combinations, plain ASCII forms only, from a small primary set + primary = [] + for name in ('product_name', 'product_serial', 'product_uuid'): + if name in dmi: + primary.append(('dmi.%s' % name, dmi[name].encode('ascii', + 'ignore'))) + for guid in guids[:8]: + primary.append(('guid:%s' % guid, guid.strip('{}').lower().encode())) + for i, value in enumerate(explicit_values[:8]): + primary.append(('value[%d]' % i, value.encode('ascii', 'ignore'))) + + for desc_a, key_a in primary: + for desc_b, key_b in primary: + for desc_c, key_c in primary: + candidates.append(('%s\\0%s\\0%s' % (desc_a, desc_b, desc_c), + key_a + b'\0' + key_b + b'\0' + key_c)) + + return candidates + + +# -------------------------------------------------------------------------- +# scanning helpers +# -------------------------------------------------------------------------- + +ID_RE = re.compile(r'^[A-Za-z0-9][A-Za-z0-9._-]{5,63}$') + + +def guid_strings_in(data: bytes): + """Text GUIDs (ASCII or UTF-16LE); raw fallback only if nothing textual.""" + found = [] + for match in GUID_BYTES_RE.finditer(data): + found.append(match.group().decode('ascii')) + try: + wide = data.decode('utf-16le', errors='ignore') + except Exception: # noqa: BLE001 + wide = '' + for match in GUID_TEXT_RE.finditer(wide): + found.append(match.group()) + + if not found: + # Windows GUIDs can also be stored as a raw 16-byte little-endian + # struct. This scan is noisy, so only use it as a fallback and + # keep the first few hits. + for i in range(0, max(0, len(data) - 16)): + chunk = data[i:i + 16] + if chunk == b'\0' * 16 or chunk == b'\xff' * 16: + continue + if chunk[6] >> 4 != 4: # version 4 GUIDs dominate in practice + continue + raw = (chunk[0:4][::-1] + chunk[4:6][::-1] + + chunk[6:8][::-1] + chunk[8:]) + found.append('%s-%s-%s-%s-%s' % ( + raw[0:4].hex(), raw[4:6].hex(), raw[6:8].hex(), + raw[8:10].hex(), raw[10:16].hex())) + if len(found) >= 24: + break + + out = [] + seen = set() + for g in found: + g = g.strip().lower() + if g not in seen: + seen.add(g) + out.append(g) + return out + + +def strings_in(data: bytes, limit=60): + out = [] + seen = set() + for match in PRINTABLE_RE.finditer(data): + text = match.group().decode('ascii').strip() + if text in seen or not ID_RE.match(text): + continue + seen.add(text) + out.append(text) + if len(out) >= limit: + break + return out + + +def scan_dump(data: bytes): + guids = guid_strings_in(data) + strings = strings_in(data) + return guids, strings + + +# -------------------------------------------------------------------------- +# hardware: read partition 4 (read-only) +# -------------------------------------------------------------------------- + +def read_part4(): + import usb.core as ucore + + import checkpair # same directory + from validitysensor.flash import get_flash_info, read_flash + from validitysensor.sensor import RebootException, reboot + from validitysensor.usb import usb + + dev = ucore.find(idVendor=checkpair.VID, idProduct=checkpair.PID) + if dev is None: + print('ERROR: 06cb:00bb not found', file=sys.stderr) + return None + + serial = (dev.serial_number or 'unknown').replace('/', '_') + print('== read partition 4 (read-only)') + usb.open_dev(dev) + try: + checkpair.init_sequence() + info = get_flash_info() + part = next((p for p in info.partitions if p.id == 4), None) + if part is None: + print('ERROR: no partition 4 on this sensor', file=sys.stderr) + return None + print(' partition 4: offset 0x%x size 0x%x access 0x%02x' + % (part.offset, part.size, part.access_lvl)) + data = b'' + while len(data) < part.size: + chunk_size = min(0x1000, part.size - len(data)) + try: + chunk = read_flash(4, len(data), chunk_size) + except Exception as error: # noqa: BLE001 + print(' stopped after %d bytes: %s' % (len(data), error)) + break + if not chunk: + break + data += chunk + if not data: + print('ERROR: partition 4 is not readable without TLS', + file=sys.stderr) + return None + outdir = PROJECT / 'backups' + outdir.mkdir(parents=True, exist_ok=True) + path = outdir / ('00bb-part4-%s-%s.bin' + % (serial, time.strftime('%Y%m%d-%H%M%S'))) + path.write_bytes(data) + os.chmod(path, 0o600) + print(' saved %d bytes to %s' % (len(data), path)) + return data + finally: + try: + reboot() + except RebootException: + pass + except Exception as error: # noqa: BLE001 + print(' reboot failed (usually harmless): %s' % error) + usb.dev = None + + +# -------------------------------------------------------------------------- +# main +# -------------------------------------------------------------------------- + +def newest_certstore(): + files = sorted((PROJECT / 'backups').glob('00bb-certstore-*.bin'), + key=lambda p: p.stat().st_mtime) + return files[-1] if files else None + + +HEX_RE = re.compile(r'^[0-9A-Fa-f]+$') +GUID_ONLY_RE = re.compile(r'^\{?' + GUID_PATTERN + r'\}?$') +# Longer decoded strings are descriptions, not keys; skipping them keeps the +# candidate count sane when a dump contains driver prose. +TEXT_CANDIDATE_MAX = 256 + + +def load_raw_blobs(hex_values, paths): + """Build (label, bytes) candidates from --raw-hex / --raw-file. + + Files may be raw binary or the text output of tools/dump_windows_pairing.sh + / scan_hive.py ("Name=hex"). Trailing "(type N)" annotations are ignored. + """ + blobs = [] + for i, value in enumerate(hex_values): + compact = ''.join(value.split()) + if len(compact) % 2 or not HEX_RE.fullmatch(compact): + print('WARNING: --raw-hex[%d] is not valid hex, ignored' % i, + file=sys.stderr) + continue + blobs.append(('raw-hex[%d]' % i, bytes.fromhex(compact))) + + for path in paths: + try: + text = path.read_text(errors='strict') + except (OSError, UnicodeDecodeError): + try: + data = path.read_bytes() + except OSError as error: + print('WARNING: cannot read %s: %s' % (path, error), + file=sys.stderr) + continue + blobs.append(('raw-file[%s]' % path.name, data)) + continue + found = False + for line in text.splitlines(): + line = line.strip() + if '=' not in line: + continue + name, value = line.split('=', 1) + name = name.strip() + # scan_hive's stdout form appends " (type N)"; --out does not. + value = re.sub(r'\s+\(type \d+\)$', '', value.strip()) + compact = ''.join(value.split()) + if GUID_ONLY_RE.match(compact): + # scan_hive decodes REG_SZ, so MachineGuid arrives as text. + guid = compact.strip('{}').lower() + blobs.append(('%s=%s ascii' % (name, path.name), guid.encode())) + blobs.append(('%s=%s utf16le' % (name, path.name), + guid.encode('utf-16le'))) + found = True + continue + if len(compact) % 2 == 0 and HEX_RE.fullmatch(compact): + blobs.append(('%s=%s' % (name, path.name), + bytes.fromhex(compact))) + found = True + continue + # Any other decoded string value (device path, serial, ...) is a + # candidate too: a hw_key is just bytes. + if 0 < len(value) <= TEXT_CANDIDATE_MAX: + blobs.append(('%s=%s ascii' % (name, path.name), + value.encode())) + blobs.append(('%s=%s utf16le' % (name, path.name), + value.encode('utf-16le'))) + found = True + if not found: + try: + blobs.append(('raw-file[%s]' % path.name, path.read_bytes())) + except OSError as error: + print('WARNING: cannot read %s: %s' % (path, error), + file=sys.stderr) + return blobs + + +def main(argv=None) -> int: + parser = argparse.ArgumentParser( + description='Offline search for the host key that protects the ' + "sensor's stored pairing.") + parser.add_argument('--certstore', type=Path, + help='cert-store backup (default: newest in backups/)') + parser.add_argument('--machine-guid', action='append', default=[], + metavar='GUID', help='candidate Windows MachineGuid ' + '(repeatable)') + parser.add_argument('--guid-file', type=Path, action='append', default=[], + help='file to scan for GUIDs (registry export, etc.)') + parser.add_argument('--part4', type=Path, + help='partition-4 dump to scan for GUIDs/strings') + parser.add_argument('--value', action='append', default=[], + metavar='TEXT', help='extra candidate value, e.g. ' + 'hostname or sensor serial') + parser.add_argument('--raw-hex', action='append', default=[], + metavar='HEX', help='raw binary candidate (hex), ' + 'e.g. a PairingData value') + parser.add_argument('--raw-file', type=Path, action='append', default=[], + help='raw binary file or "Name=hex" registry dump ' + 'to scan for 32-byte key candidates') + parser.add_argument('--read-part4', action='store_true', + help='read flash partition 4 from the sensor first ' + '(read-only, needs root)') + args = parser.parse_args(argv) + + certstore_path = args.certstore or newest_certstore() + if certstore_path is None: + print('ERROR: no cert-store backup found; pass --certstore', + file=sys.stderr) + return 2 + try: + certstore = certstore_path.read_bytes() + except OSError as error: + print('ERROR: cannot read %s: %s\n' + ' the backup is 0600 root -- run with sudo.' + % (certstore_path, error), file=sys.stderr) + return 2 + + blocks = parse_blocks(certstore) + if 4 not in blocks: + print('ERROR: cert-store has no pairing block 0x0004 (NO_PAIRING)', + file=sys.stderr) + return 1 + c, mac = split_pairing_blob(blocks[4]) + print('== cert-store %s' % certstore_path) + print(' pairing blob: %d bytes ciphertext, %d-byte HMAC' + % (len(c), len(mac))) + + guids = [] + for guid in args.machine_guid: + guids.append(guid) + for path in args.guid_file: + try: + data = path.read_bytes() + except OSError as error: + print('WARNING: cannot read %s: %s' % (path, error), + file=sys.stderr) + continue + found = guid_strings_in(data) + print(' %s: %d GUID(s)' % (path, len(found))) + guids += found + + explicit = list(args.value) + raw_blobs = load_raw_blobs(args.raw_hex, args.raw_file) + scanned = [] + if args.read_part4: + if os.geteuid() != 0: + print('ERROR: --read-part4 needs root', file=sys.stderr) + return 2 + data = read_part4() + if data is None: + return 1 + found_guids, found_strings = scan_dump(data) + print(' partition 4: %d GUID(s), %d string(s)' + % (len(found_guids), len(found_strings))) + guids += found_guids + scanned += found_strings + elif args.part4 is not None: + data = args.part4.read_bytes() + found_guids, found_strings = scan_dump(data) + print(' %s: %d GUID(s), %d string(s)' + % (args.part4, len(found_guids), len(found_strings))) + guids += found_guids + scanned += found_strings + + if not guids: + print(' (no GUID text candidates -- MachineGuid not supplied; ' + 'raw blobs are still tried)') + + dmi = read_dmi_values() + print('== candidates') + print(' DMI values: %d, GUIDs: %d, --value: %d, raw blobs: %d, scanned: %d' + % (len(dmi), len(set(g.lower() for g in guids)), len(explicit), + len(raw_blobs), len(scanned))) + candidates = build_candidates(dmi, guids, explicit, scanned, raw_blobs) + + # de-duplicate again: combinations often collide + unique = [] + seen_keys = set() + for desc, key in candidates: + if key in seen_keys: + continue + seen_keys.add(key) + unique.append((desc, key)) + candidates = unique + print(' derived hw_key candidates: %d' % len(candidates)) + + print('== HMAC check') + match = None + for label in GWK_LABELS: + for desc, key in candidates: + if hmac.compare_digest(mac_for(key, label, c), mac): + match = (desc, key, label) + break + if match is not None: + break + + if match is not None: + desc, key, label = match + print() + print('MATCH FOUND') + print(' hw_key (%d bytes): %s' % (len(key), key.hex())) + print(' as text : %r' % key) + print(' derived from : %s' % desc) + print(' GWK label : %r' % label) + print() + print(' Next step: feed this key to the driver instead of the') + print(' DMI default, then re-run tools/checkpair.py. If it says') + print(' ALREADY_PAIRED, no wipe is needed and Windows Hello') + print(' keeps working.') + report = {'stamp': time.strftime('%Y%m%d-%H%M%S'), + 'certstore': str(certstore_path), + 'result': 'MATCH', 'hw_key_hex': key.hex(), + 'description': desc, 'gwk_label': label.hex(), + 'candidates': len(candidates), + 'guids': sorted(set(guids)), + 'dmi': dmi} + outdir = PROJECT / 'backups' + path = outdir / ('00bb-hwkey-%s.json' % report['stamp']) + try: + path.write_text(json.dumps(report, indent=2) + '\n') + os.chmod(path, 0o600) + print(' report: %s' % path) + except OSError as error: + print(' (could not write report: %s)' % error) + return 0 + + print() + print('NO MATCH') + print(' None of the %d candidates reproduces the pairing HMAC.' + % len(candidates)) + print(' Likely causes:') + print(' * the secret is not machine-readable (TPM-sealed), or') + print(' * it uses data we have not supplied yet.') + print(' Try, in this order:') + print(' 1. dump the Windows registry pairing values (read-only):') + print(' sudo bash tools/dump_windows_pairing.sh') + print(' sudo python3 tools/hwkey_probe.py --raw-file /tmp/opencode/windows-pairing.txt') + print(' 2. export the Windows MachineGuid and pass --guid-file') + print(' 3. read partition 4 (may contain the host key record):') + print(' sudo python3 tools/hwkey_probe.py --read-part4') + print(' 4. pass any extra identifiers with --value (hostname, sensor') + print(' serial, Windows product ID ...)') + print(' If all of that fails, the pairing cannot be reused; the wipe in') + print(' tools/flash00bb.py is then the only option.') + return 1 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/probe00bb.py b/tools/probe00bb.py new file mode 100755 index 0000000..70d13ba --- /dev/null +++ b/tools/probe00bb.py @@ -0,0 +1,248 @@ +#!/usr/bin/env python3 +"""Read-only probe for the Synaptics 06cb:00bb sensor (HP Spectre x360 13-ap0xxx). + +What this does +-------------- +1. Opens the sensor with the patched python-validity code in this tree. +2. Sends the Prometheus/97-class init sequence (01, 19, 4302, 06 02 ). +3. Reads the flash layout (3e), the sensor identity (75) and the cleartext + TLS cert-store partition (40 / partition 1). +4. Saves the cert-store to ``backups/`` and writes a JSON report. + +What this does NOT do +--------------------- +* No flash writes, no erase, no 4f/50, no reset_blob. +* The only non-read command is the standard init blob, which python-validity + sends on every single startup. +* At the end the sensor is rebooted (05 02 00), which is exactly what the + python-validity daemon does when it exits. + +Run: + sudo python3 tools/probe00bb.py +""" + +import argparse +import hashlib +import json +import os +import sys +import time +from binascii import hexlify +from pathlib import Path +from struct import unpack + +REPO = Path(__file__).resolve().parent.parent +PROJECT = REPO +sys.path.insert(0, str(REPO)) + +import usb.core as ucore # noqa: E402 + +from validitysensor import blobs # noqa: E402 +from validitysensor.flash import get_flash_info, get_fw_info, read_flash # noqa: E402 +from validitysensor.sensor import RebootException, RomInfo, identify_sensor, reboot # noqa: E402 +from validitysensor.tls import tls # noqa: E402 +from validitysensor.usb import usb # noqa: E402 + +VID, PID = 0x06CB, 0x00BB + + +def hx(data: bytes) -> str: + return hexlify(data).decode() + + +def status_of(rsp: bytes) -> int: + return unpack('= 2 else -1 + + +def save_backup(name: str, data: bytes, outdir: Path): + outdir.mkdir(parents=True, exist_ok=True) + path = outdir / name + path.write_bytes(data) + os.chmod(path, 0o600) + digest = hashlib.sha256(data).hexdigest() + print(' saved %s (%d bytes, sha256 %s)' % (path, len(data), digest)) + return str(path), digest + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument('--output-dir', default=str(PROJECT / 'backups'), + help='where to store the cert-store backup and JSON report') + parser.add_argument('--no-reboot', action='store_true', + help='leave the sensor in the initialized state (not recommended)') + args = parser.parse_args() + + if os.geteuid() != 0: + print('ERROR: the USB device node is root-only. Re-run with sudo.', file=sys.stderr) + return 2 + + outdir = Path(args.output_dir) + report = {'usb': {}, 'steps': {}, 'partitions': [], 'firmware': None} + + print('== 0. open 06cb:00bb') + dev = ucore.find(idVendor=VID, idProduct=PID) + if dev is None: + print('ERROR: 06cb:00bb not found on the USB bus.', file=sys.stderr) + return 1 + report['usb'] = { + 'bus': int(dev.bus), + 'address': int(dev.address), + 'idVendor': '%04x' % dev.idVendor, + 'idProduct': '%04x' % dev.idProduct, + 'bcdDevice': '%04x' % dev.bcdDevice, + 'serial': dev.serial_number, + 'manufacturer': dev.manufacturer, + 'product': dev.product, + } + for key, value in report['usb'].items(): + print(' %-12s %s' % (key, value)) + + serial = (dev.serial_number or 'unknown').replace('/', '_') + stamp = time.strftime('%Y%m%d-%H%M%S') + tls.reset() + usb.open_dev(dev) + print(' opened; config set') + + try: + print('== 1. send 01 (ROM info)') + rsp = usb.cmd(b'\x01') + report['steps']['01'] = hx(rsp) + print(' status 0x%04x, %d bytes' % (status_of(rsp), len(rsp))) + if status_of(rsp) != 0: + raise RuntimeError('ROM info rejected') + rom = RomInfo.get() + print(' %r' % rom) + report['rom'] = { + 'timestamp': rom.timestamp, 'build': rom.build, 'major': rom.major, + 'minor': rom.minor, 'product': rom.product, + } + + print('== 2. send 19') + rsp = usb.cmd(b'\x19') + report['steps']['19'] = hx(rsp) + print(' status 0x%04x, %d bytes' % (status_of(rsp), len(rsp))) + + print('== 3. send 43 02 (firmware/ext info, cleartext)') + r43 = usb.cmd(b'\x43\x02') + report['steps']['4302'] = hx(r43) + print(' status 0x%04x, %d bytes' % (status_of(r43), len(r43))) + + print('== 4. send init blob (06 02 ..., 581 bytes inner)') + blob = blobs.init_hardcoded() + print(' blob %d bytes, starts %s' % (len(blob), hx(blob[:10]))) + rsp = usb.cmd(blob) + report['steps']['init_hardcoded'] = hx(rsp) + print(' status 0x%04x' % status_of(rsp)) + if status_of(rsp) != 0: + print(' -> retrying with init_hardcoded_clean_slate') + rsp = usb.cmd(blobs.init_hardcoded_clean_slate()) + report['steps']['init_hardcoded_clean_slate'] = hx(rsp) + print(' status 0x%04x' % status_of(rsp)) + if status_of(rsp) != 0: + raise RuntimeError('init blob rejected') + elif status_of(r43) != 0: + print(' no firmware reported (clean slate) -> clean-slate blob') + rsp = usb.cmd(blobs.init_hardcoded_clean_slate()) + report['steps']['init_hardcoded_clean_slate'] = hx(rsp) + print(' status 0x%04x' % status_of(rsp)) + + print('== 5. send 3e (flash info)') + info = get_flash_info() + print(' IC %s, %d bytes, %d partition(s)' % + (info.ic.name, info.ic.size, len(info.partitions))) + report['flash'] = { + 'ic': info.ic.name, 'size': info.ic.size, + 'blocks': info.blocks, 'blocksize': info.blocksize, + } + for p in info.partitions: + print(' %r' % p) + report['partitions'].append({ + 'id': p.id, 'type': p.type, 'access': p.access_lvl, + 'offset': p.offset, 'size': p.size, + }) + + print('== 6. send 75 (sensor identity)') + rsp = usb.cmd(b'\x75') + report['steps']['75'] = hx(rsp) + print(' status 0x%04x, %d bytes' % (status_of(rsp), len(rsp))) + ident = identify_sensor() + print(' type=0x%04x hw-id=0x%02x name=%r' % (ident.type, ident.version, ident.name)) + report['identity'] = {'type': ident.type, 'hw_id': ident.version, + 'name': ident.name, 'major': ident.major} + + print('== 7. send 43 02 (fwext partition metadata)') + try: + fwi = get_fw_info(2) + except Exception as error: # noqa: BLE001 + fwi = None + print(' not readable in this state: %s' % error) + if fwi is None: + print(' no firmware reported') + else: + print(' fwext %d.%d buildtime %d, %d module(s)' % + (fwi.major, fwi.minor, fwi.buildtime, len(fwi.modules))) + report['firmware'] = { + 'major': fwi.major, 'minor': fwi.minor, + 'buildtime': fwi.buildtime, 'modules': len(fwi.modules), + } + + print('== 8. read partition 1 (TLS cert-store, access level 7)') + cert = read_flash(1, 0, 0x1000) + print(' read %d bytes, head %s' % (len(cert), hx(cert[:24]))) + path, digest = save_backup('00bb-certstore-%s-%s.bin' % (serial, stamp), cert, outdir) + report['certstore'] = { + 'path': path, 'sha256': digest, 'size': len(cert), + 'is_python_validity_format': cert[:4] == b'\x00\x00\x01\x00', + } + + print('== 9. probe other partitions (expected to be TLS-only)') + for part in (2, 4, 6): + try: + data = read_flash(part, 0, 0x40) + print(' partition %d: %d bytes readable, head %s' + % (part, len(data), hx(data[:16]))) + except Exception as error: # noqa: BLE001 + print(' partition %d: not readable without TLS (%s)' % (part, error)) + + print('== 10. read-only hardware registers (preflight values)') + for addr in (0x8000205C, 0x80002080): + try: + rsp = usb.cmd(b'\x07' + addr.to_bytes(4, 'little') + b'\x04') + if status_of(rsp) == 0 and len(rsp) >= 6: + print(' 0x%08x = 0x%08x' % (addr, unpack(' status 0x%04x' % (addr, status_of(rsp))) + except Exception as error: # noqa: BLE001 + print(' 0x%08x -> %s' % (addr, error)) + + report['ok'] = True + except Exception as error: # noqa: BLE001 + import traceback + report['ok'] = False + report['error'] = repr(error) + report['traceback'] = traceback.format_exc() + print('\nERROR: %r' % error, file=sys.stderr) + print(report['traceback'], file=sys.stderr) + finally: + if not args.no_reboot: + print('== 11. reboot sensor (normal daemon exit path)') + try: + reboot() + except RebootException: + print(' sensor rebooting') + except Exception as error: # noqa: BLE001 + print(' reboot failed (usually harmless): %s' % error) + usb.dev = None + + report_path = outdir / ('00bb-probe-%s-%s.json' % (serial, stamp)) + report_path.parent.mkdir(parents=True, exist_ok=True) + report_path.write_text(json.dumps(report, indent=2) + '\n') + os.chmod(report_path, 0o600) + print('\nreport: %s' % report_path) + print('Send this JSON plus the cert-store backup to the project folder; ' + 'neither contains your fingerprint templates.') + return 0 if report.get('ok') else 1 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/scan_hive.py b/tools/scan_hive.py new file mode 100644 index 0000000..2b5089e --- /dev/null +++ b/tools/scan_hive.py @@ -0,0 +1,306 @@ +#!/usr/bin/env python3 +"""Read registry values straight out of a Windows hive file (read-only). + +Used by ``tools/find_windows_guid.sh`` to get ``MachineGuid`` without needing +``hivex`` / ``chntpw``. Parses the regf container directly: + +* base block ("regf"), then hbins ("hbin") with size-prefixed cells; +* value cells ("vk") carry a name (ASCII or UTF-16LE, see the compressed-name + flag), a data type, and either inline data (<= 4 bytes, high bit of the size + set) or an offset into the hive. + +Values larger than ~16 KiB use "db" big-data cells; these are reassembled by +following the indirect block and concatenating 0x3FD8-byte segments (verified +against python-registry's large-data fixture). + +Only reads the file; never writes to it. Large hives are mapped with mmap, +so scanning a multi-hundred-MB SOFTWARE hive does not load it into RAM. + +Usage: + python3 tools/scan_hive.py SOFTWARE --name MachineGuid + python3 tools/scan_hive.py SOFTWARE --name MachineGuid --out guid.txt + python3 tools/scan_hive.py SYSTEM --name PairingData --name fwextId + python3 tools/scan_hive.py SYSTEM --list-names '(?i)pair|gwk' # survey + +``--list-names`` prints every value name matching a regular expression together +with its type and size; it does not dump data. Use it to find the name under +which a driver stores a key, then fetch it with ``--name``. + +Exit codes: 0 value found, 1 not found, 2 unreadable / not a hive. +""" + +import argparse +import mmap +import re +import struct +import sys +from pathlib import Path + +REG_SZ = 1 +REG_EXPAND_SZ = 2 +REG_BINARY = 3 +REG_DWORD = 4 +REG_MULTI_SZ = 7 + + +def iter_cells(data: bytes): + """Yield (cell_file_offset, cell_payload) for every allocated cell.""" + if len(data) < 0x1000 or data[:4] != b'regf': + raise ValueError('not a registry hive (missing regf signature)') + + pos = 0x1000 + while pos + 0x20 <= len(data): + if data[pos:pos + 4] != b'hbin': + # tolerate trailing garbage, but stop silently + return + bin_size = struct.unpack_from(' len(data): + return + cell = pos + 0x20 + end = pos + bin_size + while cell + 4 <= end: + raw_size = struct.unpack_from(' end: + break + if raw_size < 0: # allocated + yield cell, data[cell + 4:cell + size] + cell += size + pos += bin_size + + +def parse_vk(payload: bytes): + """Parse a vk cell payload -> dict or None.""" + if len(payload) < 0x14 or payload[:2] != b'vk': + return None + name_len, data_size, data_off, data_type, flags = struct.unpack_from( + ' len(payload) - 0x14: + return None + raw_name = payload[0x14:0x14 + name_len] + if flags & 0x0001: # VALUE_COMP_NAME: name is stored as single-byte ASCII + name = raw_name.decode('latin-1') + else: + name = raw_name.decode('utf-16le', errors='replace') + return { + 'name': name, + 'data_size': data_size, + 'data_offset': data_off, + 'type': data_type, + 'flags': flags, + } + + +def read_big_data(data: bytes, list_offset: int, total_size: int) -> bytes: + """Reassemble a "db" big-data value. + + Layout (as implemented by Windows and python-registry): the db cell holds + an offset to an indirect cell whose payload is an array of dword offsets; + each referenced cell contributes at most 0x3FD8 bytes of data. + """ + list_abs = 0x1000 + list_offset + if list_abs + 4 > len(data): + return b'' + cell_size = struct.unpack_from('= 0: + return b'' + list_body = data[list_abs + 4:list_abs + abs(cell_size)] + + out = bytearray() + remaining = total_size + index = 0 + while remaining > 0: + if (index + 1) * 4 > len(list_body): + break + segment_offset = struct.unpack_from(' len(data): + break + segment_cell = struct.unpack_from('= 0: + break + chunk_size = min(chunk_size, abs(segment_cell) - 4) + out += data[segment_abs + 4:segment_abs + 4 + chunk_size] + remaining -= chunk_size + index += 1 + return bytes(out) + + +def read_value_data(data: bytes, vk: dict) -> bytes: + size = vk['data_size'] + if size & 0x80000000: # inline, stored in the offset field itself + return struct.pack(' len(data): + return b'' + cell_size = struct.unpack_from('= 0: + return b'' + body_start = absolute + 4 + if size > 0x3FD8 and data[body_start:body_start + 2] == b'db': + list_offset = struct.unpack_from(' int: + try: + names = list_value_names(data, pattern) + except re.error as error: + print('ERROR: bad --list-names regex: %s' % error, file=sys.stderr) + return 2 + if not names: + print('no value name matches %r in %s' % (pattern, hive), + file=sys.stderr) + return 1 + for name, shapes in names: + rendered = ', '.join('type %d, %d bytes' % (kind, size) + for kind, size in shapes) + print('%-48s %s' % (name, rendered)) + print('%d matching value name(s) in %s' % (len(names), hive)) + return 0 + + +def main(argv=None) -> int: + parser = argparse.ArgumentParser( + description='Read a value from a Windows registry hive without ' + 'any external tools.') + parser.add_argument('hive', type=Path) + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('--name', action='append', + help='value name, e.g. MachineGuid (repeatable)') + group.add_argument('--list-names', metavar='REGEX', + help='list value names matching REGEX (case-sensitive ' + 'Python regex; use (?i) for insensitive)') + parser.add_argument('--out', type=Path, + help='append "name=value" lines to this file') + args = parser.parse_args(argv) + + try: + handle = args.hive.open('rb') + except OSError as error: + print('ERROR: cannot read %s: %s' % (args.hive, error), + file=sys.stderr) + return 2 + + with handle: + # The SOFTWARE hive can be several hundred MB. Map it instead of + # reading it into memory; the parser only uses slicing and + # struct.unpack_from, both of which work on mmap objects. + try: + data = mmap.mmap(handle.fileno(), 0, access=mmap.ACCESS_READ) + except (ValueError, OSError): + try: + data = handle.read() + except OSError as error: + print('ERROR: cannot read %s: %s' % (args.hive, error), + file=sys.stderr) + return 2 + try: + if args.list_names is not None: + return list_names(data, args.list_names, args.hive) + values = find_many_values(data, args.name) + except ValueError as error: + print('ERROR: %s' % error, file=sys.stderr) + return 2 + finally: + if isinstance(data, mmap.mmap): + data.close() + + lines = [] + for name in args.name: + for vk, value, raw in values[name]: + if isinstance(value, bytes): + text = value.hex() + elif isinstance(value, list): + text = ';'.join(value) + else: + text = str(value) + print('%s = %s (type %d)' % (name, text, vk['type'])) + lines.append('%s=%s' % (name, text)) + + if not lines: + print('no value named %s in %s' + % (', '.join(repr(n) for n in args.name), args.hive), + file=sys.stderr) + return 1 + + if args.out is not None: + try: + with args.out.open('a', encoding='utf-8') as fh: + for line in lines: + fh.write(line + '\n') + print('appended to %s' % args.out) + except OSError as error: + print('WARNING: cannot write %s: %s' % (args.out, error), + file=sys.stderr) + + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/test_flash_smoke.py b/tools/test_flash_smoke.py new file mode 100755 index 0000000..8a05fc3 --- /dev/null +++ b/tools/test_flash_smoke.py @@ -0,0 +1,343 @@ +#!/usr/bin/env python3 +"""Offline smoke test for tools/flash00bb.py (no hardware, no root). + +Fakes the USB transport and the device-side helpers, then runs the wipe and +provision phases to check command ordering and abort behaviour. + +Run: + python3 tools/test_flash_smoke.py + +Needs pyusb importable (``apt install python3-usb``) because flash00bb imports +it at module level, but never opens a real device. +""" +import importlib.util +import sys +from pathlib import Path +from struct import pack +from types import SimpleNamespace +from unittest import mock + +REPO = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(REPO)) + +spec = importlib.util.spec_from_file_location('flash00bb', REPO / 'tools' / 'flash00bb.py') +F = importlib.util.module_from_spec(spec) +spec.loader.exec_module(F) + + +class FakeUsb: + def __init__(self): + self.commands = [] + # Interleaved view of tool calls and raw USB writes, used to check the + # ordering around the destructive factory sequence. + self.timeline = [] + self.reset_blob = b'\x06\x02' + b'R' * 100 + self.reset_response = b'\x00\x00' + self.dev = SimpleNamespace(serial_number='SERIAL123', bus=1, address=2) + + def usb_dev(self): + return self.dev + + def cmd(self, data): + if callable(data): + data = data() + self.commands.append(data) + self.timeline.append(('usb', data)) + if data in (b'\x01', b'\x19', b'\x43\x02', b'\x50'): + if data == b'\x50': + zeros = b'\x00' * 32 + pub = b'\x11' * 400 + payload = pack(' full flow') +fake = FakeUsb() +state, calls = install(fake, []) +F.get_flash_info = orig_get_flash +fw_calls = {'n': 0} + + +def fw_info_after_upload(part): + fw_calls['n'] += 1 + if fw_calls['n'] == 1: + return None + return SimpleNamespace(major=1, minor=7, buildtime=123, modules=[]) + + +F.get_fw_info = fw_info_after_upload +fake_fw = Path('/tmp/opencode-fake.xpfwext') +fake_fw.write_bytes(b'HPHEADER' + b'\x1a' + b'FW' * 200 + b'S' * 256) +F.FW_CACHE = fake_fw +try: + F.phase_provision(ARGS) +finally: + fake_fw.unlink(missing_ok=True) + +names = [c[0] for c in calls] +print('calls:', names) +check('4f' in names, 'provision must send the partition table (4f)') +check('ecdh' in names, 'provision must handle the ECDH reply') +check('priv' in names, 'provision must install the local private key') +check('tls_open' in names, 'provision must open TLS') +check('write_tls' in names, 'provision must write the new cert-store') +check('write_reg' in names and names.index('write_reg') < names.index('4f'), + 'provision must do the register preflight before the 4f partition command') +check('identify' not in names[:names.index('4f')], + 'provision must not send identify (0x75) before 4f') +check('cleanups' not in names[:names.index('4f')], + 'provision must not commit (0x1a) before 4f; that leaves the factory ' + 'state and the sensor answers 0401') +reset_idx = max(i for i, event in enumerate(fake.timeline) + if event == ('usb', fake.reset_blob)) +fourf_idx = fake.timeline.index(('call', '4f')) +check(reset_idx < fourf_idx, + 'provision must reset the sensor before the 4f partition command') +between = fake.timeline[reset_idx + 1:fourf_idx] +check(not any(event == ('usb', b'\x1a') for event in between), + 'no raw 0x1a may be sent between reset and 4f (see #283)') +check([v for t, v in between if t == 'call'] == ['write_reg'], + 'reset -> write_reg(0x8000205c) -> 4f is the only accepted sequence, ' + 'got %r' % ([v for t, v in between if t == 'call'],)) +check([a[1][0] for a in calls if a[0] == 'erase'] == [1, 2, 5, 6, 4], + 'provision must erase 1, 2, 5, 6, 4 in that order (matches init_flash)') +check('firmware_phase' in names, 'provision must hand over to the firmware phase') +print('ok provision') + +print('--- provision: already formatted sensor is not re-formatted') +fake = FakeUsb() +state, calls = install(fake, [1, 2, 6, 3, 4]) +F.phase_provision(ARGS) +names = [c[0] for c in calls] +check('4f' not in names, 'provision must not re-format an already provisioned sensor') +check('erase' not in names, 'provision must not erase an already provisioned sensor') +check('firmware_phase' in names, 'provision must still run the firmware phase') +print('ok provision (idempotent)') + +print('--- firmware: no partitions -> abort') +fake = FakeUsb() +state, calls = install(fake, []) +F.phase_firmware = REAL_PHASE_FIRMWARE +try: + F.phase_firmware(ARGS) + check(False, 'firmware phase should refuse an unprovisioned sensor') +except F.Abort as error: + print('ok abort:', str(error).splitlines()[0]) + +print('--- firmware: present -> no write') +fake = FakeUsb() +state, calls = install(fake, [1, 2, 6, 3, 4]) +F.phase_firmware = REAL_PHASE_FIRMWARE +F.get_fw_info = lambda part: SimpleNamespace(major=1, minor=7, buildtime=1, modules=[]) +F.phase_firmware(ARGS) +names = [c[0] for c in calls] +check('write_fw' not in names, 'firmware phase must not rewrite an intact fwext') +check('parse_tls' in names, 'firmware phase must load the local key from the cert-store') +print('ok firmware (present)') + +print('--- firmware: missing -> upload') +fake = FakeUsb() +state, calls = install(fake, [1, 2, 6, 3, 4]) +F.phase_firmware = REAL_PHASE_FIRMWARE +calls_fw = {'n': 0} + + +def fw_info_upload(part): + calls_fw['n'] += 1 + return None if calls_fw['n'] == 1 else SimpleNamespace( + major=1, minor=7, buildtime=1, modules=[]) + + +F.get_fw_info = fw_info_upload +fake_fw = Path('/tmp/opencode-fake2.xpfwext') +fake_fw.write_bytes(b'HPHEADER' + b'\x1a' + b'FW' * 200 + b'S' * 256) +F.FW_CACHE = fake_fw +try: + F.phase_firmware(ARGS) +finally: + fake_fw.unlink(missing_ok=True) +names = [c[0] for c in calls] +check(names.count('write_fw') == 1, 'firmware phase must write the fwext once') +check('write_fw_sig' in names, 'firmware phase must write the signature') +check('write_reg' in names, 'firmware phase must set the pre-upload register') +print('ok firmware (upload)') + +print('--- blobs: thunk form and materialised form') +F.blobs = SimpleNamespace(reset_blob=lambda: b'R' * 10) +check(F.blob('reset_blob') == b'R' * 10, 'blob() must call a thunk') +F.blobs = SimpleNamespace(reset_blob=b'R' * 10) +check(F.blob('reset_blob') == b'R' * 10, + 'blob() must not call an already materialised bytes object') +print('ok blob()') + +print('--- blobs: real module replaces its own thunks') +sys.path.insert(0, str(REPO)) +from validitysensor import blobs as real_blobs # noqa: E402 +from validitysensor import usb as real_usb # noqa: E402 + + +class FakeDev: + idVendor = 0x06cb + idProduct = 0x00bb + + +real_usb.usb.usb_dev = lambda: FakeDev() +data = real_blobs.init_hardcoded() # first call: returns bytes +check(isinstance(data, bytes) and len(data) > 500, + 'first blob call returns the init blob') +check(isinstance(real_blobs.init_hardcoded, bytes), + 'blobs module replaces the thunk with bytes (documented behaviour)') +F.blobs = real_blobs +check(F.blob('init_hardcoded') == data, + 'blob() must still work after the module materialised the blob') +print('ok blob() with the real module') + +print('--- init sequence twice in one process (the "--phase all" crash)') +fake = FakeUsb() +install(fake, [1, 2, 6, 3, 4]) +F.send_init_sequence() +F.send_init_sequence() +check(fake.commands.count(b'\x01') == 2, + 'a second init sequence in the same process must still send the blobs') +print('ok init sequence repeat') + +print('--- services: stop/mask, then unmask/start') +recorded = [] + + +def fake_run(argv, **kwargs): + recorded.append(list(argv)) + return SimpleNamespace(returncode=0) + + +real_units = F.COMPETING_UNITS +F.COMPETING_UNITS = ('fprintd.service',) +try: + with mock.patch('subprocess.run', fake_run): + F.stop_competing_services() + F.restart_services() +finally: + F.COMPETING_UNITS = real_units +flat = [' '.join(argv) for argv in recorded] +check('systemctl mask --runtime python3-validity.service' in flat, + 'must mask the daemon for the duration of the run') +check('systemctl unmask --runtime python3-validity.service' in flat, + 'must unmask the daemon again afterwards') +check('systemctl stop fprintd.service' in flat, 'must stop competing units') +check('systemctl start fprintd.service' in flat, + 'must restart units that were active before') +print('ok services') + +if failures: + print('\n%d SMOKE CHECK(S) FAILED' % len(failures)) + sys.exit(1) +print('\nALL SMOKE TESTS PASSED') diff --git a/validitysensor/blobs.py b/validitysensor/blobs.py index ec43954..a166cd6 100644 --- a/validitysensor/blobs.py +++ b/validitysensor/blobs.py @@ -20,6 +20,13 @@ def __load_blob(blob: str) -> bytes: from . import blobs_9a as blobs elif usb.usb_dev().idProduct == 0x00cb: from . import blobs_00cb as blobs # HP Pavilion x360 14-dh; 0x969, own reset_blob + elif usb.usb_dev().idProduct == 0x00bb: + # HP Spectre x360 13-ap0xxx: same 0x969 silicon (hw id 0x88, + # "57K0 FM-3439-004", W25Q80B) as 06cb:00cb. Verified on hardware: + # init_hardcoded is byte-identical to our Windows capture, and the + # 11973-byte 0x969 reset_blob (== blobs_d51) is accepted and wipes + # the flash. See uunicorn/python-validity#283. + from . import blobs_00cb as blobs globals()[blob] = getattr(blobs, blob) return globals()[blob] diff --git a/validitysensor/firmware_tables.py b/validitysensor/firmware_tables.py index b89c5a0..8fb63e6 100644 --- a/validitysensor/firmware_tables.py +++ b/validitysensor/firmware_tables.py @@ -37,6 +37,17 @@ 'driver': 'https://ftp.hp.com/pub/softpaq/sp138001-138500/sp138431.exe', 'referral': 'https://support.hp.com/us-en/drivers', 'sha512': 'b9a268773ac948a4b6bfaa7a5762c58ab482aa47ea321a429ebc8dba3fcdd17ffe750d189791d8e0325b21e95161db5f751c61d7714d7460ee0a406055060a8f' + }, + # HP Spectre x360 13-ap0xxx (06cb:00bb) ships the same 0x969 silicon as + # 06cb:00cb and the same xpfwext payload name. Joentja provisioned the + # exact 00bb machine with "6_07f_hp_mis_qm.xpfwext" from HP SoftPaq + # SP106161; this SP138431 archive carries the same file. If the sensor + # rejects the signature with 044f, override with + # sudo validity-sensors-firmware --driver-uri file:///path/to/softpaq.exe + SupportedDevices.DEV_BB: { + 'driver': 'https://ftp.hp.com/pub/softpaq/sp138001-138500/sp138431.exe', + 'referral': 'https://support.hp.com/us-en/drivers', + 'sha512': 'b9a268773ac948a4b6bfaa7a5762c58ab482aa47ea321a429ebc8dba3fcdd17ffe750d189791d8e0325b21e95161db5f751c61d7714d7460ee0a406055060a8f' } } @@ -51,4 +62,5 @@ SupportedDevices.DEV_AB: '6_07f_hp_cmit_mis_qm.xpfwext', # HP EliteBook 840 G5 SupportedDevices.DEV_B7: '6_07f_hp_cmit_mis_qm.xpfwext', # HP G6 series (same chip family) SupportedDevices.DEV_CB: '6_07f_hp_mis_qm.xpfwext', # HP Pavilion x360 14-dh (0x969) + SupportedDevices.DEV_BB: '6_07f_hp_mis_qm.xpfwext', # HP Spectre x360 13-ap0xxx (0x969) } diff --git a/validitysensor/glib_compat.py b/validitysensor/glib_compat.py new file mode 100644 index 0000000..8bcb49f --- /dev/null +++ b/validitysensor/glib_compat.py @@ -0,0 +1,37 @@ +"""Compatibility helpers for PyGObject API differences.""" + + +def unix_signal_add(): + """Return ``g_unix_signal_add`` with a 4-argument handler signature. + + PyGObject exposes the function under different names depending on the + distribution and version: + + * ``GLib.unix_signal_add`` -- the traditional override, present on most + builds and the one to prefer; + * ``GLibUnix.signal_add`` -- some split-namespace builds; + * ``GLibUnix.signal_add_full`` -- what PyGObject 3.48 ships; it takes the + same ``user_data`` argument, so it can be wrapped directly. + + Every variant is called as ``add(priority, signum, callback, user_data)``. + """ + from gi.repository import GLib + + if hasattr(GLib, 'unix_signal_add'): + return GLib.unix_signal_add + + try: + import gi + gi.require_version('GLibUnix', '2.0') + from gi.repository import GLibUnix + except (ImportError, ValueError): + GLibUnix = None + + if GLibUnix is not None and hasattr(GLibUnix, 'signal_add'): + return GLibUnix.signal_add + if GLibUnix is not None and hasattr(GLibUnix, 'signal_add_full'): + def add(priority, signum, callback, user_data): + return GLibUnix.signal_add_full(priority, signum, callback, user_data) + + return add + raise RuntimeError('No g_unix_signal_add binding is available') diff --git a/validitysensor/tls.py b/validitysensor/tls.py index ec634c3..8addba0 100644 --- a/validitysensor/tls.py +++ b/validitysensor/tls.py @@ -109,8 +109,16 @@ def reset(self): # Info about the host computer def set_hwkey(self, product_name: str, serial_number: str): - hw_key = bytes(product_name, 'ascii') + b'\0' + \ - bytes(serial_number, 'ascii') + b'\0' + # An existing pairing (e.g. created by Windows Hello) may be bound to + # a host key that is not the DMI default. tools/hwkey_probe.py can + # recover it from the cert-store backup; pass the raw bytes hex + # encoded to reuse that pairing without re-provisioning. + override = os.environ.get('PYTHON_VALIDITY_HW_KEY') + if override: + hw_key = unhexlify(override.strip()) + else: + hw_key = bytes(product_name, 'ascii') + b'\0' + \ + bytes(serial_number, 'ascii') + b'\0' # pre-TLS keys self.psk_encryption_key = prf(password_hardcoded, b'GWK' + hw_key, 0x20) diff --git a/validitysensor/usb.py b/validitysensor/usb.py index 2117a74..8a3b257 100644 --- a/validitysensor/usb.py +++ b/validitysensor/usb.py @@ -23,6 +23,7 @@ class SupportedDevices(Enum): DEV_AB = (0x138a, 0x00ab) # HP EliteBook 840 G5 — sensor type 0xd51 DEV_B7 = (0x06cb, 0x00b7) # HP G6 series — sensor type 0xd51 DEV_CB = (0x06cb, 0x00cb) # HP Pavilion x360 14-dh -- sensor type 0x969 + DEV_BB = (0x06cb, 0x00bb) # HP Spectre x360 13-ap0xxx -- sensor type 0x969 @classmethod def from_usbid(cls, vendorid, productid):