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.

200 lines
3.5 KiB

  1. #include <windows.h>
  2. #include <malloc.h>
  3. #include <stdio.h>
  4. #include <process.h>
  5. BOOL
  6. EnableCreatePermanentPrivilege(
  7. HANDLE TokenHandle,
  8. PTOKEN_PRIVILEGES OldPrivileges
  9. );
  10. BOOL
  11. OpenToken(
  12. PHANDLE TokenHandle
  13. );
  14. VOID
  15. __cdecl main (int argc, char *argv[])
  16. {
  17. int i;
  18. PACL Dacl;
  19. LPSTR FileName;
  20. TOKEN_PRIVILEGES OldPrivileges;
  21. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  22. PSID AdminAliasSid;
  23. BOOL Result;
  24. ULONG DaclSize;
  25. HANDLE TokenHandle;
  26. SECURITY_DESCRIPTOR SecurityDescriptor;
  27. Result = OpenToken( &TokenHandle );
  28. if ( !Result ) {
  29. printf("Unable to open token\n");
  30. exit(-1);
  31. }
  32. Result = EnableCreatePermanentPrivilege(
  33. TokenHandle,
  34. &OldPrivileges
  35. );
  36. if ( !Result ) {
  37. //
  38. // This account doesn't have SeCreatePermanent
  39. // privilege. Tell them to try running it again
  40. // from an account that does.
  41. //
  42. printf("Unable to enable SeCreatePermanent privilege\n");
  43. //
  44. // do what you want here...
  45. //
  46. exit(4);
  47. }
  48. //
  49. // Display privileges.
  50. //
  51. //
  52. // Put things back the way they were
  53. //
  54. (VOID) AdjustTokenPrivileges (
  55. TokenHandle,
  56. FALSE,
  57. &OldPrivileges,
  58. sizeof( TOKEN_PRIVILEGES ),
  59. NULL,
  60. NULL
  61. );
  62. if ( GetLastError() != NO_ERROR ) {
  63. //
  64. // This is unlikely to happen,
  65. //
  66. printf("AdjustTokenPrivileges failed turning off SeCreatePermanent privilege\n");
  67. }
  68. }
  69. BOOL
  70. EnableCreatePermanentPrivilege(
  71. HANDLE TokenHandle,
  72. PTOKEN_PRIVILEGES OldPrivileges
  73. )
  74. {
  75. TOKEN_PRIVILEGES NewPrivileges;
  76. BOOL Result;
  77. LUID CreatePermanentValue;
  78. ULONG ReturnLength;
  79. //
  80. // Mike: change SeCreatePermanentPrivilege to SeCreatePermanentPrivilege
  81. // and you'll be pretty much there.
  82. //
  83. Result = LookupPrivilegeValue(
  84. NULL,
  85. "SeCreatePermanetPrivilegePrivilege",
  86. &CreatePermanentValue
  87. );
  88. if ( !Result ) {
  89. printf("Unable to obtain value of CreatePermanent privilege\n");
  90. return FALSE;
  91. }
  92. //
  93. // Set up the privilege set we will need
  94. //
  95. NewPrivileges.PrivilegeCount = 1;
  96. NewPrivileges.Privileges[0].Luid = CreatePermanentValue;
  97. NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  98. (VOID) AdjustTokenPrivileges (
  99. TokenHandle,
  100. FALSE,
  101. &NewPrivileges,
  102. sizeof( TOKEN_PRIVILEGES ),
  103. OldPrivileges,
  104. &ReturnLength
  105. );
  106. if ( GetLastError() != NO_ERROR ) {
  107. return( FALSE );
  108. } else {
  109. return( TRUE );
  110. }
  111. }
  112. BOOL
  113. OpenToken(
  114. PHANDLE TokenHandle
  115. )
  116. {
  117. HANDLE Process;
  118. BOOL Result;
  119. Process = OpenProcess(
  120. PROCESS_QUERY_INFORMATION,
  121. FALSE,
  122. GetCurrentProcessId()
  123. );
  124. if ( Process == NULL ) {
  125. //
  126. // This can happen, but is unlikely.
  127. //
  128. return( FALSE );
  129. }
  130. Result = OpenProcessToken (
  131. Process,
  132. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  133. TokenHandle
  134. );
  135. CloseHandle( Process );
  136. if ( !Result ) {
  137. //
  138. // This can happen, but is unlikely.
  139. //
  140. return( FALSE );
  141. }
  142. return( TRUE );
  143. }