waf.tf 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. resource "aws_wafv2_ip_set" "blocked" {
  11. name = "blocked_ips"
  12. scope = "REGIONAL"
  13. ip_address_version = "IPV4"
  14. addresses = toset(concat(var.additional_blocked_ips, local.blocked_ips))
  15. }
  16. resource "aws_wafv2_ip_set" "allowed" {
  17. name = "allowed_ips"
  18. scope = "REGIONAL"
  19. ip_address_version = "IPV4"
  20. addresses = var.allowed_ips
  21. }
  22. resource "aws_wafv2_rule_group" "xdr_custom_rules" {
  23. name = "xdr_custom_rules"
  24. scope = "REGIONAL"
  25. capacity = 1
  26. # Note, there is visibilty config for the group and for the rule
  27. visibility_config {
  28. cloudwatch_metrics_enabled = true
  29. metric_name = "xdr_custom_rules"
  30. sampled_requests_enabled = true
  31. }
  32. rule {
  33. name = "Block_Nonpermitted_Countries"
  34. priority = 100
  35. action {
  36. block {}
  37. }
  38. statement {
  39. not_statement {
  40. statement {
  41. geo_match_statement {
  42. country_codes = [
  43. "US",
  44. "DE",
  45. ]
  46. }
  47. }
  48. }
  49. }
  50. visibility_config {
  51. cloudwatch_metrics_enabled = true
  52. metric_name = "Block_Nonpermitted_Countries"
  53. sampled_requests_enabled = true
  54. }
  55. }
  56. # Add additional custom rules here
  57. }
  58. module "wafv2" {
  59. source = "trussworks/wafv2/aws"
  60. version = "= 2.4.0"
  61. name = replace(var.fqdns[0], ".", "_")
  62. scope = "REGIONAL"
  63. alb_arn = var.resource_arn
  64. associate_alb = true
  65. default_action = "block" # Note: The final action is actually to 'allow', provided the host header is correct
  66. filtered_header_rule = {
  67. header_types = var.fqdns
  68. header_value = "host"
  69. priority = 900
  70. action = "allow"
  71. }
  72. # IP based rules are processed first.
  73. # If an IP is blocked, it is blocked no matter what.
  74. # If an IP is allowed, it is allowed only if it's not blocked, but no other WAF rules are processed.
  75. ip_sets_rule = [
  76. {
  77. name = "blocked_ips"
  78. action = "block"
  79. priority = 10
  80. ip_set_arn = aws_wafv2_ip_set.blocked.arn
  81. },
  82. {
  83. name = "allowed_ips"
  84. action = "allow"
  85. priority = 20
  86. ip_set_arn = aws_wafv2_ip_set.allowed.arn
  87. }
  88. ]
  89. # Custom Rules are defined above
  90. group_rules = [
  91. {
  92. name = aws_wafv2_rule_group.xdr_custom_rules.name
  93. arn = aws_wafv2_rule_group.xdr_custom_rules.arn
  94. priority = 100
  95. override_action = "none"
  96. excluded_rules = []
  97. }
  98. ]
  99. # 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
  100. ip_rate_based_rule = {
  101. name = "Rate_Limit"
  102. priority = 200
  103. limit = 900 # 900 requests per 5 minutes= 3 requests/second (sustained for 5 minutes)
  104. action = "block"
  105. }
  106. # AWS managed rulesets
  107. # Baseline was from trussworks/wafv2/aws, but copied here to be customized for our use and renumbered.
  108. managed_rules = [
  109. {
  110. "excluded_rules": [],
  111. "name": "AWSManagedRulesCommonRuleSet",
  112. "override_action": "none",
  113. "priority": 510
  114. },
  115. {
  116. "excluded_rules": [],
  117. "name": "AWSManagedRulesAmazonIpReputationList",
  118. "override_action": "none",
  119. "priority": 520
  120. },
  121. {
  122. "excluded_rules": [],
  123. "name": "AWSManagedRulesKnownBadInputsRuleSet",
  124. "override_action": "none",
  125. "priority": 530
  126. },
  127. {
  128. "excluded_rules": [],
  129. "name": "AWSManagedRulesSQLiRuleSet",
  130. "override_action": "none",
  131. "priority": 540
  132. },
  133. {
  134. "excluded_rules": [],
  135. "name": "AWSManagedRulesLinuxRuleSet",
  136. "override_action": "none",
  137. "priority": 550
  138. },
  139. {
  140. "excluded_rules": [],
  141. "name": "AWSManagedRulesUnixRuleSet",
  142. "override_action": "none",
  143. "priority": 560
  144. }
  145. ]
  146. tags = var.tags
  147. }
  148. resource "aws_wafv2_web_acl_logging_configuration" "waf_logs" {
  149. log_destination_configs = [ "arn:${var.aws_partition}:firehose:${var.aws_region}:${var.aws_account_id}:deliverystream/aws-waf-logs-splunk" ]
  150. resource_arn = module.wafv2.web_acl_id
  151. # logging_filter {
  152. # default_behavior = "KEEP"
  153. #
  154. # filter {
  155. # behavior = "DROP"
  156. #
  157. # condition {
  158. # action_condition {
  159. # action = "COUNT"
  160. # }
  161. # }
  162. #
  163. # condition {
  164. # label_name_condition {
  165. # label_name = "awswaf:111122223333:rulegroup:testRules:LabelNameZ"
  166. # }
  167. # }
  168. #
  169. # requirement = "MEETS_ALL"
  170. # }
  171. #
  172. # filter {
  173. # behavior = "KEEP"
  174. #
  175. # condition {
  176. # action_condition {
  177. # action = "ALLOW"
  178. # }
  179. # }
  180. #
  181. # requirement = "MEETS_ANY"
  182. # }
  183. # }
  184. }