forked from Leo-Guo/monkey
-
Notifications
You must be signed in to change notification settings - Fork 4
/
replace_unix.go
34 lines (30 loc) · 938 Bytes
/
replace_unix.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
//go:build !windows
// +build !windows
package monkey
import (
"runtime"
"syscall"
)
func mprotectCrossPage(addr uintptr, length int, prot int) {
pageSize := syscall.Getpagesize()
for p := pageStart(addr); p < addr+uintptr(length); p += uintptr(pageSize) {
page := rawMemoryAccess(p, pageSize)
err := syscall.Mprotect(page, prot)
if err != nil {
panic(err)
}
}
}
// 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))
if runtime.GOARCH == "arm64" {
mprotectCrossPage(location, len(data), syscall.PROT_READ|syscall.PROT_WRITE)
} else {
mprotectCrossPage(location, len(data), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)
}
copy(f, data[:])
mprotectCrossPage(location, len(data), syscall.PROT_READ|syscall.PROT_EXEC)
}