-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatched-Code-(Post-Patch).cpp
More file actions
34 lines (29 loc) · 1.32 KB
/
Copy pathPatched-Code-(Post-Patch).cpp
File metadata and controls
34 lines (29 loc) · 1.32 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
Patched Code (Post-Patch)
The fix introduces the FILE_FLAG_OPEN_REPARSE_POINT flag to prevent following malicious directory junctions, and utilizes ImpersonateLoggedOnUser to ensure the operation is bound to the permissions of the user requesting the scan, rather than NT AUTHORITY\SYSTEM.
// SECURE: Prevents symlink abuse and impersonates the requesting user.
// 1. Impersonate the user who triggered the Defender action
if (ImpersonateLoggedOnUser(hUserToken)) {
// 2. Open the file strictly WITHOUT following reparse points/junctions
HANDLE hFile = CreateFile(
userProvidedPath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, // Fails if it's a symlink
NULL
);
if (hFile != INVALID_HANDLE_VALUE) {
// 3. Verify the file isn't a malicious link before writing
DWORD fileAttributes = GetFileAttributes(userProvidedPath);
if (!(fileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
WriteDataToFile(hFile, data);
}
CloseHandle(hFile);
}
// 4. Revert back to SYSTEM privileges after the risky operation is done
RevertToSelf();
} else {
// Handle impersonation failure securely
LogSecurityEvent("Failed to impersonate user during file operation.");
}