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

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

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代码如下:

  1. var patch = []byte {
  2. 0x31, 0xC0, // xor rax, rax
  3. 0xC3, // ret
  4. }
  5. var oldfperms uint32
  6. virtualProt(unsafe.Pointer(&patchAddr), unsafe.Sizeof(uintptr(2)), uint32(0x40),
  7. unsafe.Pointer(&oldfperms)) // 修改区域为可读写
  8. var r uintptr
  9. for _, b := range patch {
  10. r = (r << 8) | uintptr(b)
  11. }
  12. patch := unsafe.Pointer(uintptr(r)) // 在这里尝试将数据复制到内存,但我被卡住了
  13. fmt.Println(patch)
  14. var a uint32
  15. 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:

  1. var patch = []byte {
  2. 0x31, 0xC0, // xor rax, rax
  3. 0xC3, // ret
  4. }
  5. var oldfperms uint32
  6. virtualProt(unsafe.Pointer(&amp;patchAddr), unsafe.Sizeof(uintptr(2)), uint32(0x40),
  7. unsafe.Pointer(&amp;oldfperms)) // Modify region for ReadWrite
  8. var r uintptr
  9. for _, b := range patch {
  10. r = (r &lt;&lt; 8) | uintptr(b)
  11. }
  12. patch := unsafe.Pointer(uintptr(r)) // Attempting to copy into memory here and I&#39;m stumped
  13. fmt.Println(patch)
  14. var a uint32
  15. 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

  1. func WriteProcMem(currProccess uintptr, patchAddr uintptr, patch uintptr) bool {
  2. kern32WriteMem := syscall.NewLazyDLL("kernel32.dll").NewProc("WriteProcessMemory")
  3. _, _, _ = kern32WriteMem.Call(
  4. currProccess,
  5. patchAddr,
  6. patch)
  7. fmt.Println("[+] Patched Memory!")
  8. return true
  9. }
英文:

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

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

  1. func WriteProcMem(currProccess uintptr, patchAddr uintptr, patch uintptr) bool {
  2. kern32WriteMem := syscall.NewLazyDLL(&quot;kernel32.dll&quot;).NewProc(&quot;WriteProcessMemory&quot;)
  3. _, _, _ = kern32WriteMem.Call(
  4. currProccess,
  5. patchAddr,
  6. patch)
  7. fmt.Println(&quot;[+] Patched Memory!&quot;)
  8. return true
  9. }

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:

确定