ModSecurity (WAF) 소개와 설치 가이드

ModSecurity WAF와 OWASP CRS 적용 가이드입니다. DetectionOnly 시작, CRS include, 로컬 테스트 룰, audit log, 차단 모드 전환, 오탐 튜닝 원칙을 정리했습니다.

ModSecurity는 웹 서버 앞단에서 HTTP 요청/응답을 검사하는 WAF 엔진입니다. 단독으로 모든 보안을 해결하는 도구가 아니라, 애플리케이션 수정, 인증, rate limit, 로그 모니터링과 함께 쓰는 방어 계층입니다. 운영 반영은 바로 차단 모드로 켜지 말고 탐지 모드에서 오탐을 먼저 줄이는 순서가 안전합니다.

Ubuntu/Apache 설치 예시

sudo apt update
sudo apt install -y apache2 libapache2-mod-security2
sudo a2enmod security2
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

처음에는 DetectionOnly로 시작합니다.

sudo sed -i 's/^SecRuleEngine .*/SecRuleEngine DetectionOnly/' /etc/modsecurity/modsecurity.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

OWASP CRS 적용

OWASP Core Rule Set(CRS)은 ModSecurity 호환 WAF에서 많이 쓰는 공개 룰셋입니다. 배포판 패키지나 공식 릴리스 방식 중 하나로 설치합니다.

# Ubuntu 패키지 예시
sudo apt install -y modsecurity-crs
ls -lah /usr/share/modsecurity-crs

Apache ModSecurity 설정에서 CRS를 include합니다. 경로는 배포판마다 다르므로 실제 설치 위치를 확인해야 합니다.

IncludeOptional /usr/share/modsecurity-crs/*.load
IncludeOptional /usr/share/modsecurity-crs/rules/*.conf

로컬 테스트 룰

공격 페이로드를 쓰지 않고 WAF 동작을 확인하려면 로컬 테스트 룰을 하나 만들어 검증합니다.

SecRule ARGS:test "@streq waf-test" \
  "id:100000,phase:2,deny,status:403,log,msg:'local waf test rule'"
sudo apache2ctl configtest
sudo systemctl reload apache2
curl -i 'http://127.0.0.1/?test=waf-test'

403과 audit log가 남으면 엔진이 요청을 보고 있다는 뜻입니다.

로그 확인

sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/modsec_audit.log 2>/dev/null || true
sudo grep -R \"local waf test rule\" /var/log -n 2>/dev/null

차단 모드 전환

탐지 모드에서 정상 트래픽 오탐을 충분히 정리한 뒤 차단 모드로 전환합니다.

sudo sed -i 's/^SecRuleEngine .*/SecRuleEngine On/' /etc/modsecurity/modsecurity.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

운영 튜닝 기준

  • 처음부터 전체 차단 모드로 켜지 말고 DetectionOnly 로그를 분석합니다.
  • 오탐 제외는 전체 rule disable보다 특정 URI, parameter, rule id 단위로 좁게 적용합니다.
  • WAF 로그는 개인정보와 요청 본문이 포함될 수 있으므로 접근 권한을 제한합니다.
  • WAF는 취약한 코드를 고치는 대체제가 아니라 완충 장치입니다.

공식 문서

BGM EVER