How to obtain the process name based on handle

Source: Internet
Author: User

When zwterminateprocess hook is written, the parameters that hook zwterminateprocess are handle processhandle and ntstatus exitstatus. The structure is as follows:

Ntstatus
Zwterminateprocess (
In handleProcesshandle,
In ntstatusExitstatus
);

When processhandle is null, it indicates that the process has ended. When the process ends other processes, processhandle points to the process object of the terminated process. To obtain the name of the terminated process, perform the following operations.

1. Obtain the eprocess pointer of the process through obreferenceobjectbyhandle.

2. Obtain the process name through the undisclosed function psgetprocessimagefilename.

The structure of psgetprocessimagefilename is as follows:

Uchar * psgetprocessimagefilename (
_ In peprocess Process
);

The Code is as follows:

void GetProcessNamebyHandle(HANDLE handle,PCHAR Name){PEPROCESS Process;NTSTATUS status;char *nameptr=NULL;status = ObReferenceObjectByHandle(handle,0,NULL,KernelMode,&Process,NULL);if(!NT_SUCCESS(status)){DbgPrint("GetProcessNamebyHandle failed\n");strcpy(nameptr,"Unkown");return;}//KdPrint(("Process:%x\n",Process));nameptr =PsGetProcessImageFileName(Process);strncpy(Name,nameptr,PROCNAMELEN);Name[PROCNAMELEN]=0x00;return;}
 
The fakedzwterminateprocess code is as follows:
NTSTATUS FakedZwTerminateProcess(IN HANDLE ProcessHandle,
 IN NTSTATUS ExitStatus){char aProcessName[PROCNAMELEN];char killedProName[PROCNAMELEN];NTSTATUS status;status = RealZwTerminateProcess(ProcessHandle,ExitStatus);if(NT_SUCCESS(status)&&NT_SUCCESS(ExitStatus)){GetProcessName(aProcessName); if(ProcessHandle==NULL){strncpy(killedProName,aProcessName,sizeof(aProcessName));}else{GetProcessNamebyHandle(ProcessHandle,killedProName);}sprintf(output,"TerminateProcess: %s --> %s\n",aProcessName,killedProName);KdPrint(("%s\n",output));}return status;}
 
The getprocessname (aprocessname) function is used to obtain the current process that calls zwterminateprocess.

For the implementation and principle of the Code, refer to the document on how to obtain the process name of the current user process in the kernel driver.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.