-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhookedAPI.go
More file actions
328 lines (283 loc) · 12.7 KB
/
hookedAPI.go
File metadata and controls
328 lines (283 loc) · 12.7 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
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
package main
import (
"crypto/md5"
"fmt"
"strings"
"syscall"
"time"
"unicode/utf16"
"unsafe"
"golang.org/x/sys/windows"
)
/*
#include <windows.h>
#include <winbase.h>
#include <wingdi.h>
#include <winuser.h>
typedef HANDLE (WINAPI *GETCLIPBOARDDATA)(UINT);
typedef HANDLE (WINAPI *SETCLIPBOARDDATA)(UINT, HANDLE);
typedef HRESULT (WINAPI *COPYFILE2)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS);
typedef BOOL (WINAPI *MOVEFILEEXW)(LPCWSTR, LPCWSTR, DWORD);
typedef BOOL (WINAPI *BITBLT)(HDC, int, int, int, int, HDC, int, int, DWORD);
typedef BOOL (WINAPI *PRINTWINDOW)(HWND, HDC, UINT);
*/
import "C"
// Define function pointers for WINAPI functions
var (
user32 = syscall.MustLoadDLL("user32.dll")
kernel32 = syscall.MustLoadDLL("kernel32.dll")
kernelbase = syscall.MustLoadDLL("kernelbase.dll")
shell32 = syscall.MustLoadDLL("shell32.dll")
gdi32 = syscall.MustLoadDLL("gdi32.dll")
procGetClipboardData = user32.MustFindProc("GetClipboardData")
procSetClipboardData = user32.MustFindProc("SetClipboardData")
procGetClipboardOwner = user32.MustFindProc("GetClipboardOwner")
procGetForegroundWindow = user32.MustFindProc("GetForegroundWindow")
procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
procPrintWindow = user32.MustFindProc("PrintWindow")
procGlobalLock = kernel32.MustFindProc("GlobalLock")
procGlobalUnlock = kernel32.MustFindProc("GlobalUnlock")
procGlobalSize = kernel32.MustFindProc("GlobalSize")
procMoveFileExW = kernel32.MustFindProc("MoveFileExW")
procCopyFile2 = kernelbase.MustFindProc("CopyFile2")
procDragQueryFile = shell32.MustFindProc("DragQueryFileW")
procBitBlt = gdi32.MustFindProc("BitBlt")
fpGetClipboardData C.GETCLIPBOARDDATA
fpSetClipboardData C.SETCLIPBOARDDATA
fpCopyFile2 C.COPYFILE2
fpMoveFileExW C.MOVEFILEEXW
fpBitBlt C.BITBLT
fpPrintWindow C.PRINTWINDOW
)
//export GetClipboardDataOverride
func GetClipboardDataOverride(uFormat UINT) uintptr {
ret, _, err := syscall.SyscallN(uintptr(unsafe.Pointer(fpGetClipboardData)), uintptr(uFormat), 0, 0)
// get a handle to the clipboard owner
handle := HANDLE(unsafe.Pointer(ret))
if unsafe.Pointer(handle) == nil {
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("GetClipboardData failed: %v", err))
return uintptr(0)
}
ptr := globalLock(syscall.Handle(uintptr(unsafe.Pointer(handle))))
if ptr == nil {
logMessage(LOGLEVEL_ERROR, "GlobalLock failed for GetClipboardData")
return ret
}
defer globalUnlock(syscall.Handle(uintptr(unsafe.Pointer(handle))))
size := globalSize(syscall.Handle(uintptr(unsafe.Pointer(handle))))
if size == 0 {
logMessage(LOGLEVEL_ERROR, "GlobalSize failed for GetClipboardData")
return ret
}
// read the clipboard data based on the format
switch uintptr(uFormat) {
case uintptr(CF_TEXT):
data := C.GoBytes(ptr, C.int(size))
encoding := "ANSI"
clipboardText := strings.ReplaceAll(string(data), "\x00", "")
if REDACTED_TEXT_CLIPBOARD {
clipboardText = fmt.Sprintf("%x (%d characters)", md5.Sum([]byte(clipboardText)), len(clipboardText))
}
if time.Since(lastGetClipboardDataTime) >= CLIPBOARD_MONITOR_THRESHOLD {
logMessage(LOGLEVEL_INFO, fmt.Sprintf("GetClipboardData - Data (%s): %s - Owner Window: %s", encoding, clipboardText, lastForegroundWindowTitle))
addNewPacketToQueue("Telemetry", "Clipboard", "GetClipboardData", fmt.Sprintf("Text data (%s): %s", encoding, clipboardText), fmt.Sprintf("Owner Window: %s", lastForegroundWindowTitle))
lastGetClipboardDataTime = time.Now()
}
case uintptr(CF_UNICODETEXT):
encoding := "Unicode"
if size%2 != 0 {
logMessage(LOGLEVEL_ERROR, fmt.Sprintf("Invalid UTF-16 data size: %d", size))
return ret
} else {
utf16Data := unsafe.Slice((*uint16)(ptr), size/2)
decodedString := utf16.Decode(utf16Data)
clipboardText := strings.ReplaceAll(string(decodedString), "\x00", "")
if REDACTED_TEXT_CLIPBOARD {
clipboardText = fmt.Sprintf("%x (%d characters)", md5.Sum([]byte(clipboardText)), len(clipboardText))
}
if time.Since(lastGetClipboardDataTime) >= CLIPBOARD_MONITOR_THRESHOLD {
logMessage(LOGLEVEL_INFO, fmt.Sprintf("GetClipboardData: Data (%s): %s - Owner Window: %s", encoding, clipboardText, lastForegroundWindowTitle))
addNewPacketToQueue("Telemetry", "Clipboard", "GetClipboardData", fmt.Sprintf("Text data (%s): %s", encoding, clipboardText), fmt.Sprintf("Owner Window: %s", lastForegroundWindowTitle))
lastGetClipboardDataTime = time.Now()
}
}
case uintptr(CF_DATAOBJECT):
// handle file clipboard formats. This function will be used for GetClipboardData and SetClipboardData as SetClipboardData will call GetClipboardData first
clipboardFiles, err := GetClipboardFilePaths(true)
if err != nil {
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("GetClipboardFilePaths failed: %v", err))
return ret
}
if len(clipboardFiles) == 0 {
return ret
}
// Calculate the checksum of the file paths in the clipboard
concatenated := strings.Join(clipboardFiles, "|")
h := md5.Sum([]byte(concatenated))
clipboardFilePathsChecksum := fmt.Sprintf("%x", h)
realClipboardMethod := ""
clipboardUsed := false
// if the checksum is new, it means the clipboard has changed, so we assume SetClipboardData was called
if currentSetFilesClipboardHash != clipboardFilePathsChecksum {
currentSetFilesClipboardHash = clipboardFilePathsChecksum
realClipboardMethod = "SetClipboardData"
clipboardUsed = true
lastGetClipboardDataTime = time.Now()
} else {
// GetClipboardData could be called several times with the same data (for many reasons), so we check if the time since the last call is greater than the threshold
realClipboardMethod = "GetClipboardData"
if currentGetFilesClipboardHash == clipboardFilePathsChecksum {
return ret
}
if time.Since(lastGetClipboardDataTime) >= CLIPBOARD_MONITOR_THRESHOLD && time.Since(lastClipboardOwnerTime) >= CLIPBOARD_MONITOR_THRESHOLD {
lastGetClipboardDataTime = time.Now()
currentGetFilesClipboardHash = clipboardFilePathsChecksum
clipboardUsed = true
}
}
// if the clipboard has changed both for SetClipboardData and GetClipboardData, we log the action
if clipboardUsed {
for _, file := range clipboardFiles {
logMessage(LOGLEVEL_INFO, fmt.Sprintf("%s Format: CF_DATAOBJECT (%d) - File %v - Owner Window: %s", realClipboardMethod, uFormat, file, lastForegroundWindowTitle))
addNewPacketToQueue("Telemetry", "Clipboard", realClipboardMethod, fmt.Sprintf("File: %s", file), fmt.Sprintf("Owner Window: %s", lastForegroundWindowTitle))
}
}
case uintptr(CF_BITMAP), uintptr(CF_DIB), uintptr(CF_DIBV5), uintptr(CF_BITMAPV5HEADER), uintptr(CF_DSPBITMAP):
logMessage(LOGLEVEL_INFO, fmt.Sprintf("Format: CF_BITMAP(%d) - Owner Window: %s", uFormat, lastForegroundWindowTitle))
addNewPacketToQueue("Telemetry", "Clipboard", "GetClipboardData", "Bitmap image data paste", fmt.Sprintf("Owner Window: %s", lastForegroundWindowTitle))
case uintptr(CF_OLEPRIVATEDATA):
// CF_OLEPRIVATEDATA intentionally left blank - generally handled in CF_DATAOBJECT
case uintptr(CF_HDROP):
// override to HDROP intentionally left blank - generally handled in CF_DATAOBJECT
default:
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("GetClipboardData Unhandled format: %d - Owner Window: %s", uFormat, lastForegroundWindowTitle))
}
return ret
}
//export SetClipboardDataOverride
func SetClipboardDataOverride(uFormat UINT, hMem HANDLE) uintptr {
// handle SetClipboardData only for CF_TEXT, CF_UNICODETEXT. Files are handled in GetClipboardData
ret, _, _ := syscall.SyscallN(
uintptr(unsafe.Pointer(fpSetClipboardData)),
uintptr(uFormat),
uintptr(unsafe.Pointer(hMem)),
)
if unsafe.Pointer(hMem) != nil {
switch uintptr(uFormat) {
case uintptr(CF_TEXT), uintptr(CF_UNICODETEXT):
ptr := globalLock(syscall.Handle(uintptr(unsafe.Pointer(hMem))))
if ptr != nil {
defer globalUnlock(syscall.Handle(uintptr(unsafe.Pointer(hMem))))
size := globalSize(syscall.Handle(uintptr(unsafe.Pointer(hMem))))
data := C.GoBytes(ptr, C.int(size))
encoding := ""
clipboardText := ""
if uFormat == UINT(CF_UNICODETEXT) {
encoding = "Unicode"
utf16Data := unsafe.Slice((*uint16)(ptr), size/2)
decodedString := utf16.Decode(utf16Data)
clipboardText = string(decodedString)
} else {
encoding = "ANSI"
clipboardText = string(data)
}
clipboardText = strings.ReplaceAll(string(clipboardText), "\x00", "")
if REDACTED_TEXT_CLIPBOARD {
clipboardText = fmt.Sprintf("%x (%d characters)", md5.Sum([]byte(clipboardText)), len(clipboardText))
}
if time.Since(lastGetClipboardDataTime) >= CLIPBOARD_MONITOR_THRESHOLD {
logMessage(LOGLEVEL_INFO, fmt.Sprintf("SetClipboardData - Data (%s): %s - Owner Window: %s", encoding, clipboardText, lastForegroundWindowTitle))
addNewPacketToQueue("Telemetry", "Clipboard", "SetClipboardData", fmt.Sprintf("Data (%s): %s", encoding, clipboardText), fmt.Sprintf("Owner Window: %s", lastForegroundWindowTitle))
}
} else {
logMessage(LOGLEVEL_ERROR, "GlobalLock failed for SetClipboardData with CF_TEXT / CF_UNICODE")
}
// update the last clipboard data time (also used for GetClipboardData to avoid duplicate logs)
lastSetClipboardDataTime = time.Now()
lastGetClipboardDataTime = time.Now()
default:
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("SetClipboardData Unhandled format: %d - Owner Window: %s", uFormat, lastForegroundWindowTitle))
}
}
return ret
}
//export MoveFileExWOverride
func MoveFileExWOverride(lpExistingFileName LPCWSTR, lpNewFileName LPCWSTR, dwFlags DWORD) uintptr {
ret, _, err := syscall.SyscallN(
uintptr(unsafe.Pointer(fpMoveFileExW)),
uintptr(unsafe.Pointer(lpExistingFileName)),
uintptr(unsafe.Pointer(lpNewFileName)),
uintptr(dwFlags),
)
oldPath := windows.UTF16PtrToString((*uint16)(unsafe.Pointer(lpExistingFileName)))
newPath := windows.UTF16PtrToString((*uint16)(unsafe.Pointer(lpNewFileName)))
if err != 0 {
logMessage(LOGLEVEL_ERROR, fmt.Sprintf("MoveFileExWOverride error: %v, Old Path: %s, New Path: %s", err, oldPath, newPath))
} else {
logMessage(LOGLEVEL_INFO, fmt.Sprintf("File Moved: From '%s' to '%s'", oldPath, newPath))
addNewPacketToQueue("Telemetry", "FileTransfer", "MoveFileExW", oldPath, newPath)
}
return ret
}
//export CopyFile2Override
func CopyFile2Override(lpExistingFileName PCWSTR, lpNewFileName PCWSTR, pExtendedParameters COPYFILE2_EXTENDED_PARAMETERS) uintptr {
ret, _, err := syscall.SyscallN(
uintptr(unsafe.Pointer(fpCopyFile2)),
uintptr(unsafe.Pointer(lpExistingFileName)),
uintptr(unsafe.Pointer(lpNewFileName)),
uintptr(unsafe.Pointer(pExtendedParameters)),
)
sourcePath := windows.UTF16PtrToString((*uint16)(unsafe.Pointer(lpExistingFileName)))
destinationPath := windows.UTF16PtrToString((*uint16)(unsafe.Pointer(lpNewFileName)))
if err != 0 {
logMessage(LOGLEVEL_ERROR, fmt.Sprintf("CopyFile2Override error: %v, Source: %s, Destination: %s", err, sourcePath, destinationPath))
} else {
logMessage(LOGLEVEL_INFO, fmt.Sprintf("File Copied: From '%s' to '%s'", sourcePath, destinationPath))
addNewPacketToQueue("Telemetry", "FileTransfer", "CopyFile2", sourcePath, destinationPath)
}
return ret
}
//export PrintWindowOverride
func PrintWindowOverride(hwnd HWND, hdcBlt HDC, nFlags UINT) uintptr {
ret, _, err := syscall.SyscallN(
uintptr(unsafe.Pointer(procPrintWindow)),
uintptr(unsafe.Pointer(hwnd)),
uintptr(unsafe.Pointer(hdcBlt)),
uintptr(nFlags),
)
if err != 0 {
logMessage(LOGLEVEL_ERROR, fmt.Sprintf("PrintWindow error: %v", err))
} else {
if time.Since(lastPrintScreenTime) > PRINTSCREEN_MONITOR_THRESHOLD {
lastPrintScreenTime = time.Now()
logMessage(LOGLEVEL_INFO, fmt.Sprintf("print screen (PrintWindow) succeeded"))
addNewPacketToQueue("Telemetry", "PrintScreen", "PrintWindow", fmt.Sprintf("%s - PrintScreen", lastForegroundWindowTitle), "")
}
}
return ret
}
//export BitBltOverride
func BitBltOverride(hdcDest HDC, nXDest int32, nYDest int32, nWidth int32, nHeight int32, hdcSrc HDC, nXSrc int32, nYSrc int32, dwRop DWORD) uintptr {
ret, _, err := syscall.SyscallN(
uintptr(unsafe.Pointer(fpBitBlt)),
uintptr(unsafe.Pointer(hdcDest)),
uintptr(nXDest),
uintptr(nYDest),
uintptr(nWidth),
uintptr(nHeight),
uintptr(unsafe.Pointer(hdcSrc)),
uintptr(nXSrc),
uintptr(nYSrc),
uintptr(dwRop),
)
if err != 0 {
logMessage(LOGLEVEL_ERROR, fmt.Sprintf("BitBlt error: %v", err))
} else {
if time.Since(lastPrintScreenTime) > PRINTSCREEN_MONITOR_THRESHOLD {
lastPrintScreenTime = time.Now()
logMessage(LOGLEVEL_INFO, fmt.Sprintf("print screen (BitBlt) succeeded"))
addNewPacketToQueue("Telemetry", "PrintScreen", "BitBlt", fmt.Sprintf("%s - PrintScreen", lastForegroundWindowTitle), "")
}
}
return ret
}