如何在Go中使用VirtualQueryEx读取检索进程内存信息



我正在尝试移植PSHunt的特定功能(https://github.com/Infocyte/PSHunt/blob/master/Surveys/Survey.ps1)去吧。具体地说,我正试图使用VirtualQueryEx来迭代进程内存的页面,如Powershell中的片段所示:

# Get handle to the process
$hProcess = $Kernel32::OpenProcess(0x400, $False, $ProcessID) # PROCESS_QUERY_INFORMATION (0x00000400)
if (-not $hProcess) {
throw "Unable to get a process handle for process ID: $ProcessID"
}
$MemoryInfo = New-Object $MEMORY_BASIC_INFORMATION
$BytesRead = $Kernel32::VirtualQueryEx($hProcess, $ModuleBaseAddress, [Ref] $MemoryInfo, $PageSize)
$null = $Kernel32::CloseHandle($hProcess)

请注意,上面的代码是通过以下方式从不同的函数调用的:$MemoryInfo = Get-VirtualMemoryInfo -ProcessID $ProcessID -ModuleBaseAddress ([IntPtr]::Zero) -PageSize $SysInfo.PageSize

我在Go中的实现如下:

var systemInfo SYSTEM_INFO
getSystemInfo := 
kernel32dll.NewProc("GetSystemInfo")
_, _, err = getSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo)))
openProcess := kernel32dll.NewProc("OpenProcess")
hProcess, _, err := openProcess.Call(uintptr(0x410), uintptr(0), uintptr(processId))
fmt.Println("Message from OpenProcess:",err.Error())
defer windows.CloseHandle(windows.Handle(hProcess))
var memoryBasicInformation MEMORY_BASIC_INFORMATION
dll := syscall.MustLoadDLL("kernel32.dll")
defer dll.Release()
virtualQueryEx := dll.MustFindProc("VirtualQueryEx")
bytesRead, _, err := virtualQueryEx.Call((uintptr)(unsafe.Pointer(hProcess)), (uintptr)(unsafe.Pointer(????)), (uintptr)(unsafe.Pointer(&memoryBasicInformation)), (uintptr)(unsafe.Pointer(&systemInfo.PageSize)))
fmt.Println("Bytes read:",bytesRead)
fmt.Println("Message from VirtualQueryEx:", err.Error())

无论我做什么,VirtualQueryEx都会返回"对内存位置的无效访问"。我无法确定要传递什么值作为进程的基地址(用上面的"???"表示)。微软文档中说这个参数是可选的,但如果我把它留空,我会得到一个关于命令长度不正确的错误。

正如我所提到的,我的目标是从流程的底部开始,并通过循环扫描整个过程,这将在对VirtualQueryEx的初始调用之后发生。

作为参考,我使用go syscall库(在本例中;尽管我也尝试过sys/windows库,但没有成功。)

如果我能澄清什么,请告诉我。

每个进程都有自己的地址空间。如果您访问的内存地址不属于它,那么可能会导致"无效访问内存位置"。

我的目标是从流程的基础开始整个事情通过一个循环

首先,设置"lpAddress"(我的意思是"???")从零开始。(通常你会得到进程基本地址和零地址之间的偏移量)

然后,检查memoryBasicInformation.AllocationProtectmemoryBasicInformation.Protect的值以确定是否存在访问权限。

使用循环中的"lpAddress">

lpAddress = memoryBasicInformation.BaseAddress + memoryBasicInformation.RegionSize

则继续使用CCD_ 4。当lpAddress指定的地址高于进程可访问的最高内存地址时,该函数将以ERROR_INVALID_PARAMETER失败,并中断循环。

最新更新