kogunlowo123/terraform-aws-waf

GitHub: kogunlowo123/terraform-aws-waf

一个封装 AWS WAFv2 Web ACL 配置的 Terraform 模块,支持托管规则、速率限制、地理屏蔽、Bot 控制和日志记录。

Stars: 0 | Forks: 0

# terraform-aws-waf 用于创建和管理 AWS WAFv2 Web ACL 的 Terraform 模块,支持托管规则、速率限制、地理位置屏蔽、Bot Control、IP 集合、自定义规则和日志记录。 ## 架构 ``` flowchart TB Client[Client Request] --> CF[CloudFront / ALB / API GW] subgraph WAF["AWS WAFv2 Web ACL"] direction TB R1[Managed Rules: Common Rule Set] R2[Managed Rules: SQL Injection] R3[Managed Rules: Known Bad Inputs] R4[Bot Control] R5[Rate Limiting] R6[Geo-Blocking] R7[IP Allowlist / Blocklist] R8[Custom Rules] end CF --> WAF WAF -->|ALLOW| App[Application] WAF -->|BLOCK| Block[403 Forbidden] WAF -->|COUNT| Metrics[CloudWatch Metrics] subgraph Logging["WAF Logging"] WAF --> KF[Kinesis Firehose] WAF --> CWL[CloudWatch Logs] WAF --> S3L[S3 Bucket] end style WAF fill:#FF9900,color:#fff style Logging fill:#1A73E8,color:#fff style App fill:#3F8624,color:#fff style Block fill:#DD344C,color:#fff ``` ## 功能特性 - 支持可配置默认操作(允许/阻止)的 AWS WAFv2 Web ACL - 支持覆盖操作和规则排除的 AWS 托管规则组 - 支持 COMMON 或 TARGETED 检查级别的 AWS Bot Control - 支持可选范围缩小语句的基于速率的规则 - IP 集合引用规则(允许/阻止列表) - 基于国家的地理位置匹配规则 - 自定义规则(字节匹配、地理位置匹配、大小约束) - WAF 日志记录到 Kinesis Firehose、CloudWatch Logs 或 S3 - 未指定日志目标时自动创建 CloudWatch Log Group - 日志字段脱敏(URI 路径、查询字符串、标头) - Web ACL 与 ALB、API Gateway、AppSync 和 Cognito 关联 - 同时支持 REGIONAL 和 CLOUDFRONT 范围 ## 用法 ### 基础配置 ``` module "waf" { source = "github.com/kogunlowo123/terraform-aws-waf" name = "my-web-acl" description = "Web ACL with common protections" scope = "REGIONAL" managed_rule_groups = [ { name = "AWSManagedRulesCommonRuleSet" priority = 10 override_action = "none" excluded_rules = [] }, { name = "AWSManagedRulesKnownBadInputsRuleSet" priority = 20 override_action = "none" excluded_rules = [] } ] tags = { Environment = "production" } } ``` ### 带速率限制和地理位置屏蔽 ``` module "waf" { source = "github.com/kogunlowo123/terraform-aws-waf" name = "advanced-web-acl" default_action = "allow" managed_rule_groups = [ { name = "AWSManagedRulesCommonRuleSet" priority = 10 override_action = "none" excluded_rules = [] } ] rate_limit_rules = [ { name = "global-rate-limit" priority = 100 rate = 2000 action = "block" } ] geo_match_rules = [ { name = "block-countries" country_codes = ["RU", "CN", "KP"] action = "block" priority = 200 } ] enable_bot_control = true } ``` ### 带 IP 集合和日志记录 ``` module "waf" { source = "github.com/kogunlowo123/terraform-aws-waf" name = "full-web-acl" ip_set_rules = [ { name = "block-bad-ips" ip_set_arn = aws_wafv2_ip_set.blocked.arn action = "block" priority = 5 } ] enable_logging = true log_destination_arns = [aws_kinesis_firehose_delivery_stream.waf.arn] redacted_fields = [ { type = "single_header" name = "authorization" } ] resource_arns = [aws_lb.main.arn] } ``` ## 示例 - [基础配置](examples/basic/) - 带有托管规则的简单 Web ACL - [高级配置](examples/advanced/) - 速率限制、地理位置屏蔽和 IP 集合 - [完整配置](examples/complete/) - 包含 Bot Control、自定义规则和日志记录的所有功能 ## AWS 托管规则组 以下 AWS 托管规则组可与 `managed_rule_groups` 变量一起使用。请在 `name` 字段中使用 **Rule Group Name** 值。 ### 基线规则组 | Rule Group Name | Description | |---|---| | `AWSManagedRulesCommonRuleSet` | 核心规则集 (CRS),针对常见威胁(包括 OWASP Top 10)提供保护 | | `AWSManagedRulesAdminProtectionRuleSet` | 阻止对管理页面的外部访问 | | `AWSManagedRulesKnownBadInputsRuleSet` | 阻止已知恶意的请求模式(Log4j 等) | ### 用例特定规则组 | Rule Group Name | Description | |---|---| | `AWSManagedRulesSQLiRuleSet` | 针对 SQL 注入攻击的保护 | | `AWSManagedRulesLinuxRuleSet` | 阻止 Linux 特定的 LFI 攻击 | | `AWSManagedRulesUnixRuleSet` | 阻止 POSIX/类 POSIX OS 特定的 LFI 攻击 | | `AWSManagedRulesWindowsRuleSet` | 阻止 Windows 特定攻击(PowerShell 等) | | `AWSManagedRulesPHPRuleSet` | 阻止针对 PHP 应用程序的特定攻击 | | `AWSManagedRulesWordPressRuleSet` | 阻止针对 WordPress 站点的特定攻击 | ### IP 信誉规则组 | Rule Group Name | Description | |---|---| | `AWSManagedRulesAmazonIpReputationList` | 基于 Amazon 内部威胁情报阻止与机器人或威胁关联的 IP | | `AWSManagedRulesAnonymousIpList` | 阻止来自允许身份混淆服务(VPN、代理、Tor、托管提供商)的请求 | ### Bot Control 规则组 | Rule Group Name | Description | |---|---| | `AWSManagedRulesBotControlRuleSet` | 具有 COMMON 和 TARGETED 检查级别的托管 Bot 保护。通过 `enable_bot_control` 变量启用。 | ### ATP (账户接管防护) | Rule Group Name | Description | |---|---| | `AWSManagedRulesATPRuleSet` | 检测并阻止使用被盗凭据进行的账户接管尝试 | ### ACFP (账户创建欺诈防护) | Rule Group Name | Description | |---|---| | `AWSManagedRulesACFPRuleSet` | 检测并阻止欺诈性账户创建尝试 | ## 要求 | Name | Version | |---|---| | terraform | >= 1.0 | | aws | >= 4.0 | ## 提供商 | Name | Version | |---|---| | aws | >= 4.0 | ## 资源 | Name | Type | |---|---| | [aws_wafv2_web_acl.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl) | resource | | [aws_cloudwatch_log_group.waf](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | | [aws_wafv2_web_acl_logging_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_logging_configuration) | resource | | [aws_wafv2_web_acl_association.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl_association) | resource | | [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | | [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | ## 输入变量 | Name | Description | Type | Default | Required | |---|---|---|---|---| | name | WAFv2 Web ACL 的名称 | `string` | n/a | yes | | scope | WAF 的范围 (REGIONAL 或 CLOUDFRONT) | `string` | `"REGIONAL"` | no | | description | Web ACL 的友好描述 | `string` | `""` | no | | default_action | 默认操作 (allow 或 block) | `string` | `"allow"` | no | | managed_rule_groups | AWS 托管规则组列表 | `list(object)` | `[]` | no | | rate_limit_rules | 基于速率的规则列表 | `list(object)` | `[]` | no | | ip_set_rules | IP 集合引用规则列表 | `list(object)` | `[]` | no | | geo_match_rules | 地理位置匹配(地理位置屏蔽)规则列表 | `list(object)` | `[]` | no | | custom_rules | 自定义规则列表 | `list(object)` | `[]` | no | | enable_logging | 是否启用 WAF 日志记录 | `bool` | `true` | no | | log_destination_arns | 日志目标 ARN 列表 | `list(string)` | `[]` | no | | redacted_fields | 要从日志中脱敏的字段列表 | `list(object)` | `[]` | no | | resource_arns | 要与 Web ACL 关联的资源 ARN 列表 | `list(string)` | `[]` | no | | enable_bot_control | 是否启用 AWS Bot Control | `bool` | `false` | no | | bot_control_priority | Bot Control 规则组的优先级 | `number` | `50` | no | | bot_control_inspection_level | Bot Control 检查级别 (COMMON 或 TARGETED) | `string` | `"COMMON"` | no | | tags | 添加到所有资源的标签映射 | `map(string)` | `{}` | no | ## 输出 | Name | Description | |---|---| | web_acl_id | WAFv2 Web ACL 的 ID | | web_acl_arn | WAFv2 Web ACL 的 ARN | | web_acl_name | WAFv2 Web ACL 的名称 | | web_acl_capacity | Web ACL 使用的容量单位 | | web_acl_visibility_config | Web ACL 的可见性配置 | | logging_configuration_id | WAF 日志配置的 ID | | cloudwatch_log_group_arn | CloudWatch Log Group 的 ARN(如果自动创建) | | cloudwatch_log_group_name | CloudWatch Log Group 的名称(如果自动创建) | | web_acl_association_ids | 资源 ARN 到其 WAF 关联 ID 的映射 | ## 许可证 MIT Licensed. 详见 [LICENSE](LICENSE)。
标签:ALB, API Gateway, AppImage, AWS, Bot Control, CISA项目, CloudFront, CloudWatch, DPI, EC2, ECS, ETW劫持, IaC, IP黑白名单, Kinesis Firehose, Redis利用, S3, SQL注入防护, Terraform, WAF, WAFv2, Web应用防火墙, XSS防护, 合规, 地理封锁, 安全, 数据隐私, 日志记录, 机器人管理, 模块, 流量过滤, 漏洞探索, 特权提升, 网络安全, 自动化部署, 超时处理, 隐私保护