forked from brahma-adshonor/gohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility_windows.go
48 lines (38 loc) · 1.09 KB
/
utility_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
45
46
47
48
//go:build windows
// +build windows
package gohook
import (
"syscall"
"unsafe"
)
var (
defaultFuncPrologue32 = []byte{0x65, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x89, 0xfc, 0xff, 0xff, 0xff}
defaultFuncPrologue64 = []byte{0x65, 0x48, 0x8B, 0x0C, 0x25, 0x28, 0x00, 0x00, 0x00}
)
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
}
func CopyInstruction(location uintptr, data []byte) {
f := makeSliceFromPointer(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[:])
var tmp uint32
err = virtualProtect(location, len(data), oldPerms, unsafe.Pointer(&tmp))
if err != nil {
panic(err)
}
}