Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
5.4 KiB

  1. /****************************************************************************
  2. Copyright (c) Microsoft Corporation 1997
  3. All rights reserved
  4. ***************************************************************************/
  5. #include <nt.h>
  6. #include <ntrtl.h>
  7. #include <nturtl.h>
  8. #include <windows.h>
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #include <winsock2.h>
  15. #include <ntexapi.h>
  16. #include <devioctl.h>
  17. #include <stdlib.h>
  18. #include "cpuhold.h"
  19. //
  20. // Defines for moving pointers to proper alignment within a buffer
  21. //
  22. #define ALIGN_DOWN_POINTER(address, type) \
  23. ((PVOID)((ULONG_PTR)(address) & ~((ULONG_PTR)sizeof(type) - 1)))
  24. #define ALIGN_UP_POINTER(address, type) \
  25. (ALIGN_DOWN_POINTER(((ULONG_PTR)(address) + sizeof(type) - 1), type))
  26. char GlobalBuffer[4096];
  27. #define MEM_ALLOC_SIZE (1024 * 1024)
  28. PVOID AllocatedMemory[10000];
  29. //
  30. // Routines defined below
  31. //
  32. BOOL
  33. EnableDebugPriv(
  34. VOID
  35. );
  36. //
  37. //
  38. // Main routine
  39. //
  40. //
  41. int
  42. __cdecl
  43. main(
  44. int argc,
  45. char *argv[]
  46. )
  47. {
  48. NTSTATUS Status;
  49. DWORD Error;
  50. DWORD BytesReturned;
  51. DWORD ProcessId;
  52. DWORD PrintMessage = 0;
  53. PPROCESS_PRIORITY_CLASS PriorityClass;
  54. char *NewBuffer;
  55. DCB Dcb;
  56. ULONG AllocsMade;
  57. ULONG i;
  58. KPRIORITY Priority;
  59. DWORD Bytes;
  60. UCHAR OutBuffer[512];
  61. //
  62. // Give ourselve God access if possible
  63. //
  64. if (!EnableDebugPriv()) {
  65. goto Exit;
  66. }
  67. //
  68. // Now try and bump up our priority so that we can lock people out.
  69. //
  70. PriorityClass = (PPROCESS_PRIORITY_CLASS)GlobalBuffer;
  71. PriorityClass = (PPROCESS_PRIORITY_CLASS)(ALIGN_UP_POINTER(PriorityClass, PROCESS_PRIORITY_CLASS));
  72. Status = NtQueryInformationProcess(NtCurrentProcess(),
  73. ProcessPriorityClass,
  74. PriorityClass,
  75. sizeof(PROCESS_PRIORITY_CLASS),
  76. NULL
  77. );
  78. if (!NT_SUCCESS(Status)) {
  79. goto Exit;
  80. }
  81. PriorityClass->PriorityClass = PROCESS_PRIORITY_CLASS_REALTIME;
  82. Status = NtSetInformationProcess(NtCurrentProcess(),
  83. ProcessPriorityClass,
  84. PriorityClass,
  85. sizeof(PROCESS_PRIORITY_CLASS)
  86. );
  87. if (!NT_SUCCESS(Status)) {
  88. goto Exit;
  89. }
  90. //
  91. // Now try and bump up our priority so that we can lock people out
  92. //
  93. Priority = HIGH_PRIORITY - 1;
  94. Status = NtSetInformationThread(NtCurrentThread(),
  95. ThreadPriority,
  96. &Priority,
  97. sizeof(KPRIORITY)
  98. );
  99. if (!NT_SUCCESS(Status)) {
  100. goto Exit;
  101. }
  102. //
  103. // Write the message
  104. //
  105. Bytes = FormatMessageA(FORMAT_MESSAGE_FROM_HMODULE,
  106. NULL,
  107. MSG_CPUHOLD_START,
  108. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  109. OutBuffer,
  110. ARRAYSIZE(OutBuffer),
  111. NULL
  112. );
  113. ASSERT(Bytes != 0);
  114. OutBuffer[Bytes] = '\0';
  115. printf("%s\n", OutBuffer);
  116. for (; 1;) {
  117. }
  118. Exit:
  119. return 0;
  120. }
  121. BOOL
  122. EnableDebugPriv(
  123. VOID
  124. )
  125. /*++
  126. Routine Description:
  127. Changes the process's privilige so that kill works properly.
  128. Arguments:
  129. Return Value:
  130. TRUE - success
  131. FALSE - failure
  132. --*/
  133. {
  134. HANDLE hToken;
  135. LUID DebugValue;
  136. PTOKEN_PRIVILEGES ptkp;
  137. //
  138. // Retrieve a handle of the access token
  139. //
  140. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  141. return FALSE;
  142. }
  143. //
  144. // Enable the SE_DEBUG_NAME privilege.
  145. //
  146. if (!LookupPrivilegeValue((LPSTR) NULL, SE_DEBUG_NAME, &DebugValue)) {
  147. return FALSE;
  148. }
  149. ptkp = (PTOKEN_PRIVILEGES)GlobalBuffer;
  150. ptkp->PrivilegeCount = 4;
  151. ptkp->Privileges[0].Luid = DebugValue;
  152. ptkp->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  153. //
  154. // Enable the INCREASE_BASE_PRIORITY privilege.
  155. //
  156. if (!LookupPrivilegeValue((LPSTR) NULL, SE_INC_BASE_PRIORITY_NAME, &DebugValue)) {
  157. return FALSE;
  158. }
  159. ptkp->Privileges[1].Luid = DebugValue;
  160. ptkp->Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
  161. //
  162. // Enable the SHUTDOWN privilege.
  163. //
  164. if (!LookupPrivilegeValue((LPSTR) NULL, SE_SHUTDOWN_NAME, &DebugValue)) {
  165. return FALSE;
  166. }
  167. ptkp->Privileges[2].Luid = DebugValue;
  168. ptkp->Privileges[2].Attributes = SE_PRIVILEGE_ENABLED;
  169. //
  170. // Enable the QUOTA privilege.
  171. //
  172. if (!LookupPrivilegeValue((LPSTR) NULL, SE_INCREASE_QUOTA_NAME, &DebugValue)) {
  173. return FALSE;
  174. }
  175. ptkp->Privileges[3].Luid = DebugValue;
  176. ptkp->Privileges[3].Attributes = SE_PRIVILEGE_ENABLED;
  177. if (!AdjustTokenPrivileges(hToken,
  178. FALSE,
  179. ptkp,
  180. sizeof(TOKEN_PRIVILEGES) + (3 * sizeof(LUID_AND_ATTRIBUTES)),
  181. (PTOKEN_PRIVILEGES) NULL,
  182. (PDWORD) NULL)) {
  183. //
  184. // The return value of AdjustTokenPrivileges be texted
  185. //
  186. return FALSE;
  187. }
  188. return TRUE;
  189. }