Skip to content

Commit

Permalink
[kbdsample]: Unhook the keyboard driver in case it is already hooked
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinDrab committed Oct 19, 2020
1 parent 34b5e6a commit 95504bd
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions kbdsample/kbdsample.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ int main(int argc, char* argv[])
HANDLE deviceHandle = NULL;
DRIVER_MONITOR_SETTINGS driverSettings;
PREQUEST_HEADER buffer = NULL;
PREQUEST_HEADER tmp = NULL;
DWORD bufferSize = 0;
IRPMNDRV_SETTINGS globalSettings;

Expand Down Expand Up @@ -77,6 +76,44 @@ int main(int argc, char* argv[])

fprintf(stderr, "[INFO]: Hooking the Keyboard Class driver...\n");
ret = IRPMonDllHookDriver(L"\\Driver\\kbdclass", &driverSettings, FALSE, &driverHandle, NULL);
if (ret == ERROR_ALREADY_EXISTS) {
ULONG count = 0;
PHOOKED_DRIVER_UMINFO driverHookInfo = NULL;
PHOOKED_DRIVER_UMINFO tmp = NULL;

fprintf(stderr, "[WARNING]: Driver already hooked. Let's unhook it first\n");
ret = IRPMonDllDriverHooksEnumerate(&driverHookInfo, &count);
if (ret == 0) {
ret = ERROR_FILE_NOT_FOUND;
tmp = driverHookInfo;
for (size_t i = 0; i < count; ++i) {
if (tmp->DriverName != NULL && wcsicmp(tmp->DriverName, L"\\Driver\\kbdclass") == 0) {
fprintf(stderr, "[INFO]: Found (ID 0x%p)\n", tmp->ObjectId);
ret = IRPMonDllOpenHookedDriver(tmp->ObjectId, &driverHandle);
if (ret == 0) {
ret = IRPMonDllUnhookDriver(driverHandle);
if (ret != 0)
fprintf(stderr, "[ERROR]: Unable to unhook the driver: %u\n", ret);

IRPMonDllCloseHookedDriverHandle(driverHandle);
} else fprintf(stderr, "[ERROR]: Unable to get hook driver handle: %u\n", ret);

break;
}

++tmp;
}

IRPMonDllDriverHooksFree(driverHookInfo, count);
} else fprintf(stderr, "[ERROR]: Unable to get list of hooked drivers: %u\n", ret);

if (ret == 0) {
ret = IRPMonDllHookDriver(L"\\Driver\\kbdclass", &driverSettings, FALSE, &driverHandle, NULL);
if (ret != 0)
fprintf(stderr, "[ERROR]: Unable to hook the driver %u\n", ret);
}
} else fprintf(stderr, "[ERROR]: Error %u\n", ret);

if (ret == 0) {
fprintf(stderr, "[INFO]: Hooking the primary keyboard device...\n");
ret = IRPMonDllHookDeviceByName(L"\\Device\\KeyboardClass0", &deviceHandle, NULL);
Expand Down Expand Up @@ -152,7 +189,9 @@ int main(int argc, char* argv[])
request = (PREQUEST_HEADER)((unsigned char *)request + RequestGetSize(request));
} while (TRUE);
} break;
case ERROR_INSUFFICIENT_BUFFER:
case ERROR_INSUFFICIENT_BUFFER: {
PREQUEST_HEADER tmp = NULL;

// Our buffer is not large enough, let's resize it!
fprintf(stderr, "[WARNING]: Buffer of size %u is not enough, enlarging to %u\n", bufferSize, bufferSize*2 + 128);
bufferSize = bufferSize*2 + 128;
Expand All @@ -164,7 +203,7 @@ int main(int argc, char* argv[])
}

buffer = tmp;
break;
} break;
case ERROR_NO_MORE_ITEMS:
// The queue is empty. Well, this is very common
// for keyboard devices. Let's just wait.
Expand Down Expand Up @@ -205,7 +244,7 @@ int main(int argc, char* argv[])
ret = IRPMonDllUnhookDriver(driverHandle);
if (ret != 0)
fprintf(stderr, "[WARNING]: Error %u\n", ret);
} else fprintf(stderr, "[ERROR]: Error %u\n", ret);
}

fprintf(stderr, "[INFO]: Cleanup the library\n");
IRPMonDllFinalize();
Expand Down

0 comments on commit 95504bd

Please sign in to comment.