forked from brahma-adshonor/gohook
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutility_unix.go
More file actions
32 lines (27 loc) · 819 Bytes
/
utility_unix.go
File metadata and controls
32 lines (27 loc) · 819 Bytes
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
// +build !windows
package gohook
import (
"syscall"
)
func getPageAddr(ptr uintptr) uintptr {
return ptr & ^(uintptr(syscall.Getpagesize() - 1))
}
func setPageWritable(addr uintptr, length int, prot int) {
pageSize := syscall.Getpagesize()
for p := getPageAddr(addr); p < addr+uintptr(length); p += uintptr(pageSize) {
page := makeSliceFromPointer(p, pageSize)
err := syscall.Mprotect(page, prot)
if err != nil {
panic(err)
}
}
}
func CopyInstruction(location uintptr, data []byte) {
f := makeSliceFromPointer(location, len(data))
setPageWritable(location, len(data), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)
sz := copy(f, data[:])
setPageWritable(location, len(data), syscall.PROT_READ|syscall.PROT_EXEC)
if sz != len(data) {
panic("copy instruction to target failed")
}
}