创建适当的 自由访问控制列表 (DACL)是应用程序开发的必要和重要部分。 由于 NULL DACL 允许对所有用户进行所有类型的访问,因此不要使用 NULL DACL。
警告
NULL DACL 向所有人授予完全访问权限,包括不受信任的用户和远程攻击者。 切勿在任何安全对象上使用 NULL DACL。 如果需要允许广泛访问,请创建授予所需最低权限的显式 DACL。 空 DACL(包含零 ACE 的 DACL)拒绝所有访问 - 如果不确定要授予哪些权限,则这是安全的默认值。
以下示例演示如何正确创建 DACL。 该示例包含一个函数 CreateMyDACL,该函数使用 安全描述符定义语言 (SDDL)在 DACL 中定义已授予和拒绝的访问控制。 若要为应用程序的对象提供不同的访问权限,请根据需要修改 CreateMyDACL 函数。
在示例中:
主函数将 SECURITY_ATTRIBUTES 结构的地址传递给 CreateMyDACL 函数。
CreateMyDACL 函数使用 SDDL 字符串来:
- 拒绝来宾用户和匿名登录用户访问。
- 允许已验证用户拥有读取/写入/执行权限。
- 允许管理员完全控制。
有关 SDDL 字符串格式的详细信息,请参阅 安全描述符字符串格式。
CreateMyDACL 函数调用 ConvertStringSecurityDescriptorToSecurityDescriptor 函数,将 SDDL 字符串转换为 安全描述符。 安全描述符由 SECURITY_ATTRIBUTES 结构的 lpSecurityDescriptor 成员指向。 CreateMyDACL 将 Return 值从 ConvertStringSecurityDescriptorToSecurityDescriptor 发送到主函数。
主函数使用更新后的 SECURITY_ATTRIBUTES 结构,为由 CreateDirectory 函数创建的新文件夹指定 DACL。
使用 SECURITY_ATTRIBUTES 结构完成主函数后,主函数通过调用 LocalFree 函数释放为 lpSecurityDescriptor 成员分配的内存。
注释
若要成功编译 SDDL 函数(如 ConvertStringSecurityDescriptorToSecurityDescriptor),必须将_WIN32_WINNT常量定义为0x0500或更高版本。 对于现代应用程序,至少以 Windows 10 (0x0A00) 为目标。
#include <windows.h>
#include <sddl.h>
#include <stdio.h>
#pragma comment(lib, "advapi32.lib")
BOOL CreateMyDACL(SECURITY_ATTRIBUTES *);
void main()
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
// Call function to set the DACL. The DACL
// is set in the SECURITY_ATTRIBUTES
// lpSecurityDescriptor member.
if (!CreateMyDACL(&sa))
{
// Error encountered; generate message and exit.
printf("Failed CreateMyDACL\n");
exit(1);
}
// Use the updated SECURITY_ATTRIBUTES to specify
// security attributes for securable objects.
// This example uses security attributes during
// creation of a new directory.
if (0 == CreateDirectory(TEXT("C:\\MyFolder"), &sa))
{
// Error encountered; generate message and exit.
printf("Failed CreateDirectory\n");
exit(1);
}
// Free the memory allocated for the SECURITY_DESCRIPTOR.
if (NULL != LocalFree(sa.lpSecurityDescriptor))
{
// Error encountered; generate message and exit.
printf("Failed LocalFree\n");
exit(1);
}
}
// CreateMyDACL.
// Create a security descriptor that contains the DACL
// you want.
// This function uses SDDL to make Deny and Allow ACEs.
//
// Parameter:
// SECURITY_ATTRIBUTES * pSA
// Pointer to a SECURITY_ATTRIBUTES structure. It is your
// responsibility to properly initialize the
// structure and to free the structure's
// lpSecurityDescriptor member when you have
// finished using it. To free the structure's
// lpSecurityDescriptor member, call the
// LocalFree function.
//
// Return value:
// FALSE if the address to the structure is NULL.
// Otherwise, this function returns the value from the
// ConvertStringSecurityDescriptorToSecurityDescriptor
// function.
BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
{
// Define the SDDL for the DACL. This example sets
// the following access:
// Built-in guests are denied all access.
// Anonymous logon is denied all access.
// Authenticated users are allowed
// read/write/execute access.
// Administrators are allowed full control.
// Modify these values as needed to generate the proper
// DACL for your application.
TCHAR * szSD = TEXT("D:") // Discretionary ACL
TEXT("(D;OICI;GA;;;BG)") // Deny access to
// built-in guests
TEXT("(D;OICI;GA;;;AN)") // Deny access to
// anonymous logon
TEXT("(A;OICI;GRGWGX;;;AU)") // Allow
// read/write/execute
// to authenticated
// users
TEXT("(A;OICI;GA;;;BA)"); // Allow full control
// to administrators
if (NULL == pSA)
return FALSE;
return ConvertStringSecurityDescriptorToSecurityDescriptor(
szSD,
SDDL_REVISION_1,
&(pSA->lpSecurityDescriptor),
NULL);
}