Nginx ModSecurity 설치 및 설정
Contents [숨기기]
ModSecurity
ModSecurity 는 웹 서비스의 공격을 실시간/효과적으로 차단할 수 있는 오픈소스 방화벽 모듈(WAF Mod_Security) 입니다
지원하는 웹서버로 Apache , IIS, Nginx 등을 지원 하고 있습니다.
제작사는 trustwave 이며 Apache License Version 2.0 라이센스 입니다
• 설치 환경
OS : CentOS 7.8 , Ubuntu 18.04 , CentOS 8.3
nginx : 1.19.4
ModSecurity : v3.0.4
CoreRuleSet : v3.3.2
* 설치는 root 유저 로 진행 하였습니다.
ModSecurity 다운로드 및 설치
사전 필요한 패키지 설치
컴파일 등을 위한 사전에 필요한 패키지 설치를 진행하도록 하겠습니다. 설치하는 모든 패키지가 ModSecurity 에만 필요한 것은 아니고 다른 포스팅 글과 연계되어 Nginx 이나 PHP 나 Apache 나 다른 라이브러리 컴파일 등에 필요한 여러 패키지에 대해서 함께 나열된 것이며 그외 유틸리티 도 같이 포함되어 있습니다.
• CentOS 7
• CentOS 8(RHEL8/Oracle Linux 8)
• Ubuntu
Source Code 다운로드
[root]# mkdir -p /root/pkg
[root]# cd /root/pkg
* 경로는 임의의 경로 입니다
[root]# git clone https://github.com/SpiderLabs/ModSecurity.git
설치(Config&Build)
[root]# cd ModSecurity
[root]# git submodule init
[root]# git submodule update
[root]# ./build.sh
[root]# ./configure –with-lmdb –with-ssdeep
[root]# make;make install
ModSecurity Dynamic 모듈 생성
Dynamic-module 생성을 위해 Nginx의 Source 와 Nginx Connector 를 다운 로드 받습니다
Nginx Source는 현재 사용중인 버전을 받으시면 됩니다 현재 포스팅 시스템에서 사용중인 버전은 1.19.4 입니다
현재 설치 되는 환경은 yum(rpm) / apt 으로 패키지 Nginx 가 설치된 상태 입니다
(TLS 1.3 적용을 위해 SRPM 으로 설치 하였습니다)
관련 된 내용은 아래 포스팅을 참조하시면 됩니다.
Connector 다운로드
[root]# cd /root/pkg
[root]# git clone https://github.com/SpiderLabs/ModSecurity-nginx.git
Nginx Source Code 다운로드
* Nginx 소스코드 다운은 지금 사용하는 소스코드의 버전에 맞게 다운받아서 사용하면 됩니다.
[root]# wget http://nginx.org/download/nginx-1.19.4.tar.gz
[root]# tar zxvf nginx-1.19.4.tar.gz
[root]# cd nginx-1.19.4
Module 생성
[root]# ./configure –with-compat –add-dynamic-module=../ModSecurity-nginx
[root]# make modules
생성된 모듈을 Nginx 로 복사
[root]# cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules
Nginx 설정 변경
Nginx.conf 파일에 ModSecurity 관련 설정을 추가 합니다.
[참고] modsecurity_rules_file 와 modsecurity on 설정은 server block 별로도 가능합니다
nginx.conf 에 설정하는 이유는 모든 Server Block에 공통적으로 적용 받을 수 있기 때문에 Server Block 별 반복적인 내용 설정 하기 보다 nginx.conf 에 1회 설정을 하였습니다.
nginx.conf 수정
[root]# vi /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
load_module “modules/ngx_http_modsecurity_module.so”; <– 추가
events {
<중략..>
#gzip on;
include /etc/nginx/conf.d/*.conf;
modsecurity_rules_file /etc/nginx/modsecurity.conf; <– 추가
modsecurity on; <– 추가
}
[주의] modsecurity.conf 파일을 Include 문으로 설정하면 아래처럼 에러가 발생합니다
modsecurity_rules_file 를 사용해야 합니다.
nginx: [emerg] unexpected end of file, expecting “;” or “}” in crs-setup.conf:
CoreRuleSet 설정
CoreRuleset 다운로드 및 Conf 파일 일부 수정, Nginx 에 설정 등에 대해서 확인 해 보도록 하겠습니다.
CoreRuleset 파일 다운로드
crs-setup.conf 내용 수정
중간에 아래의 SecDefaultAction 를 주석 처리 하고(96~97라인)
#SecDefaultAction “phase:1,log,auditlog,pass”
#SecDefaultAction “phase:2,log,auditlog,pass
더 아래에 403 정책이 추가된 SecDefaultAction 을 주석을 해제 합니다.
# 116~117 라인
SecDefaultAction “phase:1,log,auditlog,deny,status:403”
SecDefaultAction “phase:2,log,auditlog,deny,status:403”
모두 완료 되었다면 저장 및 종료 합니다.
:wq!
[참고사항] 룰셋 적용관련 Nginx의 Process 에러 발생
[notice] signal 17 (SIGCHLD) received from 20515
[alert] worker process 20515 exited on signal 11
설치 과정에서 위와 같이 Nginx 프로세스의 에러가 발생 되었으며 테스트 결과 아래 2개의 룰셋의 영향을 받은 것으로 확인 하였습니다.
REQUEST-949-BLOCKING-EVALUATION.conf
REQUEST-910-IP-REPUTATION.conf
다만 설치하는 버전이나 환경에 따라서 문제가 없을 수도 있습니다.
ModSecurity 및 룰셋 설정을 모두 완료 후에 이와 같이 문제가 발생한다면 제외하는것을 고려해보시면 될것 같습니다.
포스팅 환경에도 환경에 따라서 에러가 발생된 경우도 있었으며 에러가 발생되지 않은 경우도 있었습니다.
진행 후 문제가 있다면 룰을 제외하는 편을 고려해보시고 처음에는 룰 제외 없이 시작해서 정상 유무를 확인해보시면 됩니다.
[root]# cd /etc/nginx/coreruleset-3.3.2/rules
[root]# mv REQUEST-949-BLOCKING-EVALUATION.conf REQUEST-949-BLOCKING-EVALUATION.conf.ori
[root]# mv REQUEST-910-IP-REPUTATION.conf REQUEST-910-IP-REPUTATION.conf.ori
unicode.mapping 파일 복사
unicode.mapping 파일은 처음에 다운로드 받은 ModSecurity 디렉토리에 있습니다.
[root]# cd /root/pkg/ModSecurity/
[root]# cp unicode.mapping /etc/nginx/coreruleset-3.3.2
modsecurity.conf 파일 생성
[root]# cd /etc/nginx
[root]# vi modsecurity.conf
파일을 생성 하면서 내용을 입력 하겠습니다.
아래 내용 입력합니다.
Include coreruleset-3.3.2/crs-setup.conf
Include coreruleset-3.3.2/rules/*.conf
SecRuleEngine On
SecAuditEngine On
SecAuditLog /var/log/nginx/modsec_audit.log
SecAuditLogRelevantStatus “^(?:5|4(?!04))”
SecUnicodeMapFile coreruleset-3.3.2/unicode.mapping 20127
SecAuditLogType Serial
SecAuditLogParts ABCFHZ
SecDataDir /tmp
SecStatusEngine On
Nginx 재시작 및 룰셋 적용 확인
Nginx 재시작
[root]# systemctl restart nginx
룰셋 적용 테스트
ModSecurity 룰셋 적용 테스트를 진행하도록 하겠습니다. 웹 브라우저에서 아래와 같이 입력 합니다.
http://IP주소/?a=<script>Test</script>
audit.log 에러는 아래와 같이 룰셋에 의해서 403 에러가 발생되었음이 확인 됩니다
[root]# tail -100f /var/log/nginx/modsec_audit.log
ModSecurity: Access denied with code 403 (phase 2).
detected XSS using libinjection.
[file “coreruleset-3.3.0/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf”]
[line “37”] [id “941100”] [rev “”]
[msg “XSS Attack Detected via libinjection”]
[data “Matched Data: XSS data found within ARGS:a: <script>Test</script>”]
[severity “2”] [ver “OWASP_CRS/3.3.0”] [maturity “0”]
[accuracy “0”] [tag “application-multi”] [tag “language-multi”]
[tag “platform-multi”] [tag “attack-xss”] [tag “paranoia-level/1”]
[tag “OWASP_CRS”] [tag “capec/1000/152/242”] [hostname “192.168.0.62”]
[uri “/”]
[unique_id “1607522803”]
Conclusion
ModSecurity 는 오픈소스 웹 방화벽(WAF) 으로 많은 곳에서 사용되면서 계속적인 버전 업데이트가 이루어 지고 있습니다.
OWASP(The Open Web Application Security Project) 기반으로 룰셋도 계속적인 업데이트가 이루어지는 만큼 웹 서버에 꼭 설치를 해야하는 보안 솔루션 입니다.
Coreruleset 은 계속 업데이트 되고 있습니다. 아래 링크에서 정보를 확인 후 업데이트가 되면 업데이트 해주시면 됩니다.
• https://coreruleset.org/installation
• https://github.com/coreruleset/coreruleset
Reference
Reference Link
• SpiderLabs/ModSecurity-nginx
• Nginx wiki/extending/converting
• centminmod.com/nginx-failes-to-start-with-mod-security
• docs.nginx/nginx-plus-modsecurity-waf-trustwave-spiderlabs-rules
연관 된 다른글