-
Notifications
You must be signed in to change notification settings - Fork 77
/
TestCode.cpp
210 lines (170 loc) · 8.16 KB
/
TestCode.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
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
/*****************************************************************************/
/* Page09Security.cpp Copyright (c) Ladislav Zezula 2005 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 15.08.05 1.00 Lad The first version of Page09Security.cpp */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
#ifdef __TEST_MODE__
/*
static PSID GetUserSid(LPCTSTR szUserName, LPDWORD pcbSid)
{
SID_NAME_USE SidNameUse;
PSID pSid = NULL;
DWORD cbSid = 0;
DWORD cbDomain = 128;
TCHAR szDomain[128];
LookupAccountName(NULL, szUserName, pSid, &cbSid, szDomain, &cbDomain, &SidNameUse);
if(cbSid != 0)
{
if((pSid = LocalAlloc(LPTR, cbSid)) != NULL)
{
if(LookupAccountName(NULL, szUserName, pSid, &cbSid, szDomain, &cbDomain, &SidNameUse))
{
pcbSid[0] = cbSid;
return pSid;
}
LocalFree(pSid);
}
}
return NULL;
}
*/
PACL CreateEmptyDacl()
{
PACL pAcl;
if((pAcl = (PACL)LocalAlloc(LPTR, sizeof(ACL))) != NULL)
{
if(RtlCreateAcl(pAcl, sizeof(ACL), ACL_REVISION_DS) != STATUS_SUCCESS)
{
LocalFree(pAcl);
pAcl = NULL;
}
}
return pAcl;
}
PACL CreateCustomAcl()
{
PACL pAcl;
NTSTATUS Status = STATUS_INSUFFICIENT_RESOURCES;
// Allocate space for ACL
if((pAcl = (PACL)LocalAlloc(LPTR, MAX_ACL_LENGTH)) != NULL)
{
if((Status = RtlCreateAcl(pAcl, MAX_ACL_LENGTH, ACL_REVISION_DS)) == STATUS_SUCCESS)
{
PACE_HEADER pAceHeader;
WORD cbAclSize = sizeof(ACL);
if((pAceHeader = ACE_HELPER(ACCESS_ALLOWED_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
/*
if((pAceHeader = ACE_HELPER(ACCESS_DENIED_ACE_TYPE, FILE_EXECUTE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_AUDIT_ACE_TYPE, FILE_EXECUTE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_ALARM_ACE_TYPE, FILE_EXECUTE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_ALLOWED_COMPOUND_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_ALLOWED_OBJECT_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_DENIED_OBJECT_ACE_TYPE, FILE_EXECUTE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_AUDIT_OBJECT_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_ALARM_OBJECT_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_ALLOWED_CALLBACK_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_DENIED_CALLBACK_ACE_TYPE, FILE_EXECUTE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE, FILE_EXECUTE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_AUDIT_CALLBACK_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_ALARM_CALLBACK_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_MANDATORY_LABEL_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACE_CSA_HELPER(L"RESOURCE_ITEM_I64_VALUES", CLAIM_SECURITY_ATTRIBUTE_TYPE_INT64)).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACE_CSA_HELPER(L"RESOURCE_ITEM_U64_VALUES", CLAIM_SECURITY_ATTRIBUTE_TYPE_UINT64)).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
*/
if((pAceHeader = ACE_HELPER(ACE_CSA_HELPER(L"RESOURCE_ITEM_STRINGS", CLAIM_SECURITY_ATTRIBUTE_TYPE_STRING, 5)).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
/*
if((pAceHeader = ACE_HELPER(ACE_CSA_HELPER(L"RESOURCE_ITEM_SIDS", CLAIM_SECURITY_ATTRIBUTE_TYPE_SID)).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACE_CSA_HELPER(L"RESOURCE_ITEM_BOOLEANS", CLAIM_SECURITY_ATTRIBUTE_TYPE_BOOLEAN, 4)).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(ACE_CSA_HELPER(L"RESOURCE_ITEM_OCTET_STRINGS", CLAIM_SECURITY_ATTRIBUTE_TYPE_OCTET_STRING)).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_SCOPED_POLICY_ID_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_PROCESS_TRUST_LABEL_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
if((pAceHeader = ACE_HELPER(SYSTEM_ACCESS_FILTER_ACE_TYPE).AddToAcl(pAcl)) != NULL)
cbAclSize = cbAclSize + pAceHeader->AceSize;
*/
pAcl->AclSize = cbAclSize;
}
}
return pAcl;
}
static DWORD SetCustomSecurityDescriptor(HANDLE hObject)
{
SECURITY_DESCRIPTOR sd;
NTSTATUS Status = STATUS_SUCCESS;
PACL pAcl = NULL;
UNREFERENCED_PARAMETER(hObject);
// Initialize the blank security descriptor
Status = RtlCreateSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
if(NT_SUCCESS(Status))
{
// Create custom ACL
if((pAcl = CreateCustomAcl()) != NULL)
{
// Set the ACL to the security descriptor
Status = RtlSetDaclSecurityDescriptor(&sd, TRUE, pAcl, FALSE);
if(NT_SUCCESS(Status))
{
// Set the security descriptor to the object
Status = NtSetSecurityObject(hObject, DACL_SECURITY_INFORMATION, &sd);
}
LocalFree(pAcl);
}
else
{
Status = STATUS_NO_MEMORY;
}
}
return Status;
}
void SetCustomSecurityDescriptor(LPCTSTR szPath)
{
HANDLE hFolder;
// Make sure that the folder exists
ForcePathExist(szPath, TRUE);
// Open the folder and set security descriptor
hFolder = CreateFile(szPath, READ_CONTROL | WRITE_DAC | WRITE_OWNER, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if(hFolder != INVALID_HANDLE_VALUE)
{
SetCustomSecurityDescriptor(hFolder);
CloseHandle(hFolder);
}
}
void DebugCode_SecurityDescriptor(LPCTSTR /* szPath */)
{
//SetCustomSecurityDescriptor(_T("c:\\VMWARE\\TestValidAcl"));
}
#endif