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.

277 lines
7.0 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "bmcommon.h"
  4. //
  5. // S-1-5-21-397955417-626881126-188441444-2908314 (kumarp)
  6. //
  7. //WCHAR szSid[] = L"S-1-5-21-397955417-626881126-188441444-2908314";
  8. WCHAR szSid[] = L"S-1-5-21-397955417-626881126-188441444-2101332";
  9. //ULONG Sid[] = {0x00000501, 0x05000000, 0x00000015, 0x17b85159, 0x255d7266, 0x0b3b6364, 0x00201054};
  10. // S-1-5-21-397955417-626881126-188441444-2101332
  11. //ULONG Sid[] = {0x00000501, 0x05000000, 0x00000015, 0x17b85159, 0x255d7266, 0x0b3b6364, 0x00201054};
  12. BOOL b;
  13. DWORD AuthzRmAuditFlags = 0;
  14. HANDLE hProcessToken=NULL;
  15. static HANDLE hToken;
  16. DWORD AuthzAuditFlags = 0;
  17. PCWSTR ResourceManagerName = L"Speed Test Resource Manager";
  18. AUTHZ_RM_AUDIT_INFO_HANDLE hRmAuditInfo = NULL;
  19. AUTHZ_RESOURCE_MANAGER_HANDLE hAuthzResourceManager = NULL;
  20. DWORD AuthzRmFlags = 0;
  21. AUDIT_EVENT_INFO AuditEventInfo;
  22. PCWSTR szOperationType = L"Access Check";
  23. PCWSTR szObjectName = L"Joe";
  24. PCWSTR szObjectType = L"Kernel Hacker";
  25. PCWSTR szAdditionalInfo = L"None";
  26. AUTHZ_AUDIT_INFO_HANDLE hAuditInfo = NULL;
  27. AUTHZ_CLIENT_CONTEXT_HANDLE hAuthzClientContext = NULL;
  28. LUID luid = {0xdead,0xbeef};
  29. ULONG i;
  30. ULONG NumAccessChecks = 10;
  31. AUTHZ_ACCESS_REQUEST RequestOT;
  32. AUTHZ_ACCESS_REQUEST Request;
  33. UCHAR Buffer[1024];
  34. UCHAR Buffer2[1024];
  35. UCHAR TypeListBuffer[1024];
  36. PAUTHZ_ACCESS_REPLY pReply = (PAUTHZ_ACCESS_REPLY) Buffer;
  37. PAUTHZ_ACCESS_REPLY pReplyOT = (PAUTHZ_ACCESS_REPLY) Buffer2;
  38. PSECURITY_DESCRIPTOR pSD = NULL;
  39. AUTHZ_HANDLE AuthzHandle = 0;
  40. AUDIT_PARAMS AuditParams;
  41. AUDIT_PARAM ParamArray[11];
  42. PSID pSid;
  43. PSID pUserSid;
  44. BOOL
  45. AuthzInit( )
  46. {
  47. BOOL b;
  48. if (!ConvertStringSidToSid( szSid, &pSid ))
  49. {
  50. return FALSE;
  51. }
  52. AuditEventInfo.Version = AUDIT_TYPE_LEGACY;
  53. AuditEventInfo.u.Legacy.CategoryId = SE_CATEGID_OBJECT_ACCESS;
  54. AuditEventInfo.u.Legacy.AuditId = SE_AUDITID_OBJECT_OPERATION;
  55. AuditEventInfo.u.Legacy.ParameterCount = 3;
  56. //
  57. // init request for obj-type access check
  58. //
  59. RequestOT.DesiredAccess = DESIRED_ACCESS;
  60. RequestOT.ObjectTypeList = ObjectTypeList;
  61. RequestOT.ObjectTypeListLength = ObjectTypeListLength;
  62. RequestOT.OptionalArguments = NULL;
  63. RequestOT.PrincipalSelfSid = NULL;
  64. //RequestOT.PrincipalSelfSid = g_Sid1;
  65. //
  66. // init non obj-type request
  67. //
  68. Request.DesiredAccess = DESIRED_ACCESS;
  69. Request.ObjectTypeList = NULL;
  70. Request.ObjectTypeListLength = 0;
  71. Request.OptionalArguments = NULL;
  72. Request.PrincipalSelfSid = NULL;
  73. //Request.PrincipalSelfSid = g_Sid1;
  74. //
  75. // init reply for obj type list
  76. //
  77. pReplyOT->ResultListLength = ObjectTypeListLength;
  78. pReplyOT->Error = (PDWORD) (((PCHAR) pReplyOT) + sizeof(AUTHZ_ACCESS_REPLY));
  79. pReplyOT->GrantedAccessMask = (PACCESS_MASK) (pReplyOT->Error + pReplyOT->ResultListLength);
  80. //
  81. // init reply
  82. //
  83. pReply->ResultListLength = 1;
  84. pReply->Error = (PDWORD) (((PCHAR) pReply) + sizeof(AUTHZ_ACCESS_REPLY));
  85. pReply->GrantedAccessMask = (PACCESS_MASK) (pReply->Error + pReply->ResultListLength);
  86. b = AuthzInitializeResourceManager(
  87. NULL,
  88. NULL,
  89. NULL,
  90. L"Benchmark RM",
  91. AuthzRmFlags,
  92. &hAuthzResourceManager
  93. );
  94. if (!b)
  95. {
  96. printf("AuthzInitializeResourceManager\n");
  97. return FALSE;
  98. }
  99. AuditParams.Parameters = ParamArray;
  100. AuthzInitializeAuditParams(
  101. &AuditParams,
  102. &pUserSid,
  103. L"Authz Speed Tests",
  104. APF_AuditSuccess,
  105. 1,
  106. APT_String, L"Test operation"
  107. );
  108. b = AuthzInitializeAuditInfo(
  109. &hAuditInfo,
  110. 0,
  111. hAuthzResourceManager,
  112. &AuditEventInfo,
  113. &AuditParams,
  114. NULL,
  115. INFINITE,
  116. L"blah",
  117. L"blah",
  118. L"and",
  119. L"blah"
  120. );
  121. if (!b)
  122. {
  123. printf("AuthzInitAuditInfo FAILED with %d.\n", GetLastError());
  124. return 0;
  125. }
  126. b = AuthzModifyAuditQueue(
  127. NULL,
  128. AUTHZ_AUDIT_QUEUE_THREAD_PRIORITY,
  129. 0,
  130. 0,
  131. 0,
  132. THREAD_PRIORITY_LOWEST
  133. );
  134. if (!b)
  135. {
  136. printf("AuthzModifyAuditQueue FAILED with %d.\n", GetLastError());
  137. return 0;
  138. }
  139. if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY,
  140. &hProcessToken ) )
  141. {
  142. wprintf(L"OpenProcessToken failed %d\n", GetLastError());
  143. return 0;
  144. }
  145. b = AuthzInitializeContextFromToken(
  146. hProcessToken,
  147. hAuthzResourceManager,
  148. NULL,
  149. luid,
  150. 0,
  151. NULL,
  152. &hAuthzClientContext
  153. );
  154. if (!b)
  155. {
  156. printf("AuthzInitializeContextFromToken failed %d\n", GetLastError());
  157. return FALSE;
  158. }
  159. if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
  160. {
  161. return GetLastError();
  162. }
  163. b = ConvertStringSecurityDescriptorToSecurityDescriptorW(g_szSd, SDDL_REVISION_1, &pSD, NULL);
  164. if (!b)
  165. {
  166. wprintf(L"SDDL failed with %d\n", GetLastError());
  167. return FALSE;
  168. }
  169. return TRUE;
  170. }
  171. DWORD
  172. InitAuthzAccessChecks()
  173. {
  174. if (!AuthzInit())
  175. {
  176. return GetLastError();
  177. }
  178. return NO_ERROR;
  179. }
  180. DWORD
  181. AuthzDoAccessCheck(
  182. IN ULONG NumAccessChecks,
  183. IN DWORD Flags
  184. )
  185. {
  186. AUTHZ_AUDIT_INFO_HANDLE AdtInfo = Flags & BMF_GenerateAudit ? hAuditInfo : NULL;
  187. if ( Flags & BMF_UseObjTypeList )
  188. {
  189. for (i = 0; i < NumAccessChecks; i ++)
  190. {
  191. b = AuthzAccessCheck(
  192. hAuthzClientContext,
  193. &RequestOT,
  194. AdtInfo,
  195. pSD,
  196. NULL,
  197. 0,
  198. pReplyOT,
  199. //&AuthzHandle
  200. NULL
  201. );
  202. if (!b)
  203. {
  204. printf("AuthzAccessCheck failed.\n");
  205. return GetLastError();
  206. }
  207. // else
  208. // {
  209. // AuthzFreeHandle( AuthzHandle );
  210. // }
  211. }
  212. }
  213. else
  214. {
  215. for (i = 0; i < NumAccessChecks; i ++)
  216. {
  217. b = AuthzAccessCheck(
  218. hAuthzClientContext,
  219. &Request,
  220. AdtInfo,
  221. pSD,
  222. NULL,
  223. 0,
  224. pReply,
  225. //&AuthzHandle
  226. NULL
  227. );
  228. if (!b)
  229. {
  230. printf("AuthzAccessCheck failed.\n");
  231. return GetLastError();
  232. }
  233. // else
  234. // {
  235. // AuthzFreeHandle( AuthzHandle );
  236. // }
  237. }
  238. }
  239. return NO_ERROR;
  240. }