Complete a 3 Question Survey and Earn a re:Post Badge
Help improve AWS Support Official channel in re:Post and share your experience - complete a quick three-question survey to earn a re:Post badge!
如何保护我的 Elastic Beanstalk 环境免受来自已知不需要主机的攻击?
我的 AWS Elastic Beanstalk 实例收到来自不需要主机名的请求。
解决方法
在带有应用程序负载均衡器的 Elastic Beanstalk 环境中,使用 AWS WAF 作为自定义资源来保护您的实例免受攻击。您可以选择阻止一个主机名或多个主机名。
阻止一个主机名
1. 在您的 .ebextensions 目录中创建一个 waf.config 配置文件。
2. 根据此示例更新您的 waf.config 文件。
option_settings: aws:elasticbeanstalk:environment: LoadBalancerType: application aws:elasticbeanstalk:customoption: BlockedHost1: 'exampletoblock.com' Resources: BlockedHostnames: Type: "AWS::WAFv2::RegexPatternSet" Properties: Description: 'List of Hostnames to be block by WebACL' Name: BlockedHostsSet RegularExpressionList: - { "Fn::GetOptionSetting" : {"OptionName" : "BlockedHost1" }} Scope: REGIONAL WafAcl: Type: "AWS::WAFv2::WebACL" Properties: Description: "Web ACL to Block requests from unknown hosts on AWSEBV2LoadBalancer" Name: "BlockHostACL" Scope: REGIONAL DefaultAction: Allow: {} VisibilityConfig: SampledRequestsEnabled: true CloudWatchMetricsEnabled: true MetricName: BlockHostACLMetric Rules: - Name: BlockedHostsRule Priority: 1 Action: Block: {} VisibilityConfig: SampledRequestsEnabled: true CloudWatchMetricsEnabled: true MetricName: UnknownHostRule1 Statement: RegexPatternSetReferenceStatement: Arn: '`{ "Fn::GetAtt" : ["BlockedHostnames", "Arn" ]}`' FieldToMatch: SingleHeader: Name: Host TextTransformations: - Priority: 0 Type: NONE WebACLAssociation: Type: AWS::WAFv2::WebACLAssociation Properties: ResourceArn: '`{ "Ref" : "AWSEBV2LoadBalancer" }`' WebACLArn: '`{ "Fn::GetAtt" : ["WafAcl", "Arn" ]}`'
**注意:**将 BlockedHost1 替换为要在 Elastic Beanstalk 环境中阻止的主机名。
3. 使用上一步中的 waf.config 文件创建或更新 Elastic Beanstalk 环境。
**重要事项:**如果在没有应用程序负载均衡器的现有 Elastic Beanstalk 环境上运行 waf.config 文件,则会出现错误。您收到错误,是因为您只能在创建环境期间定义负载均衡器类型。使用蓝绿部署更改您的负载均衡器类型。有关更多信息,请参阅配置应用程序负载均衡器。
4. 要确认阻止 BlockedHost1 向您的 Elastic Beanstalk 环境发送请求,请先打开终端。然后,要模拟源自 exampletoblock.com 的请求,请运行以下命令:
$ curl -I -H 'host: exampletoblock.com' http://YOUR-ENV-NAME.YOUR-ENV-ID.AWS-REGION.elasticbeanstalk.com
**注意:**将 exampletoblock.com 替换为在 waf.config 上配置的要阻止的主机名。将 http://YOUR-ENV-NAME.YOUR-ENV-ID.AWS-REGION.elasticbeanstalk.com 替换为您的 Elastic Beanstalk 环境 URL。
如果主机名被阻止,则您会收到与以下示例类似的输出:
> HTTP/1.1 403 Forbidden Server: awselb/2.0 Date: Mon, 20 Apr 2020 17:31:14 GMT Content-Type: text/html Content-Length: 134 Connection: keep-alive
5. 要模拟正常请求,请运行以下命令:
$ curl -I http://ENV-NAME.ENV-ID.eu-west-1.elasticbeanstalk.com
如果请求成功,您会看到成功的 200 状态代码。您会收到类似于以下内容的输出:
> HTTP/1.1 200 OK Date: Mon, 20 Apr 2020 17:38:04 GMT Content-Type: text/html Content-Length: 3352 Connection: keep-alive Server: nginx/1.16.1
阻止多个主机名
要阻止多个主机名,请将主机名添加到使用 RegexPatternSet 的 Web 访问控制列表(WEB ACL)中。在您的 waf.config 文件中,在 RegularExpressionList 中添加其他主机名作为自定义选项:
option_settings: aws:elasticbeanstalk:environment: LoadBalancerType: application aws:elasticbeanstalk:customoption: BlockedHost1: 'exampletoblock.com' BlockedHost2: 'anothertoblock.com' Resources: BlockedHostnames: Type: "AWS::WAFv2::RegexPatternSet" Properties: Description: 'List of Hostnames to be block by WebACL' Name: BlockedHostsSet RegularExpressionList: - { "Fn::GetOptionSetting" : {"OptionName" : "BlockedHost1" }} - { "Fn::GetOptionSetting" : {"OptionName" : "BlockedHost2" }} Scope: REGIONAL
相关信息
