创建启用 FS 虚拟化的进程

发布于 2024-09-14 21:59:32 字数 1753 浏览 8 评论 0原文

禁用 UAC 后,我需要创建一个与启用 UAC 时创建的进程具有相同特征的进程 - 基本上我是在启用 UAC 的情况下模拟进程创建。

我唯一的障碍是虚拟化。下面的示例代码应在启用虚拟化的情况下以中等 IL 创建一个 notepad 实例。实际上,它会在禁用虚拟化的情况下创建中等 IL 的记事本实例。我不完全确定为什么虚拟化令牌被忽略。有什么想法吗?

BOOL bRet;
HANDLE hToken;
HANDLE hNewToken;

// Notepad is used as an example
WCHAR wszProcessName[MAX_PATH] =
L"C:\\Windows\\System32\\Notepad.exe";

// Medium integrity SID
WCHAR wszIntegritySid[20] = L"S-1-16-8192";
PSID pIntegritySid = NULL;

DWORD EnableVirtualization = 1;
TOKEN_MANDATORY_LABEL TIL = {0};
PROCESS_INFORMATION ProcInfo = {0};
STARTUPINFO StartupInfo = {0};
ULONG ExitCode = 0;

if (OpenProcessToken(GetCurrentProcess(),MAXIMUM_ALLOWED, &hToken))
{
   if (DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL,
      SecurityImpersonation, TokenPrimary, &hNewToken))
   {
      if (ConvertStringSidToSid(wszIntegritySid, &pIntegritySid))
      {
         TIL.Label.Attributes = SE_GROUP_INTEGRITY;
         TIL.Label.Sid = pIntegritySid;

         // Set the process integrity level
         if (SetTokenInformation(hNewToken, TokenIntegrityLevel, &TIL,
            sizeof(TOKEN_MANDATORY_LABEL) + GetLengthSid(pIntegritySid)))
         {
            // Enable FS Virtualization
            if (SetTokenInformation(hNewToken, TokenVirtualizationEnabled,
               &EnableVirtualization, sizeof(EnableVirtualization)))
            {
               // Create the new process at Low integrity
               bRet = CreateProcessAsUser(hNewToken, NULL,
                  wszProcessName, NULL, NULL, FALSE,
                  0, NULL, NULL, &StartupInfo, &ProcInfo);
            }
         }
         LocalFree(pIntegritySid);
      }
      CloseHandle(hNewToken);
   }
   CloseHandle(hToken);
}

With UAC disabled, I need to create a process with the same characteristics as the process created with UAC enabled - basically I'm emulating process creation with UAC enabled.

My only roadblock is virtualization. The sample code below should create an instance of notedpad at medium IL with virtualization enabled. In actuality, it creates an instance of notepad at medium IL with virtualization disabled. I'm not entirely sure why the virtualization token is being ignored. Any ideas?

BOOL bRet;
HANDLE hToken;
HANDLE hNewToken;

// Notepad is used as an example
WCHAR wszProcessName[MAX_PATH] =
L"C:\\Windows\\System32\\Notepad.exe";

// Medium integrity SID
WCHAR wszIntegritySid[20] = L"S-1-16-8192";
PSID pIntegritySid = NULL;

DWORD EnableVirtualization = 1;
TOKEN_MANDATORY_LABEL TIL = {0};
PROCESS_INFORMATION ProcInfo = {0};
STARTUPINFO StartupInfo = {0};
ULONG ExitCode = 0;

if (OpenProcessToken(GetCurrentProcess(),MAXIMUM_ALLOWED, &hToken))
{
   if (DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL,
      SecurityImpersonation, TokenPrimary, &hNewToken))
   {
      if (ConvertStringSidToSid(wszIntegritySid, &pIntegritySid))
      {
         TIL.Label.Attributes = SE_GROUP_INTEGRITY;
         TIL.Label.Sid = pIntegritySid;

         // Set the process integrity level
         if (SetTokenInformation(hNewToken, TokenIntegrityLevel, &TIL,
            sizeof(TOKEN_MANDATORY_LABEL) + GetLengthSid(pIntegritySid)))
         {
            // Enable FS Virtualization
            if (SetTokenInformation(hNewToken, TokenVirtualizationEnabled,
               &EnableVirtualization, sizeof(EnableVirtualization)))
            {
               // Create the new process at Low integrity
               bRet = CreateProcessAsUser(hNewToken, NULL,
                  wszProcessName, NULL, NULL, FALSE,
                  0, NULL, NULL, &StartupInfo, &ProcInfo);
            }
         }
         LocalFree(pIntegritySid);
      }
      CloseHandle(hNewToken);
   }
   CloseHandle(hToken);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-09-21 21:59:32

所以,我的做法是错误的 - fs 虚拟化不是我想要的。要模拟 UAC,如上所述,需要创建一个禁用管理员组的受限令牌,并使用该令牌来创建进程。

So, I was approaching this incorrectly - fs virtualization is not what I want. To emulate UAC, as described above, its necessary to create a restricted token with the administrators group disabled and use that token to create the process.

可可 2024-09-21 21:59:32

这不起作用的原因是,用于打开虚拟化的 SetTokenInformation 调用正在处理为 CreateProcessAsUser 创建的主令牌。需要的是实际流程的访问令牌。这可以通过使用 CreationFlag CREATE_SUSPENDED 创建进程并使用 ProcInfo 中的进程句柄调用 OpenProcessToken 来获得。在该令牌上使用 SetTokenInformation 来启用虚拟化,然后使用 ResumeThread 来运行该进程。

The reason that this doesn't work, is that the SetTokenInformation call to turn on virtualisation is working on the primary token created for CreateProcessAsUser. What's needed is an access token for the actual process. This can be obtained by creating the process with the CreationFlag CREATE_SUSPENDED, and calling OpenProcessToken with the process handle from ProcInfo. Use SetTokenInformation on that token to enable virtualisation, and then ResumeThread to run the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文