windows 驱动实例分析系列: HidHide驱动分析-HidHideCLI 篇(三)
HidHide 驱动分析 - HidHideCLI 篇三设备枚举与路径转换一、HID 设备枚举的核心实现HID.cpp模块实现了完整的设备枚举与信息采集这些功能与 GUI 版本共享相同的核心逻辑但在输出格式上存在差异——CLI 版本输出 JSON而 GUI 版本填充树形控件。1.1 设备枚举入口HidDevices 函数FriendlyNamesAndHidDeviceInformationHidDevices(boolgamingDevicesOnly){BaseContainerDeviceInstancePathAndHidDeviceInformation baseContainerMap;IterateAllHidDevicesPresentOrNotPresent([](DeviceInstancePathconstpath,std::filesystem::pathconstsymlink){autoconstinfoHidModelInfo(path,symlink);baseContainerMap.emplace(std::make_pair(info.baseContainerDeviceInstancePath,info));returntrue;});// 按容器聚合生成友好名称// ...}流程分为两步枚举所有 HID 设备包括已断开但残留的调用HidModelInfo获取详细信息。按baseContainerDeviceInstancePath基容器设备路径聚合同一物理设备的多个 HID 子设备归为一组。1.2 设备存在性检测与符号链接获取获取 HID 接口 GUIDGUID hidDeviceInterfaceGuid;HidD_GetHidGuid(hidDeviceInterfaceGuid);枚举设备实例DeviceInstancePathsDeviceInstancePathsPresentOrNot(GUIDconstclassGuid){CM_Get_Device_ID_List_SizeW(needed,GuidToString(classGuid).c_str(),CM_GETIDLIST_FILTER_CLASS);std::vectorWCHARbuffer(needed);CM_Get_Device_ID_ListW(GuidToString(classGuid).c_str(),buffer.data(),needed,CM_GETIDLIST_FILTER_CLASS);returnStringListToStringSet(MultiStringToStringList(buffer));}CM_GETIDLIST_FILTER_CLASS标志确保只返回 HID 类设备而CM_GETIDLIST_FILTER_PRESENT未被使用因此同时包含已连接和已断开设备。设备存在性判断boolDevicePresent(DeviceInstancePathconstpath){DEVINST devInst;autoresultCM_Locate_DevNodeW(devInst,path.c_str(),CM_LOCATE_DEVNODE_NORMAL);return(CR_SUCCESSresult);}CM_LOCATE_DEVNODE_NORMAL标志要求设备当前必须存在不能为幻影节点。1.3 设备详细信息的采集HidModelInfo函数是信息采集的核心它打开设备的符号链接通过 HID API 获取详细信息打开设备autodeviceObjectCloseHandlePtr(CreateFileW(symbolicLink.c_str(),GENERIC_READ,(FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE),nullptr,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,nullptr),CloseHandle);当设备被其他应用独占打开ERROR_ACCESS_DENIED或被 HidHide 自身隐藏ERROR_SHARING_VIOLATION时返回特殊的 “denied” 状态标记。获取 HID 属性HIDD_ATTRIBUTES attributes;HidD_GetAttributes(deviceObject.get(),attributes);// attributes.VendorID, attributes.ProductID, attributes.VersionNumber获取设备字符串HidD_GetProductString(deviceObject.get(),buffer.data(),buffer.size());HidD_GetManufacturerString(deviceObject.get(),buffer.data(),buffer.size());HidD_GetSerialNumberString(deviceObject.get(),buffer.data(),buffer.size());获取 HID 能力Usage Page / UsagePHIDP_PREPARSED_DATA preParsedData;HidD_GetPreparsedData(deviceObject.get(),preParsedData);HIDP_CAPS capabilities;HidP_GetCaps(preParsedData,capabilities);Usage Page 和 Usage 用于判断设备类型和生成可读的用法名称通过内置的字符串表资源。1.4 游戏设备判定逻辑boolGamingDevice(HIDD_ATTRIBUTESconstattributes,HIDP_CAPSconstcapabilities){// Valve Steam 控制器特殊处理if((attributes.VendorID0x28DE)(attributes.ProductID0x1205))returntrue;if((attributes.VendorID0x28DE)(attributes.ProductID0x1142))returntrue;// Usage Page 0x05游戏控制器或 Usage Page 0x01 下的 Usage 0x04摇杆/ 0x05游戏手柄return(0x05capabilities.UsagePage)||(0x01capabilities.UsagePage(0x04capabilities.Usage||0x05capabilities.Usage));}此判定逻辑覆盖了绝大多数主流游戏外设包括标准 DirectInput 设备和特定厂商设备。1.5 容器聚合Base Container获取容器 IDGUIDBaseContainerId(DeviceInstancePathconstpath){DEVINST devInst;CM_Locate_DevNodeW(devInst,path.c_str(),CM_LOCATE_DEVNODE_PHANTOM);CM_Get_DevNode_PropertyW(devInst,DEVPKEY_Device_ContainerId,devPropType,...);}容器 ID 是 PnP 管理器用于将属于同一物理设备的所有子设备归组的标识符。获取基容器路径DeviceInstancePathBaseContainerDeviceInstancePath(DeviceInstancePathconstpath){autoconstbaseContainerIdBaseContainerId(path);for(autoitpath;;){autoparentDeviceInstancePathParent(it);if(baseContainerIdBaseContainerId(parent))itparent;elsereturnit;}}沿设备树向上追溯直到找到容器 ID 与当前设备不同的父节点该父节点即为基容器设备。获取容器设备计数size_tBaseContainerDeviceCount(DeviceInstancePathconstpath){CM_Get_Child(devInstChild,devInst,0);for(size_t count1;;count){CM_Get_Sibling(devInstChild,devInstChild,0);// 到达末尾返回 count}}用于判断基容器下的所有子设备是否均为 HID 设备从而决定是否可以整体隐藏该容器。二、路径转换子系统Volume 模块Volume.cpp实现了逻辑盘符与 NT 设备路径之间的双向转换这是白名单管理的核心依赖。2.1 完整路径与全镜像名的概念概念示例说明逻辑路径C:\Program Files\app.exe用户界面使用的盘符路径全镜像名Full Image Name\Device\HarddiskVolume1\Program Files\app.exe驱动白名单使用的 NT 设备路径驱动使用全镜像名是因为盘符是逻辑概念在系统启动早期或不同用户会话中可能变化而设备路径是稳定的。2.2 逻辑路径 → 全镜像名FileNameToFullImageNameFullImageNameFileNameToFullImageName(std::filesystem::pathconstfullyQualifiedFileName){autoconstvolumeMountPointFindVolumeMountPointForFullyQualifiedFileName(fullyQualifiedFileName);if(volumeMountPoint.empty())return{};autoconstdosDeviceNameDosDeviceNameForVolumeName(VolumeNameForVolumeMountPoint(volumeMountPoint));autoconstfileNameWithoutMountPointfullyQualifiedFileName.native().substr(volumeMountPoint.native().size());returndosDeviceName/fileNameWithoutMountPoint;}实现步骤FindVolumeMountPointForFullyQualifiedFileName遍历所有卷挂载点找到最长匹配的逻辑路径前缀如C:\。VolumeNameForVolumeMountPoint将挂载点如C:\转换为卷名称如\\?\Volume{...}。DosDeviceNameForVolumeName将卷名称转换为 NT 设备名称如\Device\HarddiskVolume1。拼接设备名称和文件相对路径得到完整镜像名。2.3 全镜像名 → 逻辑路径FullImageNameToFileNamestd::filesystem::pathFullImageNameToFileName(FullImageNameconstfullImageName){autoconst[volumeMountPoint,dosDeviceName]FindVolumeMountPointAndDosDeviceNameForFullImageName(fullImageName);if(volumeMountPoint.empty())return{};autoconstfullImageNameWithoutDosDeviceNamefullImageName.native().substr(dosDeviceName.native().size()1);returnvolumeMountPoint/fullImageNameWithoutDosDeviceName;}实现步骤FindVolumeMountPointAndDosDeviceNameForFullImageName遍历所有卷找到与全镜像名前缀匹配的卷。剥离设备名称前缀得到相对路径。拼接挂载点逻辑盘符与相对路径。2.4 遍历所有卷的机制voidIterateAllVolumeMountPointsForFileStorage(IterateAllVolumeMountPointsForFileStorageCallback cb){std::vectorWCHARvolumeName(UNICODE_STRING_MAX_CHARS);autofindVolumeClosePtrFindVolumeClosePtr(FindFirstVolumeW(volumeName.data(),...),FindVolumeClose);while(true){for(autoconstvolumeMountPoint:VolumeMountPoints(volumeName.data())){if(!cb(volumeName.data(),volumeMountPoint))return;}if(!FindNextVolumeW(...))break;}}FindFirstVolumeW和FindNextVolumeW枚举系统中的所有卷GetVolumePathNamesForVolumeNameW获取每个卷的所有挂载点包括盘符和挂载文件夹。2.5 盘符变化的鲁棒性由于转换过程不依赖盘符的静态映射即使系统盘符发生变化如 D: 变为 E:只要 NT 设备名称不变白名单依然能正确匹配。这一设计使得 HidHide 的配置在系统盘符变更后依然有效。