gpt4 book ai didi

c++ - SECURITY_ATTRIBUTES 结构和 CreateNamedPipe()

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:15:45 27 4
gpt4 key购买 nike

我的场景如下:使用 CreateNamedPipe() 创建命名管道对象的过程具有管理员权限,但客户端进程“连接”到它 CreateFile()才不是。通过 NULL作为 CreateNamedPipe() 的最后一个参数似乎默认为仅管理员访问权限。

作为 hack,我尝试做一个服务器端 ImpersonateLoggedOnUser()/RevertToSelf()管道相关代码期间的方法,但它失败了。在我看来,最好的办法就是实际设置一个合适的 SECURITY_ATTRIBUTES结构到 CreateNamedPipe() 的最后一个参数,但我不知道该怎么做。

MSDN example有一个与注册表项操作有关的示例,但我缺乏根据我的目的调整该示例的专业知识。

这是我试过的:

if (!AllocateAndInitializeSid(&SIDAuthWorld, 1,
SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0,
&pEveryoneSID))
{
_tprintf(_T("AllocateAndInitializeSid Error %u\n"), GetLastError());
ret_val = 0;
goto Cleanup;
}

// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow Everyone read access to the key.
ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
ea[0].grfAccessPermissions = STANDARD_RIGHTS_ALL;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance = NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName = (LPTSTR)pEveryoneSID;

// there's another ACE for administrators in between, but is of no relevance here

dwRes = SetEntriesInAcl(2, ea, NULL, &pACL);

// Initialize a security descriptor.
pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);
if (NULL == pSD)
{
_tprintf(_T("LocalAlloc Error %u\n"), GetLastError());
ret_val = 0;
goto Cleanup;
}

if (!InitializeSecurityDescriptor(pSD,
SECURITY_DESCRIPTOR_REVISION))
{
_tprintf(_T("InitializeSecurityDescriptor Error %u\n"),
GetLastError());
ret_val = 0;
goto Cleanup;
}

// Add the ACL to the security descriptor.
if (!SetSecurityDescriptorDacl(pSD,
TRUE, // bDaclPresent flag
pACL,
FALSE)) // not a default DACL
{
_tprintf(_T("SetSecurityDescriptorDacl Error %u\n"),
GetLastError());
ret_val = 0;
goto Cleanup;
}

// Initialize a security attributes structure.
*sa = new SECURITY_ATTRIBUTES;

(*sa)->nLength = sizeof(SECURITY_ATTRIBUTES);
(*sa)->lpSecurityDescriptor = pSD;
(*sa)->bInheritHandle = FALSE;

结果是客户端得到错误0x5 (访问被拒绝)在 CreateFile() .这里有什么问题?

最佳答案

您可以将描述符的 DACL 设置为 NULL,以允许任何人访问管道:

pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (!pSD)
{
...
}

if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
{
...
}

if (!SetSecurityDescriptorDacl(pSD, TRUE, NULL, FALSE))
{
...
}

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;

... = CreateNamedPipe(..., &sa);

关于c++ - SECURITY_ATTRIBUTES 结构和 CreateNamedPipe(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412919/

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