| 345 | } |
| 346 | |
| 347 | int ZZwmain(int argc, WCHAR *argv[]) |
| 348 | { |
| 349 | _NtQuerySystemInformation NtQuerySystemInformation = (_NtQuerySystemInformation) |
| 350 | GetLibraryProcAddress("ntdll.dll", "NtQuerySystemInformation"); |
| 351 | _NtDuplicateObject NtDuplicateObject = (_NtDuplicateObject) |
| 352 | GetLibraryProcAddress("ntdll.dll", "NtDuplicateObject"); |
| 353 | _NtQueryObject NtQueryObject = (_NtQueryObject) |
| 354 | GetLibraryProcAddress("ntdll.dll", "NtQueryObject"); |
| 355 | NTSTATUS status; |
| 356 | PSYSTEM_HANDLE_INFORMATION handleInfo; |
| 357 | ULONG handleInfoSize = 0x10000; |
| 358 | ULONG pid; |
| 359 | HANDLE processHandle; |
| 360 | ULONG i; |
| 361 | |
| 362 | if (argc < 2) |
| 363 | { |
| 364 | printf("Usage: handles [pid]\n"); |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | pid = _wtoi(argv[1]); |
| 369 | |
| 370 | if (!(processHandle = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid))) |
| 371 | { |
| 372 | printf("Could not open PID %d! (Don't try to open a system process.)\n", pid); |
| 373 | return 1; |
| 374 | } |
| 375 | |
| 376 | handleInfo = (PSYSTEM_HANDLE_INFORMATION)malloc(handleInfoSize); |
| 377 | |
| 378 | /* NtQuerySystemInformation won't give us the correct buffer size, |
| 379 | so we guess by doubling the buffer size. */ |
| 380 | while ((status = NtQuerySystemInformation( |
| 381 | SystemHandleInformation, |
| 382 | handleInfo, |
| 383 | handleInfoSize, |
| 384 | NULL |
| 385 | )) == STATUS_INFO_LENGTH_MISMATCH) |
| 386 | handleInfo = (PSYSTEM_HANDLE_INFORMATION)realloc(handleInfo, handleInfoSize *= 2); |
| 387 | |
| 388 | /* NtQuerySystemInformation stopped giving us STATUS_INFO_LENGTH_MISMATCH. */ |
| 389 | if (!NT_SUCCESS(status)) |
| 390 | { |
| 391 | printf("NtQuerySystemInformation failed!\n"); |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | for (i = 0; i < handleInfo->HandleCount; i++) |
| 396 | { |
| 397 | SYSTEM_HANDLE handle = handleInfo->Handles[i]; |
| 398 | HANDLE dupHandle = NULL; |
| 399 | POBJECT_TYPE_INFORMATION objectTypeInfo; |
| 400 | PVOID objectNameInfo; |
| 401 | UNICODE_STRING objectName; |
| 402 | ULONG returnLength; |
| 403 | |
| 404 | /* Check if this handle belongs to the PID the user specified. */ |
nothing calls this directly
no test coverage detected