-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproc.go
More file actions
155 lines (125 loc) · 4.42 KB
/
Copy pathproc.go
File metadata and controls
155 lines (125 loc) · 4.42 KB
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
package myproc
import (
"reflect"
"strings"
"unsafe"
"golang.org/x/sys/windows"
)
// NewProc is the reimplementation of GetProcAddress using binary search (3x faster) with a linear search fallback.
// It implements search by name, ordinal or hash.
func NewProc[T ~string | ~uint16 | ~uint32](dll *windows.DLL, procedure T) *windows.Proc {
if dll == nil || dll.Handle == 0 {
return nil
}
// In case this is a forwarded function
var parent string
proc := new(windows.Proc)
proc.Dll = dll
switch reflect.TypeOf(procedure).Kind() {
case reflect.String:
proc.Name = any(procedure).(string)
}
dataDir := GetDataDirectory(dll.Handle, IMAGE_DIRECTORY_ENTRY_EXPORT)
exportDir := GetExportDirectory(dll.Handle)
procAddr := ResolveFunctionAddr(dll, procedure)
// Check if this is a forwarded function
for IsForwardedFunction(procAddr, exportDir, dataDir.Size) {
forwardName := windows.BytePtrToString((*byte)(unsafe.Pointer(procAddr)))
forward := strings.Split(forwardName, ".")
// Check if it starts with api or ext
if strings.HasPrefix(forward[0], "api-") || strings.HasPrefix(forward[0], "ext-") {
host := ResolveApiSet(forward[0], parent)
// Forwarded again ?
if !strings.HasPrefix(host, "api-") && !strings.HasPrefix(host, "ext-") {
parent = host
}
// Set new dll
proc.Dll = NewDLL(host)
exportDir = GetExportDirectory(proc.Dll.Handle)
// Resolve again
procAddr = ResolveFunctionAddr(proc.Dll, forward[1])
} else {
procAddr = ResolveFunctionAddr(NewDLL(forward[0]+".dll"), forward[1])
}
}
// Still not found ? return nil
if procAddr != 0 {
// trick to set unexported addr field in windows.Proc structure
addr := reflect.ValueOf(proc).Elem().FieldByName("addr")
reflect.NewAt(addr.Type(), unsafe.Pointer(addr.UnsafeAddr())).Elem().Set(reflect.ValueOf(procAddr))
return proc
}
return nil
}
// ResolveFunctionAddr returns the address of a function. Search by name, ordinal or hash.
func ResolveFunctionAddr[T ~string | ~uint16 | ~uint32](dll *windows.DLL, procedure T) uintptr {
if dll == nil || dll.Handle == 0 {
return 0
}
var procName string
var procOrdinal uint16
var procHash uint32
var procAddr uintptr
switch reflect.TypeOf(procedure).Kind() {
case reflect.String:
procName = any(procedure).(string)
case reflect.Uint16:
procOrdinal = any(procedure).(uint16)
case reflect.Uint32:
procHash = any(procedure).(uint32)
}
if procHash == 0 && procName != "" {
procHash = Hash(procName)
}
module := unsafe.Pointer(dll.Handle)
exportDir := GetExportDirectory(dll.Handle)
addrOfFunctions := unsafe.Add(module, exportDir.AddressOfFunctions)
addrOfNames := unsafe.Add(module, exportDir.AddressOfNames)
addrOfNameOrdinals := unsafe.Add(module, exportDir.AddressOfNameOrdinals)
sliceOfFunctions := unsafe.Slice((*uint32)(addrOfFunctions), exportDir.NumberOfFunctions)
sliceOfNames := unsafe.Slice((*uint32)(addrOfNames), exportDir.NumberOfNames)
sliceOfAddrOfNameOrdinals := unsafe.Slice((*uint16)(addrOfNameOrdinals), exportDir.NumberOfNames)
// ordinal search
if procOrdinal != 0 {
procOrdinal -= uint16(exportDir.Base)
procAddr = uintptr(unsafe.Add(module, sliceOfFunctions[procOrdinal]))
return procAddr
}
// binary search
if procName != "" {
left := uintptr(0)
right := uintptr(exportDir.NumberOfNames - 1)
for left != right {
middle := left + ((right - left) >> 1)
currentName := windows.BytePtrToString((*byte)(unsafe.Add(module, sliceOfNames[middle])))
if Hash(currentName) == procHash {
index := sliceOfAddrOfNameOrdinals[middle]
procAddr = uintptr(unsafe.Add(module, sliceOfFunctions[index]))
return procAddr
} else if currentName < procName {
left = middle + 1
} else {
right = middle - 1
}
}
}
// linear search
if procAddr == 0 {
for i := uintptr(0); i < uintptr(exportDir.NumberOfNames); i++ {
currentName := windows.BytePtrToString((*byte)(unsafe.Add(module, sliceOfNames[i])))
if Hash(currentName) == procHash {
index := sliceOfAddrOfNameOrdinals[i]
procAddr = uintptr(unsafe.Add(module, sliceOfFunctions[index]))
return procAddr
}
}
}
return 0
}
// IsForwardedFunction checks if the proc is valid, if not it's a forwarded function
func IsForwardedFunction(procAddr uintptr, exportDir *IMAGE_EXPORT_DIRECTORY, exportDirSize uint32) bool {
if procAddr >= uintptr(unsafe.Pointer(exportDir)) && procAddr < uintptr(unsafe.Pointer(exportDir))+uintptr(exportDirSize) {
return true
}
return false
}