A forum for reverse engineering, OS internals and malware analysis 

Forum for discussion about kernel-mode development.
 #18518  by Xearinox
 Wed Mar 13, 2013 11:08 pm
Hello.

How to get PID from process name in kernel mode?

Maybe simply answer, but how to get all PIDs, if application has multiple instances, so multiple PIDs exists.

How to get all ?

Thanks.
 #18520  by r2nwcnydc
 Thu Mar 14, 2013 1:41 am
You could use ZwQueryInformationProcess with ProcessImageFileName to get the process' image name and path:
http://msdn.microsoft.com/en-us/library ... s.85).aspx

You can use ZwQuerySystemInformation with SystemProcessInformation to enumerate all process:
http://msdn.microsoft.com/en-us/library ... s.85).aspx

Then you'll just loop over each process in the list, open the process, get its file name, and compare it to the name you want to enumerate. There will be a race condition with this approach, so you'll need to handle that if that concerns you.
 #18558  by darklich
 Sun Mar 17, 2013 5:54 pm
well as r2nwcnydc say, enum the running process and compare the process name with the name you have, then return its PID...

and here is some code:
Code: Select all
DWORD GetProcessIdByName(TCHAR *pName)
{
	TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
	TCHAR szProcessPath[MAX_PATH * 2] = TEXT("<unknown>");
	
	DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;
	int err=0;

	if(!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
	{
		err=GetLastError();
		printf("Error list running process, code: %d\n",err);
		return -1;
	}

  cProcesses = cbNeeded / sizeof(DWORD);
	 

	for(i=1;i<cProcesses;i++)
	{
		if(aProcesses[i] != 4)
		{
		
			HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS,FALSE, aProcesses[i] );
			if (NULL != hProcess )
			{
				HMODULE hMod;
				DWORD cbNeeded;

				if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),&cbNeeded) )
				{
					GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
					if(_tcscmp(pName,szProcessName) == 0)
					{
						CloseHandle(hProcess);
						return aProcesses[i];
					}
				}

				CloseHandle(hProcess);
			}


			hProcess = NULL;
		}
	}

	printf(" process %s not found!\n",pName);
	return -1;
				
}
Hope it helps :)
 #18561  by EP_X0FF
 Mon Mar 18, 2013 2:59 am
How to get PID from process name in kernel mode?
How does all this Tool Help and PSAPI can help in kernel mode?
 #18642  by Brock
 Fri Mar 22, 2013 2:09 am
As mentioned already, usermode TLHELP and PSAPI libs will not help you in kernel land. Using Zw* APIs will though (r2nwcnydc mentions this) ZwQuerySystemInformation and ZwQueryInformationProcess is all you need to do what you ask :lol:
 #18656  by reverser
 Fri Mar 22, 2013 8:46 pm
EP_X0FF wrote:
How to get PID from process name in kernel mode?
How does all this Tool Help and PSAPI can help in kernel mode?
Sorry, missed that part.
 #21787  by FileSystem_Driver
 Wed Dec 25, 2013 11:17 am
hi ,
You can use the following function to get the handle of the process : :ugeek:
Code: Select all
NTSTATUS NTAPI ObOpenObjectByName 	( 	IN POBJECT_ATTRIBUTES  	ObjectAttributes,
		IN POBJECT_TYPE  	ObjectType,
		IN KPROCESSOR_MODE  	AccessMode,
		IN PACCESS_STATE  	PassedAccessState,
		IN ACCESS_MASK  	DesiredAccess,
		IN OUT PVOID  	ParseContext,
		OUT PHANDLE  	Handle 
	) 	

and then , enter the following code into kernel mode can easily do , :)

DWORD WINAPI GetProcessIDbyProcessHandle(HANDLE hProcess)
{
    if (hProcess == NULL)    return 0xffffffff;
    PTHREAD_START_ROUTINE lpStartAddress = (PTHREAD_START_ROUTINE)
        GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "GetCurrentProcessId");
    if (lpStartAddress == NULL) return 0xffffffff;
 
    HANDLE hProcessAccAdj;
    BOOL bRes = DuplicateHandle(GetCurrentProcess(), 
                                hProcess, GetCurrentProcess(), &hProcessAccAdj, 
                                PROCESS_QUERY_INFORMATION|PROCESS_CREATE_THREAD|
                                PROCESS_VM_OPERATION|PROCESS_VM_WRITE, 
                                FALSE, 0);
    if (!bRes || hProcessAccAdj == NULL)
    {
        UINT unError = GetLastError();
        return 0xffffffff;
    }
 
    DWORD dwThreadID;
    HANDLE hRemoteThread = CreateRemoteThread(hProcessAccAdj, NULL, 
        0, lpStartAddress, 0, 0, &dwThreadID);
    CloseHandle(hProcessAccAdj);
    if (hRemoteThread == NULL) return 0xffffffff;

 
    WaitForSingleObject(hRemoteThread, INFINITE);
    DWORD dwExitCode;
    if (GetExitCodeThread(hRemoteThread, &dwExitCode) == 0)    dwExitCode = 0xffffffff;
    CloseHandle(hRemoteThread);
    return dwExitCode;
}
Last edited by EP_X0FF on Thu Dec 26, 2013 8:19 am, edited 1 time in total. Reason: code tags
 #21795  by EP_X0FF
 Thu Dec 26, 2013 8:20 am
Use [ code ] [ / code] tags and stay away from posting in dead 9 months old thread.
Necroposting, closed.