你这代码看得好乱啊,给你一个从peb获取结构的例子吧 - -#
#include "ntifs.h"
#include "GetSystemVersion.h"
DWORD GetDllNameFromPeb(PEPROCESS Process)
{
DWORD PebOffset=0;
DWORD Peb;
PPEB_LDR_DATA PebLdr;
PLDR_DATA_TABLE_ENTRY LdrTableEntry;
PLIST_ENTRY pListHead,pListNext;
WIN_VER_DETAIL WinVer;
BOOL bRetOK = FALSE;
__try
{
WinVer=GetWindowsVersion();
switch (WinVer)
{
case WINDOWS_VERSION_XP: //xp
PebOffset=0x1b0;
break;
case WINDOWS_VERSION_2K3: //2003
PebOffset=0x190;
break;
case WINDOWS_VERSION_2K3_SP1_SP2: //2003
PebOffset=0x1a0;
break;
case WINDOWS_VERSION_VISTA_2008: //Vista+2008
PebOffset=0x188;
break;
/*case 6002: //2008
PebOffset=0x188;
break;*/
case WINDOWS_VERSION_7: //win7
PebOffset=0x1a8;
break;
}
if (PebOffset==0)
{
bRetOK = FALSE;
__leave;
}
Peb=*(DWORD*)((DWORD)Process+PebOffset);
if (Peb == 0)
{
DbgPrint("Peb is null\n");
bRetOK = FALSE;
__leave;
}
PebLdr=(PPEB_LDR_DATA)*(DWORD*)(Peb+0x0C);
if (!MmIsAddressValid(PebLdr))
{
DbgPrint("PebLdr offset is null\n");
bRetOK = FALSE;
__leave;
}
pListHead=&PebLdr->InLoadOrderModuleList;
pListNext=pListHead->Flink;
while (pListHead!=pListNext)
{
LdrTableEntry=(PLDR_DATA_TABLE_ENTRY)pListNext;
if (MmIsAddressValid(&LdrTableEntry->BaseDllName.Buffer[LdrTableEntry->BaseDllName.Length]))
{
DbgPrint("DllBase:%x dll path %ws\n",LdrTableEntry->DllBase,LdrTableEntry->BaseDllName.Buffer);
}
pListNext=pListNext->Flink;
}
bRetOK = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
DbgPrint("GetDllNameFromPeb Error \r\n");
}
return bRetOK;
}
#pragma LOCKEDDATA
PEPROCESS GetEprocessFromPid(DWORD Pid)
{
HANDLE hProcess;
NTSTATUS status;
OBJECT_ATTRIBUTES ObjectAttributes;
PEPROCESS Process;
CLIENT_ID ClientId={0};
ClientId.UniqueProcess = (HANDLE)Pid;
InitializeObjectAttributes( &ObjectAttributes, NULL, 0, NULL, NULL );
status=ZwOpenProcess(&hProcess,PROCESS_ALL_ACCESS,&ObjectAttributes,&ClientId);
if (!NT_SUCCESS(status))
{
return NULL;
}
status = ObReferenceObjectByHandle (hProcess,
PROCESS_ALL_ACCESS,
*PsProcessType,
KernelMode,
(PVOID *)&Process,
NULL);
if (!NT_SUCCESS(status))
{
ZwClose(hProcess);
}
ObDereferenceObject(Process);
return Process;
}
VOID DriverUnload( IN PDRIVER_OBJECT DriverObject )
{
return STATUS_SUCCESS;
}
NTSTATUS DriverEntry(
IN OUT PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
{
PEPROCESS Eprocess;
BOOL bRet;
DriverObject->DriverUnload = DriverUnload;
KdPrint(("DriverLoad\n"));
Eprocess=GetEprocessFromPid(1912);
if (Eprocess==NULL)
{
KdPrint(("get eprocess failed\n"));
return STATUS_SUCCESS;
}
KeAttachProcess(Eprocess);
bRet=GetDllNameFromPeb(Eprocess);
if (!bRet)
{
KdPrint(("get dllname failed\n"));
}
KeDetachProcess();
return STATUS_SUCCESS;
}