下面的函数在Win2k3、XP下能运行成功, 但在Win2k SP1、SP4下却出现找不到对象的类型为21端口的错误??
//获取csrss.exe进程
ULONG GetCsrPid()
{
NTSTATUS ntStatus;
HANDLE Process, hObject;
NTSTATUS St;
ULONG CsrId = 0;
OBJECT_ATTRIBUTES obj;
CLIENT_ID cid;
POBJECT_NAME_INFORMATION ObjName;
UNICODE_STRING ApiPortName;
PSYSTEM_HANDLE_INFORMATION_EX Handles;
int i;
RtlInitUnicodeString(&ApiPortName, L"\\Windows\\ApiPort");
Handles = GetInfoTable( SystemHandleInformation );
if( Handles == NULL )
{
DbgPrint("GetCsrPid():GetInfoTable Error.\n");
return 0;
}
ObjName = ExAllocatePool( PagedPool, 0x2000 );
DbgPrint("Number of handles %d\n", Handles->NumberOfHandles);
for( i = 0; i != Handles->NumberOfHandles; i++ )
{ //打开的对象的类型是否为21
if (Handles->Information[i].ObjectTypeNumber == 21) //Port object Win2kSP1下找不到21端口
{
InitializeObjectAttributes( &obj, NULL, OBJ_KERNEL_HANDLE, NULL, NULL );
cid.UniqueProcess = (HANDLE)Handles->Information[i].ProcessId;
cid.UniqueThread = 0;
ntStatus = ZwOpenProcess(&Process, PROCESS_DUP_HANDLE, &obj, &cid);
if( NT_SUCCESS(ntStatus) )
{
ntStatus = ZwDuplicateObject( Process, (HANDLE)Handles->Information[i].Handle,
NtCurrentProcess(), &hObject, 0, 0, DUPLICATE_SAME_ACCESS);
if( NT_SUCCESS(ntStatus) )
{
ntStatus = ZwQueryObject( hObject, ObjectNameInformation, ObjName, 0x2000, NULL);
if( NT_SUCCESS(ntStatus) )
{
if (ObjName->Name.Buffer != NULL)
{
if ( wcsncmp(ApiPortName.Buffer, ObjName->Name.Buffer, 20) == 0 )
{
DbgPrint("****************************************************\n");
DbgPrint("Csrss PID:%d\n", Handles->Information[i].ProcessId);
DbgPrint("Csrss Port - %ws\n", ObjName->Name.Buffer);
DbgPrint("****************************************************\n");
CsrId = Handles->Information[i].ProcessId;
ZwClose( Process );
ZwClose( hObject );
CsrId = Handles->Information[i].ProcessId;
ExFreePool( Handles );
ExFreePool( ObjName );
return CsrId;
}
}
} else
DbgPrint("Error in Query Object\n");
ZwClose(hObject);
} else
DbgPrint("Error on duplicating object\n");
ZwClose(Process);
} else
DbgPrint("Could not open process\n");
}
}
ExFreePool( Handles );
ExFreePool( ObjName );
return 0;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课