我正在尝试读取notepad.exe的PEB 目前我正在尝试通过注册ProcessCreation回调来访问PEB,然后等待,直到创建notepad.exe。创建记事本时,我使用它的PID打开过程并使用ZwQuerryProcess(PROCESS_BASIC_INFORMATION)找到PEB。
但是当我尝试阅读INFORMATION-> PEB时会引发异常(我认为这是因为我无法访问内存)
当我第一次发现这个时,我记得有人提到KeStackAttachProcess,它与访问另一个进程上下文中的地址相对应。
问题是我不知道如何检查上下文更改是否成功。一旦我被认为在另一个背景下,我仍然无法访问peb。有谁知道如何访问记事本的PEB?
以下是我目前用于查找和访问PEB的代码:
假设hgtPid =记事本的PID
void ModuleDumperThread(){
NTSTATUS Status = STATUS_SUCCESS;
HANDLE hProcessHandle = NULL;
PLIST_ENTRY Next;
PLDR_DATA_TABLE_ENTRY LdrDataTableEntry;
CLIENT_ID clientID;
ACCESS_MASK DesiredAccess = PROCESS_ALL_ACCESS;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE hProcessId = hgtPid;
PROCESS_BASIC_INFORMATION BasicInfoReal;
ULONG SizeReturned;
PEPROCESS ep;
KAPC_STATE *ka_state = NULL;
InitializeObjectAttributes (&ObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
clientID.UniqueProcess = hProcessId;
clientID.UniqueThread = NULL;
__try{
Status = ZwOpenProcess(&hProcessHandle, DesiredAccess, &ObjectAttributes, &clientID);
if(Status != STATUS_SUCCESS){
DbgPrint("Failed to open process\n");
DbgPrint("NtStatus: 0x%x", Status);
return;
}
Status = gZwQueryprocess(hProcessHandle, ProcessBasicInformation, (PVOID)&BasicInfoReal, sizeof(PROCESS_BASIC_INFORMATION), &SizeReturned);
if(Status != STATUS_SUCCESS){
DbgPrint("gZwQueryprocess failed\n");
DbgPrint("Size returned: 0x%x\nNtStatus: 0x%x\n", SizeReturned, Status);
ZwClose(hProcessHandle);
return;
}
ZwClose(hProcessHandle);
Status = PsLookupProcessByProcessId(hProcessId, &ep);
if(Status != STATUS_SUCCESS){
DbgPrint("PsLookupProcessByProcessId failed\n");
DbgPrint("NtStatus: 0x%x\n", Status);
return;
}
ka_state = ExAllocatePoolWithTag(NonPagedPool, sizeof(KAPC_STATE),'trak');
KeStackAttachProcess(ep, ka_state);
__try{
if(BasicInfoReal.PebBaseAddress->Ldr){
Next = BasicInfoReal.PebBaseAddress->Ldr->InLoadOrderModuleList.Blink;
LdrDataTableEntry = CONTAINING_RECORD( Next,
LDR_DATA_TABLE_ENTRY,
LoadOrder
);
DbgPrint("Module base address: 0x%x", LdrDataTableEntry->ModuleBaseAddress);
}
}__except( EXCEPTION_EXECUTE_HANDLER ) {
DbgPrint("Exception while trying to access the PEB\n");
}
KeUnstackDetachProcess(ka_state);
ExFreePool(ka_state);
}__except( EXCEPTION_EXECUTE_HANDLER ) {
DbgPrint("Exception in ModuleDumper\n");
}
if(ep){
ObDereferenceObject(ep);
}
return;
}
有没有人发现任何错误/错误?
提前致谢
修改
我改变了一些事情,这就是它变得非常奇怪的地方。为了确保我将KeStackAttachProcess()的'ep'更改为msdn指定的PRKPROCESS类型,当我调用KeStackAttachProcess()时,现在执行就消失了。在通话之前一切都很顺利,通话结束后一切都没有。没有错误没有例外没有BSOD:没什么。 发生了什么事??
的变化:
__asm{
mov eax, ep
mov eax, [eax]
mov myPKPROCESS, eax // just dereferencing my pointer (I don't have the structs)
}
DbgPrint("Test print\n"); // gets printed just fine
KeStackAttachProcess(&myPKPROCESS, ka_state);
DbgPrint("Test print\n"); // nothing happens
EDIT2:
我已经解决了这个问题。我仍然不知道上面的代码出了什么问题,但是这段代码似乎正常工作:
void ModuleDumperThread(){
NTSTATUS Status = STATUS_SUCCESS;
HANDLE hProcessHandle = NULL;
PLIST_ENTRY Next;
PLDR_DATA_TABLE_ENTRY LdrDataTableEntry;
CLIENT_ID clientID;
ACCESS_MASK DesiredAccess = PROCESS_ALL_ACCESS;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE hProcessId = hgtPid;
PROCESS_BASIC_INFORMATION BasicInfoReal;
ULONG SizeReturned;
PEPROCESS ep = NULL;
unsigned int Index = 0;
InitializeObjectAttributes (&ObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
clientID.UniqueProcess = hProcessId;
clientID.UniqueThread = NULL;
__try{
Status = ZwOpenProcess(&hProcessHandle, DesiredAccess, &ObjectAttributes, &clientID);
if(Status != STATUS_SUCCESS){
DbgPrint("Failed to open process\n");
DbgPrint("NtStatus: 0x%x", Status);
return;
}
Status = gZwQueryprocess(hProcessHandle, ProcessBasicInformation, (PVOID)&BasicInfoReal, sizeof(PROCESS_BASIC_INFORMATION), &SizeReturned);
if(Status != STATUS_SUCCESS){
DbgPrint("gZwQueryprocess failed\n");
DbgPrint("Size returned: 0x%x\nNtStatus: 0x%x\n", SizeReturned, Status);
ZwClose(hProcessHandle);
return;
}
//DbgPrint("Basic info: 0x%x\n", BasicInfoReal);
//DbgPrint("BasicInfoReal->PebBaseAddress: 0x%x\n", BasicInfoReal->PebBaseAddress);
//DbgPrint("RealPeb: 0x%x\n", RealPeb);
//DbgPrint("gZwReadVirtualMemory: 0x%x\n", gZwReadVirtualMemory);
Status = PsLookupProcessByProcessId(hProcessId, &ep);
if(Status != STATUS_SUCCESS){
DbgPrint("PsLookupProcessByProcessId failed\n");
DbgPrint("NtStatus: 0x%x\n", Status);
ZwClose(hProcessHandle);
return;
}
Timeout((INT64)0x1FFFFFF);
KeAttachProcess(ep);
__try{
DbgPrint("ImageBaseAddress of notepad.exe: 0x%x\n", BasicInfoReal.PebBaseAddress->ImageBaseAddress);
Next = BasicInfoReal.PebBaseAddress->Ldr->InLoadOrderModuleList.Blink;
LdrDataTableEntry = CONTAINING_RECORD( Next, LDR_DATA_TABLE_ENTRY, LoadOrder);
for(Index = 0; Index != 17; Index++){
DbgPrint("%d: ImageBase of %wZ in Notepad.exe: 0x%x\n", Index, &(LdrDataTableEntry->ModuleName), LdrDataTableEntry->ModuleBaseAddress);
Next = Next->Blink;
LdrDataTableEntry = CONTAINING_RECORD(Next, LDR_DATA_TABLE_ENTRY, LoadOrder);
}
}__except( EXCEPTION_EXECUTE_HANDLER ) {
DbgPrint("Exception while accessing the LDR\n");
}
KeDetachProcess();
}__except( EXCEPTION_EXECUTE_HANDLER ) {
DbgPrint("Exception in ModuleDumper\n");
}
ObDereferenceObject((PVOID)ep);
ZwClose(hProcessHandle);
return;
}
答案 0 :(得分:2)
您可以使用PPEB PsGetProcessPeb ( IN PEPROCESS Process )
。您需要使用MmGetSystemRoutineAddress
来获取此API的地址。看一下这个文件中的GetDllByPeb
函数:
http://code.google.com/p/arkitlib/source/browse/trunk/ARKitDrv/Ps.c
答案 1 :(得分:0)
这是你的PsGetProcessPeb:
/base/ntos/ps/pshelper.c
PPEB
PsGetProcessPeb(
__in PEPROCESS Process
)
{
return Process->Peb;
}
无需额外工作从PEB
结构中提取EPROCESS
指针(对于所有Windows版本都是如此,除了32/64-bit
差异)
您可以EPROCESS
使用PsLookupProcessByProcessId
(系统流程)获取ID=4
。