Bearbeiten von DACL Wenn Sie keine Berechtigungen in eine Datei habenC++

Programme in C++. Entwicklerforum
Anonymous
 Bearbeiten von DACL Wenn Sie keine Berechtigungen in eine Datei haben

Post by Anonymous »

Ich habe eine modifyFiledacl -Funktion, die das DACL einer Datei ändert. Wenn das ACE existiert, modifiziert die Funktion das vorhandene ACE, andernfalls fügt sie ein neues ACE hinzu. SID. Datei_generic_write . Dies ist jedoch eine vollkommen rechtliche Anfrage, da Sie das Verweigern, die -Lehre für eine Datei in Windows -Eigenschaften schreiben und dann die DACL mithilfe der System -Benutzeroberfläche weiter ändern können. Mit anderen Worten, ich möchte verhindern, dass ein bestimmtes SID die Datei bearbeitet, aber gleichzeitig die Fähigkeit beibehalten, die DACL zu ändern, wenn diese Erlaubnis zu Beginn vorhanden war.

Code: Select all

#include 
#include 
#include 
#include 
#include 
#include 
#include 

DWORD GetSecurityDescriptor(HANDLE handle, std::shared_ptr& pSD)
{
DWORD dwSDLen;
std::shared_ptr buffer;

if (GetKernelObjectSecurity(handle, DACL_SECURITY_INFORMATION, NULL, 0, &dwSDLen) == 0)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
buffer.reset(new(std::nothrow) BYTE[dwSDLen]);

if (!buffer)
{
return ERROR_NOT_ENOUGH_MEMORY;
}

if (GetKernelObjectSecurity(handle, DACL_SECURITY_INFORMATION, buffer.get(), dwSDLen, &dwSDLen) == 0)
{
return GetLastError();
}
}
else
{
return GetLastError();
}
}

pSD = buffer;
return ERROR_SUCCESS;
}

DWORD ToAbsoluteSD(std::shared_ptr selfRelativeSD, std::shared_ptr& absoluteSD)
{
DWORD absSDSize = 0;
DWORD dwDaclSize = 0;
DWORD dwSaclSize = 0;
DWORD dwOwnerSize = 0;
DWORD dwPrimaryGroupSize = 0;

DWORD result = MakeAbsoluteSD(selfRelativeSD.get(), NULL, &absSDSize, NULL, &dwDaclSize, NULL, &dwSaclSize, NULL, &dwOwnerSize, NULL, &dwPrimaryGroupSize);
if (result == 0)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
std::shared_ptr absSD(new(std::nothrow) BYTE[absSDSize]);
std::unique_ptr dacl(new(std::nothrow) BYTE[dwDaclSize]);
std::unique_ptr sacl(new(std::nothrow) BYTE[dwSaclSize]);
std::unique_ptr owner(new(std::nothrow) BYTE[dwOwnerSize]);
std::unique_ptr primaryGroup(new(std::nothrow) BYTE[dwPrimaryGroupSize]);

if (!absSD || !dacl || !sacl || !owner || !primaryGroup)
{
return ERROR_NOT_ENOUGH_MEMORY;
}

result = MakeAbsoluteSD(selfRelativeSD.get(), absSD.get(), &absSDSize,
(PACL)dacl.get(), &dwDaclSize,
(PACL)sacl.get(), &dwSaclSize,
(PSID)owner.get(), &dwOwnerSize,
(PSID)primaryGroup.get(), &dwPrimaryGroupSize);
if (result == 0)
{
return GetLastError();
}

absoluteSD = absSD;
}
}

return ERROR_SUCCESS;
}

DWORD ModifyFileDACL(LPTSTR fileName, PSID pSID, bool allowed, ACCESS_MASK accessRights, bool set)
{
if (!fileName)
{
return ERROR_INVALID_PARAMETER;
}

if (!pSID)
{
return ERROR_INVALID_PARAMETER;
}

auto flag = std::filesystem::is_directory(fileName) ? FILE_FLAG_BACKUP_SEMANTICS : FILE_ATTRIBUTE_NORMAL;
wil::unique_handle hFile(CreateFile(fileName, READ_CONTROL | WRITE_DAC, 0, NULL, OPEN_EXISTING, flag, NULL));
if (!hFile)
{
return GetLastError();
}

std::shared_ptr pSD;
DWORD result = GetSecurityDescriptor(hFile.get(), pSD);
if (result != ERROR_SUCCESS)
{
return result;
}

PACL dacl = nullptr;
BOOL present;
BOOL defaulted;

if (GetSecurityDescriptorDacl(pSD.get(), &present, &dacl, &defaulted) == 0)
{
return GetLastError();
}

if (!present)
{
return ERROR_NOT_SUPPORTED;
}

bool aceType = allowed ? ACCESS_ALLOWED_ACE_TYPE : ACCESS_DENIED_ACE_TYPE;

// if DACL present and not NULL
if (dacl)
{
PACE_HEADER header;
for (WORD i = 0; i < dacl->AceCount; i++)
{
if (GetAce(dacl, i, (PVOID*)&header))
{
if (header->AceType == aceType)
{
auto ace = (ACCESS_ALLOWED_ACE*)header;  // have the same binary layout as ACCESS_DENIED_ACE
if (EqualSid((PSID)&ace->SidStart, pSID))
{
set ? ace->Mask |= accessRights : ace->Mask &= ~accessRights;
if (SetKernelObjectSecurity(hFile.get(), DACL_SECURITY_INFORMATION, pSD.get()) == 0)
{
return GetLastError();
}

return ERROR_SUCCESS;
}
}
}
}
}

EXPLICIT_ACCESS ea;
PACL pNewDACL = NULL;

// Initialize an EXPLICIT_ACCESS structure for the new ACE.
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = accessRights;
ea.grfAccessMode = allowed ? SET_ACCESS : DENY_ACCESS;
ea.grfInheritance = NO_INHERITANCE;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.ptstrName = (PWSTR)pSID;

result = SetEntriesInAcl(1, &ea, dacl, &pNewDACL);
if (result != ERROR_SUCCESS) {
return result;
}

std::shared_ptr absoluteSD;
result = ToAbsoluteSD(pSD, absoluteSD);
if (result != ERROR_SUCCESS)
{
return result;
}

if (SetSecurityDescriptorDacl(absoluteSD.get(), TRUE, pNewDACL, FALSE) == 0)
{
return GetLastError();
}

if (SetKernelObjectSecurity(hFile.get(), DACL_SECURITY_INFORMATION, absoluteSD.get()) == 0)
{
return GetLastError();
}

return result;
}

int main()
{
wchar_t trusteeSID[] = L"S-1-5-21-#-#-#-#";
PSID pSid;
if (!ConvertStringSidToSid(trusteeSID, &pSid)) {
std::cout

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post