-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_windows.go
44 lines (37 loc) · 1.13 KB
/
replace_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
36
37
38
39
40
41
42
43
44
package ifix
import (
"syscall"
"unsafe"
)
const PAGE_EXECUTE_READWRITE = 0x40
var procVirtualProtect = syscall.NewLazyDLL("kernel32.dll").NewProc("VirtualProtect")
func virtualProtect(lpAddress uintptr, dwSize int, flNewProtect uint32, lpflOldProtect unsafe.Pointer) error {
ret, _, _ := procVirtualProtect.Call(
lpAddress,
uintptr(dwSize),
uintptr(flNewProtect),
uintptr(lpflOldProtect))
if ret == 0 {
return syscall.GetLastError()
}
return nil
}
// this function is super unsafe
// aww yeah
// It copies a slice to a raw memory location, disabling all memory protection before doing so.
func copyToLocation(location uintptr, data []byte) {
f := rawMemoryAccess(location, len(data))
var oldPerms uint32
err := virtualProtect(location, len(data), PAGE_EXECUTE_READWRITE, unsafe.Pointer(&oldPerms))
if err != nil {
panic(err)
}
copy(f, data[:])
// VirtualProtect requires you to pass in a pointer which it can write the
// current memory protection permissions to, even if you don't want them.
var tmp uint32
err = virtualProtect(location, len(data), oldPerms, unsafe.Pointer(&tmp))
if err != nil {
panic(err)
}
}