이번 주 AWS를 사용하면서 겪은 실패 사례

보안을 위해 server response의 header중 server signature를 변경해보자

개요

  • REST API나 Web Application의 경우 HTTP응답에는 Header가 존재
  • Header에는 여러 정보가 포함되어 있으며, 그 중 Server Signature는 서버의 정보를 알려줌
    • Server: nginx/1.10.2
    • Server: Apache-Coyote/1.1
    • 위 처럼 서버의 종류와 버전이 표시됨
  • 이는 해당 서버의 보안 취약점을 알아내기 쉽게 만들기 때문에 보안상 숨기는게 좋음
  • EC2는 ssh로 접근하여 수정
  • ElasticBeanstalk를 통해 생성되는 EC2는 어떻게 수정 할 수 있을까?

nginx의 Server Signature 숨기는 법

  • Google에서 검색해보면 다양하게 안내하고 있음 : nginx server signature off
  • 위 링크들을 정리해보면
    • /etc/nginx/nginx.conf 파일을 찾아서 http 아래에
      • server_tokens off; 를 추가
      • 이것으로 서버 버전을 숨길 수 있음
    • 추가로 서버종류를 숨기거나 바꿀 수 도 있는데
      • more_set_headers ‘Server: NONE-OF-YOUR-BUISINESS’; 로 서버 이름을 변경
      • more_clear_headers ‘Server’; 로 서버 정보 자체를 삭제
      • 이 부분은 안된다는 글도 본 것 같음
    • 그리고 서버 서비스를 재시작

ElasticBeanstalk에서 nginx 설정 변경

  • ElasticBeanstalk는 reserved proxy로 nginx를 지원
  • .ebextension 폴더에 설정 파일을 넣어두면 Deployment시 서버에 반영함
  • 공식 가이드(한글)에서 다음과 같이 안내
    • .ebextensions/nginx/nginx.conf 을 추가하면 구성을 완전히 재정의 함
      • 처음엔 nginx.conf 파일에서 설정 일부만 작성하면 해당 설정을 override될 줄 알았는데 아니었음
      • 다음과 같은 에러 발생
        [2017-11-13T04:55:21.342Z] ERROR [5197]  : Command execution failed: Activity failed. (ElasticBeanstalk::ActivityFatalError)
        caused by: Executing: /opt/elasticbeanstalk/bin/log-conf -n nginx -l'/var/log/nginx/*'
        .
        Nginx configuration detected in the '.ebextensions/nginx' directory. AWS Elastic Beanstalk will no longer manage the Nginx configuration for this environment.
        Executing: /usr/sbin/nginx -t -c /var/elasticbeanstalk/staging/nginx/nginx.conf
        nginx: [emerg] unknown directive "more_set_headers" in /var/elasticbeanstalk/staging/nginx/nginx.conf:2
        nginx: configuration file /var/elasticbeanstalk/staging/nginx/nginx.conf test failed
        Failed to execute '/usr/sbin/nginx -t -c /var/elasticbeanstalk/staging/nginx/nginx.conf'
        Failed to execute '/usr/sbin/nginx -t -c /var/elasticbeanstalk/staging/nginx/nginx.conf' (ElasticBeanstalk::ExternalInvocationError)
        caused by: Executing: /opt/elasticbeanstalk/bin/log-conf -n nginx -l'/var/log/nginx/*'
        
      • 아마 nginx.conf 파일의 구성이 완벽하지 못해 load가 되지 않는 다는 뜻인듯
    • .ebextensions/nginx/conf.d/myconf.conf 을 추가하면 해당 설정을 override함
      • AWS Blog 설명 참조
      • 여기서 http 아래에 server_tokens off;를 입력할 필요 없이, 바로 본문에 작성하면 됨
      • X: http { server_tokens off; }
      • O: server_tokens off;

Apache Tomcat의 Server Signature 숨기는 법

ElasticBeanstalk에서 Apache 설정 변경

  • nginx와 달리 Apache는 ebextension설정이 훨씬 복잡
    • config라는 확장자를 가진 YAML형식의 파일로 설정 수행
    • files로 필요한 파일들을 EC2로 복사(또는 생성)
    • container_commands로 EC2에서 shell script 수행
  • 오래된 글(2012년)이지만 Amazon Linux에서 동작하지 않는다는 글도 존재
  • ElasticBeasntalk가 Virtual host를 생성하기 떄문이라는 설명
    • .ebextensions/httpd/conf.d/elasticbeanstalk 폴더 아래에 00_application.conf 파일을 만들어 설정해야 한다고 함
    • 전반적으로 너무 복잡해지기 시작해서 어려움…
  • Tomcat설정을 변경하면 간단하다는 글 발견
    • Response header에서 Server를 삭제할 수 없음
    • Tomcat의 server.xml에서 Connector에 server property를 입력하면 그걸로 표시됨
  • .ebextensions/에서 config파일을 생성
      container_commands:
      replace-config:
        command: cp .ebextensions/server.xml /etc/tomcat7/server.xml
    
    • shell script로 server.xml파일을 덮어쓰기

Server Signature를 숨긴 것의 의미

이렇게 숨긴다고 하더라도 다양한 방법(에러 페이지 호출 등)으로 서버정보를 알아 낼 수 있기 때문에, 실질적인 보안강화 요소가 아님.
필요한 port만 개방하고, 서버 보안 패치를 하는게 중요할 듯

기타 참고사항

  • Private Subnet의 EC2에 붙기
    https://aws.amazon.com/ko/blogs/security/securely-connect-to-linux-instances-running-in-a-private-amazon-vpc/
  • Putty로 EC2에 붙기
    https://linuxacademy.com/howtoguides/posts/show/topic/17385-use-putty-to-access-ec2-linux-instances-via-ssh-from-windows
    https://cpuu.postype.com/post/30065