Leaked source code of windows server 2003
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.

196 lines
4.9 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "bmcommon.h"
  4. static GENERIC_MAPPING FileGenericMapping =
  5. {
  6. FILE_GENERIC_READ,
  7. FILE_GENERIC_WRITE,
  8. FILE_GENERIC_EXECUTE,
  9. FILE_ALL_ACCESS
  10. };
  11. static PSECURITY_DESCRIPTOR pSD;
  12. static HANDLE hToken;
  13. EXTERN_C
  14. DWORD
  15. InitNtAccessChecks()
  16. {
  17. DWORD dwError=NO_ERROR;
  18. BOOL b;
  19. PWCHAR szMsg=NULL;
  20. HANDLE hProcessToken=NULL;
  21. b = ConvertStringSecurityDescriptorToSecurityDescriptorW(g_szSd,
  22. SDDL_REVISION_1,
  23. &pSD, NULL);
  24. if (!b)
  25. {
  26. szMsg = L"SDDL";
  27. goto GetError;
  28. }
  29. if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_DUPLICATE,
  30. &hProcessToken ) )
  31. {
  32. szMsg = L"OpenProcessToken";
  33. goto GetError;
  34. }
  35. if ( !DuplicateToken( hProcessToken, SecurityImpersonation, &hToken ) )
  36. {
  37. szMsg = L"DuplicateToken";
  38. goto GetError;
  39. }
  40. if ( !SetThreadToken( NULL, hToken ) )
  41. {
  42. szMsg = L"SetThreadToken";
  43. goto GetError;
  44. }
  45. Cleanup:
  46. if ( hProcessToken )
  47. {
  48. CloseHandle( hProcessToken );
  49. }
  50. if ( szMsg )
  51. {
  52. wprintf (L"InitNtAccessChecks: %s: %x\n", szMsg, dwError);
  53. }
  54. return dwError;
  55. GetError:
  56. dwError = GetLastError();
  57. goto Cleanup;
  58. }
  59. EXTERN_C
  60. DWORD
  61. DoNtAccessChecks(
  62. IN ULONG NumChecks,
  63. IN DWORD Flags
  64. )
  65. {
  66. DWORD dwError=NO_ERROR;
  67. PWCHAR StringSD = L"O:BAG:BAD:(OA;;GA;;;WD)S:(AU;FASA;GA;;;WD)";
  68. BOOL b;
  69. ULONG i;
  70. PRIVILEGE_SET Privs = { 0 };
  71. DWORD dwPrivLength=20*sizeof(LUID_AND_ATTRIBUTES);
  72. BOOL fGenOnClose[100];
  73. PWCHAR szMsg=NULL;
  74. HANDLE hObj= (HANDLE) 333444;
  75. if ( Flags & BMF_GenerateAudit )
  76. {
  77. if ( Flags & BMF_UseObjTypeList )
  78. {
  79. for (i=0; i < NumChecks; i++)
  80. {
  81. if (!AccessCheckByTypeResultListAndAuditAlarm(
  82. L"supersystemwithaudit",
  83. hObj,
  84. L"Kernel speed test",
  85. L"sample operation",
  86. pSD,
  87. g_Sid1,
  88. DESIRED_ACCESS,
  89. AuditEventObjectAccess,
  90. 0,
  91. ObjectTypeList,
  92. ObjectTypeListLength,
  93. &FileGenericMapping,
  94. FALSE,
  95. dwNtGrantedAccess,
  96. fNtAccessCheckResult,
  97. fGenOnClose ))
  98. {
  99. szMsg = L"AccessCheck";
  100. goto GetError;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. for (i=0; i < NumChecks; i++)
  107. {
  108. if (!AccessCheckAndAuditAlarm(
  109. L"mysystem",
  110. hObj,
  111. L"File",
  112. L"file-object",
  113. pSD, DESIRED_ACCESS,
  114. &FileGenericMapping,
  115. FALSE,
  116. &dwNtGrantedAccess[0],
  117. &fNtAccessCheckResult[0],
  118. &fGenOnClose[0] ))
  119. {
  120. szMsg = L"AccessCheck";
  121. goto GetError;
  122. }
  123. }
  124. }
  125. }
  126. else
  127. {
  128. if ( Flags & BMF_UseObjTypeList )
  129. {
  130. for (i=0; i < NumChecks; i++)
  131. {
  132. if (!AccessCheckByTypeResultList(
  133. pSD,
  134. g_Sid1,
  135. hToken, DESIRED_ACCESS,
  136. ObjectTypeList,
  137. ObjectTypeListLength,
  138. &FileGenericMapping,
  139. &Privs, &dwPrivLength,
  140. dwNtGrantedAccess,
  141. fNtAccessCheckResult ))
  142. {
  143. szMsg = L"AccessCheck";
  144. goto GetError;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. for (i=0; i < NumChecks; i++)
  151. {
  152. if (!AccessCheck( pSD, hToken, DESIRED_ACCESS,
  153. &FileGenericMapping,
  154. &Privs, &dwPrivLength,
  155. &dwNtGrantedAccess[0],
  156. &fNtAccessCheckResult[0] ))
  157. {
  158. szMsg = L"AccessCheck";
  159. goto GetError;
  160. }
  161. }
  162. }
  163. }
  164. Cleanup:
  165. if ( szMsg )
  166. {
  167. wprintf (L"%s: %x\n", szMsg, dwError);
  168. }
  169. return dwError;
  170. GetError:
  171. dwError = GetLastError();
  172. goto Cleanup;
  173. }