|
| 1 | + |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <windows.h> |
| 6 | +#include "request.h" |
| 7 | +#include "irpmondll.h" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +static volatile BOOL _terminate = FALSE; |
| 12 | + |
| 13 | + |
| 14 | +static BOOL WINAPI _CtrlHandler(DWORD fdwCtrlType) |
| 15 | +{ |
| 16 | + BOOL ret = FALSE; |
| 17 | + |
| 18 | + switch (fdwCtrlType) { |
| 19 | + case CTRL_CLOSE_EVENT: |
| 20 | + case CTRL_C_EVENT: |
| 21 | + _terminate = TRUE; |
| 22 | + ret = TRUE; |
| 23 | + break; |
| 24 | + default: |
| 25 | + break; |
| 26 | + } |
| 27 | + |
| 28 | + return ret; |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +int main(int argc, char* argv[]) |
| 33 | +{ |
| 34 | + int ret = 0; |
| 35 | + IRPMON_INIT_INFO initInfo; |
| 36 | + HANDLE driverHandle = NULL; |
| 37 | + HANDLE deviceHandle = NULL; |
| 38 | + DRIVER_MONITOR_SETTINGS driverSettings; |
| 39 | + PREQUEST_HEADER buffer = NULL; |
| 40 | + DWORD bufferSize = 0; |
| 41 | + IRPMNDRV_SETTINGS globalSettings; |
| 42 | + |
| 43 | + if (!SetConsoleCtrlHandler(_CtrlHandler, TRUE)) { |
| 44 | + ret = GetLastError(); |
| 45 | + fprintf(stderr, "[ERROR]: Unable to register Control Handler: %u\n", ret); |
| 46 | + return ret; |
| 47 | + } |
| 48 | + |
| 49 | + memset(&initInfo, 0, sizeof(initInfo)); |
| 50 | + // We are connecting to driver instance running |
| 51 | + // this computer. Since we specify no device name, |
| 52 | + // default one will be used. |
| 53 | + fprintf(stderr, "[INFO]: Initializing the library...\n"); |
| 54 | + initInfo.ConnectorType = ictDevice; |
| 55 | + ret = IRPMonDllInitialize(&initInfo); |
| 56 | + if (ret == 0) { |
| 57 | + memset(&driverSettings, 0, sizeof(driverSettings)); |
| 58 | + // Watch for AddDevice, IRPs, IRP completions, FastIOs and driver unload |
| 59 | + driverSettings.MonitorAddDevice = TRUE; |
| 60 | + driverSettings.MonitorIRP = TRUE; |
| 61 | + driverSettings.MonitorIRPCompletion = TRUE; |
| 62 | + driverSettings.MonitorUnload = TRUE; |
| 63 | + // Let's capture also data specific to each request. |
| 64 | + driverSettings.MonitorData = TRUE; |
| 65 | + // Since we plan to monitor only one device, devices |
| 66 | + // appearing after we "go to town" do not interest us. |
| 67 | + // StartIo does not interest us too. |
| 68 | + // Also, FastIO is not used with keyboard drivers, |
| 69 | + // so let's skip it too. |
| 70 | + driverSettings.MonitorFastIo = FALSE; |
| 71 | + driverSettings.MonitorNewDevices = FALSE; |
| 72 | + driverSettings.MonitorStartIo = FALSE; |
| 73 | + // We are interested in all types of IRPs |
| 74 | + for (size_t i = 0; i < sizeof(driverSettings.IRPSettings) / sizeof(driverSettings.IRPSettings[0]); ++i) |
| 75 | + driverSettings.IRPSettings[i] = TRUE; |
| 76 | + |
| 77 | + fprintf(stderr, "[INFO]: Hooking the Keyboard Class driver...\n"); |
| 78 | + ret = IRPMonDllHookDriver(L"\\Driver\\kbdclass", &driverSettings, FALSE, &driverHandle, NULL); |
| 79 | + if (ret == ERROR_ALREADY_EXISTS) { |
| 80 | + ULONG count = 0; |
| 81 | + PHOOKED_DRIVER_UMINFO driverHookInfo = NULL; |
| 82 | + PHOOKED_DRIVER_UMINFO tmp = NULL; |
| 83 | + |
| 84 | + fprintf(stderr, "[WARNING]: Driver already hooked. Let's unhook it first\n"); |
| 85 | + ret = IRPMonDllDriverHooksEnumerate(&driverHookInfo, &count); |
| 86 | + if (ret == 0) { |
| 87 | + ret = ERROR_FILE_NOT_FOUND; |
| 88 | + tmp = driverHookInfo; |
| 89 | + for (size_t i = 0; i < count; ++i) { |
| 90 | + if (tmp->DriverName != NULL && wcsicmp(tmp->DriverName, L"\\Driver\\kbdclass") == 0) { |
| 91 | + fprintf(stderr, "[INFO]: Found (ID 0x%p)\n", tmp->ObjectId); |
| 92 | + ret = IRPMonDllOpenHookedDriver(tmp->ObjectId, &driverHandle); |
| 93 | + if (ret == 0) { |
| 94 | + ret = IRPMonDllUnhookDriver(driverHandle); |
| 95 | + if (ret != 0) |
| 96 | + fprintf(stderr, "[ERROR]: Unable to unhook the driver: %u\n", ret); |
| 97 | + |
| 98 | + IRPMonDllCloseHookedDriverHandle(driverHandle); |
| 99 | + } else fprintf(stderr, "[ERROR]: Unable to get hook driver handle: %u\n", ret); |
| 100 | + |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + ++tmp; |
| 105 | + } |
| 106 | + |
| 107 | + IRPMonDllDriverHooksFree(driverHookInfo, count); |
| 108 | + } else fprintf(stderr, "[ERROR]: Unable to get list of hooked drivers: %u\n", ret); |
| 109 | + |
| 110 | + if (ret == 0) { |
| 111 | + ret = IRPMonDllHookDriver(L"\\Driver\\kbdclass", &driverSettings, FALSE, &driverHandle, NULL); |
| 112 | + if (ret != 0) |
| 113 | + fprintf(stderr, "[ERROR]: Unable to hook the driver %u\n", ret); |
| 114 | + } |
| 115 | + } else if (ret != 0) |
| 116 | + fprintf(stderr, "[ERROR]: Error %u\n", ret); |
| 117 | + |
| 118 | + if (ret == 0) { |
| 119 | + fprintf(stderr, "[INFO]: Hooking the primary keyboard device...\n"); |
| 120 | + ret = IRPMonDllHookDeviceByName(L"\\Device\\KeyboardClass0", &deviceHandle, NULL); |
| 121 | + if (ret == 0) { |
| 122 | + // Attempt to chan ge the global driver settings in order |
| 123 | + // not to get information about all drivers and devices and running processed |
| 124 | + // after connecting to the request queue. |
| 125 | + ret = IRPMonDllSettingsQuery(&globalSettings); |
| 126 | + if (ret == 0) { |
| 127 | + globalSettings.DriverSnapshotOnConnect = FALSE; |
| 128 | + globalSettings.ProcessEmulateOnConnect = FALSE; |
| 129 | + ret = IRPMonDllSettingsSet(&globalSettings, FALSE); |
| 130 | + if (ret != 0) |
| 131 | + fprintf(stderr, "[WARNING]: Unable to alter global driver settings: %u\n", ret); |
| 132 | + } else fprintf(stderr, "[WARNING]: Unable to query global driver settings: %u\n", ret); |
| 133 | + |
| 134 | + // Now, everything is set. Let's tell the IRPMon driver |
| 135 | + // to start actually monitoring the requests. |
| 136 | + fprintf(stderr, "[INFO]: Starting to watch for requests...\n"); |
| 137 | + ret = IRPMonDllDriverStartMonitoring(driverHandle); |
| 138 | + if (ret == 0) { |
| 139 | + // Connect to the request queue. |
| 140 | + fprintf(stderr, "[INFO]: Connecting to the request queue...\n"); |
| 141 | + ret = IRPMonDllConnect(); |
| 142 | + if (ret == 0) { |
| 143 | + fprintf(stderr, "[INFO]: We are now set! Press CTRL+C to stop monitoring and exit the application peacefully\n"); |
| 144 | + while (!_terminate) { |
| 145 | + // Attempt to get a request from the queue |
| 146 | + ret = IRPMonDllGetRequest(buffer, bufferSize); |
| 147 | + switch (ret) { |
| 148 | + case ERROR_SUCCESS: { |
| 149 | + PREQUEST_HEADER request = NULL; |
| 150 | + |
| 151 | + // Since multiple requests can be returned |
| 152 | + // in one call to IRPMonDllGetRequest, let's |
| 153 | + // carefully examine all of them. |
| 154 | + request = buffer; |
| 155 | + do { |
| 156 | + PREQUEST_IRP irp = NULL; |
| 157 | + PREQUEST_IRP_COMPLETION irpc = NULL; |
| 158 | + PREQUEST_ADDDEVICE ad = NULL; |
| 159 | + PREQUEST_UNLOAD du = NULL; |
| 160 | + |
| 161 | + switch (request->Type) { |
| 162 | + case ertIRP: |
| 163 | + irp = CONTAINING_RECORD(request, REQUEST_IRP, Header); |
| 164 | + fprintf(stderr, "[IRP]: irp=0x%p, major=%u, minor=%u, datasize=%zu\n", irp->IRPAddress, irp->MajorFunction, irp->MinorFunction, irp->DataSize); |
| 165 | + break; |
| 166 | + case ertIRPCompletion: |
| 167 | + irpc = CONTAINING_RECORD(request, REQUEST_IRP_COMPLETION, Header); |
| 168 | + fprintf(stderr, "[IRPCOMPLETE]: irp=0x%p, status=0x%lx, info=%zu, datasize=%zu\n", irpc->IRPAddress, irpc->CompletionStatus, irpc->CompletionInformation, irpc->DataSize); |
| 169 | + break; |
| 170 | + case ertAddDevice: |
| 171 | + ad = CONTAINING_RECORD(request, REQUEST_ADDDEVICE, Header); |
| 172 | + fprintf(stderr, "[ADDDEVICE]: driver=0x%p, device=0x%p\n", ad->Header.Driver, ad->Header.Device); |
| 173 | + break; |
| 174 | + case ertDriverUnload: |
| 175 | + du = CONTAINING_RECORD(request, REQUEST_UNLOAD, Header); |
| 176 | + fprintf(stderr, "[UNLOAD]: driver=0x%p", du->Header.Driver); |
| 177 | + break; |
| 178 | + default: |
| 179 | + fprintf(stderr, "[REQUEST]: Type %u\n", request->Type); |
| 180 | + break; |
| 181 | + } |
| 182 | + |
| 183 | + // This value is nonzero when the buffer returned |
| 184 | + // by the driver contains at least one request behind |
| 185 | + // this one. If the REQUEST_FLAG_NEXT_AVAILABLE is set |
| 186 | + // in the request->Flags, more requests are still present |
| 187 | + // in the request queue in the kernel. |
| 188 | + if (request->Entry.Flink == NULL) |
| 189 | + break; |
| 190 | + |
| 191 | + request = (PREQUEST_HEADER)((unsigned char *)request + RequestGetSize(request)); |
| 192 | + } while (TRUE); |
| 193 | + } break; |
| 194 | + case ERROR_INSUFFICIENT_BUFFER: { |
| 195 | + PREQUEST_HEADER tmp = NULL; |
| 196 | + |
| 197 | + // Our buffer is not large enough, let's resize it! |
| 198 | + fprintf(stderr, "[WARNING]: Buffer of size %u is not enough, enlarging to %u\n", bufferSize, bufferSize*2 + 128); |
| 199 | + bufferSize = bufferSize*2 + 128; |
| 200 | + tmp = realloc(buffer, bufferSize); |
| 201 | + if (tmp == NULL) { |
| 202 | + fprintf(stderr, "[ERROR]: Buffer allocation failed with errno of %u\n", errno); |
| 203 | + _terminate = TRUE; |
| 204 | + continue; |
| 205 | + } |
| 206 | + |
| 207 | + buffer = tmp; |
| 208 | + } break; |
| 209 | + case ERROR_NO_MORE_ITEMS: |
| 210 | + // The queue is empty. Well, this is very common |
| 211 | + // for keyboard devices. Let's just wait. |
| 212 | + fputc('.', stderr); |
| 213 | + Sleep(1000); |
| 214 | + break; |
| 215 | + default: |
| 216 | + // An unexpected error occurred. |
| 217 | + // Well, no software is perfect, right? |
| 218 | + fprintf(stderr, "[ERROR]: Unable to get request: %u\n", ret); |
| 219 | + _terminate = TRUE; |
| 220 | + break; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + // Let's free the buffer. This will also work |
| 225 | + // when the buffer is NUULL (i.e. have never been allocated). |
| 226 | + free(buffer); |
| 227 | + fprintf(stderr, "[INFO]: Disconnecting from the request queue\n"); |
| 228 | + ret = IRPMonDllDisconnect(); |
| 229 | + if (ret != 0) |
| 230 | + fprintf(stderr, "[WARNING]: Error %u\n", ret); |
| 231 | + } else fprintf(stderr, "[ERROR]: Error %u\n", ret); |
| 232 | + |
| 233 | + fprintf(stderr, "[INFO]: Stopping the monitoring\n"); |
| 234 | + ret = IRPMonDllDriverStopMonitoring(driverHandle); |
| 235 | + if (ret != 0) |
| 236 | + fprintf(stderr, "[WARNING]: Error %u\n", ret); |
| 237 | + } else fprintf(stderr, "[ERROR]: Error %u\n", ret); |
| 238 | + |
| 239 | + fprintf(stderr, "[INFO]: Unhooking the primary keyboard device\n"); |
| 240 | + ret = IRPMonDllUnhookDevice(deviceHandle); |
| 241 | + if (ret != 0) |
| 242 | + fprintf(stderr, "[WARNING]: Error %u\n", ret); |
| 243 | + } else fprintf(stderr, "[ERROR]: Error %u\n", ret); |
| 244 | + |
| 245 | + fprintf(stderr, "[INFO]: Unhooking the Keyboard Class driver\n"); |
| 246 | + ret = IRPMonDllUnhookDriver(driverHandle); |
| 247 | + if (ret != 0) |
| 248 | + fprintf(stderr, "[WARNING]: Error %u\n", ret); |
| 249 | + } |
| 250 | + |
| 251 | + fprintf(stderr, "[INFO]: Cleanup the library\n"); |
| 252 | + IRPMonDllFinalize(); |
| 253 | + } else fprintf(stderr, "[ERROR]: Error %u\n", ret); |
| 254 | + |
| 255 | + SetConsoleCtrlHandler(_CtrlHandler, FALSE); |
| 256 | + |
| 257 | + return ret; |
| 258 | +} |
0 commit comments