Alexander
2006-01-05 14:33:40 UTC
Hi all,
I want to retrieve, in a kernel driver , the full image path name for
the current process, without using PSAPI.lib (only works in user mode).
I use the following strategy:
I use ZwQueryInformationProcess to retrieve a pointer to
PROCESS_BASIC_INFORMATION structure. Now, from here we obtain the
PebBAseAddress field; from this we retrieve a pointer to a
RTL_USER_PROCESS_PARAMETERS structure called ProcessParametes. from
this structure is possible to extract the ImagePathName.Buffer field
(the type is WCHAR).
Here is my code:
---------------------------------------
PROCESS_BASIC_INFORMATION ProcInfo;
PPEB myPEB;
PRTL_USER_PROCESS_PARAMETERS pupp = NULL;
RTL_USER_PROCESS_PARAMETERS procParams;
ULONG cbRet, ret;
CHAR s3[256];
WCHAR wstr[MAX_PATH];
hProcess = NtCurrentProcess();
if( ProcessNameOffset )
{
pid = (ULONG)PsGetCurrentProcessId();
if(pid==4) //System process
{
strcpy( PathImage, "System - no path" );
return;
}
if(!NT_SUCCESS(ntStatus = ZwQueryInformationProcess(
hProcess,
ProcessBasicInformation,
&ProcInfo,
sizeof(ProcInfo),
&cbRet))) {
DbgPrint("Error1");
return; }
if ((myPEB = (PEB*)ProcInfo.PebBaseAddress)!=NULL) {
pupp = myPEB->ProcessParameters;
if (pupp) {
DbgPrint("%ws", pupp->ImagePathName.Buffer);
}
/* in this way the complete path is correctly printed on DbgView,
when I try to access it I get BSOD: e.g. if I try to do
sprintf(s3,"%ws",pupp->ImagePathName.Buffer)
I get BSOD.
*/
/* Adding this code... */
if(!NT_SUCCESS(ntStatus = ZwReadVirtualMemory(
hProcess,
pupp->ImagePathName.Buffer,
&wstr,
pupp->ImagePathName.Length,
&cbRet)))
{
sprintf(PathImage, "Error2 - code: %d\n", ntStatus);
return; }
wstr[procParams.ImagePathName.Length / sizeof(WCHAR)] = 0;
//DbgPrint( "%ws",wstr);
sprintf(s3,"%ws",wstr);
/*--------------------------------------------------------*/
...with ZwReadVirtualMemory I retrieve in wstr a copy of the path , and
I can use it in any way. The problem is that this method doesn't work
for every calling process; this function often returns the NTSTATUS
code -1073741819 (corrisponding to STATUS_ACCESS_VIOLATION).
I just want to discover how to avoid this error, and why this is
generated...
If someone knows any other working solution to my problem , or an
alternative way to get the complete image path forma kernel driver
please tell me...
Alexander
I want to retrieve, in a kernel driver , the full image path name for
the current process, without using PSAPI.lib (only works in user mode).
I use the following strategy:
I use ZwQueryInformationProcess to retrieve a pointer to
PROCESS_BASIC_INFORMATION structure. Now, from here we obtain the
PebBAseAddress field; from this we retrieve a pointer to a
RTL_USER_PROCESS_PARAMETERS structure called ProcessParametes. from
this structure is possible to extract the ImagePathName.Buffer field
(the type is WCHAR).
Here is my code:
---------------------------------------
PROCESS_BASIC_INFORMATION ProcInfo;
PPEB myPEB;
PRTL_USER_PROCESS_PARAMETERS pupp = NULL;
RTL_USER_PROCESS_PARAMETERS procParams;
ULONG cbRet, ret;
CHAR s3[256];
WCHAR wstr[MAX_PATH];
hProcess = NtCurrentProcess();
if( ProcessNameOffset )
{
pid = (ULONG)PsGetCurrentProcessId();
if(pid==4) //System process
{
strcpy( PathImage, "System - no path" );
return;
}
if(!NT_SUCCESS(ntStatus = ZwQueryInformationProcess(
hProcess,
ProcessBasicInformation,
&ProcInfo,
sizeof(ProcInfo),
&cbRet))) {
DbgPrint("Error1");
return; }
if ((myPEB = (PEB*)ProcInfo.PebBaseAddress)!=NULL) {
pupp = myPEB->ProcessParameters;
if (pupp) {
DbgPrint("%ws", pupp->ImagePathName.Buffer);
}
/* in this way the complete path is correctly printed on DbgView,
when I try to access it I get BSOD: e.g. if I try to do
sprintf(s3,"%ws",pupp->ImagePathName.Buffer)
I get BSOD.
*/
/* Adding this code... */
if(!NT_SUCCESS(ntStatus = ZwReadVirtualMemory(
hProcess,
pupp->ImagePathName.Buffer,
&wstr,
pupp->ImagePathName.Length,
&cbRet)))
{
sprintf(PathImage, "Error2 - code: %d\n", ntStatus);
return; }
wstr[procParams.ImagePathName.Length / sizeof(WCHAR)] = 0;
//DbgPrint( "%ws",wstr);
sprintf(s3,"%ws",wstr);
/*--------------------------------------------------------*/
...with ZwReadVirtualMemory I retrieve in wstr a copy of the path , and
I can use it in any way. The problem is that this method doesn't work
for every calling process; this function often returns the NTSTATUS
code -1073741819 (corrisponding to STATUS_ACCESS_VIOLATION).
I just want to discover how to avoid this error, and why this is
generated...
If someone knows any other working solution to my problem , or an
alternative way to get the complete image path forma kernel driver
please tell me...
Alexander