-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamsi.go
325 lines (259 loc) · 9.67 KB
/
amsi.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
// go:build windows
// The amsi package provides a Go wrapper of the Microsoft Antimalware Scan Interface.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/
package amsi
import (
"errors"
"fmt"
"runtime"
"syscall"
"unsafe"
)
// Context holds references to an initialized AMSI context
type Context struct {
amsiDLL *syscall.LazyDLL
handle uintptr
procNotifyOperation *syscall.LazyProc
procScanBuffer *syscall.LazyProc
procScanString *syscall.LazyProc
}
// Session holds references to a session within an AMSI context and provides the main scanning functions
type Session struct {
context *Context
handle uintptr
}
// Result values are provider specific return values.
//
// The antimalware provider may return a result between 1 and 32767, inclusive, as an estimated risk level.
// Go-AMSI adds an additional value (-1) to indicate that a result could not be obtained through AMSI.
//
// Any result equal to or larger than 32768 is considered malware, and the content should be blocked.
// Use the IsMalware() function to determine if a result considers the data to be malware.
//
// See: https://docs.microsoft.com/de-de/windows/win32/api/amsi/ne-amsi-amsi_result
type Result int
const (
// Could not determine the risk level through AMSI
ResultUnknown Result = -1
// Known good. No detection found, and the result is likely not going to change after a future definition update.
ResultClean Result = 0
// No detection found, but the result might change after a future definition update.
ResultNotDetected Result = 1
// Administrator policy blocked this content on this machine (beginning of range).
ResultBlockedByAdminStart Result = 16384
// Administrator policy blocked this content on this machine (end of range).
ResultBlockedByAdminEnd Result = 20479
// Detection found. The content is considered malware and should be blocked.
ResultDetected Result = 32768
)
// No data has been provided as input. Go-AMSI considers empty data to be clean
var ErrEmptyInputData = errors.New("empty input data")
// The AMSI context has not been initialized or already unininitialized
var ErrContextNotInitialized = errors.New("context not initialized")
// The AMSI session has already been closed
var ErrSessionClosed = errors.New("session closed")
// AMSI is not available on the host system
var ErrAmsiUnavailable = errors.New("AMSI is not available")
// Initialize initializes the AMSI API. It will return ErrAmsiUnavailable if AMSI is unavailable on the System.
// The appName can be chosen arbitrarily to identify the calling application to the Antimalware Scanning Interface.
//
// See: https://docs.microsoft.com/de-de/windows/win32/api/amsi/nf-amsi-amsiinitialize
func Initialize(appName string) (c *Context, err error) {
runtime.LockOSThread()
c = &Context{}
appNamePtr, err := syscall.UTF16PtrFromString(appName)
if err != nil {
return nil, fmt.Errorf("invalid application name: %w", err)
}
c.amsiDLL = syscall.NewLazyDLL("Amsi.dll")
err = c.amsiDLL.Load()
if err != nil {
return nil, ErrAmsiUnavailable
}
procAmsiInitialize := c.amsiDLL.NewProc("AmsiInitialize")
_, _, err = procAmsiInitialize.Call(uintptr(unsafe.Pointer(appNamePtr)), uintptr(unsafe.Pointer(&c.handle)))
if err != syscall.Errno(0) {
return nil, fmt.Errorf("failed to initialize AMSI: %w", err)
}
// Cache procs here to avoid lookups for repeated calls
c.procNotifyOperation = c.amsiDLL.NewProc("AmsiNotifyOperation")
c.procScanBuffer = c.amsiDLL.NewProc("AmsiScanBuffer")
c.procScanString = c.amsiDLL.NewProc("AmsiScanString")
return c, nil
}
// OpenSession opens a session within which multiple scan requests can be correlated.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiopensession
func (c *Context) OpenSession() (s *Session, err error) {
if c.handle == 0 {
return nil, ErrContextNotInitialized
}
procAmsiOpenSession := c.amsiDLL.NewProc("AmsiOpenSession")
s = &Session{
context: c,
}
_, _, err = procAmsiOpenSession.Call(c.handle, uintptr(unsafe.Pointer(&s.handle)))
if err != syscall.Errno(0) {
return nil, fmt.Errorf("failed to open AMSI session: %w", err)
}
return s, nil
}
// NotifyOperation sends to the antimalware provider a notification of an arbitrary operation.
// The notification doesn't imply the request of an antivirus scan.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsinotifyoperation
func (c *Context) NotifyOperation(buffer []byte, contentName string) (result Result, err error) {
if len(buffer) == 0 {
return ResultClean, ErrEmptyInputData
}
if c.handle == 0 {
return ResultUnknown, ErrContextNotInitialized
}
contentNamePtr, err := syscall.UTF16PtrFromString(contentName)
if err != nil {
return ResultUnknown, fmt.Errorf("invalid content name: %w", err)
}
var amsiResult int32
_, _, err = c.procNotifyOperation.Call(
c.handle,
uintptr(unsafe.Pointer(&buffer[0])),
uintptr(uint32(len(buffer))),
uintptr(unsafe.Pointer(contentNamePtr)),
uintptr(unsafe.Pointer(&amsiResult)),
)
if err != syscall.Errno(0) {
return ResultUnknown, fmt.Errorf("AmsiNotifyOperation failed: %w", err)
}
return Result(amsiResult), nil
}
// Uninitialize removes the instance of the AMSI API that was originally opened by Initialize.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiuninitialize
func (c *Context) Uninitialize() error {
runtime.UnlockOSThread()
if c.handle == 0 {
return ErrContextNotInitialized
}
procAmsiUninitialize := c.amsiDLL.NewProc("AmsiUninitialize")
_, _, err := procAmsiUninitialize.Call(c.handle)
c.handle = 0
// BUG(jonas-koeritz): The AmsiUninitialize call will always return "The handle is invalid"
// on my machine when a session has been created before.
// Functionality doesn't seem to be affected by this and it can be ignored.
if err != syscall.Errno(0) {
return fmt.Errorf("failed to uninitialize AMSI: %w", err)
}
return nil
}
// Close is a convenience function that wraps Uninitialize().
func (c *Context) Close() error {
if c.handle != 0 {
return c.Uninitialize()
}
return ErrContextNotInitialized
}
// ScanString scans a string for malware.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanstring
func (s *Session) ScanString(data string, contentName string) (result Result, err error) {
return s.context.ScanString(data, contentName, s)
}
// ScanString scans a string for malware.
// The session parameter is optional, pass nil to scan without associating the scan with a Session.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanstring
func (c *Context) ScanString(data string, contentName string, session *Session) (result Result, err error) {
if len(data) == 0 {
return ResultClean, ErrEmptyInputData
}
if c.handle == 0 {
return ResultUnknown, ErrContextNotInitialized
}
sessionHandle := uintptr(0)
if session != nil {
sessionHandle = session.handle
}
contentNamePtr, err := syscall.UTF16PtrFromString(contentName)
if err != nil {
return ResultUnknown, fmt.Errorf("invalid content name: %w", err)
}
var amsiResult int32
dataPtr, err := syscall.UTF16PtrFromString(data)
if err != nil {
return ResultUnknown, fmt.Errorf("invalid input data: %w", err)
}
_, _, err = c.procScanString.Call(
c.handle,
uintptr(unsafe.Pointer(dataPtr)),
uintptr(unsafe.Pointer(contentNamePtr)),
sessionHandle,
uintptr(unsafe.Pointer(&amsiResult)),
)
if err != syscall.Errno(0) {
return ResultUnknown, fmt.Errorf("AmsiScanString failed: %w", err)
}
return Result(amsiResult), nil
}
// ScanBuffer Scans a buffer-full of content for malware.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanbuffer
func (s *Session) ScanBuffer(buffer []byte, contentName string) (result Result, err error) {
return s.context.ScanBuffer(buffer, contentName, s)
}
// ScanBuffer Scans a buffer-full of content for malware.
// The session parameter is optional, pass nil to scan without associating the scan with a Session.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanbuffer
func (c *Context) ScanBuffer(buffer []byte, contentName string, session *Session) (result Result, err error) {
if len(buffer) == 0 {
return ResultClean, ErrEmptyInputData
}
if c.handle == 0 {
return ResultUnknown, ErrContextNotInitialized
}
sessionHandle := uintptr(0)
if session != nil {
sessionHandle = session.handle
}
contentNamePtr, err := syscall.UTF16PtrFromString(contentName)
if err != nil {
return ResultUnknown, fmt.Errorf("invalid content name: %w", err)
}
var amsiResult int32
_, _, err = c.procScanBuffer.Call(
c.handle,
uintptr(unsafe.Pointer(&buffer[0])),
uintptr(uint32(len(buffer))),
uintptr(unsafe.Pointer(contentNamePtr)),
sessionHandle,
uintptr(unsafe.Pointer(&amsiResult)),
)
if err != syscall.Errno(0) {
return ResultUnknown, fmt.Errorf("AmsiScanBuffer failed: %w", err)
}
return Result(amsiResult), nil
}
// Close closes a session that was opened by OpenSession.
// Close will return an error if the underlying Context has been uninitialized or the session has already been closed.
//
// See: https://docs.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiclosesession
func (s *Session) Close() error {
if s.context.handle == 0 {
return ErrContextNotInitialized
}
if s.handle == 0 {
return ErrSessionClosed
}
procCloseSession := s.context.amsiDLL.NewProc("AmsiCloseSession")
_, _, err := procCloseSession.Call(s.context.handle, s.handle)
if err != syscall.Errno(0) {
return fmt.Errorf("failed to close AMSI session: %w", err)
}
s.handle = 0
return nil
}
// IsMalware checks if the given result considers the data to be malware.
func (r Result) IsMalware() bool {
return r >= ResultDetected
}