AWS Elastic Beanstalk 인스턴스가 원치 않는 호스트 이름에서 요청을 받고 있습니다. 이 호스트 이름에서 요청을 받지 않도록 Elastic Beanstalk 인스턴스를 보호하려면 어떻게 해야 합니까?
간략한 설명
Application Load Balancer를 사용하는 Elastic Beanstalk 환경에서는 AWS WAF를 사용자 지정 리소스로 사용하여 원치 않는 호스트 이름의 공격으로부터 인스턴스를 보호할 수 있습니다.
원치 않는 호스트 이름으로부터 Elastic Beanstalk 환경을 차단하려면 다음 섹션 중 하나의 단계를 완료하십시오.
- 하나의 호스트 이름 차단
- 여러 호스트 이름 차단
해결 방법
하나의 호스트 이름 차단
1. .ebextensions 디렉터리에서 waf.config 구성 파일을 생성합니다.
2. 다음 예제를 기반으로 waf.config 파일을 업데이트합니다. BlockedHost1를 Elastic Beanstalk 환경에서 차단하려는 호스트 이름으로 바꿉니다.
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" ]}`'
3. 2단계의 waf.config 파일로 Elastic Beanstalk 환경을 생성하거나 업데이트합니다.
중요: waf.config 파일을 Application Load Balancer가 없는 기존 Elastic Beanstalk 환경에서 실행하면 오류가 발생합니다. 오류가 발생하는 이유는 로드 밸런서 유형은 환경 생성 중에만 정의할 수 있기 때문입니다. 자세한 내용은 Application Load Balancer 구성을 참조하십시오. 블루/그린 배포로 로드 밸런서 유형을 변경할 수 있습니다.
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에서 구성된 호스트 이름으로 변경합니다. 명령에서 URL을 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를 사용하는 웹 액세스 제어 목록(웹 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
관련 정보
Elastic Beanstalk 환경 리소스 추가 및 사용자 지정