한 걸음씩..

드라이버에서 프로세스 검색 본문

프로그래밍

드라이버에서 프로세스 검색

반엘 2013. 7. 17. 11:54
PsSetLoadImageNotifyRoutine, PsRemoveLoadImageNotifyRoutine 함수를 사용하면 

새롭게 Load 되는 프로세스 정보를 알 수 있다. 

하지만 현재 시스템의 모든 프로세스 정보를 알아낼 수는 없다! 

현재 시스템의 모든 프로세스를 알아내기 위해서는 ZwQuerySystemInformation 함수를 사용하면 된다. 

UnDocument 함수이기 때문에 

 NTSTATUS ZwQuerySystemInformation( 
ULONG SystemInformationClass, PVOID SystemInformation, 
ULONG SystemInformationLength, PULONG ReturnLength 
); 

위와 같은 정의를 미리 해 줘야 한다. 사용방법은 아래와 같다.

PSYSTEM_PROCESSES pSysBuffer = NULL;
ULONG ulRet = 0;
NTSTATUS NtStatus = STATUS_UNSUCCESSFUL; 
int i = 0; 

while( 1 )
i += 1;
ulRet = i * sizeof(SYSTEM_PROCESSES); // 현재 프로세스가 몇 개나 떠 있는지 모르기 때문에 while 구문을 사용한다. 
pSysBuffer = ExAllocatePool( NonPagedPool, ulRet );
if ( pSysBuffer == NULL ) 
break; 

RtlZeroMemory( pSysBuffer, ulRet ); 

NtStatus = ZwQuerySystemInformation( 5, (PVOID)pSysBuffer, ulRet, &ulRet ); 
if ( NT_SUCCESS(NtStatus) ) 
{
PSYSTEM_PROCESSES curr = (struct _SYSTEM_PROCESSES *)pSysBuffer; 
PSYSTEM_PROCESSES prev = NULL; 
while( curr != NULL ) 
 // 
 // 상황에 맞는 코드 
 // 
prev = curr; 
if( curr->NextEntryDelta ) 
((char *)curr += curr->NextEntryDelta); 
else 
curr = NULL;
             }
         }
         ExFreePool( pSysBuffer ); 
 }