Golang中与.Net的Marshal.Copy方法等效的方法是什么?

huangapple go评论67阅读模式
英文:

What is the Golang equivalent of .Net's Marshal.Copy method?

问题

我正在尝试在Golang中修补一块内存。我已经实现了VirtualProtect功能,并且内存块已被更改为可读写,但我找不到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)) // 修改区域为可读写

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

patch := unsafe.Pointer(uintptr(r)) // 在这里尝试将数据复制到内存,但我被卡住了
fmt.Println(patch)

var a uint32
virtualProt(unsafe.Pointer(&patchAddr), unsafe.Sizeof(uintptr(2)), oldfperms, unsafe.Pointer(&a)) // 将区域恢复为正常

以上是翻译好的内容,请确认。

英文:

I am trying to patch a chunk of memory in Golang. I have the VirtualProtect functionality down and the memory chunk is being changed to RW, but I can't find figure out the Golang functionality for Copying into memory.

I want to emulate this from a Powershell Script:

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

The Golang code I currently have is below:

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

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

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

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

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

答案1

得分: 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
}
英文:

Nevermind. Found the reference to the Win32 WriteProcessMemory function and used that.

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

func WriteProcMem(currProccess uintptr, patchAddr uintptr, patch uintptr) bool {

    kern32WriteMem := syscall.NewLazyDLL(&quot;kernel32.dll&quot;).NewProc(&quot;WriteProcessMemory&quot;)
    _, _, _ = kern32WriteMem.Call(
    currProccess,
    patchAddr,
    patch)
    fmt.Println(&quot;[+] Patched Memory!&quot;)
    return true

}

huangapple
  • 本文由 发表于 2021年10月9日 03:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69500858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定