-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemprocfs.go
195 lines (174 loc) · 4.94 KB
/
memprocfs.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package memprocfs
// #include <stdlib.h>
// #include "leechcore.h"
// #include "vmmdll.h"
import "C"
import (
"bytes"
"fmt"
"reflect"
"unsafe"
)
type MemProcFS struct {
vmDllHandle C.VMM_HANDLE
}
// Initialize VMMDLL
// for ex New("-device", "fpga", "-memmap", "memmap.txt")
func New(args ...string) (*MemProcFS, error) {
if len(args) == 0 {
//Default
args = []string{"-device", "fpga", "-memmap", "memmap.txt"}
}
cArgs := make([]C.LPCSTR, len(args))
for i, arg := range args {
cArgs[i] = C.LPCSTR(C.CString(arg))
defer C.free(unsafe.Pointer(cArgs[i]))
}
vmmHandle := C.VMMDLL_Initialize(C.DWORD(len(args)), &cArgs[0])
if vmmHandle == nil {
return nil, fmt.Errorf("failed to initialize FPGA")
}
return &MemProcFS{
vmDllHandle: vmmHandle,
}, nil
}
func (h *MemProcFS) Close() {
C.VMMDLL_Close(h.vmDllHandle)
}
func (h *MemProcFS) GetPidByName(name string) (int32, error) {
pid := C.DWORD(0)
ok := C.VMMDLL_PidGetFromName(h.vmDllHandle, C.LPSTR(C.CString(name)), &pid)
if pid == 0 || ok == 0 {
return 0, fmt.Errorf("failed to find process by name: %s", name)
}
return int32(pid), nil
}
type VMMDLLProcessInformation struct {
Magic uint64
WVersion uint16
WSize uint16
TpMemoryModel uint32
TpSystem uint32
FUserOnly bool
DwPID uint32
DwPPID uint32
DwState uint32
SzName string
SzNameLong string
PaDTB uint64
PaDTB_UserOpt uint64
Win struct {
VaEPROCESS uint64
VaPEB uint64
Reserved1 uint64
FWow64 bool
VaPEB32 uint32
DwSessionId uint32
QwLUID uint64
SzSID [260]byte
IntegrityLevel uint32
}
}
func (h *MemProcFS) GetProcessInfo(pid int32) (*VMMDLLProcessInformation, error) {
size := C.SIZE_T(0)
C.VMMDLL_ProcessGetInformation(h.vmDllHandle, C.DWORD(pid), nil, &size)
pInfo := C.VMMDLL_PROCESS_INFORMATION{
magic: C.VMMDLL_PROCESS_INFORMATION_MAGIC,
wVersion: C.VMMDLL_PROCESS_INFORMATION_VERSION,
}
ok := C.VMMDLL_ProcessGetInformation(h.vmDllHandle, C.DWORD(pid), &pInfo, &size)
if ok == 0 {
return nil, fmt.Errorf("failed to get process info: %d", pid)
}
result := &VMMDLLProcessInformation{
Magic: uint64(pInfo.magic),
WVersion: uint16(pInfo.wVersion),
WSize: uint16(pInfo.wSize),
TpMemoryModel: uint32(pInfo.tpMemoryModel),
TpSystem: uint32(pInfo.tpSystem),
FUserOnly: pInfo.fUserOnly != 0,
DwPID: uint32(pInfo.dwPID),
DwPPID: uint32(pInfo.dwPPID),
DwState: uint32(pInfo.dwState),
PaDTB: uint64(pInfo.paDTB),
PaDTB_UserOpt: uint64(pInfo.paDTB_UserOpt),
Win: struct {
VaEPROCESS uint64
VaPEB uint64
Reserved1 uint64
FWow64 bool
VaPEB32 uint32
DwSessionId uint32
QwLUID uint64
SzSID [260]byte
IntegrityLevel uint32
}{
VaEPROCESS: uint64(pInfo.win.vaEPROCESS),
VaPEB: uint64(pInfo.win.vaPEB),
Reserved1: uint64(pInfo.win._Reserved1),
FWow64: pInfo.win.fWow64 != 0,
VaPEB32: uint32(pInfo.win.vaPEB32),
DwSessionId: uint32(pInfo.win.dwSessionId),
QwLUID: uint64(pInfo.win.qwLUID),
IntegrityLevel: uint32(pInfo.win.IntegrityLevel),
SzSID: *(*[260]byte)(unsafe.Pointer(&pInfo.win.szSID)),
},
}
szName := (*(*[16]byte)(unsafe.Pointer(&pInfo.szName)))[:]
n := bytes.Index(szName, []byte{0})
result.SzName = string(szName[:n])
szLongName := (*(*[64]byte)(unsafe.Pointer(&pInfo.szNameLong)))[:]
n = bytes.Index(szLongName, []byte{0})
result.SzNameLong = string(szLongName[:n])
return result, nil
}
func (h *MemProcFS) MemWrite(pid int32, addr uintptr, data []byte) error {
// TODO: Provide data array by pointer into function to avoid copying
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ok := C.VMMDLL_MemWrite(
h.vmDllHandle,
C.DWORD(pid),
C.ULONG64(addr),
C.PBYTE(unsafe.Pointer(sh.Data)),
C.DWORD(len(data)))
if ok == 0 {
return fmt.Errorf("failed to MemWrite: %d %d", pid, addr)
}
return nil
}
func (h *MemProcFS) MemRead(pid int32, addr uintptr, size int32) ([]byte, error) {
if !IsValidAddress(addr) {
return nil, fmt.Errorf("failed to read, invalid address: %X", addr)
}
buff := make([]byte, size, size)
sh := (*reflect.SliceHeader)(unsafe.Pointer(&buff))
ok := C.VMMDLL_MemReadEx(
h.vmDllHandle,
C.DWORD(pid),
C.ULONG64(addr),
C.PBYTE(unsafe.Pointer(sh.Data)),
C.DWORD(size),
nil,
C.ULONG64(0x0003))
if ok == 0 {
return nil, fmt.Errorf("failed to MemRead: %d %X", pid, addr)
}
return buff, nil
}
func (h *MemProcFS) MemReadToBuff(pid int32, addr uintptr, size int32, ptr unsafe.Pointer) error {
if !IsValidAddress(addr) {
return fmt.Errorf("failed to read, invalid address: %X", addr)
}
ok := C.VMMDLL_MemReadEx(
h.vmDllHandle,
C.DWORD(pid),
C.ULONG64(addr),
C.PBYTE(ptr),
C.DWORD(size),
nil,
C.ULONG64(0x0003))
if ok == 0 {
return fmt.Errorf("failed to MemRead: %d %X", pid, addr)
}
return nil
}