SSL/TLS
SSL(Secure Sockets Layer) 및 TLS(Transport Layer Security)는 통신 세션의 데이터를 암호화하여 인터넷을 통한 통신이 보안되게 하는 프로토콜
- SSL
- 여러 버전의 SSL이 발표되었고, SSL 3.0이 마지막 버전
- Netscape에 의해 1990년대 초에 개발
- TLS
- SSL 3.0을 기반으로 IETF에 의해 개발되었고, 사실상 SSL의 후속 버전
- SSL에 비해 보안 강화 및 다양한 향상 사항이 포함
정리
SSL과 TLS는 주로 HTTPS(보안 HTTP) 연결에서 사용되며, 이를 통해 웹 사이트와 사용자 간의 데이터가 보안되게 전송
현재 모든 SSL 인증서가 더 이상 사용되지 않습니다. TLS 인증서가 업계 표준
사용하는 이유
HTTP 프로토콜의 취약점
HTTP로 모든 통신을 하게되면 브라우저에서 전송된 정보를 제3자가 가로채고 읽을 수 있는 문제가 발생합니다.(중간자 공격)
해결방법
HTTP 통신에 또 다른 보안 계층을 추가해 통신에 데이터를 암호화해 탈취를 방어합니다. HTTPS는 HTTP 프로토콜에 TLS 프토콜이 결합됩니다.
HTTPS 핸드쉐이크 과정
- 사용자 브라우저의 주소 표시줄에 https:// URL 형식을 입력하여 HTTPS 웹 사이트를 방문합니다.
- 브라우저는 서버의 SSL 인증서를 요청하여 사이트의 신뢰성을 검증하려고 시도합니다.
- 서버는 퍼블릭 키가 포함된 SSL 인증서를 회신으로 전송합니다.
- 웹 사이트의 SSL 인증서는 서버 아이덴티티를 증명합니다. 브라우저에서 인증되면, 브라우저가 퍼블릭 키를 사용하여 비밀 세션 키가 포함된 메시지를 암호화하고 전송합니다.
- 웹 서버는 프라이빗 키를 사용하여 메시지를 해독하고 세션 키를 검색합니다. 그런 다음, 세션 키를 암호화하고 브라우저에 승인 메시지를 전송합니다.
- 이제 브라우저와 웹 서버 모두 동일한 세션 키를 사용하여 메시지를 안전하게 교환하도록 전환합니다.
적용 과정 설명
Nginx에 TLS를 적용하는 방법
⛔ SSL/TLS 인증서를 발급받고 Nginx에 설정을 변경해 인증서를 설치한다.
Let's Encrypt
- 무료로 공개적인 SSL/TLS 인증서를 제공하는 Certificate Authority (CA)입니다.
Certbot
- Let's Encrypt의 인증서의 발급, 갱신, 설치 등의 프로세스를 자동화하는 클라이언트
- Certbot는 인증서를 자동으로 발급,설치,갱신하는 클라이언트 중 하나
두 도구를 함께 사용하는 이유
- Certbot을 사용하면 Let's Encrypt 인증서의 발급, 설치, 그리고 갱신이 자동화되어 편함
- Let's Encrypt는 무료 인증서를 제공하며, Certbot은 이를 쉽게 설치하고 관리할 수 있는 무료 도구
Certbot 설정 전 nginx.conf (리버스 프록시만 적용된 상태)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name bbacklog.site; // 도메인 주소
location / {
proxy_pass http://52.79.126.226:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Certbot 설치 과정
apt update
//certbot 설치 명령어
apt install certbot python3-certbot-nginx -y
// 인증서 획득 명령어
certbot --nginx
// 이메일 입력 example@email.com
// 서비스 이용 약관 Y
// 광고 동의 N
nginx -s reload
// 이후 nginx.conf 확인후 적용됐는지 확인
https가 자동 적용된 nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
server_name bbacklog.site; // 도메인네임
location / {
proxy_pass http://52.79.126.226:8080; // 전달할곳 nginx와 같은곳이라면 localhost
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/bbacklog.site/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/bbacklog.site/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = bbacklog.site) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name bbacklog.site;
return 404; # managed by Certbot
}}
장, 단점
TLS/SSL로 접근할 때의 장점과 단점
장점
- 암호화: 데이터는 클라이언트와 서버 간에 전송될 때 암호화되므로 중간자 공격에서 보호됨
- 데이터 무결성: 데이터가 변조되지 않고 전송되었는지 확인이 가능
단점
- 초기 연결에 대한 성능 지연: TLS 핸드쉐이크는 추가적인 라운드 트립을 필요로 하므로 초기 연결 설정에 약간의 지연이 발생
- 하지만 아주 미비함. 최신 버전에서는 최적화 기술을 사용해 체감되지 않음
정리
💡 `Let's Encrypt`는 인증서를 발급하는 기관 그리고 `Certbot`은 인증서의 발급과 갱신을 자동화하기 위한 클라이언트
트러블 슈팅
Question
- Nginx에 TLS를 적용하면 사용자가 접근할 때 주소가 https:// 로 고정되는가?
- 결론은 X Nginx에 TLS를 적용하면 사용자가 https:// 를 사용해 보안성을 높여 이용할 수 있으나, 단순 적용으로 TLS를 적용했다고 고정되지 않음. http:// 역시 사용이 가능하며 https:// 를 강제하고 싶다면 http 요청을 https로 리다이렉트하게 설정하면 됨.
- Nginx에 TLS을 적용했을 때 데이터가 변조되지 않았는지 확인은 어떻게 하는가?만약 데이터가 변조되면 TLS 계층에서 감지하고 패킷은 거부됨. 즉 Nginx의 TLS 계층에서 변조를 감지하면 더 Nginx에 의해 더 패킷을 전달받지 않음.
- 변조 여부는 HMAC (해시 기반 메시지 인증 코드) 또는 AEAD (인증 암호화) 방식 등의 메시지 인증 코드 (MAC)를 사용해 검증
참고자료
https://velog.io/@juhyeon1114/certbot-설치-사용법-ubuntu
https://jcdgods.tistory.com/475
https://tech.chhanz.xyz/linux/2023/07/09/al2023-certbot-nginx/
'BE' 카테고리의 다른 글
캐시를 통한 성능개선 - 이론편 (0) | 2023.12.26 |
---|---|
N+1문제와 인덱스를 통한 조회 성능 개선 (0) | 2023.12.12 |
Promtail & Loki (0) | 2023.12.09 |
프로메테우스 & 그라파나 (0) | 2023.11.29 |
Spring Boot Actuator & micrometer (0) | 2023.11.22 |