-
Notifications
You must be signed in to change notification settings - Fork 8
/
conditional_ace_test.c
360 lines (304 loc) · 10.2 KB
/
conditional_ace_test.c
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
#include <Windows.h>
#include <stdio.h>
#include <AclAPI.h>
#include <sddl.h>
#define CLOSE(x) CloseHandle(x);x=NULL;
#define LFREE(x) if(x!=NULL){LocalFree(x);x=NULL;}
#define MFREE(x) if(x!=NULL){free(x);x=NULL;}
#define ADDRIGHTSSDDL_FULL_ACCESS "(XA;;FA;;;BU; (!(1 == 1)))"
#define ADDRIGHTSSDDL_KEY_ALL_ACCESS "(XA;;KA;;;BU; (!(1 == 1)))"
BOOL AddCustomACEToSD(PSECURITY_DESCRIPTOR pOldSecurityDescriptor, PSECURITY_DESCRIPTOR* pNewSecurityDescriptor, char* NewACESDDL) {
LPSTR sddl = NULL;
char NewSDDL[0x1000] = { 0 };
if (ConvertSecurityDescriptorToStringSecurityDescriptorA(
pOldSecurityDescriptor,
SDDL_REVISION_1,
DACL_SECURITY_INFORMATION,
&sddl,
NULL) == FALSE)
return FALSE;
strcpy_s(NewSDDL, 0x1000, sddl);
// already set ?
if (strstr(NewSDDL, "1 == 1") == NULL) {
if (strcat_s(NewSDDL, 0x1000, NewACESDDL) != 0)
return FALSE;
}
LFREE(sddl);
if (ConvertStringSecurityDescriptorToSecurityDescriptorA(NewSDDL,
SDDL_REVISION_1,
pNewSecurityDescriptor,
NULL) == FALSE){
printf("GetLastError: %x %s", GetLastError(), NewSDDL);
return FALSE;
}
return TRUE;
}
VOID ParseDACL(PACL Acl) {
ULONG i = 0, j = 0;
PVOID xPtr = NULL;
PACE_HEADER pACE = NULL;
PACCESS_ALLOWED_ACE pAACE = NULL;
PACCESS_ALLOWED_OBJECT_ACE pAOACE = NULL;
PISID pSid = NULL;
printf("\tDACL raw data:\n"
"\t\tDacl->AceCount: %d\n"
"\t\tDacl->AclSize: %d\n"
"\t\tDacl->AclRevision: %d\n",
Acl->AceCount,
Acl->AclSize,
Acl->AclRevision);
pACE = (PACE_HEADER)((SIZE_T)Acl + sizeof(ACL));
for(i = 0; i< Acl->AceCount;i++) {
printf("\t\tDacl->ACE #%d\n", i);
printf("\t\t\tHeader.AceSize: %d\n"
"\t\t\tHeader.AceType: %x\n",
pACE->AceSize,
pACE->AceType);
if (pACE->AceType == ACCESS_ALLOWED_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_ALLOWED_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_DENIED_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_DENIED_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_ALLOWED_COMPOUND_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_ALLOWED_COMPOUND_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_ALLOWED_OBJECT_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_ALLOWED_OBJECT_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_DENIED_OBJECT_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_DENIED_OBJECT_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_ALLOWED_CALLBACK_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_ALLOWED_CALLBACK_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_DENIED_CALLBACK_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_DENIED_CALLBACK_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE\n");
else if (pACE->AceType == ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE)
printf("\t\t\tHeader.AceType: ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE\n");
printf("\t\t\tHeader.AceFlags: %d ", pACE->AceFlags);
if ((pACE->AceFlags & OBJECT_INHERIT_ACE) != 0)
printf("OBJECT_INHERIT_ACE ");
if ((pACE->AceFlags & CONTAINER_INHERIT_ACE) != 0)
printf("CONTAINER_INHERIT_ACE ");
if ((pACE->AceFlags & NO_PROPAGATE_INHERIT_ACE) != 0)
printf("NO_PROPAGATE_INHERIT_ACE ");
if ((pACE->AceFlags & INHERIT_ONLY_ACE) != 0)
printf("INHERIT_ONLY_ACE ");
if ((pACE->AceFlags & INHERITED_ACE) != 0)
printf("INHERITED_ACE ");
if ((pACE->AceFlags & VALID_INHERIT_FLAGS) != 0)
printf("VALID_INHERIT_FLAGS ");
if ((pACE->AceFlags & SUCCESSFUL_ACCESS_ACE_FLAG) != 0)
printf("SUCCESSFUL_ACCESS_ACE_FLAG ");
if ((pACE->AceFlags & FAILED_ACCESS_ACE_FLAG) != 0)
printf("FAILED_ACCESS_ACE_FLAG ");
printf("\n");
if (pACE->AceType == ACCESS_ALLOWED_OBJECT_ACE_TYPE ||
pACE->AceType == ACCESS_DENIED_OBJECT_ACE_TYPE ||
pACE->AceType == ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE ||
pACE->AceType == ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE
) {
pAOACE = (PACCESS_ALLOWED_OBJECT_ACE)pACE;
printf("\t\t\tInheritedObjectType: %.8x-%.4x-%.4x-%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n"
"\t\t\tMask: 0x%x\n"
"\t\t\tObjectType: %.8x-%.4x-%.4x-%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
pAOACE->InheritedObjectType.Data1,
pAOACE->InheritedObjectType.Data2,
pAOACE->InheritedObjectType.Data3,
pAOACE->InheritedObjectType.Data4[0],
pAOACE->InheritedObjectType.Data4[1],
pAOACE->InheritedObjectType.Data4[2],
pAOACE->InheritedObjectType.Data4[3],
pAOACE->InheritedObjectType.Data4[4],
pAOACE->InheritedObjectType.Data4[5],
pAOACE->InheritedObjectType.Data4[6],
pAOACE->InheritedObjectType.Data4[7],
pAOACE->Mask,
pAOACE->ObjectType.Data1,
pAOACE->ObjectType.Data2,
pAOACE->ObjectType.Data3,
pAOACE->ObjectType.Data4[0],
pAOACE->ObjectType.Data4[1],
pAOACE->ObjectType.Data4[2],
pAOACE->ObjectType.Data4[3],
pAOACE->ObjectType.Data4[4],
pAOACE->ObjectType.Data4[5],
pAOACE->ObjectType.Data4[6],
pAOACE->ObjectType.Data4[7]);
pSid = (PISID)&pAOACE->SidStart;
}
else {
pAACE = (PACCESS_ALLOWED_ACE)pACE;
printf("\t\t\tMask: 0x%x\n", pAACE->Mask);
pSid = (PISID)&pAACE->SidStart;
}
printf(
"\t\t\tSID.SubAuthorityCount: %d\n"
"\t\t\tSID : S-%d-0x%x%x%x%x%x%x",
pSid->SubAuthorityCount,
pSid->Revision,
pSid->IdentifierAuthority.Value[0],
pSid->IdentifierAuthority.Value[1],
pSid->IdentifierAuthority.Value[2],
pSid->IdentifierAuthority.Value[3],
pSid->IdentifierAuthority.Value[4],
pSid->IdentifierAuthority.Value[5]);
xPtr = &pSid->SubAuthority;
for (j = 0; j < pSid->SubAuthorityCount; j++) {
printf("-%d", *(PULONG)xPtr);
xPtr = (PVOID)((SIZE_T)xPtr + sizeof(ULONG));
}
printf("\n");
if ((SIZE_T)xPtr < (SIZE_T)pACE + pACE->AceSize){
printf("\t\t\tRemaining data:\n\t\t\t\t");
while ((SIZE_T)xPtr < (SIZE_T)pACE + pACE->AceSize) {
printf("%.2x", *(PUCHAR)xPtr);
xPtr = (PVOID)((SIZE_T)xPtr + 1);
}
printf("\n");
}
pACE = (PACE_HEADER)((SIZE_T)pACE + pACE->AceSize);
}
}
VOID PrintSD(PSECURITY_DESCRIPTOR pSecurityDescriptor) {
LPSTR sddl = NULL;
ConvertSecurityDescriptorToStringSecurityDescriptorA(
pSecurityDescriptor,
SDDL_REVISION_1,
DACL_SECURITY_INFORMATION,
&sddl,
NULL);
printf("\tSDDL: %s\n"
"\tSD->Revision: %d\n"
"\tSD->Control: %x\n"
"\tSD->Sbz1: %d\n"
"\tSD->Dacl: %p\n"
"",
sddl,
((PISECURITY_DESCRIPTOR_RELATIVE)pSecurityDescriptor)->Revision,
((PISECURITY_DESCRIPTOR_RELATIVE)pSecurityDescriptor)->Control,
((PISECURITY_DESCRIPTOR_RELATIVE)pSecurityDescriptor)->Sbz1,
(SIZE_T)((PISECURITY_DESCRIPTOR_RELATIVE)pSecurityDescriptor)->Dacl + (SIZE_T)pSecurityDescriptor);
ParseDACL(((PACL)(((PISECURITY_DESCRIPTOR_RELATIVE)pSecurityDescriptor)->Dacl + (SIZE_T)pSecurityDescriptor)));
}
void setToFile(char* FileName){
HANDLE hFile = NULL;
ULONG dwSize = 0;
LPSTR str = NULL;
PACL pDacl = NULL;
PSECURITY_DESCRIPTOR pOldSecurityDescriptor = NULL, pNewSecurityDescriptor = NULL;
hFile = CreateFileA(FileName, // name of the write
GENERIC_ALL, // open for writing
0, // do not share
NULL, // default security
OPEN_EXISTING, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("!!! CreateFileA failed !!!\n");
goto fail;
}
if (GetSecurityInfo(hFile,
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
NULL,
NULL,
&pDacl,
NULL,
&pOldSecurityDescriptor) != ERROR_SUCCESS) {
printf("!!! GetSecurityInfo failed (1) !!!\n");
goto fail;
}
printf("=== ACTUAL %s SD ===\n", FileName);
PrintSD(pOldSecurityDescriptor);
if (AddCustomACEToSD(pOldSecurityDescriptor, &pNewSecurityDescriptor, ADDRIGHTSSDDL_FULL_ACCESS) == FALSE) {
printf("!!! AddCustomACEToSD failed !!!\n");
goto fail;
}
LFREE(pOldSecurityDescriptor);
printf("=== GENERATED NEW SD ===\n");
PrintSD(pNewSecurityDescriptor);
dwSize = SetSecurityInfo(hFile,
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
NULL,
NULL,
(PACL)((SIZE_T)((PISECURITY_DESCRIPTOR_RELATIVE)pNewSecurityDescriptor)->Dacl + (SIZE_T)pNewSecurityDescriptor),
NULL);
printf("=== SetSecurityInfo returned %d ===\n", dwSize);
CLOSE(hFile);
hFile = CreateFileA(FileName,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (GetSecurityInfo(hFile,
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
NULL,
NULL,
&pDacl,
NULL,
&pOldSecurityDescriptor) != ERROR_SUCCESS) {
printf("!!! GetSecurityInfo failed (2) !!!\n");
goto fail;
}
printf("=== NEW FILE SD ===\n");
PrintSD(pOldSecurityDescriptor);
fail:
CLOSE(hFile);
LFREE(pOldSecurityDescriptor);
LFREE(pNewSecurityDescriptor);
return;
}
void setToReg(HKEY baseKey, char* KeyPath){
HKEY oK;
PSECURITY_DESCRIPTOR pNewSecurityDescriptor = NULL;
PSECURITY_DESCRIPTOR pSecurityDescriptor = (PSECURITY_DESCRIPTOR)malloc(0x1000);
LPSTR str = NULL;
DWORD sz = 0x1000;
if (RegOpenKeyExA(baseKey, KeyPath, 0, KEY_ALL_ACCESS, &oK) != ERROR_SUCCESS){
printf("!!! RegOpenKeyExA failed !!!\n");
goto fail;
}
if (RegGetKeySecurity(oK, DACL_SECURITY_INFORMATION, pSecurityDescriptor, &sz) != ERROR_SUCCESS){
printf("!!! RegGetKeySecurity failed (1) !!!\n");
goto fail;
}
printf("=== ACTUAL %s SD ===\n", KeyPath);
PrintSD(pSecurityDescriptor);
if (AddCustomACEToSD(pSecurityDescriptor, &pNewSecurityDescriptor, ADDRIGHTSSDDL_KEY_ALL_ACCESS) == FALSE) {
printf("!!! AddCustomACEToSD failed !!!\n");
goto fail;
}
printf("=== GENERATED NEW SD ===\n");
PrintSD(pNewSecurityDescriptor);
if (RegSetKeySecurity(oK, DACL_SECURITY_INFORMATION, pNewSecurityDescriptor) != ERROR_SUCCESS){
printf("!!! RegSetKeySecurity failed !!!\n");
goto fail;
}
sz = 1000;
if (RegGetKeySecurity(oK, DACL_SECURITY_INFORMATION, pSecurityDescriptor, &sz) != ERROR_SUCCESS){
printf("!!! RegGetKeySecurity failed (2) !!!\n");
goto fail;
}
printf("=== NEW SD ===\n");
PrintSD(pSecurityDescriptor);
fail:
MFREE(pSecurityDescriptor);
LFREE(pNewSecurityDescriptor);
}
int main(int argc, char** argv){
char* target = NULL;
if (argc > 2) {
if (!_stricmp(argv[1], "-reg"))
setToReg(HKEY_LOCAL_MACHINE, argv[2]);
if (!_stricmp(argv[1], "-file"))
setToFile(argv[2]);
}
else{
printf("Usage: %s -reg|-file <path>\n"
"e.g: -file C:\\test.txt\n"
"e.g: -reg SOFTWARE\\Microsoft (HKLM hive)\n", argv[0]);
}
return 0;
}