gpt4 book ai didi

c - 在 C 中存储 NTFS 安全描述符

转载 作者:太空宇宙 更新时间:2023-11-04 04:58:06 25 4
gpt4 key购买 nike

我的目标是以相同的 native 状态存储 NTFS 安全描述符。目的是按需恢复它。

我设法为此目的编写了代码,我想知道是否有人介意验证它的示例? (for循环代表了我存储原生描述符的方式)

此示例仅包含“OWNER”标志,但我的意图是对所有安全描述符标志应用相同的方法。

我只是一个初学者,不胜感激。谢谢,Doori 酒吧

#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;
}

最佳答案

基本上没问题 - 我会在声明时将 owner、ownerstr、PSecurityD1 和 PSecurityD2 归零。否则(如果失败,您可能会释放未分配的内存)

你也可以使用 memcpy instread 复制循环

关于c - 在 C 中存储 NTFS 安全描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com