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.

746 lines
21 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 <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #define _TST_USER_ // User mode test
  18. LUID SystemAuthenticationId = SYSTEM_LUID;
  19. //
  20. // Universal well known SIDs
  21. //
  22. PSID NullSid;
  23. PSID WorldSid;
  24. PSID LocalSid;
  25. PSID CreatorOwnerSid;
  26. //
  27. // Sids defined by NT
  28. //
  29. PSID NtAuthoritySid;
  30. PSID DialupSid;
  31. PSID NetworkSid;
  32. PSID BatchSid;
  33. PSID InteractiveSid;
  34. PSID LocalSystemSid;
  35. ////////////////////////////////////////////////////////////////////////
  36. // //
  37. // Define the well known privileges //
  38. // //
  39. ////////////////////////////////////////////////////////////////////////
  40. LUID CreateTokenPrivilege;
  41. LUID AssignPrimaryTokenPrivilege;
  42. LUID LockMemoryPrivilege;
  43. LUID IncreaseQuotaPrivilege;
  44. LUID UnsolicitedInputPrivilege;
  45. LUID TcbPrivilege;
  46. LUID SecurityPrivilege;
  47. LUID TakeOwnershipPrivilege;
  48. LUID LpcReplyBoostPrivilege;
  49. LUID CreatePagefilePrivilege;
  50. LUID IncreaseBasePriorityPrivilege;
  51. LUID SystemProfilePrivilege;
  52. LUID SystemtimePrivilege;
  53. LUID ProfileSingleProcessPrivilege;
  54. LUID RestorePrivilege;
  55. LUID BackupPrivilege;
  56. LUID CreatePermanentPrivilege;
  57. LUID ShutdownPrivilege;
  58. LUID DebugPrivilege;
  59. VOID
  60. DisplaySecurityContext(
  61. IN HANDLE TokenHandle
  62. );
  63. VOID
  64. DisplayAccountSid(
  65. PSID Sid
  66. );
  67. BOOLEAN
  68. SidTranslation(
  69. PSID Sid,
  70. PSTRING AccountName
  71. );
  72. ////////////////////////////////////////////////////////////////
  73. // //
  74. // Private Macros //
  75. // //
  76. ////////////////////////////////////////////////////////////////
  77. #define PrintGuid(G) \
  78. printf( "(0x%lx-%hx-%hx-%hx-%hx-%hx-%hx-%hx-%hx-%hx-%hx)\n", \
  79. (G)->Data1, (G)->Data2, (G)->Data3, \
  80. (G)->Data4[0], (G)->Data4[1], (G)->Data4[2], \
  81. (G)->Data4[3], (G)->Data4[4], (G)->Data4[5], \
  82. (G)->Data4[6], (G)->Data4[7]); \
  83. BOOLEAN
  84. SidTranslation(
  85. PSID Sid,
  86. PSTRING AccountName
  87. )
  88. // AccountName is expected to have a large maximum length
  89. {
  90. if (RtlEqualSid(Sid, WorldSid)) {
  91. RtlInitString( AccountName, "WORLD");
  92. return(TRUE);
  93. }
  94. if (RtlEqualSid(Sid, LocalSid)) {
  95. RtlInitString( AccountName, "LOCAL");
  96. return(TRUE);
  97. }
  98. if (RtlEqualSid(Sid, NetworkSid)) {
  99. RtlInitString( AccountName, "NETWORK");
  100. return(TRUE);
  101. }
  102. if (RtlEqualSid(Sid, BatchSid)) {
  103. RtlInitString( AccountName, "BATCH");
  104. return(TRUE);
  105. }
  106. if (RtlEqualSid(Sid, InteractiveSid)) {
  107. RtlInitString( AccountName, "INTERACTIVE");
  108. return(TRUE);
  109. }
  110. if (RtlEqualSid(Sid, LocalSystemSid)) {
  111. RtlInitString( AccountName, "SYSTEM");
  112. return(TRUE);
  113. }
  114. return(FALSE);
  115. }
  116. VOID
  117. DisplayAccountSid(
  118. PSID Sid
  119. )
  120. {
  121. UCHAR Buffer[128];
  122. STRING AccountName;
  123. UCHAR i;
  124. ULONG Tmp;
  125. PSID_IDENTIFIER_AUTHORITY IdentifierAuthority;
  126. UCHAR SubAuthorityCount;
  127. Buffer[0] = 0;
  128. AccountName.MaximumLength = 127;
  129. AccountName.Length = 0;
  130. AccountName.Buffer = (PVOID)&Buffer[0];
  131. if (SidTranslation( (PSID)Sid, &AccountName) ) {
  132. printf("%s\n", AccountName.Buffer );
  133. } else {
  134. IdentifierAuthority = RtlIdentifierAuthoritySid(Sid);
  135. //
  136. // HACK! HACK!
  137. // The next line prints the revision of the SID. Since there is no
  138. // rtl routine which gives us the SID revision, we must make due.
  139. // luckily, the revision field is the first field in the SID, so we
  140. // can just cast the pointer.
  141. //
  142. printf("S-%u-", (USHORT) *((PUCHAR) Sid) );
  143. if ( (IdentifierAuthority->Value[0] != 0) ||
  144. (IdentifierAuthority->Value[1] != 0) ){
  145. printf("0x%02hx%02hx%02hx%02hx%02hx%02hx",
  146. IdentifierAuthority->Value[0],
  147. IdentifierAuthority->Value[1],
  148. IdentifierAuthority->Value[2],
  149. IdentifierAuthority->Value[3],
  150. IdentifierAuthority->Value[4],
  151. IdentifierAuthority->Value[5] );
  152. } else {
  153. Tmp = IdentifierAuthority->Value[5] +
  154. (IdentifierAuthority->Value[4] << 8) +
  155. (IdentifierAuthority->Value[3] << 16) +
  156. (IdentifierAuthority->Value[2] << 24);
  157. printf("%lu", Tmp);
  158. }
  159. SubAuthorityCount = *RtlSubAuthorityCountSid(Sid);
  160. for (i=0;i<SubAuthorityCount ;i++ ) {
  161. printf("-%lu", (*RtlSubAuthoritySid(Sid, i)));
  162. }
  163. printf("\n");
  164. }
  165. }
  166. BOOLEAN
  167. DisplayPrivilegeName(
  168. PLUID Privilege
  169. )
  170. {
  171. //
  172. // This should be rewritten to use RtlLookupPrivilegeName.
  173. //
  174. // First we should probably spec and write RtlLookupPrivilegeName.
  175. //
  176. if (Privilege->QuadPart == CreateTokenPrivilege.QuadPart) {
  177. printf("SeCreateTokenPrivilege ");
  178. return(TRUE);
  179. }
  180. if (Privilege->QuadPart == AssignPrimaryTokenPrivilege.QuadPart) {
  181. printf("SeAssignPrimaryTokenPrivilege ");
  182. return(TRUE);
  183. }
  184. if (Privilege->QuadPart == LockMemoryPrivilege.QuadPart) {
  185. printf("SeLockMemoryPrivilege ");
  186. return(TRUE);
  187. }
  188. if (Privilege->QuadPart == IncreaseQuotaPrivilege.QuadPart) {
  189. printf("SeIncreaseQuotaPrivilege ");
  190. return(TRUE);
  191. }
  192. if ( Privilege->QuadPart == UnsolicitedInputPrivilege.QuadPart) {
  193. printf("SeUnsolicitedInputPrivilege ");
  194. return(TRUE);
  195. }
  196. if ( Privilege->QuadPart == TcbPrivilege.QuadPart) {
  197. printf("SeTcbPrivilege ");
  198. return(TRUE);
  199. }
  200. if ( Privilege->QuadPart == SecurityPrivilege.QuadPart) {
  201. printf("SeSecurityPrivilege (Security Operator) ");
  202. return(TRUE);
  203. }
  204. if ( Privilege->QuadPart == TakeOwnershipPrivilege.QuadPart) {
  205. printf("SeTakeOwnershipPrivilege ");
  206. return(TRUE);
  207. }
  208. if ( Privilege->QuadPart == LpcReplyBoostPrivilege.QuadPart) {
  209. printf("SeLpcReplyBoostPrivilege ");
  210. return(TRUE);
  211. }
  212. if ( Privilege->QuadPart == CreatePagefilePrivilege.QuadPart) {
  213. printf("SeCreatePagefilePrivilege ");
  214. return(TRUE);
  215. }
  216. if ( Privilege->QuadPart == IncreaseBasePriorityPrivilege.QuadPart) {
  217. printf("SeIncreaseBasePriorityPrivilege ");
  218. return(TRUE);
  219. }
  220. if ( Privilege->QuadPart == SystemProfilePrivilege.QuadPart) {
  221. printf("SeSystemProfilePrivilege ");
  222. return(TRUE);
  223. }
  224. if ( Privilege->QuadPart == SystemtimePrivilege.QuadPart) {
  225. printf("SeSystemtimePrivilege ");
  226. return(TRUE);
  227. }
  228. if ( Privilege->QuadPart == ProfileSingleProcessPrivilege.QuadPart) {
  229. printf("SeProfileSingleProcessPrivilege ");
  230. return(TRUE);
  231. }
  232. if ( Privilege->QuadPart == CreatePermanentPrivilege.QuadPart) {
  233. printf("SeCreatePermanentPrivilege ");
  234. return(TRUE);
  235. }
  236. if ( Privilege->QuadPart == BackupPrivilege.QuadPart) {
  237. printf("SeBackupPrivilege ");
  238. return(TRUE);
  239. }
  240. if ( Privilege->QuadPart == RestorePrivilege.QuadPart) {
  241. printf("SeRestorePrivilege ");
  242. return(TRUE);
  243. }
  244. if ( Privilege->QuadPart == ShutdownPrivilege.QuadPart) {
  245. printf("SeShutdownPrivilege ");
  246. return(TRUE);
  247. }
  248. if ( Privilege->QuadPart == DebugPrivilege.QuadPart) {
  249. printf("SeDebugPrivilege ");
  250. return(TRUE);
  251. }
  252. return(FALSE);
  253. }
  254. VOID
  255. DisplayPrivilege(
  256. PLUID_AND_ATTRIBUTES Privilege
  257. )
  258. {
  259. //
  260. // Display the attributes assigned to the privilege.
  261. //
  262. printf(" [");
  263. if (!(Privilege->Attributes & SE_PRIVILEGE_ENABLED)) {
  264. printf("Disabled");
  265. } else {
  266. printf(" Enabled");
  267. }
  268. //printf(", ");
  269. //if (!(Privilege->Attributes & SE_PRIVILEGE_ENABLED_BY_DEFAULT)) {
  270. // printf("Not ");
  271. //}
  272. //printf("Enabled By Default");
  273. printf("] ");
  274. if (!DisplayPrivilegeName(&Privilege->Luid)) {
  275. printf("(Unknown Privilege. Value is: (0x%lx,0x%lx))",
  276. Privilege->Luid.HighPart,
  277. Privilege->Luid.LowPart
  278. );
  279. }
  280. printf("\n");
  281. return;
  282. }
  283. VOID
  284. DisplaySecurityContext(
  285. IN HANDLE TokenHandle
  286. )
  287. {
  288. #define BUFFER_SIZE (2048)
  289. NTSTATUS Status;
  290. ULONG i;
  291. ULONG ReturnLength;
  292. TOKEN_STATISTICS ProcessTokenStatistics;
  293. LUID AuthenticationId;
  294. UCHAR Buffer[BUFFER_SIZE];
  295. PTOKEN_USER UserId;
  296. PTOKEN_OWNER DefaultOwner;
  297. PTOKEN_PRIMARY_GROUP PrimaryGroup;
  298. PTOKEN_GROUPS GroupIds;
  299. PTOKEN_PRIVILEGES Privileges;
  300. /////////////////////////////////////////////////////////////////////////
  301. // //
  302. // Logon ID //
  303. // //
  304. /////////////////////////////////////////////////////////////////////////
  305. Status = NtQueryInformationToken(
  306. TokenHandle, // Handle
  307. TokenStatistics, // TokenInformationClass
  308. &ProcessTokenStatistics, // TokenInformation
  309. sizeof(TOKEN_STATISTICS), // TokenInformationLength
  310. &ReturnLength // ReturnLength
  311. );
  312. ASSERT(NT_SUCCESS(Status));
  313. AuthenticationId = ProcessTokenStatistics.AuthenticationId;
  314. printf(" Logon Session: ");
  315. if (RtlEqualLuid(&AuthenticationId, &SystemAuthenticationId )) {
  316. printf("(System Logon Session)\n");
  317. } else {
  318. printf( "(0x%lx, 0x%lx)",AuthenticationId.LowPart, AuthenticationId.HighPart);
  319. }
  320. /////////////////////////////////////////////////////////////////////////
  321. // //
  322. // User Id //
  323. // //
  324. /////////////////////////////////////////////////////////////////////////
  325. UserId = (PTOKEN_USER)&Buffer[0];
  326. Status = NtQueryInformationToken(
  327. TokenHandle, // Handle
  328. TokenUser, // TokenInformationClass
  329. UserId, // TokenInformation
  330. BUFFER_SIZE, // TokenInformationLength
  331. &ReturnLength // ReturnLength
  332. );
  333. ASSERT(NT_SUCCESS(Status));
  334. printf(" User id: ");
  335. DisplayAccountSid( UserId->User.Sid );
  336. /////////////////////////////////////////////////////////////////////////
  337. // //
  338. // Default Owner //
  339. // //
  340. /////////////////////////////////////////////////////////////////////////
  341. DefaultOwner = (PTOKEN_OWNER)&Buffer[0];
  342. Status = NtQueryInformationToken(
  343. TokenHandle, // Handle
  344. TokenOwner, // TokenInformationClass
  345. DefaultOwner, // TokenInformation
  346. BUFFER_SIZE, // TokenInformationLength
  347. &ReturnLength // ReturnLength
  348. );
  349. ASSERT(NT_SUCCESS(Status));
  350. printf(" Default Owner: ");
  351. DisplayAccountSid( DefaultOwner->Owner );
  352. /////////////////////////////////////////////////////////////////////////
  353. // //
  354. // Primary Group //
  355. // //
  356. /////////////////////////////////////////////////////////////////////////
  357. PrimaryGroup = (PTOKEN_PRIMARY_GROUP)&Buffer[0];
  358. Status = NtQueryInformationToken(
  359. TokenHandle, // Handle
  360. TokenPrimaryGroup, // TokenInformationClass
  361. PrimaryGroup, // TokenInformation
  362. BUFFER_SIZE, // TokenInformationLength
  363. &ReturnLength // ReturnLength
  364. );
  365. ASSERT(NT_SUCCESS(Status));
  366. printf(" Primary Group: ");
  367. DisplayAccountSid( PrimaryGroup->PrimaryGroup );
  368. /////////////////////////////////////////////////////////////////////////
  369. // //
  370. // Group Ids //
  371. // //
  372. /////////////////////////////////////////////////////////////////////////
  373. printf("\n");
  374. GroupIds = (PTOKEN_GROUPS)&Buffer[0];
  375. Status = NtQueryInformationToken(
  376. TokenHandle, // Handle
  377. TokenGroups, // TokenInformationClass
  378. GroupIds, // TokenInformation
  379. BUFFER_SIZE, // TokenInformationLength
  380. &ReturnLength // ReturnLength
  381. );
  382. ASSERT(NT_SUCCESS(Status));
  383. //printf(" Number of groups: %ld\n", GroupIds->GroupCount);
  384. printf(" Groups: ");
  385. for (i=0; i < GroupIds->GroupCount; i++ ) {
  386. //printf(" Group %ld: ", i);
  387. DisplayAccountSid( GroupIds->Groups[i].Sid );
  388. printf(" ");
  389. }
  390. /////////////////////////////////////////////////////////////////////////
  391. // //
  392. // Privileges //
  393. // //
  394. /////////////////////////////////////////////////////////////////////////
  395. printf("\n");
  396. Privileges = (PTOKEN_PRIVILEGES)&Buffer[0];
  397. Status = NtQueryInformationToken(
  398. TokenHandle, // Handle
  399. TokenPrivileges, // TokenInformationClass
  400. Privileges, // TokenInformation
  401. BUFFER_SIZE, // TokenInformationLength
  402. &ReturnLength // ReturnLength
  403. );
  404. ASSERT(NT_SUCCESS(Status));
  405. printf(" Privileges: \n");
  406. if (Privileges->PrivilegeCount > 0) {
  407. for (i=0; i < Privileges->PrivilegeCount; i++ ) {
  408. DisplayPrivilege( &(Privileges->Privileges[i]) );
  409. }
  410. } else {
  411. printf("(none assigned)\n");
  412. }
  413. return;
  414. }
  415. VOID
  416. InitVars()
  417. {
  418. ULONG SidWithZeroSubAuthorities;
  419. ULONG SidWithOneSubAuthority;
  420. ULONG SidWithThreeSubAuthorities;
  421. ULONG SidWithFourSubAuthorities;
  422. SID_IDENTIFIER_AUTHORITY NullSidAuthority = SECURITY_NULL_SID_AUTHORITY;
  423. SID_IDENTIFIER_AUTHORITY WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  424. SID_IDENTIFIER_AUTHORITY LocalSidAuthority = SECURITY_LOCAL_SID_AUTHORITY;
  425. SID_IDENTIFIER_AUTHORITY CreatorSidAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  426. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  427. //
  428. // The following SID sizes need to be allocated
  429. //
  430. SidWithZeroSubAuthorities = RtlLengthRequiredSid( 0 );
  431. SidWithOneSubAuthority = RtlLengthRequiredSid( 1 );
  432. SidWithThreeSubAuthorities = RtlLengthRequiredSid( 3 );
  433. SidWithFourSubAuthorities = RtlLengthRequiredSid( 4 );
  434. //
  435. // Allocate and initialize the universal SIDs
  436. //
  437. NullSid = (PSID)malloc(SidWithOneSubAuthority);
  438. WorldSid = (PSID)malloc(SidWithOneSubAuthority);
  439. LocalSid = (PSID)malloc(SidWithOneSubAuthority);
  440. CreatorOwnerSid = (PSID)malloc(SidWithOneSubAuthority);
  441. RtlInitializeSid( NullSid, &NullSidAuthority, 1 );
  442. RtlInitializeSid( WorldSid, &WorldSidAuthority, 1 );
  443. RtlInitializeSid( LocalSid, &LocalSidAuthority, 1 );
  444. RtlInitializeSid( CreatorOwnerSid, &CreatorSidAuthority, 1 );
  445. *(RtlSubAuthoritySid( NullSid, 0 )) = SECURITY_NULL_RID;
  446. *(RtlSubAuthoritySid( WorldSid, 0 )) = SECURITY_WORLD_RID;
  447. *(RtlSubAuthoritySid( LocalSid, 0 )) = SECURITY_LOCAL_RID;
  448. *(RtlSubAuthoritySid( CreatorOwnerSid, 0 )) = SECURITY_CREATOR_OWNER_RID;
  449. //
  450. // Allocate and initialize the NT defined SIDs
  451. //
  452. NtAuthoritySid = (PSID)malloc(SidWithZeroSubAuthorities);
  453. DialupSid = (PSID)malloc(SidWithOneSubAuthority);
  454. NetworkSid = (PSID)malloc(SidWithOneSubAuthority);
  455. BatchSid = (PSID)malloc(SidWithOneSubAuthority);
  456. InteractiveSid = (PSID)malloc(SidWithOneSubAuthority);
  457. LocalSystemSid = (PSID)malloc(SidWithOneSubAuthority);
  458. RtlInitializeSid( NtAuthoritySid, &NtAuthority, 0 );
  459. RtlInitializeSid( DialupSid, &NtAuthority, 1 );
  460. RtlInitializeSid( NetworkSid, &NtAuthority, 1 );
  461. RtlInitializeSid( BatchSid, &NtAuthority, 1 );
  462. RtlInitializeSid( InteractiveSid, &NtAuthority, 1 );
  463. RtlInitializeSid( LocalSystemSid, &NtAuthority, 1 );
  464. *(RtlSubAuthoritySid( DialupSid, 0 )) = SECURITY_DIALUP_RID;
  465. *(RtlSubAuthoritySid( NetworkSid, 0 )) = SECURITY_NETWORK_RID;
  466. *(RtlSubAuthoritySid( BatchSid, 0 )) = SECURITY_BATCH_RID;
  467. *(RtlSubAuthoritySid( InteractiveSid, 0 )) = SECURITY_INTERACTIVE_RID;
  468. *(RtlSubAuthoritySid( LocalSystemSid, 0 )) = SECURITY_LOCAL_SYSTEM_RID;
  469. CreateTokenPrivilege =
  470. RtlConvertLongToLargeInteger(SE_CREATE_TOKEN_PRIVILEGE);
  471. AssignPrimaryTokenPrivilege =
  472. RtlConvertLongToLargeInteger(SE_ASSIGNPRIMARYTOKEN_PRIVILEGE);
  473. LockMemoryPrivilege =
  474. RtlConvertLongToLargeInteger(SE_LOCK_MEMORY_PRIVILEGE);
  475. IncreaseQuotaPrivilege =
  476. RtlConvertLongToLargeInteger(SE_INCREASE_QUOTA_PRIVILEGE);
  477. UnsolicitedInputPrivilege =
  478. RtlConvertLongToLargeInteger(SE_UNSOLICITED_INPUT_PRIVILEGE);
  479. TcbPrivilege =
  480. RtlConvertLongToLargeInteger(SE_TCB_PRIVILEGE);
  481. SecurityPrivilege =
  482. RtlConvertLongToLargeInteger(SE_SECURITY_PRIVILEGE);
  483. TakeOwnershipPrivilege =
  484. RtlConvertLongToLargeInteger(SE_TAKE_OWNERSHIP_PRIVILEGE);
  485. CreatePagefilePrivilege =
  486. RtlConvertLongToLargeInteger(SE_CREATE_PAGEFILE_PRIVILEGE);
  487. IncreaseBasePriorityPrivilege =
  488. RtlConvertLongToLargeInteger(SE_INC_BASE_PRIORITY_PRIVILEGE);
  489. SystemProfilePrivilege =
  490. RtlConvertLongToLargeInteger(SE_SYSTEM_PROFILE_PRIVILEGE);
  491. SystemtimePrivilege =
  492. RtlConvertLongToLargeInteger(SE_SYSTEMTIME_PRIVILEGE);
  493. ProfileSingleProcessPrivilege =
  494. RtlConvertLongToLargeInteger(SE_PROF_SINGLE_PROCESS_PRIVILEGE);
  495. CreatePermanentPrivilege =
  496. RtlConvertLongToLargeInteger(SE_CREATE_PERMANENT_PRIVILEGE);
  497. BackupPrivilege =
  498. RtlConvertLongToLargeInteger(SE_BACKUP_PRIVILEGE);
  499. RestorePrivilege =
  500. RtlConvertLongToLargeInteger(SE_RESTORE_PRIVILEGE);
  501. ShutdownPrivilege =
  502. RtlConvertLongToLargeInteger(SE_SHUTDOWN_PRIVILEGE);
  503. DebugPrivilege =
  504. RtlConvertLongToLargeInteger(SE_DEBUG_PRIVILEGE);
  505. }
  506. BOOLEAN
  507. _CRTAPI1 main()
  508. {
  509. NTSTATUS Status;
  510. HANDLE ProcessToken;
  511. InitVars(); // Initialize global variables
  512. printf("\n");
  513. //
  514. // Open our process token
  515. //
  516. Status = NtOpenProcessToken(
  517. NtCurrentProcess(),
  518. TOKEN_QUERY,
  519. &ProcessToken
  520. );
  521. if (!NT_SUCCESS(Status)) {
  522. printf("I'm terribly sorry, but you don't seem to have access to\n");
  523. printf("open your own process's token.\n");
  524. printf("\n");
  525. return(FALSE);
  526. }
  527. printf("Your process level security context is:\n");
  528. printf("\n");
  529. DisplaySecurityContext( ProcessToken );
  530. Status = NtClose( ProcessToken );
  531. return(TRUE);
  532. }