【问题标题】:What is the Golang equivalent of .Net's Marshal.Copy method?.Net 的 Marshal.Copy 方法的 Golang 等价物是什么?
【发布时间】:2021-11-28 16:59:44
【问题描述】:

我正在尝试在 Golang 中修补一块内存。我已经关闭了 VirtualProtect 功能,并且内存块正在更改为 RW,但我找不到用于复制到内存中的 Golang 功能。

我想从 Powershell 脚本中模拟这个:

[System.Runtime.InteropServices.Marshal]::Copy($patch, 0, $targetedAddress, 3)

我目前拥有的 Golang 代码如下:

var patch = []byte {
    0x31, 0xC0, // xor rax, rax
    0xC3,        // ret
}

var oldfperms uint32
virtualProt(unsafe.Pointer(&patchAddr), unsafe.Sizeof(uintptr(2)), uint32(0x40), 
unsafe.Pointer(&oldfperms)) // Modify region for ReadWrite

var r uintptr
for _, b := range patch {
    r = (r << 8) | uintptr(b)
}

patch := unsafe.Pointer(uintptr(r)) // Attempting to copy into memory here and I'm stumped
fmt.Println(patch)

var a uint32
virtualProt(unsafe.Pointer(&patchAddr), unsafe.Sizeof(uintptr(2)), oldfperms, unsafe.Pointer(&a)) // Change region back to normal

【问题讨论】:

    标签: go memory memory-management marshalling


    【解决方案1】:

    没关系。找到对 Win32 WriteProcessMemory 函数的引用并使用它。

    https://pkg.go.dev/github.com/0xrawsec/golang-win32/win32/kernel32#WriteProcessMemory

    func WriteProcMem(currProccess uintptr, patchAddr uintptr, patch uintptr) bool {
    
        kern32WriteMem := syscall.NewLazyDLL("kernel32.dll").NewProc("WriteProcessMemory")
        _, _, _ = kern32WriteMem.Call(
        currProccess,
        patchAddr,
        patch)
        fmt.Println("[+] Patched Memory!")
        return true
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2010-10-02
      • 1970-01-01
      • 2015-04-27
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多