用 C 存储 NTFS 安全描述符
我的目标是以相同的本机状态存储 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;
}
My goal is to store a NTFS Security Descriptor in its identical native state. The purpose is to restore it on-demand.
I managed to write the code for that purpose, I was wondering if anybody mind to validate a sample of it? (The for loop represents the way I store the native descriptor)
This sample only contains the flag for "OWNER", but my intention is to apply the same method for all of the security descriptor flags.
I'm just a beginner, would appreciate the heads up.
Thanks, Doori Bar
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上没问题 - 声明时我会将owner、ownerstr、PSecurityD1 和PSecurityD2 归零。否则(失败时,您可能会释放未分配的内存)
您也可以使用 memcpy 代替复制循环
Basically ok - I'd zero out owner, ownerstr, PSecurityD1 and PSecurityD2 when declared. Otherwise (on failure you might be freeing unallocated memory)
Also you can use a memcpy instread of the copy loop