forked from windows-internals-guide/security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_empty_dacl.cpp
86 lines (63 loc) · 1.84 KB
/
02_empty_dacl.cpp
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
#include <windows.h>
#include <aclapi.h>
#include <strsafe.h>
#define MUTEX_NAME L"my_mutex"
BOOL TestEmptyDacl();
BOOL TestNullDacl();
BOOL TestMutexOpen();
BOOL SetUntrustedLabel();
// 空のDACLにはアクセスできず、NULL DACLは誰でもアクセスできることを確認
int main()
{
BOOL bResult1, bResult2;
HANDLE hMutex;
hMutex = CreateMutex(NULL, TRUE, MUTEX_NAME);
if (hMutex == NULL)
return -1;
bResult1 = TestEmptyDacl();
bResult2 = TestNullDacl();
int nExitCode = -1;
if (!bResult1 && bResult2) {
printf("空のDACLにはアクセス失敗、NULL DACLにはアクセス成功");
nExitCode = 0;
}
else
printf("想定しないアクセス結果");
CloseHandle(hMutex);
return nExitCode;
}
BOOL TestEmptyDacl()
{
BYTE dacl[1024];
PACL pDacl = (PACL)dacl;
InitializeAcl(pDacl, 1024, ACL_REVISION);
SetNamedSecurityInfo((LPWSTR)MUTEX_NAME, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pDacl, NULL);
return TestMutexOpen();
}
BOOL TestNullDacl()
{
SetNamedSecurityInfo((LPWSTR)MUTEX_NAME, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, NULL);
SetUntrustedLabel();
ImpersonateAnonymousToken(GetCurrentThread());
return TestMutexOpen();
}
BOOL TestMutexOpen()
{
HANDLE hMutex = OpenMutex(SYNCHRONIZE, FALSE, MUTEX_NAME);
BOOL bResult = hMutex != NULL;
if (bResult)
CloseHandle(hMutex);
return bResult;
}
BOOL SetUntrustedLabel()
{
BYTE sacl[1024];
PACL pSacl = (PACL)sacl;
BYTE sid[SECURITY_MAX_SID_SIZE];
PSID pSid = (PSID)sid;
DWORD dwSidSize = SECURITY_MAX_SID_SIZE;
InitializeAcl(pSacl, 1024, ACL_REVISION);
CreateWellKnownSid(WinUntrustedLabelSid, NULL, pSid, &dwSidSize);
AddMandatoryAce(pSacl, ACL_REVISION, 0, 0, pSid);
return SetNamedSecurityInfo((LPWSTR)MUTEX_NAME, SE_KERNEL_OBJECT, LABEL_SECURITY_INFORMATION, NULL, NULL, NULL, pSacl) == ERROR_SUCCESS;
}