Windows NT 4.0 source code leak
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.

580 lines
15 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. uipers.c
  5. Abstract:
  6. Temporary security context display command.
  7. Author:
  8. Jim Kelly (JimK) 23-May-1991
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #define _TST_USER_ // User mode test
  17. #include "tsevars.c" // Common test variables
  18. #include "tsecomm.c" // Mode dependent macros and routines.
  19. GUID SystemAuthenticationId = SYSTEM_GUID;
  20. VOID
  21. DisplaySecurityContext(
  22. IN HANDLE TokenHandle
  23. );
  24. VOID
  25. DisplayAccountSid(
  26. PISID Sid
  27. );
  28. BOOLEAN
  29. SidTranslation(
  30. PSID Sid,
  31. PSTRING AccountName
  32. );
  33. ////////////////////////////////////////////////////////////////
  34. // //
  35. // Private Macros //
  36. // //
  37. ////////////////////////////////////////////////////////////////
  38. #define PrintGuid(G) \
  39. printf( "(0x%lx-%hx-%hx-%hx-%hx-%hx-%hx-%hx-%hx-%hx-%hx)\n", \
  40. (G)->Data1, (G)->Data2, (G)->Data3, \
  41. (G)->Data4[0], (G)->Data4[1], (G)->Data4[2], \
  42. (G)->Data4[3], (G)->Data4[4], (G)->Data4[5], \
  43. (G)->Data4[6], (G)->Data4[7]); \
  44. BOOLEAN
  45. SidTranslation(
  46. PSID Sid,
  47. PSTRING AccountName
  48. )
  49. // AccountName is expected to have a large maximum length
  50. {
  51. if (RtlEqualSid(Sid, WorldSid)) {
  52. RtlInitString( AccountName, "WORLD");
  53. return(TRUE);
  54. }
  55. if (RtlEqualSid(Sid, LocalSid)) {
  56. RtlInitString( AccountName, "LOCAL");
  57. return(TRUE);
  58. }
  59. if (RtlEqualSid(Sid, NetworkSid)) {
  60. RtlInitString( AccountName, "NETWORK");
  61. return(TRUE);
  62. }
  63. if (RtlEqualSid(Sid, BatchSid)) {
  64. RtlInitString( AccountName, "BATCH");
  65. return(TRUE);
  66. }
  67. if (RtlEqualSid(Sid, InteractiveSid)) {
  68. RtlInitString( AccountName, "INTERACTIVE");
  69. return(TRUE);
  70. }
  71. if (RtlEqualSid(Sid, LocalSystemSid)) {
  72. RtlInitString( AccountName, "SYSTEM");
  73. return(TRUE);
  74. }
  75. if (RtlEqualSid(Sid, LocalManagerSid)) {
  76. RtlInitString( AccountName, "LOCAL MANAGER");
  77. return(TRUE);
  78. }
  79. if (RtlEqualSid(Sid, LocalAdminSid)) {
  80. RtlInitString( AccountName, "LOCAL ADMIN");
  81. return(TRUE);
  82. }
  83. return(FALSE);
  84. }
  85. VOID
  86. DisplayAccountSid(
  87. PISID Sid
  88. )
  89. {
  90. UCHAR Buffer[128];
  91. STRING AccountName;
  92. UCHAR i;
  93. ULONG Tmp;
  94. Buffer[0] = 0;
  95. AccountName.MaximumLength = 127;
  96. AccountName.Length = 0;
  97. AccountName.Buffer = (PVOID)&Buffer[0];
  98. if (SidTranslation( (PSID)Sid, &AccountName) ) {
  99. printf("%s\n", AccountName.Buffer );
  100. } else {
  101. printf("S-%lu-", (USHORT)Sid->Revision );
  102. if ( (Sid->IdentifierAuthority.Value[0] != 0) ||
  103. (Sid->IdentifierAuthority.Value[1] != 0) ){
  104. printf("0x%02hx%02hx%02hx%02hx%02hx%02hx",
  105. (USHORT)Sid->IdentifierAuthority.Value[0],
  106. (USHORT)Sid->IdentifierAuthority.Value[1],
  107. (USHORT)Sid->IdentifierAuthority.Value[2],
  108. (USHORT)Sid->IdentifierAuthority.Value[3],
  109. (USHORT)Sid->IdentifierAuthority.Value[4],
  110. (USHORT)Sid->IdentifierAuthority.Value[5] );
  111. } else {
  112. Tmp = (ULONG)Sid->IdentifierAuthority.Value[5] +
  113. (ULONG)(Sid->IdentifierAuthority.Value[4] << 8) +
  114. (ULONG)(Sid->IdentifierAuthority.Value[3] << 16) +
  115. (ULONG)(Sid->IdentifierAuthority.Value[2] << 24);
  116. printf("%lu", Tmp);
  117. }
  118. for (i=0;i<Sid->SubAuthorityCount ;i++ ) {
  119. printf("-%lu", Sid->SubAuthority[i]);
  120. }
  121. printf("\n");
  122. }
  123. }
  124. BOOLEAN
  125. DisplayPrivilegeName(
  126. PLUID Privilege
  127. )
  128. {
  129. //
  130. // This should be rewritten to use RtlLookupPrivilegeName.
  131. //
  132. // First we should probably spec and write RtlLookupPrivilegeName.
  133. //
  134. if ( ((*Privilege)QuadPart == CreateTokenPrivilege.QuadPart)) {
  135. printf("SeCreateTokenPrivilege ");
  136. return(TRUE);
  137. }
  138. if ( ((*Privilege).QuadPart == AssignPrimaryTokenPrivilege.QuadPart)) {
  139. printf("SeAssignPrimaryTokenPrivilege ");
  140. return(TRUE);
  141. }
  142. if ( ((*Privilege).QuadPart == LockMemoryPrivilege.QuadPart)) {
  143. printf("SeLockMemoryPrivilege ");
  144. return(TRUE);
  145. }
  146. if ( ((*Privilege).QuadPart == IncreaseQuotaPrivilege.QuadPart)) {
  147. printf("SeIncreaseQuotaPrivilege ");
  148. return(TRUE);
  149. }
  150. if ( ((*Privilege).QuadPart == UnsolicitedInputPrivilege.QuadPart)) {
  151. printf("SeUnsolicitedInputPrivilege ");
  152. return(TRUE);
  153. }
  154. if ( ((*Privilege).QuadPart == TcbPrivilege.QuadPart)) {
  155. printf("SeTcbPrivilege ");
  156. return(TRUE);
  157. }
  158. if ( ((*Privilege).QuadPart == SecurityPrivilege.QuadPart)) {
  159. printf("SeSecurityPrivilege (Security Operator) ");
  160. return(TRUE);
  161. }
  162. if ( ((*Privilege).QuadPart == TakeOwnershipPrivilege.QuadPart)) {
  163. printf("SeTakeOwnershipPrivilege ");
  164. return(TRUE);
  165. }
  166. if ( ((*Privilege).QuadPart == LpcReplyBoostPrivilege.QuadPart)) {
  167. printf("SeLpcReplyBoostPrivilege ");
  168. return(TRUE);
  169. }
  170. if ( ((*Privilege).QuadPart == CreatePagefilePrivilege.QuadPart)) {
  171. printf("SeCreatePagefilePrivilege ");
  172. return(TRUE);
  173. }
  174. if ( ((*Privilege).QuadPart == IncreaseBasePriorityPrivilege.QuadPart)) {
  175. printf("SeIncreaseBasePriorityPrivilege ");
  176. return(TRUE);
  177. }
  178. if ( ((*Privilege).QuadPart == SystemProfilePrivilege.QuadPart)) {
  179. printf("SeSystemProfilePrivilege ");
  180. return(TRUE);
  181. }
  182. if ( ((*Privilege).QuadPart == SystemtimePrivilege.QuadPart)) {
  183. printf("SeSystemtimePrivilege ");
  184. return(TRUE);
  185. }
  186. if ( ((*Privilege).QuadPart == ProfileSingleProcessPrivilege.QuadPart)) {
  187. printf("SeProfileSingleProcessPrivilege ");
  188. return(TRUE);
  189. }
  190. if ( ((*Privilege).QuadPart == CreatePermanentPrivilege.QuadPart)) {
  191. printf("SeCreatePermanentPrivilege ");
  192. return(TRUE);
  193. }
  194. if ( ((*Privilege).QuadPart == BackupPrivilege.QuadPart)) {
  195. printf("SeBackupPrivilege ");
  196. return(TRUE);
  197. }
  198. if ( ((*Privilege).QuadPart == RestorePrivilege.QuadPart)) {
  199. printf("SeRestorePrivilege ");
  200. return(TRUE);
  201. }
  202. if ( ((*Privilege).QuadPart == ShutdownPrivilege.QuadPart)) {
  203. printf("SeShutdownPrivilege ");
  204. return(TRUE);
  205. }
  206. if ( ((*Privilege).QuadPart == DebugPrivilege.QuadPart)) {
  207. printf("SeDebugPrivilege ");
  208. return(TRUE);
  209. }
  210. if ( ((*Privilege).QuadPart == SystemEnvironmentPrivilege.QuadPart)) {
  211. printf("SeSystemEnvironmentPrivilege ");
  212. return(TRUE);
  213. }
  214. return(FALSE);
  215. }
  216. VOID
  217. DisplayPrivilege(
  218. PLUID_AND_ATTRIBUTES Privilege
  219. )
  220. {
  221. if (!DisplayPrivilegeName(&Privilege->Luid)) {
  222. printf("(Unknown Privilege. Value is: (0x%lx,0x%lx))",
  223. Privilege->Luid.HighPart,
  224. Privilege->Luid.LowPart
  225. );
  226. }
  227. //
  228. // Display the attributes assigned to the privilege.
  229. //
  230. printf("\n [");
  231. if (!(Privilege->Attributes & SE_PRIVILEGE_ENABLED)) {
  232. printf("Not ");
  233. }
  234. printf("Enabled");
  235. //printf(" / ");
  236. //if (!(Privilege->Attributes & SE_PRIVILEGE_ENABLED_BY_DEFAULT)) {
  237. // printf("Not ");
  238. //}
  239. //printf("Enabled By Default");
  240. printf("]\n");
  241. printf(" ");
  242. return;
  243. }
  244. VOID
  245. DisplaySecurityContext(
  246. IN HANDLE TokenHandle
  247. )
  248. {
  249. #define BUFFER_SIZE (2048)
  250. NTSTATUS Status;
  251. ULONG i;
  252. ULONG ReturnLength;
  253. TOKEN_STATISTICS ProcessTokenStatistics;
  254. GUID AuthenticationId;
  255. UCHAR Buffer[BUFFER_SIZE];
  256. PTOKEN_USER UserId;
  257. PTOKEN_OWNER DefaultOwner;
  258. PTOKEN_PRIMARY_GROUP PrimaryGroup;
  259. PTOKEN_GROUPS GroupIds;
  260. PTOKEN_PRIVILEGES Privileges;
  261. /////////////////////////////////////////////////////////////////////////
  262. // //
  263. // Logon ID //
  264. // //
  265. /////////////////////////////////////////////////////////////////////////
  266. Status = NtQueryInformationToken(
  267. TokenHandle, // Handle
  268. TokenStatistics, // TokenInformationClass
  269. &ProcessTokenStatistics, // TokenInformation
  270. sizeof(TOKEN_STATISTICS), // TokenInformationLength
  271. &ReturnLength // ReturnLength
  272. );
  273. ASSERT(NT_SUCCESS(Status));
  274. AuthenticationId = ProcessTokenStatistics.AuthenticationId;
  275. printf(" Logon Session: ");
  276. if (RtlEqualGuid(&AuthenticationId, &SystemAuthenticationId )) {
  277. printf("(System Logon Session)\n");
  278. } else {
  279. PrintGuid( &AuthenticationId );
  280. }
  281. /////////////////////////////////////////////////////////////////////////
  282. // //
  283. // User Id //
  284. // //
  285. /////////////////////////////////////////////////////////////////////////
  286. UserId = (PTOKEN_USER)&Buffer[0];
  287. Status = NtQueryInformationToken(
  288. TokenHandle, // Handle
  289. TokenUser, // TokenInformationClass
  290. UserId, // TokenInformation
  291. BUFFER_SIZE, // TokenInformationLength
  292. &ReturnLength // ReturnLength
  293. );
  294. ASSERT(NT_SUCCESS(Status));
  295. printf(" User id: ");
  296. DisplayAccountSid( (PISID)UserId->User.Sid );
  297. /////////////////////////////////////////////////////////////////////////
  298. // //
  299. // Default Owner //
  300. // //
  301. /////////////////////////////////////////////////////////////////////////
  302. DefaultOwner = (PTOKEN_OWNER)&Buffer[0];
  303. Status = NtQueryInformationToken(
  304. TokenHandle, // Handle
  305. TokenOwner, // TokenInformationClass
  306. DefaultOwner, // TokenInformation
  307. BUFFER_SIZE, // TokenInformationLength
  308. &ReturnLength // ReturnLength
  309. );
  310. ASSERT(NT_SUCCESS(Status));
  311. printf(" Default Owner: ");
  312. DisplayAccountSid( (PISID)DefaultOwner->Owner );
  313. /////////////////////////////////////////////////////////////////////////
  314. // //
  315. // Primary Group //
  316. // //
  317. /////////////////////////////////////////////////////////////////////////
  318. PrimaryGroup = (PTOKEN_PRIMARY_GROUP)&Buffer[0];
  319. Status = NtQueryInformationToken(
  320. TokenHandle, // Handle
  321. TokenPrimaryGroup, // TokenInformationClass
  322. PrimaryGroup, // TokenInformation
  323. BUFFER_SIZE, // TokenInformationLength
  324. &ReturnLength // ReturnLength
  325. );
  326. ASSERT(NT_SUCCESS(Status));
  327. printf(" Primary Group: ");
  328. DisplayAccountSid( (PISID)PrimaryGroup->PrimaryGroup );
  329. /////////////////////////////////////////////////////////////////////////
  330. // //
  331. // Group Ids //
  332. // //
  333. /////////////////////////////////////////////////////////////////////////
  334. printf("\n");
  335. GroupIds = (PTOKEN_GROUPS)&Buffer[0];
  336. Status = NtQueryInformationToken(
  337. TokenHandle, // Handle
  338. TokenGroups, // TokenInformationClass
  339. GroupIds, // TokenInformation
  340. BUFFER_SIZE, // TokenInformationLength
  341. &ReturnLength // ReturnLength
  342. );
  343. ASSERT(NT_SUCCESS(Status));
  344. //printf(" Number of groups: %ld\n", GroupIds->GroupCount);
  345. printf(" Groups: ");
  346. for (i=0; i < GroupIds->GroupCount; i++ ) {
  347. //printf(" Group %ld: ", i);
  348. DisplayAccountSid( (PISID)GroupIds->Groups[i].Sid );
  349. printf(" ");
  350. }
  351. /////////////////////////////////////////////////////////////////////////
  352. // //
  353. // Privileges //
  354. // //
  355. /////////////////////////////////////////////////////////////////////////
  356. printf("\n");
  357. Privileges = (PTOKEN_PRIVILEGES)&Buffer[0];
  358. Status = NtQueryInformationToken(
  359. TokenHandle, // Handle
  360. TokenPrivileges, // TokenInformationClass
  361. Privileges, // TokenInformation
  362. BUFFER_SIZE, // TokenInformationLength
  363. &ReturnLength // ReturnLength
  364. );
  365. ASSERT(NT_SUCCESS(Status));
  366. printf(" Privileges: ");
  367. if (Privileges->PrivilegeCount > 0) {
  368. for (i=0; i < Privileges->PrivilegeCount; i++ ) {
  369. DisplayPrivilege( &(Privileges->Privileges[i]) );
  370. }
  371. } else {
  372. printf("(none assigned)\n");
  373. }
  374. return;
  375. }
  376. BOOLEAN
  377. main()
  378. {
  379. NTSTATUS Status;
  380. HANDLE ProcessToken;
  381. TSeVariableInitialization(); // Initialize global variables
  382. printf("\n");
  383. //
  384. // Open our process token
  385. //
  386. Status = NtOpenProcessToken(
  387. NtCurrentProcess(),
  388. TOKEN_QUERY,
  389. &ProcessToken
  390. );
  391. if (!NT_SUCCESS(Status)) {
  392. printf("I'm terribly sorry, but you don't seem to have access to\n");
  393. printf("open your own process's token.\n");
  394. printf("\n");
  395. return(FALSE);
  396. }
  397. printf("Your process level security context is:\n");
  398. printf("\n");
  399. DisplaySecurityContext( ProcessToken );
  400. Status = NtClose( ProcessToken );
  401. return(TRUE);
  402. }