fork download
  1. #include <windows.h>
  2. #include <fwpmu.h>
  3. #include <accctrl.h>
  4. #include <aclapi.h>
  5. #include <stdio.h>
  6.  
  7. #pragma comment (lib, "fwpuclnt.lib")
  8. #pragma comment (lib, "advapi32.lib")
  9.  
  10. #define SESSION_NAME L"SDK Examples"
  11.  
  12. #define EXIT_ON_ERROR(fnName) \
  13.   if (result != ERROR_SUCCESS) \
  14.   { \
  15.   printf(#fnName " = 0x%08X\n", result); \
  16.   goto CLEANUP; \
  17.   }
  18.  
  19. DWORD FilterByUserAndApp(
  20. __in HANDLE engine,
  21. __in PCWSTR filterName,
  22. __in_opt const GUID* providerKey,
  23. __in const GUID* layerKey,
  24. __in_opt const GUID* subLayerKey,
  25. __in_opt PCWSTR userName,
  26. __in_opt PCWSTR appPath,
  27. __in FWP_ACTION_TYPE actionType,
  28. __out_opt UINT64* filterId
  29. )
  30. {
  31. DWORD result = ERROR_SUCCESS;
  32. FWPM_FILTER_CONDITION0 conds[2];
  33. UINT32 numConds = 0;
  34. EXPLICIT_ACCESS_W access;
  35. ULONG sdLen;
  36. PSECURITY_DESCRIPTOR sd = NULL;
  37. FWP_BYTE_BLOB sdBlob, *appBlob = NULL;
  38. FWPM_FILTER0 filter;
  39.  
  40. // Add an FWPM_CONDITION_ALE_USER_ID condition if requested.
  41. if (userName != NULL)
  42. {
  43. // When evaluating SECURITY_DESCRIPTOR conditions, the filter engine
  44. // checks for FWP_ACTRL_MATCH_FILTER access. If the DACL grants access,
  45. // it does not mean that the traffic is allowed; it just means that the
  46. // condition evaluates to true. Likewise if it denies access, the
  47. // condition evaluates to false.
  48. BuildExplicitAccessWithNameW(
  49. &access,
  50. (PWSTR)userName,
  51. FWP_ACTRL_MATCH_FILTER,
  52. GRANT_ACCESS,
  53. 0
  54. );
  55.  
  56. result = BuildSecurityDescriptorW(
  57. NULL,
  58. NULL,
  59. 1,
  60. &access,
  61. 0,
  62. NULL,
  63. NULL,
  64. &sdLen,
  65. &sd
  66. );
  67. EXIT_ON_ERROR(BuildSecurityDescriptorW);
  68.  
  69. // Security descriptors must be in self-relative form (i.e., contiguous).
  70. // The security descriptor returned by BuildSecurityDescriptorW is
  71. // already self-relative, but if you're using another mechanism to build
  72. // the descriptor, you may have to convert it. See MakeSelfRelativeSD for
  73. // details.
  74. sdBlob.size = sdLen;
  75. sdBlob.data = (UINT8*)sd;
  76.  
  77. conds[numConds].fieldKey = FWPM_CONDITION_ALE_USER_ID;
  78. conds[numConds].matchType = FWP_MATCH_EQUAL;
  79. conds[numConds].conditionValue.type = FWP_SECURITY_DESCRIPTOR_TYPE;
  80. conds[numConds].conditionValue.sd = &sdBlob;
  81. ++numConds;
  82. }
  83.  
  84. // Add an FWPM_CONDITION_ALE_APP_ID condition if requested.
  85. if (appPath != NULL)
  86. {
  87. // appPath must be a fully-qualified file name, and the file must
  88. // exist on the local machine.
  89. result = FwpmGetAppIdFromFileName0(appPath, &appBlob);
  90. EXIT_ON_ERROR(FwpmGetAppIdFromFileName0);
  91.  
  92. conds[numConds].fieldKey = FWPM_CONDITION_ALE_APP_ID;
  93. conds[numConds].matchType = FWP_MATCH_EQUAL;
  94. conds[numConds].conditionValue.type = FWP_BYTE_BLOB_TYPE;
  95. conds[numConds].conditionValue.byteBlob = appBlob;
  96. ++numConds;
  97. }
  98.  
  99. memset(&filter, 0, sizeof(filter));
  100. // For MUI compatibility, object names should be indirect strings. See
  101. // SHLoadIndirectString for details.
  102. filter.displayData.name = (PWSTR)filterName;
  103. // Link all objects to our provider. When multiple providers are installed
  104. // on a computer, this makes it easy to determine who added what.
  105. filter.providerKey = (GUID*)providerKey;
  106. filter.layerKey = *layerKey;
  107. // Generally, it's best to add filters to our own sublayer, so we don't have
  108. // to worry about being overridden by filters added by another provider.
  109. if (subLayerKey != NULL)
  110. {
  111. filter.subLayerKey = *subLayerKey;
  112. }
  113. filter.numFilterConditions = numConds;
  114. if (numConds > 0)
  115. {
  116. filter.filterCondition = conds;
  117. }
  118. filter.action.type = actionType;
  119.  
  120. result = FwpmFilterAdd0(engine, &filter, NULL, filterId);
  121. EXIT_ON_ERROR(FwpmFilterAdd0);
  122.  
  123. CLEANUP:
  124. FwpmFreeMemory0((void**)&appBlob);
  125. LocalFree(sd);
  126. return result;
  127. }
  128.  
  129.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: windows.h: No such file or directory
compilation terminated.
stdout
Standard output is empty