|
| 1 | +from typing import Iterator |
| 2 | + |
| 3 | +from dissect.util.ts import wintimestamp |
| 4 | +from flow.record.fieldtypes import windows_path |
| 5 | + |
| 6 | +from dissect.target.exceptions import UnsupportedPluginError |
| 7 | +from dissect.target.helpers.descriptor_extensions import ( |
| 8 | + RegistryRecordDescriptorExtension, |
| 9 | + UserRecordDescriptorExtension, |
| 10 | +) |
| 11 | +from dissect.target.helpers.record import create_extended_descriptor |
| 12 | +from dissect.target.helpers.regutil import RegistryKey, RegistryValueNotFoundError |
| 13 | +from dissect.target.plugin import Plugin, export |
| 14 | +from dissect.target.target import Target |
| 15 | + |
| 16 | +CamRecord = create_extended_descriptor([RegistryRecordDescriptorExtension, UserRecordDescriptorExtension])( |
| 17 | + "windows/registry/cam", |
| 18 | + [ |
| 19 | + ("datetime", "ts"), |
| 20 | + ("string", "device"), |
| 21 | + ("string", "app_name"), |
| 22 | + ("path", "path"), |
| 23 | + ("datetime", "last_started"), |
| 24 | + ("datetime", "last_stopped"), |
| 25 | + ("varint", "duration"), |
| 26 | + ], |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class CamPlugin(Plugin): |
| 31 | + """Plugin that iterates various Capability Access Manager registry key locations.""" |
| 32 | + |
| 33 | + CONSENT_STORES = [ |
| 34 | + "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore", |
| 35 | + "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore", |
| 36 | + ] |
| 37 | + |
| 38 | + def __init__(self, target: Target): |
| 39 | + super().__init__(target) |
| 40 | + self.app_regf_keys = self._find_apps() |
| 41 | + |
| 42 | + def _find_apps(self) -> list[RegistryKey]: |
| 43 | + apps = [] |
| 44 | + for store in self.target.registry.keys(self.CONSENT_STORES): |
| 45 | + for key in store.subkeys(): |
| 46 | + apps.append(key) |
| 47 | + |
| 48 | + return apps |
| 49 | + |
| 50 | + def check_compatible(self) -> None: |
| 51 | + if not self.app_regf_keys: |
| 52 | + raise UnsupportedPluginError("No Capability Access Manager keys found") |
| 53 | + |
| 54 | + def yield_apps(self) -> Iterator[RegistryKey]: |
| 55 | + for app in self.app_regf_keys: |
| 56 | + for key in app.subkeys(): |
| 57 | + if key.name == "NonPackaged": # NonPackaged registry key has more apps, so yield those apps |
| 58 | + yield from key.subkeys() |
| 59 | + else: |
| 60 | + yield key |
| 61 | + |
| 62 | + @export(record=CamRecord) |
| 63 | + def cam(self) -> Iterator[CamRecord]: |
| 64 | + """Iterate Capability Access Manager key locations. |
| 65 | +
|
| 66 | + The Capability Access Manager keeps track of processes that access I/O devices, like the webcam or microphone. |
| 67 | + Applications are divided into packaged and non-packaged applications meaning Microsoft or |
| 68 | + non-Microsoft applications. |
| 69 | +
|
| 70 | + References: |
| 71 | + - https://docs.velociraptor.app/exchange/artifacts/pages/windows.registry.capabilityaccessmanager/ |
| 72 | + - https://svch0st.medium.com/can-you-track-processes-accessing-the-camera-and-microphone-7e6885b37072 |
| 73 | +
|
| 74 | + Yields ``CamRecord`` with the following fields: |
| 75 | +
|
| 76 | + .. code-block:: text |
| 77 | +
|
| 78 | + hostname (string): The target hostname. |
| 79 | + domain (string): The target domain. |
| 80 | + ts (datetime): The modification timestamp of the registry key. |
| 81 | + device (string): Name of the device privacy permission where asked for. |
| 82 | + app_name (string): The name of the application. |
| 83 | + path (path): The possible path to the application. |
| 84 | + last_started (datetime): When the application last started using the device. |
| 85 | + last_stopped (datetime): When the application last stopped using the device. |
| 86 | + duration (datetime): How long the application used the device (seconds). |
| 87 | + """ |
| 88 | + |
| 89 | + for key in self.yield_apps(): |
| 90 | + last_started = None |
| 91 | + last_stopped = None |
| 92 | + duration = None |
| 93 | + |
| 94 | + try: |
| 95 | + last_started = wintimestamp(key.value("LastUsedTimeStart").value) |
| 96 | + except RegistryValueNotFoundError: |
| 97 | + self.target.log.warning("No LastUsedTimeStart for application: %s", key.name) |
| 98 | + |
| 99 | + try: |
| 100 | + last_stopped = wintimestamp(key.value("LastUsedTimeStop").value) |
| 101 | + except RegistryValueNotFoundError: |
| 102 | + self.target.log.warning("No LastUsedTimeStop for application: %s", key.name) |
| 103 | + |
| 104 | + if last_started and last_stopped: |
| 105 | + duration = (last_stopped - last_started).seconds |
| 106 | + |
| 107 | + yield CamRecord( |
| 108 | + ts=key.ts, |
| 109 | + device=key.path.split("\\")[-2], |
| 110 | + app_name=key.name, |
| 111 | + path=windows_path(key.name.replace("#", "\\")) if "#" in key.name else None, |
| 112 | + last_started=last_started, |
| 113 | + last_stopped=last_stopped, |
| 114 | + duration=duration, |
| 115 | + _target=self.target, |
| 116 | + _key=key, |
| 117 | + _user=self.target.registry.get_user(key), |
| 118 | + ) |
0 commit comments