-
Notifications
You must be signed in to change notification settings - Fork 7
/
gomem_windows_amd64.go
129 lines (95 loc) · 2.68 KB
/
gomem_windows_amd64.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package gomem
import (
"unsafe"
"github.com/jamesmoriarty/gomem/internal/kernel32"
"github.com/jamesmoriarty/gomem/internal/user32"
)
// Process is a struct representing a windows process.
type Process struct {
ID uint32
Name string
Handle uintptr
}
// GetProcessFromName converts a process name to a Process struct.
func GetProcessFromName(name string) (*Process, error) {
pid, err := kernel32.GetProcessID(name)
if err != nil {
return nil, err
}
process := Process{ID: pid, Name: name}
return &process, nil
}
// GetOpenProcessFromName converts a process name to a Process struct with open handle.
func GetOpenProcessFromName(name string) (*Process, error) {
process, err := GetProcessFromName(name)
if err != nil {
return nil, err
}
_, err = process.Open()
if err != nil {
return nil, err
}
return process, nil
}
// Open process handle.
func (p *Process) Open() (uintptr, error) {
handle, err := kernel32.OpenProcess(kernel32.PROCESS_ALL_ACCESS, false, p.ID)
if err != nil {
return 0, err
}
p.Handle = handle
return handle, err
}
// Read process memory.
func (p *Process) Read(offset uintptr, buffer uintptr, length uintptr) error {
_, err := kernel32.ReadProcessMemory(p.Handle, offset, buffer, length)
return err
}
// Read byte from process memory.
func (p *Process) ReadByte(offset uintptr) (byte, error) {
var (
value byte
valuePtr = (uintptr)(unsafe.Pointer(&value))
)
err := p.Read(offset, valuePtr, unsafe.Sizeof(value))
return value, err
}
// Read uint32 from process memory.
func (p *Process) ReadUInt32(offset uintptr) (uint32, error) {
var (
value uint32
valuePtr = (uintptr)(unsafe.Pointer(&value))
)
err := p.Read(offset, valuePtr, unsafe.Sizeof(value))
return value, err
}
// Read uint64 from process memory.
func (p *Process) ReadUInt64(offset uintptr) (uint64, error) {
var (
value uint64
valuePtr = (uintptr)(unsafe.Pointer(&value))
)
err := p.Read(offset, valuePtr, unsafe.Sizeof(value))
return value, err
}
// Write process memory.
func (p *Process) Write(offset uintptr, buffer uintptr, length uintptr) error {
_, err := kernel32.WriteProcessMemory(p.Handle, offset, buffer, length)
return err
}
// Write byte to process memory.
func (p *Process) WriteByte(offset uintptr, value byte) error {
var (
valuePtr = (uintptr)(unsafe.Pointer(&value))
)
return p.Write(offset, valuePtr, unsafe.Sizeof(value))
}
// GetModule address.
func (p *Process) GetModule(name string) (uintptr, error) {
ptr, err := kernel32.GetModule(name, p.ID)
return ptr, err
}
// IsKeyDown https://docs.microsoft.com/en-gb/windows/win32/inputdev/virtual-key-codes
func IsKeyDown(v int) bool {
return user32.GetAsyncKeyState(v) > 0
}