This repository has been archived by the owner on Oct 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm.go
46 lines (36 loc) · 1.38 KB
/
pm.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
package pm
import (
"unsafe"
"github.com/kencyke/pm/syscall/linux"
)
// Some codes originally taken from https://stackoverflow.com/questions/32223562/how-to-convert-uintptr-to-byte-in-golang
const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))
func uintptrToBytes(u uintptr) []byte {
return (*[sizeOfUintPtr]byte)(unsafe.Pointer(&u))[:]
}
func uintptrToIovecBase(u uintptr) *byte {
return &uintptrToBytes(u)[0]
}
// ReadAddress copies memory from another process into an already allocated byte buffer.
func ReadAddress(pid int, address uintptr, buffer []byte) error {
var length uint64
if len(buffer) > 0 {
length = uint64(len(buffer))
}
liov := make([]linux.Iovec, 0)
liov = append(liov, linux.Iovec{Base: &buffer[0], Len: length})
riov := make([]linux.Iovec, 0)
riov = append(riov, linux.Iovec{Base: uintptrToIovecBase(address), Len: length})
_, err := linux.ProcessVMReadv(pid, liov, 1, riov, 1, 0)
return err
}
// CopyAddress copies memory from another process by allocating memory for you.
func CopyAddress(pid int, address uintptr, length uint64) (data []byte, err error) {
data = make([]byte, length)
liov := make([]linux.Iovec, 0)
liov = append(liov, linux.Iovec{Base: &data[0], Len: length})
riov := make([]linux.Iovec, 0)
riov = append(riov, linux.Iovec{Base: uintptrToIovecBase(address), Len: length})
_, err = linux.ProcessVMReadv(pid, liov, 1, riov, 1, 0)
return data, err
}