typedef struct _HANDLE_TABLE_ENTRY
{
union
{
LONG_PTR VolatileLowValue;
LONG_PTR LowValue;
PVOID InfoTable;
LONG_PTR RefCountField;
struct
{
ULONG_PTR Unlocked :
1
;
ULONG_PTR RefCnt :
16
;
ULONG_PTR Attributes :
3
;
ULONG_PTR ObjectPointerBits :
44
;
};
};
/
*
union
{
LONG_PTR HighValue;
struct _HANDLE_TABLE_ENTRY
*
NextFreeHandleEntry;
EXHANDLE LeafHandleValue;
struct
{
ULONG32 GrantedAccessBits :
25
;
ULONG32 NoRightsUpgrade :
1
;
ULONG32 Spare1 :
6
;
};
ULONG32 Spare2;
};
*
/
} HANDLE_TABLE_ENTRY,
*
PHANDLE_TABLE_ENTRY;
typedef struct _HANDLE_TABLE_FREE_LIST
{
ULONG_PTR FreeListLock;
PHANDLE_TABLE_ENTRY FirstFreeHandleEntry;
PHANDLE_TABLE_ENTRY lastFreeHandleEntry;
LONG32 HandleCount;
ULONG32 HighWaterMark;
ULONG32 Reserved[
8
];
} HANDLE_TABLE_FREE_LIST,
*
PHANDLE_TABLE_FREE_LIST;
typedef struct _HANDLE_TABLE
{
ULONG32 NextHandleNeedingPool;
LONG32 ExtraInfoPages;
ULONG_PTR TableCode;
PEPROCESS QuotaProcess;
LIST_ENTRY HandleTableList;
ULONG32 UniqueProcessId;
union
{
ULONG32 Flags;
struct
{
BOOLEAN StrictFIFO :
1
;
BOOLEAN EnableHandleExceptions :
1
;
BOOLEAN Rundown :
1
;
BOOLEAN Duplicated :
1
;
BOOLEAN RaiseUMExceptionOnInvalidHandleClose :
1
;
};
};
ULONG_PTR HandleContentionEvent;
ULONG_PTR HandleTableLock;
union
{
HANDLE_TABLE_FREE_LIST FreeLists[
1
];
BOOLEAN ActualEntry[
32
];
};
PVOID DebugInfo;
} HANDLE_TABLE,
*
PHANDLE_TABLE;
BOOLEAN TestLookupHandleTable(ULONG64 pid) {
PUCHAR lookupAddr
=
(PUCHAR)PsLookupProcessByProcessId;
PVOID E8Addr
=
NULL;
ULONG32 offset
=
0
;
for
(size_t i
=
0
; i <
0x100
; i
+
+
)
{
if
(lookupAddr[i]
=
=
0xe8
) {
E8Addr
=
&lookupAddr[i];
offset
=
*
(PULONG32)(&lookupAddr[i
+
1
]);
break
;
}
}
if
(E8Addr
=
=
NULL) {
return
FALSE;
}
PUCHAR PspReferenceCidTableEntryAddr
=
NULL;
PspReferenceCidTableEntryAddr
=
(PUCHAR)((ULONG64)E8Addr
+
4
+
offset);
PVOID X35Addr
=
NULL;
PLONG64 PsCidTable
=
NULL;
offset
=
0
;
for
(size_t i
=
0
; i <
0x150
; i
+
+
)
{
if
(PspReferenceCidTableEntryAddr[i]
=
=
0x4c
&&
PspReferenceCidTableEntryAddr[i
+
1
]
=
=
0x8b
&&
PspReferenceCidTableEntryAddr[i
+
2
]
=
=
0x35
) {
offset
=
*
(PULONG32)&PspReferenceCidTableEntryAddr[i
+
3
];
X35Addr
=
&PspReferenceCidTableEntryAddr[i
+
3
];
break
;
}
}
if
(offset
=
=
0
) {
return
FALSE;
}
PsCidTable
=
(PLONG64)((ULONG64)X35Addr
+
4
+
offset);
KdPrint((
"[PsCidTable Addr] %p\r\n"
, PsCidTable));
/
/
我们这里只做演示所以默认就是二级句柄表
PHANDLE_TABLE handleTable
=
PsCidTable[
0
];
PULONG64 tableArr
=
(PULONG64)((ULONG64)handleTable
-
>TableCode &
0xfffffffffffffff0
);
KdPrint((
"[HANDLE_TABLE_ENTRY Addr] %p\r\n"
, tableArr));
pid
/
=
4
;
int
pages
=
pid
/
256
;
int
index
=
pid
%
256
;
KdPrint((
"[attributes]pages:%d---index:%d\r\n"
, pages, index));
PHANDLE_TABLE_ENTRY handleTableEntry
=
(PHANDLE_TABLE_ENTRY)(tableArr[pages]
+
0x10
*
index);
KdPrint((
"[HANDLE_TABLE_ENTRY Addr] %p\r\n"
, handleTableEntry));
ULONG64 pointer
=
handleTableEntry
-
>ObjectPointerBits;
PUCHAR proc
=
((pointer <<
4
) |
0xffff000000000000
);
KdPrint((
"[process name] %s\r\n"
, (PCHAR)(proc
+
0x5a8
)));
return
TRUE;
}