我的目标是以相同的本机状态存储 NTFS 安全描述符。目的是按需恢复它。
我设法为此目的编写代码,我想知道是否有人介意验证它的样本?(for循环代表我存储本机描述符的方式)
此示例仅包含“OWNER”标志,但我的意图是对所有安全描述符标志应用相同的方法。
我只是一个初学者,将不胜感激。谢谢, 多里酒吧
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#include <stdio.h>
#include <windows.h>
#include "accctrl.h"
#include "aclapi.h"
#include "sddl.h"
int main (void)
{
DWORD lasterror;
PSECURITY_DESCRIPTOR PSecurityD1, PSecurityD2;
HANDLE hFile;
PSID owner;
LPTSTR ownerstr;
BOOL ownerdefault;
int ret = 0;
unsigned int i;
hFile = CreateFile("c:\\boot.ini", GENERIC_READ | ACCESS_SYSTEM_SECURITY,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
fprintf(stderr,"CreateFile() failed. Error: INVALID_HANDLE_VALUE\n");
return 1;
}
lasterror = GetSecurityInfo(hFile, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION
, &owner, NULL, NULL, NULL, &PSecurityD1);
if (lasterror != ERROR_SUCCESS) {
fprintf(stderr,"GetSecurityInfo() failed. Error: %lu;\n", lasterror);
ret = 1;
goto ret1;
}
ConvertSidToStringSid(owner,&ownerstr);
printf("ownerstr of PSecurityD1: %s\n", ownerstr);
/* The for loop represents the way I store the native descriptor */
PSecurityD2 = malloc( GetSecurityDescriptorLength(PSecurityD1) *
sizeof(unsigned char) );
for (i=0; i < GetSecurityDescriptorLength(PSecurityD1); i++)
((unsigned char *) PSecurityD2)[i] = ((unsigned char *) PSecurityD1)[i];
if (IsValidSecurityDescriptor(PSecurityD2) == 0) {
fprintf(stderr,"IsValidSecurityDescriptor(PSecurityD2) failed.\n");
ret = 2;
goto ret2;
}
if (GetSecurityDescriptorOwner(PSecurityD2,&owner,&ownerdefault) == 0) {
fprintf(stderr,"GetSecurityDescriptorOwner() failed.");
ret = 2;
goto ret2;
}
ConvertSidToStringSid(owner,&ownerstr);
printf("ownerstr of PSecurityD2: %s\n", ownerstr);
ret2:
free(owner);
free(ownerstr);
free(PSecurityD1);
free(PSecurityD2);
ret1:
CloseHandle(hFile);
return ret;
}