Study/Web

Nginx ModSecurity 및 CRS 적용하기

seomj 2024. 3. 1. 17:35

ModSecurity

표준 오픈 소스 웹 애플리케이션 방화벽(WAF) 엔진

Apache HTTP Server용 모듈로 설계되었지만 다양한 플랫폼에서 HTTP 요청 및 응답 필터링 기능을 제공하도록 발전했다.

 

상세 설정 사항은 owasp modsecurity wiki를 참조하자.

 

Reference Manual (v3.x)

ModSecurity is an open source, cross platform web application firewall (WAF) engine for Apache, IIS and Nginx that is developed by Trustwave's SpiderLabs. It has a robust event-based programmin...

github.com

 

간단한 환경 설정 및 로깅 관련 지시자는 다음 블로그에서 참조했다.

 

[Modsecurity] Modsecurity란?

Modsecurity에 대해 공부하기 전에 먼저 웹 방화벽에 대해서 알아야 한다. 웹 방화벽 웹 방화벽(Web Application Firewall, WAF)이란, 일반적인 네트워크 방화벽(Firewall)과는 달리 웹 애플리케이션 보안에 특

dyoerr9030.tistory.com


실습

환경

ubuntu 23.10

nginx 1.24

libnginx-mod-http-modsecurity 1.0.3

modsecurity-crs 3.3.5

 

 

ModSecurity 설치

apt-cache search modsecurity
apt install libnginx-mod-http-modsecurity
apt install modsecurity-crs

 

CRS에 대해 확인해보자.

/usr/share/modsecurity-crs/owasp-crs.load 파일을 확인해보면 Include 형태로 규칙이 들어있는 파일들이 지정되어 있다.

 

*왜 /usr/share 아래에 load 파일이 생기는가?

→ /usr/share 디렉터리는 시스템 전체에서 공유되는 리소스를 저장하는 디렉터리. 이 디렉터리에는 설치된 패키지들의 공유 데이터, 문서, 예제 파일 등이 포함. 보통 시스템 관리자가 직접 관리하는 파일들이 아니라 패키지 관리 시스템을 통해 설치된 파일들이 이 디렉터리에 위치.

 

 

Nginx 설정

nginx가 없다면 설치부터 해주도록 하자.

apt install nginx
systemctl start nginx
systemctl status nginx
systemctl enable nginx

 

본인의 경우 기존에 php 설정이 되어 있는 환경에서 테스트를 했기에 조금 다를 수 있다.

server 설정 파일을 열어준다.

본인의 경우는 /etc/nginx/sites-available/sitephp.com 을 수정했다.

modsecurity on;
modsecurity_rules_file /etc/nginx/modsecurity_includes.conf;

구문을 추가했다.

 

 

테스트 예제

Redhat에 나와있던 SecRule을 사용했다.

data에 evil이 포함되어 있다면 접근을 막도록 하는 예제이다.

Redhat Page

 

 

ModSecurity 설정

/etc/nignx/modsecurity.conf

modsecurity 기능을 활성화 시킨다.

 

/etc/nginx/modsecurity_includes.conf

nginx 모듈로 설치해서 그런지 이미 nginx 폴더 내에 참조하는 conf 파일들이 존재했다. 여기서 Include로 내가 만든 conf를 적용해주었다. 

 

/etc/nginx/modsec/modsec_evil.conf

data 매개변수에 evil 문자열이 포함된 경우 사용자에게 리소스 사용을 금지한다.

 

 

테스트 결과

 

data에 evil 값을 넣으니 403 forbidden 된 것을 확인할 수 있다.

 

log를 확인해보자.

/etc/nginx/modsecurity.conf에 SecAuditLog 항목을 보면 감사 로그 파일의 경로를 알 수 있다.

 

 

 

참고

https://docs.nginx.com/nginx-waf/admin-guide/nginx-plus-modsecurity-waf-installation-logging/

'Study > Web' 카테고리의 다른 글

[Web]Django 쿼리셋(QuerySet)  (0) 2023.07.04
[Web]Django urls.py 구성  (0) 2023.07.04
[Web]Django 애플리케이션 생성 및 관리자 설정  (0) 2023.07.03
[Web]Django 가상환경 구성 및 설치  (0) 2023.07.02
[Web]Django MTV 패턴  (0) 2023.07.02