Sysinternals Homepage
Forum Home Forum Home > Windows Discussions > Internals
  New Posts New Posts RSS Feed: Get PEPROCESS by PID!
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Get PEPROCESS by PID!

 Post Reply Post Reply Page  <12
Author
Message
  Topic Search Topic Search  Topic Options Topic Options
MP_ART View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 March 2006
Location: Russian Federation
Online Status: Offline
Posts: 947
  Quote MP_ART Quote  Post ReplyReply Direct Link To This Post Topic: Get PEPROCESS by PID!
    Posted: 12 August 2006 at 4:19am
PVOID pid;

pid = (PVOID)IdOfYourProcess;
status = PsLookupProcessByProcessId( pid, &process );
Back to Top
Headium2006 View Drop Down
Groupie
Groupie
Avatar

Joined: 25 July 2006
Location: China
Online Status: Offline
Posts: 79
  Quote Headium2006 Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 4:38am

Ok, I'll try it!

Thanks, MP_ART!

Back to Top
Headium2006 View Drop Down
Groupie
Groupie
Avatar

Joined: 25 July 2006
Location: China
Online Status: Offline
Posts: 79
  Quote Headium2006 Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 4:42am

By the way, how are all these unpublished functions got?I mean in IFS Document, there is no info about those functions. How can U get them? I want to know about that for I think the way to get them must be very interesting!

Back to Top
EP_X0FF View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 March 2006
Location: Russian Federation
Online Status: Offline
Posts: 4753
  Quote EP_X0FF Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 4:47am
By the way, how are all these unpublished functions got?I mean in IFS Document, there is no info about those functions. How can U get them? I want to know about that for I think the way to get them must be very interesting


Windows NT Kernel Reverse Engineering.
Ring0 - the source of inspiration
Back to Top
Headium2006 View Drop Down
Groupie
Groupie
Avatar

Joined: 25 July 2006
Location: China
Online Status: Offline
Posts: 79
  Quote Headium2006 Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 12:45pm

Hi, EP_XOFF!

I tried hard but I still failed to call the function of PsLookupProcessByProcessId. But I have proven that I can easily get the name of parent process of certain process by traversing the ActiveProcessLinks.

I wrote the code below to enumurate all the active process and I succeed. If I add some restriction condition, I can of course get the name of parent process.

VOID TraverseProcessLinkList( IN PEPROCESS EProcess ){
 int nCount = 0;
 int     *iPtr;
 char    *cPtr;
 PEPROCESS   pProc;
 PLIST_ENTRY   pNext;
 PLIST_ENTRY   pFirst;

 pProc = EProcess;
 pNext = pFirst = (LIST_ENTRY *)((char *)EProcess + 0x0A0);

 do {
  nCount ++;
  pProc = (PEPROCESS)((char *)pNext - 0X0A0);
  DbgPrint("--- process %d --- \n", nCount);
  
  //Get PID of the process pointed by pProc
  iPtr = (int *)((char *)pProc + 0x09C);
  DbgPrint("process id: %d \n", *iPtr );

  //Get name of the process pointed by pProc
  cPtr = (char *)pProc + 0x1FC;
  DbgPrint("process image file: %s \n", cPtr );

  //Forward the pNext pointer
  pNext = pNext->Flink;
  DbgPrint("\n");
   
 } while(pNext != pFirst);

 DbgPrint("proces total count %d \n", nCount);
}

But I don't konw whether getting the parent process's name in this way may lead to some unsafe status or not. Do I need to acquire a lock when accessing the active process linked-list?

Waiting for your suggestion!Thanks!

Back to Top
EP_X0FF View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 March 2006
Location: Russian Federation
Online Status: Offline
Posts: 4753
  Quote EP_X0FF Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 1:08pm
Hello, Headium2006.

You should know that information about process name in EPRPOCESS struct is very limited by length, so I suggest you make KeAttachProcess, read information about process executable name from Process Environment Block, LDR entry, then of course make ObfDereferenceObject and KeDetachProcess.

BTW, PsLookupProcessByProcessId is very easy and safe to use, so what is your problem? Why you cant use it? Maybe you dont have needed libs?
Ring0 - the source of inspiration
Back to Top
Headium2006 View Drop Down
Groupie
Groupie
Avatar

Joined: 25 July 2006
Location: China
Online Status: Offline
Posts: 79
  Quote Headium2006 Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 9:44pm

In fact, I did try to call the function PsLookupProcessByProcessId. But every time I call this function, it never returns a successful status. I mean  my call to this function never succeeded in get the PEPROCESS.

But I did pass through the compiling. No error occured duiring compiling.

Back to Top
EP_X0FF View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 March 2006
Location: Russian Federation
Online Status: Offline
Posts: 4753
  Quote EP_X0FF Quote  Post ReplyReply Direct Link To This Post Posted: 12 August 2006 at 10:39pm
But I did pass through the compiling. No error occured duiring compiling.


Show us how you calling that function. Full source code of place where used PsLookupProcessByProcessId including variable types.
Ring0 - the source of inspiration
Back to Top
Headium2006 View Drop Down
Groupie
Groupie
Avatar

Joined: 25 July 2006
Location: China
Online Status: Offline
Posts: 79
  Quote Headium2006 Quote  Post ReplyReply Direct Link To This Post Posted: 13 August 2006 at 2:56am

Hi, EP_XOFF! My code is below. The problem is that I can pass compiling, but every time I call PsLookupProcessByProcessId it

never returns a successful status.

//I declare PsLookupProcessByProcessId
NTSTATUS NTAPI PsLookupProcessByProcessId (IN PVOID ProcessId, OUT PEPROCESS *      Process );

//Code where PsLookupProcessByProcessId is called
//A simple dispatch funcion called when a IRP_MJ_CREATE is intercepted
//The code is just a test to demonstrate my idea.
NTSTATUS HDCreate(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
 PVOID    pProc;
 PEPROCESS   EProcess;
 NTSTATUS   status;
 PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation( Irp );
 PDEVICE_EXTENSION devExt;

 //Display the MajorFunction code
 DbgPrint( "HEAD(HDDispatch): IRP CODE : %s \n", IRP_CODE[irpSp->MajorFunction] );

 //Get the EPROCESS pointer of parent process
 pProc = (PVOID)((char *)PsGetCurrentProcess() + 0x1C8);
 status = PsLookupProcessByProcessId( pProc, &EProcess );
 if( NT_SUCCESS(status) )
  DbgPrint( "HEAD(HDCreate): Name of parent process is %s\n", (char *)EProcess + 0x1FC);//Never executed
 else
  DbgPrint( "HEAD(HDCreate): Fail to get PEPROCESS of parent process...\n" );//This code is always executed

 //Relay the IRP packet
 IoSkipCurrentIrpStackLocation( Irp );
 devExt=DeviceObject->DeviceExtension;
 return IoCallDriver( devExt->AttachedToDeviceObject, Irp );
}

I really do not konw why it can never work properly!

Back to Top
EP_X0FF View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 March 2006
Location: Russian Federation
Online Status: Offline
Posts: 4753
  Quote EP_X0FF Quote  Post ReplyReply Direct Link To This Post Posted: 13 August 2006 at 3:11am
Perhaps you are sending a pointer instead of real process Id.
Ring0 - the source of inspiration
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down

Privacy Statement