gpt4 book ai didi

c - 如何访问用户态内存?

转载 作者:可可西里 更新时间:2023-11-01 13:45:33 28 4
gpt4 key购买 nike

我正在尝试阅读 notepad.exe 的 PEB目前我正在尝试通过注册一个 ProcessCreation 回调然后等待 notepad.exe 创建来访问 PEB。创建记事本后,我使用它的 PID 打开进程并使用 ZwQuerryProcess(PROCESS_BASIC_INFORMATION) 找到 PEB。

但是当我尝试阅读 INFORMATION->PEB 以外的内容时,会引发异常(我认为这是因为我无法访问内存)

当我第一次发现这个时,我记得有人提到了 KeStackAttachProcess,它对应于访问另一个进程上下文中的地址。

问题是我不知道如何检查上下文更改是否成功。一旦我应该在另一个上下文中,我仍然无法访问 peb。有谁知道如何访问记事本的PEB?

这是我目前用来查找和访问 PEB 的代码:

假设 hgtPid = notepad 的 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() 现在执行消失了。通话前一切正常,通话后就什么都没有了。没有错误没有异常没有蓝屏死机:没有。 这是怎么回事?!?

变化:

    __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

编辑 2:

我已经解决了这个问题。我仍然不知道上面的代码出了什么问题,但是这段代码似乎可以正常工作:

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;
}

最佳答案

您可以使用 PPEB PsGetProcessPeb(在 PEPROCESS 进程中)。您需要使用 MmGetSystemRoutineAddress 来获取此 API 的地址。查看此文件中的 GetDllByPeb 函数: http://code.google.com/p/arkitlib/source/browse/trunk/ARKitDrv/Ps.c

关于c - 如何访问用户态内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6316880/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com