[go: up one dir, main page]

Menu

[r1]: / ModDacl / accessfunc.c  Maximize  Restore  History

Download this file

154 lines (140 with data), 3.9 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
 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
#include "master.h"
#include <aclapi.h>
#include <stdlib.h>
#include "utility.h"
#include "accessfunc.h"
PACL GetAcl(REQUEST* rq)
{
PACL lpAcl = NULL;
if(rq->ko == NULL)
{
if(GetNamedSecurityInfo(rq->szPath, rq->iType, DACL_SECURITY_INFORMATION, NULL, NULL, &lpAcl, NULL, NULL) == ERROR_SUCCESS)
{
_tprintf(TEXT("Retrieved DACL\n"));
}
else ConErrorInfo(__FUNCTION__);
}
else
{
DWORD dwBytesRequired = 0;
GetKernelObjectSecurity(rq->ko->hHandle, DACL_SECURITY_INFORMATION, NULL, 0, &dwBytesRequired);
rq->ko->lpsd = malloc(dwBytesRequired);
memset(rq->ko->lpsd, 0, dwBytesRequired);
if(GetKernelObjectSecurity(rq->ko->hHandle, DACL_SECURITY_INFORMATION, rq->ko->lpsd, dwBytesRequired, &dwBytesRequired) != 0)
{
BOOL bDacl = FALSE, bDefaulted = FALSE;
GetSecurityDescriptorDacl(rq->ko->lpsd, &bDacl, &lpAcl, &bDefaulted);
_tprintf(TEXT("\nRetrieved DACL\n"));
}
else
{
free(rq->ko->lpsd);
ConErrorInfo(__FUNCTION__);
}
}
return lpAcl;
}
/* We return a flag here, so we can reset the DACL to it's original state
* in case our failed attempt has corrupted or otherwise invalidated the DACL */
BOOL SetNewAcl(REQUEST* rq, PACL lpNewAcl)
{
if(rq->ko == NULL) /* Get/SetNamedSecurityInfo doesn't work for Kernel Objects*/
{
if(SetNamedSecurityInfo(rq->szPath, rq->iType, DACL_SECURITY_INFORMATION, NULL, NULL, lpNewAcl, NULL) == ERROR_SUCCESS)
{
_tprintf(TEXT("New DACL set successfully\n"));
return FALSE;
}
else
{
ConErrorInfo(__FUNCTION__);
return TRUE;
}
}
else
{
if(SetKernelObjectSecurity(rq->ko->hHandle, DACL_SECURITY_INFORMATION, rq->ko->lpsd) != 0)
{
_tprintf(TEXT("New DACL set successfully\n"));
return FALSE;
}
else
{
ConErrorInfo(__FUNCTION__);
return TRUE;
}
}
}
void AddAceToAcl(REQUEST* rq)
{
PACL pExistingAcl = NULL, pNewAcl = NULL;
PSID pSid = NULL;
SID_NAME_USE snu = 0;
pExistingAcl = GetAcl(rq);
if(rq->bDenied != -1) /* If the "clear" option wasn't specified on the commandline, make a new ACL */
{
snu = GetSID(rq->szAccountName, &pSid);
pNewAcl = MakeNewAcl(rq, pSid, pExistingAcl, snu);
}
if(pNewAcl != NULL || rq->bDenied == -1)
{
if(rq->ko != NULL)
{
/* SetSecurityDescriptorDacl requires an absolute SD but GetKernelObjectSecurity
* returns a self-relative one so we must convert between the two */
PSECURITY_DESCRIPTOR pSecDesc = GetAbsoluteSDFromRelative(rq->ko->lpsd);
if(SetSecurityDescriptorDacl(pSecDesc, TRUE, pNewAcl, FALSE) == FALSE)
{
ConErrorInfo(__FUNCTION__);
}
else
{
if(SetNewAcl(rq, pNewAcl) == TRUE)
{
_tprintf(TEXT("\nFailed to set new DACL, restoring the previous one\n"));
SetNewAcl(rq, pExistingAcl);
}
}
free(pSecDesc);
free(rq->ko->lpsd);
free(rq->ko);
}
else
{
if(SetNewAcl(rq, pNewAcl) == TRUE)
{
SetNewAcl(rq, pExistingAcl); /* Reset the previous ACL if we fail */
}
}
}
free(pSid);
if(pNewAcl != NULL)
{
LocalFree(pNewAcl);
}
return;
}
PACL MakeNewAcl(REQUEST* rq, PSID pSid, PACL lpOldAcl, SID_NAME_USE snu)
{
ULONG ulEntries = 1;
EXPLICIT_ACCESS ea = {0};
PACL lpNewAcl = NULL;
ea.grfAccessPermissions = rq->amPermissions;
if(rq->bDenied == TRUE)
{
ea.grfAccessMode = DENY_ACCESS;
}
else ea.grfAccessMode = GRANT_ACCESS;
ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
ea.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
ea.Trustee.pMultipleTrustee = NULL;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = snu;
ea.Trustee.ptstrName = (LPTSTR)pSid;
if(SetEntriesInAcl(ulEntries, &ea, lpOldAcl, &lpNewAcl) == ERROR_SUCCESS)
{
_tprintf(TEXT("Created New DACL\n"));
}
else ConErrorInfo(__FUNCTION__);
return lpNewAcl;
}