forked from ropnop/go-clr
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgo-clr.go
378 lines (336 loc) · 9.35 KB
/
go-clr.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//go:build windows
// +build windows
// Package clr is a PoC package that wraps Windows syscalls necessary to load and the CLR into the current process and
// execute a managed DLL from disk or a managed EXE from memory
package clr
import (
"fmt"
"strings"
"syscall"
"unsafe"
)
// GetInstalledRuntimes is a wrapper function that returns an array of installed runtimes. Requires an existing ICLRMetaHost
func GetInstalledRuntimes(metahost *ICLRMetaHost) ([]string, error) {
var runtimes []string
enumICLRRuntimeInfo, err := metahost.EnumerateInstalledRuntimes()
if err != nil {
return runtimes, err
}
var hr int
for hr != S_FALSE {
var runtimeInfo *ICLRRuntimeInfo
var fetched = uint32(0)
hr, err = enumICLRRuntimeInfo.Next(1, unsafe.Pointer(&runtimeInfo), &fetched)
if err != nil {
return runtimes, fmt.Errorf("InstalledRuntimes Next Error:\r\n%s\n", err)
}
if hr == S_FALSE {
break
}
// Only release if an interface pointer was returned
runtimeInfo.Release()
version, err := runtimeInfo.GetVersionString()
if err != nil {
return runtimes, err
}
runtimes = append(runtimes, version)
}
if len(runtimes) == 0 {
return runtimes, fmt.Errorf("Could not find any installed runtimes")
}
return runtimes, err
}
// ExecuteDLLFromDisk is a wrapper function that will automatically load the latest installed CLR into the current process
// and execute a DLL on disk in the default app domain. It takes in the target runtime, DLLPath, TypeName, MethodName
// and Argument to use as strings. It returns the return code from the assembly
func ExecuteDLLFromDisk(targetRuntime, dllpath, typeName, methodName, argument string) (retCode int16, err error) {
retCode = -1
if targetRuntime == "" {
targetRuntime = "v4"
}
metahost, err := CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost)
if err != nil {
return
}
runtimes, err := GetInstalledRuntimes(metahost)
if err != nil {
return
}
var latestRuntime string
for _, r := range runtimes {
if strings.Contains(r, targetRuntime) {
latestRuntime = r
break
} else {
latestRuntime = r
}
}
runtimeInfo, err := GetRuntimeInfo(metahost, latestRuntime)
if err != nil {
return
}
isLoadable, err := runtimeInfo.IsLoadable()
if err != nil {
return
}
if !isLoadable {
return -1, fmt.Errorf("%s is not loadable for some reason", latestRuntime)
}
runtimeHost, err := GetICLRRuntimeHost(runtimeInfo)
if err != nil {
return
}
pDLLPath, err := syscall.UTF16PtrFromString(dllpath)
if err != nil {
return
}
pTypeName, err := syscall.UTF16PtrFromString(typeName)
if err != nil {
return
}
pMethodName, err := syscall.UTF16PtrFromString(methodName)
if err != nil {
return
}
pArgument, err := syscall.UTF16PtrFromString(argument)
if err != nil {
return
}
ret, err := runtimeHost.ExecuteInDefaultAppDomain(pDLLPath, pTypeName, pMethodName, pArgument)
if err != nil {
return
}
if *ret != 0 {
return int16(*ret), fmt.Errorf("the ICLRRuntimeHost::ExecuteInDefaultAppDomain method returned a non-zero return value: %d", *ret)
}
runtimeHost.Release()
runtimeInfo.Release()
metahost.Release()
return 0, nil
}
// ExecuteByteArray is a wrapper function that will automatically loads the supplied target framework into the current
// process using the legacy APIs, then load and execute an executable from memory. If no targetRuntime is specified, it
// will default to latest. It takes in a byte array of the executable to load and run and returns the return code.
// You can supply an array of strings as command line arguments.
func ExecuteByteArray(targetRuntime string, rawBytes []byte, params []string) (retCode int32, err error) {
retCode = -1
if targetRuntime == "" {
targetRuntime = "v4"
}
metahost, err := CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost)
if err != nil {
return
}
runtimes, err := GetInstalledRuntimes(metahost)
if err != nil {
return
}
var latestRuntime string
for _, r := range runtimes {
if strings.Contains(r, targetRuntime) {
latestRuntime = r
break
} else {
latestRuntime = r
}
}
runtimeInfo, err := GetRuntimeInfo(metahost, latestRuntime)
if err != nil {
return
}
isLoadable, err := runtimeInfo.IsLoadable()
if err != nil {
return
}
if !isLoadable {
return -1, fmt.Errorf("%s is not loadable for some reason", latestRuntime)
}
runtimeHost, err := GetICORRuntimeHost(runtimeInfo)
if err != nil {
return
}
appDomain, err := GetAppDomain(runtimeHost)
if err != nil {
return
}
safeArrayPtr, err := CreateSafeArray(rawBytes)
if err != nil {
return
}
assembly, err := appDomain.Load_3(safeArrayPtr)
if err != nil {
return
}
methodInfo, err := assembly.GetEntryPoint()
if err != nil {
return
}
var paramSafeArray *SafeArray
methodSignature, err := methodInfo.GetString()
if err != nil {
return
}
if expectsParams(methodSignature) {
if paramSafeArray, err = PrepareParameters(params); err != nil {
return
}
}
nullVariant := Variant{
VT: 1,
Val: uintptr(0),
}
err = methodInfo.Invoke_3(nullVariant, paramSafeArray)
if err != nil {
return
}
appDomain.Release()
runtimeHost.Release()
runtimeInfo.Release()
metahost.Release()
return 0, nil
}
// LoadCLR loads the target runtime into the current process and returns the runtimehost
// The intended purpose is for the runtimehost to be reused for subsequent operations
// throughout the duration of the program. Commonly used with C2 frameworks
func LoadCLR(targetRuntime string) (runtimeHost *ICORRuntimeHost, err error) {
if targetRuntime == "" {
targetRuntime = "v4"
}
metahost, err := CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost)
if err != nil {
return runtimeHost, fmt.Errorf("there was an error enumerating the installed CLR runtimes:\n%s", err)
}
runtimes, err := GetInstalledRuntimes(metahost)
debugPrint(fmt.Sprintf("Installed Runtimes: %v", runtimes))
if err != nil {
return
}
var latestRuntime string
for _, r := range runtimes {
if strings.Contains(r, targetRuntime) {
latestRuntime = r
break
} else {
latestRuntime = r
}
}
runtimeInfo, err := GetRuntimeInfo(metahost, latestRuntime)
if err != nil {
return
}
isLoadable, err := runtimeInfo.IsLoadable()
if err != nil {
return
}
if !isLoadable {
err = fmt.Errorf("%s is not loadable for some reason", latestRuntime)
}
return GetICORRuntimeHost(runtimeInfo)
}
// ExecuteByteArrayDefaultDomain uses a previously instantiated runtimehost, gets the default AppDomain,
// loads the assembly into, executes the assembly, and then releases AppDomain
// Intended to be used by C2 frameworks to quickly execute an assembly one time
func ExecuteByteArrayDefaultDomain(runtimeHost *ICORRuntimeHost, rawBytes []byte, params []string) (stdout string, stderr string) {
appDomain, err := GetAppDomain(runtimeHost)
if err != nil {
stderr = err.Error()
return
}
safeArrayPtr, err := CreateSafeArray(rawBytes)
if err != nil {
stderr = err.Error()
return
}
assembly, err := appDomain.Load_3(safeArrayPtr)
if err != nil {
stderr = err.Error()
return
}
methodInfo, err := assembly.GetEntryPoint()
if err != nil {
stderr = err.Error()
return
}
var paramSafeArray *SafeArray
methodSignature, err := methodInfo.GetString()
if err != nil {
stderr = err.Error()
return
}
if expectsParams(methodSignature) {
if paramSafeArray, err = PrepareParameters(params); err != nil {
stderr = err.Error()
return
}
}
nullVariant := Variant{
VT: 1,
Val: uintptr(0),
}
err = methodInfo.Invoke_3(nullVariant, paramSafeArray)
if err != nil {
stderr = err.Error()
return
}
assembly.Release()
appDomain.Release()
return
}
// LoadAssembly uses a previously instantiated runtimehost and loads an assembly into the default AppDomain
// and returns the assembly's methodInfo structure. The intended purpose is for the assembly to be loaded
// once but executed many times throughout the duration of the program. Commonly used with C2 frameworks
func LoadAssembly(runtimeHost *ICORRuntimeHost, rawBytes []byte) (methodInfo *MethodInfo, err error) {
appDomain, err := GetAppDomain(runtimeHost)
if err != nil {
return
}
safeArrayPtr, err := CreateSafeArray(rawBytes)
if err != nil {
return
}
assembly, err := appDomain.Load_3(safeArrayPtr)
if err != nil {
return
}
return assembly.GetEntryPoint()
}
// InvokeAssembly uses the MethodInfo structure of a previously loaded assembly and executes it.
// The intended purpose is for the assembly to be executed many times throughout the duration of the
// program. Commonly used with C2 frameworks
func InvokeAssembly(methodInfo *MethodInfo, params []string) (stdout string, stderr string) {
var paramSafeArray *SafeArray
methodSignature, err := methodInfo.GetString()
if err != nil {
stderr = err.Error()
return
}
if expectsParams(methodSignature) {
if paramSafeArray, err = PrepareParameters(params); err != nil {
stderr = err.Error()
return
}
}
nullVariant := Variant{
VT: 1,
Val: uintptr(0),
}
defer SafeArrayDestroy(paramSafeArray)
// Ensure exclusive access to read/write STDOUT/STDERR
mutex.Lock()
defer mutex.Unlock()
err = methodInfo.Invoke_3(nullVariant, paramSafeArray)
if err != nil {
stderr = err.Error()
// Don't return because there could be data on STDOUT/STDERR
}
// Read data from previously redirected STDOUT/STDERR
if wSTDOUT != nil {
var e string
stdout, e, err = ReadStdoutStderr()
stderr += e
if err != nil {
stderr += err.Error()
}
}
return
}