using fNtQuerySystemInformation
=
NTSTATUS(WINAPI
*
)(
ULONG SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
);
/
/
handle information
typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO
{
USHORT UniqueProcessId;
USHORT CreatorBackTraceIndex;
UCHAR ObjectTypeIndex;
UCHAR HandleAttributes;
USHORT HandleValue;
PVOID
Object
;
ULONG GrantedAccess;
} SYSTEM_HANDLE_TABLE_ENTRY_INFO,
*
PSYSTEM_HANDLE_TABLE_ENTRY_INFO;
/
/
handle table information
typedef struct _SYSTEM_HANDLE_INFORMATION
{
ULONG NumberOfHandles;
SYSTEM_HANDLE_TABLE_ENTRY_INFO Handles[
1
];
} SYSTEM_HANDLE_INFORMATION,
*
PSYSTEM_HANDLE_INFORMATION;
int
main()
{
ULONG returnLength
=
0
;
fNtQuerySystemInformation NtQuerySystemInformation
=
(fNtQuerySystemInformation)GetProcAddress(GetModuleHandle(L
"ntdll"
),
"NtQuerySystemInformation"
);
PSYSTEM_HANDLE_INFORMATION handleTableInformation
=
(PSYSTEM_HANDLE_INFORMATION)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SystemHandleInformationSize);
NtQuerySystemInformation(SystemHandleInformation, handleTableInformation,SystemHandleInformationSize, &returnLength);
for
(
int
i
=
0
; i < handleTableInformation
-
>NumberOfHandles; i
+
+
)
{
SYSTEM_HANDLE_TABLE_ENTRY_INFO handleInfo
=
(SYSTEM_HANDLE_TABLE_ENTRY_INFO)handleTableInformation
-
>Handles[i];
/
/
指定进程的PID,
16
进制的形式
if
(handleInfo.UniqueProcessId
=
=
0x1234
)
{
printf_s(
"Handle 0x%x at 0x%p, PID: %x\n"
, handleInfo.HandleValue, handleInfo.
Object
, handleInfo.UniqueProcessId);
}
else
{
break
;
}
}
return
0
;
}