【发布时间】:2019-09-27 21:51:32
【问题描述】:
我编写了代码来更改对象(不是进程)(在本例中为文件)的完整性级别。正如我们所知,我们从中等完整性级别开始,但我想将其降低到“低”。我想运行一个完整性低的 .txt 文件,而不是默认介质。
我主要为此目的使用 WINAPI。所以我创建了一个 .txt 文件以将其完整性从中等降低到低。
void SetLowLabelToFile()
{
int k = 0;
// The LABEL_SECURITY_INFORMATION SDDL SACL to be set for low integrity
#define LOW_INTEGRITY_SDDL_SACL_W L"S:(ML;;NW;;;LW)"
DWORD dwErr = ERROR_SUCCESS;
PSECURITY_DESCRIPTOR pSD = NULL;
PACL pSacl = NULL; // not allocated
BOOL fSaclPresent = FALSE;
BOOL fSaclDefaulted = FALSE;
LPCWSTR pwszFileName = L"C:\\Users\\Dan\\Documents\\testIntegrity\\hi1.txt";
if (ConvertStringSecurityDescriptorToSecurityDescriptorW(
LOW_INTEGRITY_SDDL_SACL_W, SDDL_REVISION_1, &pSD, NULL))
{
k = GetLastError();
if (GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl,
&fSaclDefaulted))
{
k = GetLastError();
// Note that psidOwner, psidGroup, and pDacl are
// all NULL and set the new LABEL_SECURITY_INFORMATION
dwErr = SetNamedSecurityInfoW((LPWSTR)pwszFileName,
SE_FILE_OBJECT, LABEL_SECURITY_INFORMATION,
NULL, NULL, NULL, pSacl);
k = GetLastError();
}
LocalFree(pSD);
}
}
我设置了 3 个 GetLastErrors 来获取错误代码,最后一个是 1008。这意味着 An attempt was made to reference a token that does not exist。我没有得到它,因为文件的方式是有效的。谁能帮忙解决这个问题?
【问题讨论】:
-
您错误地使用 GetLastError() - 在错误的地方。如果 ConvertStringSecurityDescriptorToSecurityDescriptorW 和 GetSecurityDescriptorSacl 返回 true - 调用 GetLastError() - 没有意义。 SetNamedSecurityInfoW 根本没有设置 GetLastError() 而是直接返回错误代码。除了这个 - 你的代码是正确的