Hi Alex,
I am using following code :
if(gLock && Data->Iopb->MajorFunction == IRP_MJ_CREATE ) // Access Denied (Locking Case)
{
uStrParentPath.Length = 0 ;
uStrParentPath.MaximumLength = nameInfo->Name.Length + 520;
uStrParentPath.Buffer = (PWSTR)ExAllocatePoolWithTag(NonPagedPool,nameInfo->Name.Length + 520,‘uPr3’);
if(uStrParentPath.Buffer != NULL)
{
RtlAppendUnicodeStringToString(&uStrParentPath,&uStrGuidName);
RtlAppendUnicodeStringToString(&uStrParentPath,&nameInfo->ParentDir);
// File Execution Scenario
bMatched = Traverseinto_Lock_List(uStrPath,uStrParentPath,bApp); // Search existing path in list
pRocess = IoThreadToProcess(Data->Thread); // Get Current Process that belongs to existing thread
if(pRocess != NULL && nameInfo->Extension.Length > 0 && gAppRunning)
{
hProcess = PsGetProcessId(pRocess);
szProcess = PsGetProcessImageFileName(pRocess);
if(szProcess != NULL)
{
RtlMultiByteToUnicodeSize( &exLen, szProcess, strlen(szProcess)-3);
exeName.Length = 0 ;
exeName.MaximumLength = 50;
exeName.Buffer = (PWSTR)ExAllocatePoolWithTag(NonPagedPool,50,‘uPe1’);
uStr1.Length = 0 ;
uStr1.MaximumLength = 50;
uStr1.Buffer = (PWSTR)ExAllocatePoolWithTag(NonPagedPool,50,‘uEt1’);
if(exeName.Buffer != NULL && uStr1.Buffer != NULL)
{
RtlMultiByteToUnicodeN(exeName.Buffer, exLen, &len, szProcess, strlen(szProcess)-3);
exeName.Length = (USHORT)len;
RtlUnicodeStringCbCatN(&uStr1,&exeName,22);
if( NT_SUCCESS(GetParentProcessId (hProcess, &hParent)) ) // Get handle of Parent Process
{
if(hParent != 0)
{
exePName.Length = 0 ;
exePName.MaximumLength = 50;
exePName.Buffer = (PWSTR)ExAllocatePoolWithTag(NonPagedPool,50,‘uPe2’);
uStrP1.Length = 0 ;
uStrP1.MaximumLength = 50;
uStrP1.Buffer = (PWSTR)ExAllocatePoolWithTag(NonPagedPool,50,‘uEt2’);
if(exePName.Buffer != NULL && uStrP1.Buffer != NULL)
{
PsLookupProcessByProcessId(hParent, &pParentProcess);
pParentImageName= PsGetProcessImageFileName(pParentProcess);
if(pParentImageName != NULL)
{
if(NT_SUCCESS (RtlMultiByteToUnicodeSize( &exPLen, (PCHAR)pParentImageName, strlen((PCHAR)pParentImageName)-3)) )
{
RtlMultiByteToUnicodeN(exePName.Buffer, exPLen, &Plen, (PCHAR)pParentImageName, strlen((PCHAR)pParentImageName)-3);
exePName.Length = (USHORT)Plen;
RtlUnicodeStringCbCatN(&uStrP1,&exePName,22);
if(bMatched && (RtlCompareUnicodeString(&uStr1,&exeFolderLock,TRUE) == 0 || RtlCompareUnicodeString(&uStrP1,&exeFolderLock,TRUE) == 0))
{
bMatched = FALSE;
}
}
}
ExFreePoolWithTag(uStrP1.Buffer,‘uEt2’);
ExFreePoolWithTag(exePName.Buffer,‘uPe2’);
ObDereferenceObject((PVOID)pParentProcess);
}
}
}
#ifdef _RELEASE
DbgPrint(“File Accessed in Locking %ws”,uStrPath.Buffer);
#endif
ExFreePoolWithTag(uStr1.Buffer,‘uEt1’);
ExFreePoolWithTag(exeName.Buffer,‘uPe1’);
}
}
}
// File Execution Scenario
ExFreePoolWithTag(uStrParentPath.Buffer,‘uPr3’);
}
}
NTSTATUS GetParentProcessId ( __in HANDLE processId, __out PHANDLE parentProcessId )
{
NTSTATUS status;
PEPROCESS eProcess;
HANDLE hProcess;
PROCESS_BASIC_INFORMATION pbi;
PAGED_CODE();
if ( processId == (HANDLE) 4 )
{
*parentProcessId = 0;
return STATUS_SUCCESS;
}
status = PsLookupProcessByProcessId(processId, &eProcess);
if(NT_SUCCESS(status))
{
status = ObOpenObjectByPointer(eProcess,0, NULL, 0,0,KernelMode,&hProcess);
if( ! NT_SUCCESS(status))
{
// DbgPrint(“Error: ObOpenObjectByPointer Failed: %08x\n”, status);
}
ObDereferenceObject(eProcess);
}
else
{
//DbgPrint(“Error: PsLookupProcessByProcessId Failed: %08x\n”, status);
}
if (NULL == ZwQueryInformationProcess)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");
ZwQueryInformationProcess = (QUERY_INFO_PROCESS)
MmGetSystemRoutineAddress(&routineName);
if (NULL == ZwQueryInformationProcess)
{
DbgPrint(“Cannot resolve ZwQueryInformationProcess\n”);
}
}
status = ZwQueryInformationProcess( hProcess, ProcessBasicInformation, &pbi, sizeof (PROCESS_BASIC_INFORMATION), NULL);
if (NT_SUCCESS(status))
{
*parentProcessId = (HANDLE) pbi.InheritedFromUniqueProcessId;
//DbgPrint(“Get Current Process…”);
}
return status;
}
Thanks