waf.tf 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # trussworks/wafv2/aws has a basic WAF with the AWS Managed Ruleset
  2. # See https://registry.terraform.io/modules/trussworks/wafv2/aws/latest
  3. #
  4. # Attempted to add some sane defaults so we can customize as needed
  5. #
  6. # IMPORTANT NOTES:
  7. # An 'Allow' action stops processing immediately. Avoid them!
  8. # Goals:
  9. # - US IPs only - https://docs.aws.amazon.com/waf/latest/developerguide/waf-rule-statement-type-geo-match.html
  10. locals {
  11. waf_name = replace(var.fqdns[0], ".", "_")
  12. }
  13. resource "aws_wafv2_ip_set" "blocked" {
  14. name = "${local.waf_name}_blocked_ips"
  15. scope = "REGIONAL"
  16. ip_address_version = "IPV4"
  17. addresses = toset(concat(var.additional_blocked_ips, local.blocked_ips))
  18. lifecycle {
  19. create_before_destroy = true
  20. }
  21. }
  22. resource "aws_wafv2_ip_set" "allowed" {
  23. name = "${local.waf_name}_allowed_ips"
  24. scope = "REGIONAL"
  25. ip_address_version = "IPV4"
  26. addresses = var.allowed_ips
  27. lifecycle {
  28. create_before_destroy = true
  29. }
  30. }
  31. resource "aws_wafv2_rule_group" "xdr_custom_rules" {
  32. name = "${local.waf_name}_xdr_custom_rules"
  33. scope = "REGIONAL"
  34. capacity = 1
  35. # Note, there is visibilty config for the group and for the rule
  36. visibility_config {
  37. cloudwatch_metrics_enabled = true
  38. metric_name = "${local.waf_name}_xdr_custom_rules"
  39. #metric_name = "xdr_custom_rules"
  40. sampled_requests_enabled = true
  41. }
  42. rule {
  43. name = "Block_Nonpermitted_Countries"
  44. priority = 100
  45. action {
  46. block {}
  47. }
  48. statement {
  49. not_statement {
  50. statement {
  51. geo_match_statement {
  52. country_codes = [
  53. "US",
  54. "DE",
  55. ]
  56. }
  57. }
  58. }
  59. }
  60. visibility_config {
  61. cloudwatch_metrics_enabled = true
  62. metric_name = "Block_Nonpermitted_Countries"
  63. sampled_requests_enabled = true
  64. }
  65. }
  66. # Add additional custom rules here
  67. lifecycle {
  68. create_before_destroy = true
  69. }
  70. }
  71. module "wafv2" {
  72. source = "trussworks/wafv2/aws"
  73. version = "= 2.4.0"
  74. name = local.waf_name
  75. scope = "REGIONAL"
  76. alb_arn = var.resource_arn
  77. associate_alb = true
  78. default_action = "block" # Note: The final action is actually to 'allow', provided the host header is correct
  79. filtered_header_rule = {
  80. header_types = var.fqdns
  81. header_value = "host"
  82. priority = 900
  83. action = "allow"
  84. }
  85. # IP based rules are processed first.
  86. # If an IP is blocked, it is blocked no matter what.
  87. # If an IP is allowed, it is allowed only if it's not blocked, but no other WAF rules are processed.
  88. ip_sets_rule = [
  89. {
  90. name = "blocked_ips"
  91. action = "block"
  92. priority = 10
  93. ip_set_arn = aws_wafv2_ip_set.blocked.arn
  94. },
  95. {
  96. name = "allowed_ips"
  97. action = "allow"
  98. priority = 20
  99. ip_set_arn = aws_wafv2_ip_set.allowed.arn
  100. }
  101. ]
  102. # Custom Rules are defined above
  103. group_rules = [
  104. {
  105. name = aws_wafv2_rule_group.xdr_custom_rules.name
  106. arn = aws_wafv2_rule_group.xdr_custom_rules.arn
  107. priority = 100
  108. override_action = "none"
  109. excluded_rules = []
  110. }
  111. ]
  112. # A rate-based rule tracks the rate of requests for each originating IP address, and triggers the rule action when the rate exceeds a limit that you specify on the number of requests in any 5-minute time span
  113. # Because of zscalar, this is completely ineffective for us.
  114. #ip_rate_based_rule = {
  115. # name = "Rate_Limit"
  116. # priority = 200
  117. # limit = 3000 # 6000 requests per 5 minutes= 10 requests/second (sustained for 5 minutes)
  118. # action = "block"
  119. #}
  120. # AWS managed rulesets
  121. # Baseline was from trussworks/wafv2/aws, but copied here to be customized for our use and renumbered.
  122. managed_rules = [
  123. {
  124. "excluded_rules": var.excluded_rules_AWSManagedRulesCommonRuleSet,
  125. "name": "AWSManagedRulesCommonRuleSet",
  126. "override_action": "none",
  127. "priority": 510
  128. },
  129. {
  130. "excluded_rules": var.excluded_rules_AWSManagedRulesAmazonIpReputationList,
  131. "name": "AWSManagedRulesAmazonIpReputationList",
  132. "override_action": "none",
  133. "priority": 520
  134. },
  135. {
  136. "excluded_rules": var.excluded_rules_AWSManagedRulesKnownBadInputsRuleSet,
  137. "name": "AWSManagedRulesKnownBadInputsRuleSet",
  138. "override_action": "none",
  139. "priority": 530
  140. },
  141. {
  142. "excluded_rules": var.excluded_rules_AWSManagedRulesSQLiRuleSet,
  143. "name": "AWSManagedRulesSQLiRuleSet",
  144. "override_action": "none",
  145. "priority": 540
  146. },
  147. {
  148. "excluded_rules": var.excluded_rules_AWSManagedRulesLinuxRuleSet,
  149. "name": "AWSManagedRulesLinuxRuleSet",
  150. "override_action": "none",
  151. "priority": 550
  152. },
  153. {
  154. "excluded_rules": var.excluded_rules_AWSManagedRulesUnixRuleSet,
  155. "name": "AWSManagedRulesUnixRuleSet",
  156. "override_action": "none",
  157. "priority": 560
  158. }
  159. ]
  160. tags = var.tags
  161. }
  162. resource "aws_wafv2_web_acl_logging_configuration" "waf_logs" {
  163. log_destination_configs = [ "arn:${var.aws_partition}:firehose:${var.aws_region}:${var.aws_account_id}:deliverystream/aws-waf-logs-splunk" ]
  164. resource_arn = module.wafv2.web_acl_id
  165. # logging_filter {
  166. # default_behavior = "KEEP"
  167. #
  168. # filter {
  169. # behavior = "DROP"
  170. #
  171. # condition {
  172. # action_condition {
  173. # action = "COUNT"
  174. # }
  175. # }
  176. #
  177. # condition {
  178. # label_name_condition {
  179. # label_name = "awswaf:111122223333:rulegroup:testRules:LabelNameZ"
  180. # }
  181. # }
  182. #
  183. # requirement = "MEETS_ALL"
  184. # }
  185. #
  186. # filter {
  187. # behavior = "KEEP"
  188. #
  189. # condition {
  190. # action_condition {
  191. # action = "ALLOW"
  192. # }
  193. # }
  194. #
  195. # requirement = "MEETS_ANY"
  196. # }
  197. # }
  198. }