mirror of
https://gitea.tendokyu.moe/Hay1tsme/bananatools.git
synced 2026-12-05 07:49:50 -06:00
571 lines
15 KiB
C
571 lines
15 KiB
C
#include <windows.h>
|
|
#include <setupapi.h>
|
|
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "hook/table.h"
|
|
|
|
#include "hooklib/setupapi.h"
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
struct setupapi_class {
|
|
const GUID *guid;
|
|
const wchar_t *path;
|
|
HDEVINFO cur_handle;
|
|
};
|
|
|
|
struct setupapi_blocked_class {
|
|
GUID guid;
|
|
const char *name;
|
|
};
|
|
|
|
static const struct setupapi_blocked_class setupapi_blocked_classes[] = {
|
|
{
|
|
/* GUID_DEVINTERFACE_DISK */
|
|
{ 0x53F56307, 0xB6BF, 0x11D0, { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B } },
|
|
"disk drives",
|
|
}, {
|
|
/* GUID_DEVCLASS_DISKDRIVE */
|
|
{ 0x4D36E967, 0xE325, 0x11CE, { 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } },
|
|
"disk drives",
|
|
}, {
|
|
/* GUID_DEVINTERFACE_WPD */
|
|
{ 0x6AC27878, 0xA6FA, 0x4155, { 0xBA, 0x85, 0xF9, 0x8F, 0x49, 0x1D, 0x4F, 0x33 } },
|
|
"portable devices",
|
|
}, {
|
|
/* GUID_DEVINTERFACE_HID */
|
|
{ 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
|
|
"HID devices",
|
|
},
|
|
};
|
|
|
|
/* API hooks */
|
|
|
|
static HDEVINFO WINAPI my_SetupDiGetClassDevsA(
|
|
const GUID *ClassGuid,
|
|
char *Enumerator,
|
|
HWND hwndParent,
|
|
DWORD Flags);
|
|
|
|
static HDEVINFO WINAPI my_SetupDiGetClassDevsW(
|
|
const GUID *ClassGuid,
|
|
wchar_t *Enumerator,
|
|
HWND hwndParent,
|
|
DWORD Flags);
|
|
|
|
static BOOL WINAPI my_SetupDiEnumDeviceInterfaces(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVINFO_DATA *DeviceInfoData,
|
|
const GUID *InterfaceClassGuid,
|
|
DWORD MemberIndex,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData);
|
|
|
|
static BOOL WINAPI my_SetupDiGetDeviceInterfaceDetailW(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData,
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_W *DeviceInterfaceDetailData,
|
|
DWORD DeviceInterfaceDetailDataSize,
|
|
DWORD *RequiredSize,
|
|
SP_DEVINFO_DATA *DeviceInfoData);
|
|
|
|
static BOOL WINAPI my_SetupDiGetDeviceInterfaceDetailA(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData,
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_A *DeviceInterfaceDetailData,
|
|
DWORD DeviceInterfaceDetailDataSize,
|
|
DWORD *RequiredSize,
|
|
SP_DEVINFO_DATA *DeviceInfoData);
|
|
|
|
static BOOL WINAPI my_SetupDiDestroyDeviceInfoList(HDEVINFO DeviceInfoSet);
|
|
|
|
static bool setupapi_block_class(
|
|
const GUID *class_guid,
|
|
HWND hwnd,
|
|
DWORD flags,
|
|
HDEVINFO *result);
|
|
|
|
static void setupapi_track_class_devs(const GUID *class_guid, HDEVINFO handle);
|
|
|
|
static const wchar_t *setupapi_find_phantom_path(
|
|
HDEVINFO set,
|
|
const SP_DEVICE_INTERFACE_DATA *data);
|
|
|
|
/* Links */
|
|
|
|
static HDEVINFO (WINAPI *next_SetupDiGetClassDevsA)(
|
|
const GUID *ClassGuid,
|
|
char *Enumerator,
|
|
HWND hwndParent,
|
|
DWORD Flags);
|
|
|
|
static HDEVINFO (WINAPI *next_SetupDiGetClassDevsW)(
|
|
const GUID *ClassGuid,
|
|
wchar_t *Enumerator,
|
|
HWND hwndParent,
|
|
DWORD Flags);
|
|
|
|
static BOOL (WINAPI *next_SetupDiEnumDeviceInterfaces)(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVINFO_DATA *DeviceInfoData,
|
|
const GUID *InterfaceClassGuid,
|
|
DWORD MemberIndex,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData);
|
|
|
|
static BOOL (WINAPI *next_SetupDiGetDeviceInterfaceDetailW)(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData,
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_W *DeviceInterfaceDetailData,
|
|
DWORD DeviceInterfaceDetailDataSize,
|
|
DWORD *RequiredSize,
|
|
SP_DEVINFO_DATA *DeviceInfoData);
|
|
|
|
static BOOL (WINAPI *next_SetupDiGetDeviceInterfaceDetailA)(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData,
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_A *DeviceInterfaceDetailData,
|
|
DWORD DeviceInterfaceDetailDataSize,
|
|
DWORD *RequiredSize,
|
|
SP_DEVINFO_DATA *DeviceInfoData);
|
|
|
|
static BOOL (WINAPI *next_SetupDiDestroyDeviceInfoList)(HDEVINFO DeviceInfoSet);
|
|
|
|
/* Hook tbl */
|
|
|
|
static const struct hook_symbol setupapi_syms[] = {
|
|
{
|
|
.name = "SetupDiGetClassDevsA",
|
|
.patch = my_SetupDiGetClassDevsA,
|
|
.link = (void *) &next_SetupDiGetClassDevsA,
|
|
}, {
|
|
.name = "SetupDiGetClassDevsW",
|
|
.patch = my_SetupDiGetClassDevsW,
|
|
.link = (void *) &next_SetupDiGetClassDevsW,
|
|
}, {
|
|
.name = "SetupDiEnumDeviceInterfaces",
|
|
.patch = my_SetupDiEnumDeviceInterfaces,
|
|
.link = (void *) &next_SetupDiEnumDeviceInterfaces,
|
|
}, {
|
|
.name = "SetupDiGetDeviceInterfaceDetailW",
|
|
.patch = my_SetupDiGetDeviceInterfaceDetailW,
|
|
.link = (void *) &next_SetupDiGetDeviceInterfaceDetailW,
|
|
}, {
|
|
.name = "SetupDiGetDeviceInterfaceDetailA",
|
|
.patch = my_SetupDiGetDeviceInterfaceDetailA,
|
|
.link = (void *) &next_SetupDiGetDeviceInterfaceDetailA,
|
|
}, {
|
|
.name = "SetupDiDestroyDeviceInfoList",
|
|
.patch = my_SetupDiDestroyDeviceInfoList,
|
|
.link = (void *) &next_SetupDiDestroyDeviceInfoList,
|
|
}
|
|
};
|
|
|
|
static bool setupapi_initted;
|
|
static CRITICAL_SECTION setupapi_lock;
|
|
static struct setupapi_class *setupapi_classes;
|
|
static size_t setupapi_nclasses;
|
|
|
|
HRESULT setupapi_add_phantom_dev(const GUID *iface_class, const wchar_t *path)
|
|
{
|
|
struct setupapi_class *class_;
|
|
struct setupapi_class *new_array;
|
|
HRESULT hr;
|
|
|
|
assert(iface_class != NULL);
|
|
assert(path != NULL);
|
|
|
|
setupapi_hook_init();
|
|
|
|
EnterCriticalSection(&setupapi_lock);
|
|
|
|
new_array = realloc(
|
|
setupapi_classes,
|
|
(setupapi_nclasses + 1) * sizeof(struct setupapi_class));
|
|
|
|
if (new_array == NULL) {
|
|
hr = E_OUTOFMEMORY;
|
|
|
|
goto end;
|
|
}
|
|
|
|
setupapi_classes = new_array;
|
|
|
|
class_ = &setupapi_classes[setupapi_nclasses++];
|
|
class_->guid = iface_class;
|
|
class_->path = path;
|
|
class_->cur_handle = NULL;
|
|
hr = S_OK;
|
|
|
|
end:
|
|
LeaveCriticalSection(&setupapi_lock);
|
|
|
|
return hr;
|
|
}
|
|
|
|
void setupapi_hook_init(void)
|
|
{
|
|
if (setupapi_initted) {
|
|
return;
|
|
}
|
|
|
|
/* The hooks take the lock, so it has to exist before they go live */
|
|
InitializeCriticalSection(&setupapi_lock);
|
|
setupapi_initted = true;
|
|
|
|
setupapi_hook_insert_hooks(NULL);
|
|
}
|
|
|
|
void setupapi_hook_insert_hooks(HMODULE target)
|
|
{
|
|
assert(setupapi_initted);
|
|
|
|
hook_table_apply(
|
|
target,
|
|
"setupapi.dll",
|
|
setupapi_syms,
|
|
_countof(setupapi_syms));
|
|
}
|
|
|
|
static HDEVINFO WINAPI my_SetupDiGetClassDevsA(
|
|
const GUID *ClassGuid,
|
|
char *Enumerator,
|
|
HWND hwndParent,
|
|
DWORD Flags)
|
|
{
|
|
HDEVINFO result;
|
|
|
|
if (setupapi_block_class(ClassGuid, hwndParent, Flags, &result)) {
|
|
return result;
|
|
}
|
|
|
|
result = next_SetupDiGetClassDevsA(
|
|
ClassGuid,
|
|
Enumerator,
|
|
hwndParent,
|
|
Flags);
|
|
|
|
setupapi_track_class_devs(ClassGuid, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
static HDEVINFO WINAPI my_SetupDiGetClassDevsW(
|
|
const GUID *ClassGuid,
|
|
wchar_t *Enumerator,
|
|
HWND hwndParent,
|
|
DWORD Flags)
|
|
{
|
|
HDEVINFO result;
|
|
|
|
if (setupapi_block_class(ClassGuid, hwndParent, Flags, &result)) {
|
|
return result;
|
|
}
|
|
|
|
result = next_SetupDiGetClassDevsW(
|
|
ClassGuid,
|
|
Enumerator,
|
|
hwndParent,
|
|
Flags);
|
|
|
|
setupapi_track_class_devs(ClassGuid, result);
|
|
|
|
return result;
|
|
}
|
|
|
|
static BOOL WINAPI my_SetupDiEnumDeviceInterfaces(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVINFO_DATA *DeviceInfoData,
|
|
const GUID *InterfaceClassGuid,
|
|
DWORD MemberIndex,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData)
|
|
{
|
|
const GUID *guid;
|
|
const wchar_t *path;
|
|
size_t i;
|
|
|
|
if ( DeviceInfoSet == INVALID_HANDLE_VALUE ||
|
|
DeviceInterfaceData == NULL ||
|
|
DeviceInterfaceData->cbSize != sizeof(*DeviceInterfaceData)) {
|
|
goto pass;
|
|
}
|
|
|
|
guid = NULL;
|
|
path = NULL;
|
|
|
|
EnterCriticalSection(&setupapi_lock);
|
|
|
|
for (i = 0 ; i < setupapi_nclasses ; i++) {
|
|
if (DeviceInfoSet == setupapi_classes[i].cur_handle) {
|
|
guid = setupapi_classes[i].guid;
|
|
path = setupapi_classes[i].path;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
LeaveCriticalSection(&setupapi_lock);
|
|
|
|
if (path == NULL) {
|
|
/* Not a list with a phantom device in it */
|
|
goto pass;
|
|
}
|
|
|
|
if (MemberIndex > 0) {
|
|
/* The phantom device is member 0, the real ones follow it */
|
|
MemberIndex--;
|
|
|
|
goto pass;
|
|
}
|
|
|
|
dprintf("SetupAPI: Interface {%08lx-...} -> Device node %S\n",
|
|
guid->Data1,
|
|
path);
|
|
|
|
memcpy(&DeviceInterfaceData->InterfaceClassGuid, guid, sizeof(GUID));
|
|
DeviceInterfaceData->Flags = SPINT_ACTIVE;
|
|
DeviceInterfaceData->Reserved = (ULONG_PTR) path;
|
|
|
|
SetLastError(ERROR_SUCCESS);
|
|
|
|
return TRUE;
|
|
|
|
pass:
|
|
return next_SetupDiEnumDeviceInterfaces(
|
|
DeviceInfoSet,
|
|
DeviceInfoData,
|
|
InterfaceClassGuid,
|
|
MemberIndex,
|
|
DeviceInterfaceData);
|
|
}
|
|
|
|
static BOOL WINAPI my_SetupDiGetDeviceInterfaceDetailW(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData,
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_W *DeviceInterfaceDetailData,
|
|
DWORD DeviceInterfaceDetailDataSize,
|
|
DWORD *RequiredSize,
|
|
SP_DEVINFO_DATA *DeviceInfoData)
|
|
{
|
|
const wchar_t *path;
|
|
size_t nbytes_path;
|
|
size_t nbytes_total;
|
|
|
|
path = setupapi_find_phantom_path(DeviceInfoSet, DeviceInterfaceData);
|
|
|
|
if (path == NULL) {
|
|
return next_SetupDiGetDeviceInterfaceDetailW(
|
|
DeviceInfoSet,
|
|
DeviceInterfaceData,
|
|
DeviceInterfaceDetailData,
|
|
DeviceInterfaceDetailDataSize,
|
|
RequiredSize,
|
|
DeviceInfoData);
|
|
}
|
|
|
|
nbytes_path = (wcslen(path) + 1) * sizeof(wchar_t);
|
|
nbytes_total = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath);
|
|
nbytes_total += nbytes_path;
|
|
|
|
if (RequiredSize != NULL) {
|
|
*RequiredSize = (DWORD) nbytes_total;
|
|
}
|
|
|
|
if ( DeviceInterfaceDetailData == NULL ||
|
|
DeviceInterfaceDetailDataSize < nbytes_total) {
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
if (DeviceInterfaceDetailData->cbSize != sizeof(*DeviceInterfaceDetailData)) {
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
memcpy(DeviceInterfaceDetailData->DevicePath, path, nbytes_path);
|
|
SetLastError(ERROR_SUCCESS);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL WINAPI my_SetupDiGetDeviceInterfaceDetailA(
|
|
HDEVINFO DeviceInfoSet,
|
|
SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData,
|
|
SP_DEVICE_INTERFACE_DETAIL_DATA_A *DeviceInterfaceDetailData,
|
|
DWORD DeviceInterfaceDetailDataSize,
|
|
DWORD *RequiredSize,
|
|
SP_DEVINFO_DATA *DeviceInfoData)
|
|
{
|
|
const wchar_t *path;
|
|
size_t nbytes_path;
|
|
size_t nbytes_total;
|
|
int nchars;
|
|
|
|
path = setupapi_find_phantom_path(DeviceInfoSet, DeviceInterfaceData);
|
|
|
|
if (path == NULL) {
|
|
return next_SetupDiGetDeviceInterfaceDetailA(
|
|
DeviceInfoSet,
|
|
DeviceInterfaceData,
|
|
DeviceInterfaceDetailData,
|
|
DeviceInterfaceDetailDataSize,
|
|
RequiredSize,
|
|
DeviceInfoData);
|
|
}
|
|
|
|
/* Size of the ANSI path including its NUL terminator */
|
|
nchars = WideCharToMultiByte(CP_ACP, 0, path, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (nchars <= 0) {
|
|
return FALSE;
|
|
}
|
|
|
|
nbytes_path = (size_t) nchars;
|
|
nbytes_total = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath);
|
|
nbytes_total += nbytes_path;
|
|
|
|
if (RequiredSize != NULL) {
|
|
*RequiredSize = (DWORD) nbytes_total;
|
|
}
|
|
|
|
if ( DeviceInterfaceDetailData == NULL ||
|
|
DeviceInterfaceDetailDataSize < nbytes_total) {
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
if (DeviceInterfaceDetailData->cbSize != sizeof(*DeviceInterfaceDetailData)) {
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
WideCharToMultiByte(
|
|
CP_ACP,
|
|
0,
|
|
path,
|
|
-1,
|
|
DeviceInterfaceDetailData->DevicePath,
|
|
nchars,
|
|
NULL,
|
|
NULL);
|
|
SetLastError(ERROR_SUCCESS);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL WINAPI my_SetupDiDestroyDeviceInfoList(HDEVINFO DeviceInfoSet)
|
|
{
|
|
size_t i;
|
|
|
|
EnterCriticalSection(&setupapi_lock);
|
|
|
|
for (i = 0 ; i < setupapi_nclasses ; i++) {
|
|
if (setupapi_classes[i].cur_handle == DeviceInfoSet) {
|
|
setupapi_classes[i].cur_handle = NULL;
|
|
}
|
|
}
|
|
|
|
LeaveCriticalSection(&setupapi_lock);
|
|
|
|
return next_SetupDiDestroyDeviceInfoList(DeviceInfoSet);
|
|
}
|
|
|
|
static bool setupapi_block_class(
|
|
const GUID *class_guid,
|
|
HWND hwnd,
|
|
DWORD flags,
|
|
HDEVINFO *result)
|
|
{
|
|
size_t i;
|
|
|
|
#if 0
|
|
if (class_guid != NULL) {
|
|
dprintf("SetupAPI: GetClassDevs for class "
|
|
"{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
|
|
class_guid->Data1,
|
|
class_guid->Data2,
|
|
class_guid->Data3,
|
|
class_guid->Data4[0], class_guid->Data4[1],
|
|
class_guid->Data4[2], class_guid->Data4[3],
|
|
class_guid->Data4[4], class_guid->Data4[5],
|
|
class_guid->Data4[6], class_guid->Data4[7]);
|
|
}
|
|
#endif
|
|
|
|
/* With DIGCF_ALLCLASSES Windows ignores ClassGuid */
|
|
if (class_guid == NULL || (flags & DIGCF_ALLCLASSES)) {
|
|
return false;
|
|
}
|
|
|
|
for (i = 0 ; i < _countof(setupapi_blocked_classes) ; i++) {
|
|
if (IsEqualGUID(class_guid, &setupapi_blocked_classes[i].guid)) {
|
|
dprintf("SetupAPI: Hiding %s\n", setupapi_blocked_classes[i].name);
|
|
|
|
/* An empty list, enumerating it fails with ERROR_NO_MORE_ITEMS.
|
|
A device interface list is not tied to a setup class. */
|
|
*result = SetupDiCreateDeviceInfoList(
|
|
flags & DIGCF_DEVICEINTERFACE ? NULL : class_guid,
|
|
hwnd);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static void setupapi_track_class_devs(const GUID *class_guid, HDEVINFO handle)
|
|
{
|
|
size_t i;
|
|
|
|
if (class_guid == NULL || handle == INVALID_HANDLE_VALUE) {
|
|
return;
|
|
}
|
|
|
|
EnterCriticalSection(&setupapi_lock);
|
|
|
|
for (i = 0 ; i < setupapi_nclasses ; i++) {
|
|
if (IsEqualGUID(class_guid, setupapi_classes[i].guid)) {
|
|
setupapi_classes[i].cur_handle = handle;
|
|
}
|
|
}
|
|
|
|
LeaveCriticalSection(&setupapi_lock);
|
|
}
|
|
|
|
static const wchar_t *setupapi_find_phantom_path(
|
|
HDEVINFO set,
|
|
const SP_DEVICE_INTERFACE_DATA *data)
|
|
{
|
|
const wchar_t *path;
|
|
size_t i;
|
|
|
|
if (set == INVALID_HANDLE_VALUE || data == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
path = NULL;
|
|
|
|
EnterCriticalSection(&setupapi_lock);
|
|
|
|
for (i = 0 ; i < setupapi_nclasses ; i++) {
|
|
if ( set == setupapi_classes[i].cur_handle &&
|
|
data->Reserved == (ULONG_PTR) setupapi_classes[i].path) {
|
|
path = setupapi_classes[i].path;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
LeaveCriticalSection(&setupapi_lock);
|
|
|
|
return path;
|
|
}
|