[L] rpm 패키지로 설치된 Apache에 mod_security 추가하기
💡 요약 정리
- rpm 패키지로 설치된 Apache 환경에 mod_security 웹 방화벽을 추가하는 방법입니다.
- pcre 라이브러리 확인 및 설치 후, mod_security 소스를 다운로드하고 컴파일합니다.
- 설치 후 httpd.conf 파일을 수정하여 모듈을 활성화합니다.
- Apache를 재시작하고 정상 적용 여부를 curl 명령어로 확인할 수 있습니다.
1. 설치환경
- CentOS 5.8 x86 (64bit)
- apache 2.2.3 (rpm 설치 버전)
- mod security 2.5.11
2. Mod security 란?
- Mod Security 는 Apache용 웹방화벽이다. 가장 널리 알려져 있는 HTTP,HTTPS를 이용한 공격을 차단 할 수 있는 웹 방화벽이다.
- XSS, SQL Injection, Command Execute 등을 차단하여 웹 보안에 있어서 최소한(?) 의 보안을 해주는 Apache 모듈이다.
- 사이트 : http://www.modsecurity.org/
3. pcre 설치 확인
- pcre 이라는 라이브러리가 설치되어 있어야 정상적인 사용이 가능합니다.
- 컴파일 하는 방법과 yum 설치 방법이 있으나 간편하게 yum 으로 설치하는 방법으로 설명하겠습니다.
[root@localhost /]# rpm -qa | grep pcre
[root@localhost /]# yum install pcre pcre-devel
Loaded plugins: fastestmirror, security
Determining fastest mirrors
* base: ftp.daum.net
* extras: ftp.daum.net
* updates: ftp.daum.net
base | 1.1 kB 00:00
extras | 2.1 kB 00:00
extras/primary_db | 207 kB 00:00
updates | 1.9 kB 00:00
updates/primary_db | 1.0 MB 00:00
Setting up Install Process
Package pcre-6.6-6.el5_6.1.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package pcre.i386 0:6.6-6.el5_6.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================
Package Arch Version Repository Size
========================================================================
Installing:
pcre i386 6.6-6.el5_6.1 base 117 k
pcre-devel i386 6.6-6.el5_6.1 base 181 k
pcre-devel x86_64 6.6-6.el5_6.1 base 184 k
Transaction Summary
========================================================================
Install 1 Package(s)
Upgrade 0 Package(s)
Total download size: 117 k
Is this ok [y/N]: y
Downloading Packages:
(... 생략 ...)
Complete!
4. mod_security 다운 및 설치
- http://www.modsecurity.org/download 에서 최신의 mod_security 설치파일을 다운 받아 설치합니다.
- 매뉴얼 작성시 최신버전은 2.7.xx 버전이나 매뉴얼에서는 2.5.11 버전으로 설명하겠습니다.
[root@localhost /]# tar zxvf modsecurity-apache_2.5.11.tar.gz
(...)
[root@localhost /]# cd modsecurity-apache_2.5.11
[root@localhost /]# cd apache2
[root@localhost /]# ./configure --with-apxs=/usr/sbin/apxs --with-pcre=/usr/bin/pcre-config --with-apr=/usr/bin/apr-1-config --with-apu=/usr/bin/apr-1-config
configure: error: couldn't find APXS
- 위 오류가 발생하면, 다음 명령어로 httpd-devel 패키지를 설치합니다.
[root@cafe24 apache2]# yum install httpd-*
(...)
[root@localhost /]# ./configure --with-apxs=/usr/sbin/apxs --with-pcre=/usr/bin/pcre-config --with-apr=/usr/bin/apr-1-config --with-apu=/usr/bin/apr-1-config
(...)
[root@localhost /]# echo $?
0
[root@localhost /]# make
(... 생략 ...)
[root@localhost /]# make install
(...)
설치 이후 확인:
[root@localhost /]# ll /usr/lib64/httpd/modules/mod_security2.so
-rwxr-xr-x 1 root root 1125446 Jan 14 16:03 /usr/lib64/httpd/modules/mod_security2.so
5. httpd.conf 파일 수정
- httpd.conf 파일을 수정하여 mod_security2.so 모듈을 load 시켜 준다. 약 200번째줄에 위치하고 있다.
[root@localhost /]# vi /etc/httpd/conf/httpd.conf
LoadModule cache_module modules/mod_cache.so
LoadModule suexec_module modules/mod_suexec.so
LoadModule disk_cache_module modules/mod_disk_cache.so
LoadModule file_cache_module modules/mod_file_cache.so
LoadModule mem_cache_module modules/mod_mem_cache.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule version_module modules/mod_version.so
LoadModule version_module modules/mod_version.so
LoadModule security2_module modules/mod_security2.so <-- 추가
# Mod_Security RollSet
Include conf/mod_security.conf <-- 추가
6. mod_security 차단룰셋 다운받기
- http://toolbox.krcert.or.kr 사이트의 공개 웹방화벽 자료실 → mod_security 자료실 → ModSecurity용 차단 샘플룰 [‘09.03.11]
- 다운받은 차단 룰셋 파일을 /etc/httpd/conf/mod_security.conf 파일로 복사 해 넣는다.
[root@localhost ]# cd /etc/httpd/conf
[root@localhost ]# ll
total 76
-rw-r--r-- 1 root root 33887 Jan 14 16:15 httpd.conf
-rw-r--r-- 1 root root 13139 Nov 12 22:52 magic
-rw-r--r-- 1 root root 23610 Jan 14 2013 mod_security.conf
7. apache를 재시작합니다
[root@localhost /]# /etc/rc.d/init.d/httpd restart
httpd를 종료 중: [ OK ]
httpd를 시작합니다: [ OK ]
8. mod_security 적용 확인
- 아래 명령어를 통해 header값에 나타나는 Server 응답을 통해 적용 여부를 확인합니다.
- 정상 적용 시 Apache가 아닌 Microsoft-IIS/5.0 로 출력됩니다.
[root@localhost /]# curl --head 127.0.0.1
HTTP/1.1 403 Forbidden
Date: Mon, 14 Jan 2013 07:26:23 GMT
Server: Microsoft-IIS/5.0
Accept-Ranges: bytes
Content-Length: 5043
Connection: close
Content-Type: text/html; charset=UTF-8