-
Notifications
You must be signed in to change notification settings - Fork 10
/
patcher_windows.go
35 lines (29 loc) · 978 Bytes
/
patcher_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//go:build windows
// +build windows
package mpatch
import (
"syscall"
"unsafe"
)
const pageExecuteReadAndWrite = 0x40
var virtualProtectProc = syscall.NewLazyDLL("kernel32.dll").NewProc("VirtualProtect")
func callVirtualProtect(lpAddress unsafe.Pointer, dwSize int, flNewProtect uint32, lpflOldProtect unsafe.Pointer) error {
ret, _, _ := virtualProtectProc.Call(uintptr(lpAddress), uintptr(dwSize), uintptr(flNewProtect), uintptr(lpflOldProtect))
if ret == 0 {
return syscall.GetLastError()
}
return nil
}
func writeDataToPointer(ptr unsafe.Pointer, data []byte) error {
var oldPerms, tmp uint32
dataLength := len(data)
ptrByteSlice := getMemorySliceFromPointer(ptr, len(data))
if err := callVirtualProtect(ptr, dataLength, pageExecuteReadAndWrite, unsafe.Pointer(&oldPerms)); err != nil {
return err
}
copy(ptrByteSlice, data[:])
if err := callVirtualProtect(ptr, dataLength, oldPerms, unsafe.Pointer(&tmp)); err != nil {
return err
}
return nil
}