|
找到方法了吗?
先在管理员下提升权限,代码如下,我调试过了,肯定行的!
BYTE aclBuffer[1024];
PACL pacl=(PACL)&aclBuffer; //声明一个ACL,长度是1024
BYTE sidBuffer[100];
PSID psid=(PSID) &sidBuffer; //声明一个SID,长度是100
DWORD sidBufferSize = 100;
// char domainBuffer[80];
DWORD domainBufferSize = 80;
// SID_NAME_USE snu;
HANDLE file;
UINT i = 0;
SECURITY_DESCRIPTOR sd; //声明一个SD BYTE aclBuffer[1024];
SECURITY_ATTRIBUTES sa; //和文件有关的安全结构
//初始化一个ACL
//初始化一个SD
HANDLE hAccessToken;
PSID psidAdministrators;
//当获得TOKEN句柄之后,我们还得获取这个TOKEN的分组信息:
BYTE InfoBuffer[1024];
DWORD dwInfoBufferSize;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
//初始化一个SD
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
//初始化一个ACL
InitializeAcl(pacl, 1024, ACL_REVISION);
//查找一个用户hchen,并取该用户的SID
OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken);
BOOL flag = GetTokenInformation(
hAccessToken,
TokenGroups,
InfoBuffer,
1024,
&dwInfoBufferSize);
AllocateAndInitializeSid(
&siaNtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0,0,0,0,0,0,
&psidAdministrators);
PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS)InfoBuffer;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
InitializeAcl(pacl, 1024, ACL_REVISION);
for( i=0; iGroupCount; i++)
{
// if(EqualSid(psidAdministrators, ptgGroups->Groups.Sid))
{
// 此进程有管理权限
AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL, ptgGroups->Groups.Sid);
// break;
}
}
//设置该用户的Access-Allowed的ACE,其权限为“所有权限”
AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL, psid);
//把ACL设置到SD中
SetSecurityDescriptorDacl(&sd, TRUE, pacl, FALSE);
//把SD放到文件安全结构SA中
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
sa.lpSecurityDescriptor = &sd;
HANDLE hDevUSB = NULL;
hDevUSB = CreateFile(
lpszFileName,//&guidHID_1,//
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,//FILE_ATTRIBUTE_NORMAL
NULL);
有问题可以和我联系,QQ 99592177 fangyx@wellcom.cn 共同学习! |
|