waf.tf 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. # zscalar needs high rates
  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": [
  125. "SizeRestrictions_BODY" # Breaks too many things
  126. ],
  127. "name": "AWSManagedRulesCommonRuleSet",
  128. "override_action": "none",
  129. "priority": 510
  130. },
  131. {
  132. "excluded_rules": [],
  133. "name": "AWSManagedRulesAmazonIpReputationList",
  134. "override_action": "none",
  135. "priority": 520
  136. },
  137. {
  138. "excluded_rules": [],
  139. "name": "AWSManagedRulesKnownBadInputsRuleSet",
  140. "override_action": "none",
  141. "priority": 530
  142. },
  143. {
  144. "excluded_rules": [],
  145. "name": "AWSManagedRulesSQLiRuleSet",
  146. "override_action": "none",
  147. "priority": 540
  148. },
  149. {
  150. "excluded_rules": [],
  151. "name": "AWSManagedRulesLinuxRuleSet",
  152. "override_action": "none",
  153. "priority": 550
  154. },
  155. {
  156. "excluded_rules": [],
  157. "name": "AWSManagedRulesUnixRuleSet",
  158. "override_action": "none",
  159. "priority": 560
  160. }
  161. ]
  162. tags = var.tags
  163. }
  164. resource "aws_wafv2_web_acl_logging_configuration" "waf_logs" {
  165. log_destination_configs = [ "arn:${var.aws_partition}:firehose:${var.aws_region}:${var.aws_account_id}:deliverystream/aws-waf-logs-splunk" ]
  166. resource_arn = module.wafv2.web_acl_id
  167. # logging_filter {
  168. # default_behavior = "KEEP"
  169. #
  170. # filter {
  171. # behavior = "DROP"
  172. #
  173. # condition {
  174. # action_condition {
  175. # action = "COUNT"
  176. # }
  177. # }
  178. #
  179. # condition {
  180. # label_name_condition {
  181. # label_name = "awswaf:111122223333:rulegroup:testRules:LabelNameZ"
  182. # }
  183. # }
  184. #
  185. # requirement = "MEETS_ALL"
  186. # }
  187. #
  188. # filter {
  189. # behavior = "KEEP"
  190. #
  191. # condition {
  192. # action_condition {
  193. # action = "ALLOW"
  194. # }
  195. # }
  196. #
  197. # requirement = "MEETS_ANY"
  198. # }
  199. # }
  200. }