EPROCESS获取FileObject
1,通过PsGetCurrentProcess获取EPROCESS,EPROCESS->SectionObject->Segment->ControlArea->FileObject,不过貌似wdk的EPROCESS被定义成了KPROCESS,这个EPROCESS的结构就不一样了;2,通过获取process的Handle,然后调用ObReferenceObjectByHandle, ObjectType 填写IoFileObjectType,然后获取其FileObject;
目前只想到上面两种方法,不知道还有没有其他更简单点的办法。
PS:用的框架是文件过滤驱动。
先谢谢了 写了验证一下,第二个方法好像行不通,不知道是不是process的handle不能用IoFileObjectType来获取FileObject。
第一个方法因为没有wdk定义的结构体,暂时不想使用哪个方法。 ZwQueryInformationProcess, ZwCreateFile , ObReferenceObjectByHandle 谢谢MJ。
网上找的一段代码,贴出来留给后面的同学看看。
// 注: 当函数返回TRUE时,参数pImageName所指向的 PANSI_STRING 对象需要用RtlFreeAnsiString()释放。
// 否则,标记为 "Strg" 的内存池会产生泄露。
typedef NTSTATUS (*QUERY_INFO_PROCESS) (
__in HANDLE ProcessHandle,
__in PROCESSINFOCLASS ProcessInformationClass,
__out_bcount(ProcessInformationLength) PVOID ProcessInformation,
__in ULONG ProcessInformationLength,
__out_opt PULONG ReturnLength
);
QUERY_INFO_PROCESS ZwQueryInformationProcess;
#define ProcessImageFileName 27
BOOLEAN GetProcessImageName(PANSI_STRING pImageName)
{
KIRQL CurIRQL ;
NTSTATUS status;
ULONG returnedLength;
ULONG bufferLength;
PVOID buffer;
PUNICODE_STRING imageName;
BOOLEAN bRet = FALSE ;
PAGED_CODE(); // this eliminates the possibility of the IDLE Thread/Process
CurIRQL = KeGetCurrentIrql() ;
DbgPrint ("Current IRQL is %d\\r\\n", CurIRQL) ;
if (PASSIVE_LEVEL != CurIRQL)
{
return FALSE ;
}
try
{
if ( ! MmIsAddressValid (pImageName))
{
return FALSE ;
}
pImageName->Length = 0 ;
pImageName->MaximumLength = 0 ;
pImageName->Buffer = NULL ;
}
except (EXCEPTION_EXECUTE_HANDLER)
{
return FALSE ;
}
if (NULL == ZwQueryInformationProcess) {
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");
ZwQueryInformationProcess =
(QUERY_INFO_PROCESS) MmGetSystemRoutineAddress(&routineName);
if (NULL == ZwQueryInformationProcess) {
DbgPrint("Cannot resolve ZwQueryInformationProcess\\n");
return FALSE ;
}
}
//
// Step one - get the size we need
//
status = ZwQueryInformationProcess( NtCurrentProcess(),
ProcessImageFileName,
NULL, // buffer
0, // buffer size
&returnedLength);
if (STATUS_INFO_LENGTH_MISMATCH != status) {
return FALSE;
}
//
// Is the passed-in buffer going to be big enough for us?
// This function returns a single contguous buffer model...
//
bufferLength = returnedLength - sizeof(UNICODE_STRING);
//
// If we get here, the buffer IS going to be big enough for us, so
// let's allocate some storage.
//
buffer = ExAllocatePoolWithTag(PagedPool, returnedLength, 'ipgD');
if (NULL == buffer) {
return FALSE ;
}
try
{
//
// Now lets go get the data
//
status = ZwQueryInformationProcess( NtCurrentProcess(),
ProcessImageFileName,
buffer,
returnedLength,
&returnedLength);
if (NT_SUCCESS(status)) {
//
// Ah, we got what we needed
//
imageName = (PUNICODE_STRING) buffer;
// RtlCopyUnicodeString(ProcessImageName, imageName);
//RtlZeroMemory (pBuffer, cbBuffer) ;
//if (((size_t)-1) != wcstombs (pBuffer, imageName->Buffer, cbBuffer))
//{
// bRet = TRUE ;
//}
//KdPrint (("Current ProcessImageFileName: \\"%s\\"\\r\\n", pBuffer)) ;
if (STATUS_SUCCESS != RtlUnicodeStringToAnsiString (pImageName, imageName, TRUE))
{
bRet = FALSE ;
KdPrint (("Current ProcessImageFileName: Unknow\\r\\n")) ;
}
else
{
bRet = TRUE ;
KdPrint (("Current ProcessImageFileName: \\"%s\\"\\r\\n", pImageName->Buffer)) ;
}
}
}
except (EXCEPTION_EXECUTE_HANDLER)
{
bRet = FALSE ;
}
//
// free our buffer
//
ExFreePool(buffer);
//
// And tell the caller what happened.
//
return bRet ;
}