ModSecurity(무료 오픈소스 웹방화벽) 설치방법

ModSecurity란?

 

리눅스서버를 사용하면 보안문제가 언제든 발생할 수 있습니다. 잘 모르고 있다가 어떤 공격이 들어와서 웹페이지를 변조하거나 DB에서 무슨 짓을 해도 속수무책인 경우들이 있는데요. 호스팅 서비스를 받는 경우 업체에선 자체 방화벽과 웹방화벽을 서비스 합니다. 이 비용이 만만치 않아서 오픈소스(무료) 웹방화벽을 알아보다가 modsecurity를 알게 되어 이번에 관련 공부를 하는 김에 정리해보려 합니다. 

저는 개발자도 아니고 서버 관리자도 아니라서 초보수준으로 차근차근 정리해나가도록 하겠습니다.

 

ModSecurity에 관해 아래의 순서로 업데이트 해나가겠습니다.

 

목차(아래 (1)내용이 이 페이지에 담겨 있습니다)

- ModSecurity란, 특징 (1)

- ModSecurity 다운로드 및 설치 방법(1)

- ModSecurity 활성화 및 Rule 설정(2)
  * 활성화방법
  * 기본환경 설정
  * 사용자 Rule설정
  * 기타

- 동작확인(2)

 

 

모드시큐리티(ModSecurity)란?

modsecurity는 공개웹방화벽이며 오픈소스입니다. 아파치서버를 기반으로 해서 동작합니다. GNU GPL을 따르는 공개버전이 있고 방화벽을 개발한 회사에서 만든 상용버전이 있습니다. 어차피 상용버전을 쓸거면 호스팅업체의 웹방화벽을 사용하면 되는 거라 오픈소스에 대해서만 살펴보겠습니다. 현재 ModSecurity는 3.0.4버전이 최신버전입니다.

 

https://modsecurity.org

 

ModSecurity: Open Source Web Application Firewall

 

modsecurity.org

위 홈페이지에서 내려받을 수 있으며 앞으로 설치 방법도 살펴보겠습니다.

 

 ModSecurity의 특징

- CLI(Command Line Interface) 기반입니다. GUI(Graphic User Interface)가 아니라서 좀 불편하고 익숙하지 않으면 접근이 꺼려지는 방식입니다. 반면 정책설정이 유연하고 입력값 검증이 우수하다는 장점이 있습니다.

- 세계적으로 널리쓰이는 Apache 웹서버 모듈로서 통합되어 동작합니다.

- 무료 웹방화벽입니다. 웹방화벽 비용이 호스팅 업체마다 차이가 있고 금액도 다양한데요. 저가 라인도 월 10만원 이상인 경우가 많습니다. 그에 비해 모드 시큐리티는 비용 0입니다. 비용절감이 크죠. 저희 회사도 그래서 모드시큐리티로 바꿨습니다.

- 무료이지만 안정적이고 보안을 위한 기능은 부족함이 거의 없다고 합니다.

- KISA에서 추천할 정도로 믿을만하고 지속적으로 업데이트가 잘 되고 있습니다.

- 2006년에 Breach Security가 인수했고 상용버전도 있지만 무료를 유지해주고 있습니다.

- 클라이언트로부터 웹 요청이 들어올 때 여러가지 웹서버(소프트웨어)나 모듈이 같이 동작합니다. 모드시큐리티는 웹서버의 다른 모듈보다 앞에서 방어막이 되고 해킹 탐지를 할 수 있습니다.

 

- 다양한 우회방지기술을 가지고 있습니다.

  • 경로, 파라미터분석 전에 정규화(Normalization) 시켜 우회 공격을 차단합니다. (정규화는 정보보호와 분석을 위한 개념인데 지금은 일단 넘어가겠습니다.)
  • 예를 들면 “//”, “\/”, “.”, “%00” 등 우회 공격용 스트링을 제거하고, 인코딩된 URL을 디코딩한다고 합니다.

- 모드 시큐리티의 보안 엔진(?)이 HTTP 프로토콜을 이해하기 때문에 정밀하고 전문적인 필터링이 가능하다고 합니다.

- 전송방식인 GET, POST 메소드 모두 컨텐츠 분석이 가능(서버에 뭔가 값을 전달하는 것을 불순한지 아닌지 파악이 가능하다는 것이겠죠)

- 분석한 정보를 로깅- 기록으로 남깁니다.

- 웹서버에 임베디드되어 있어서 복호화 한 후 요청데이터에 접근하여 HTTPS 를 통한 공격까지도 필터링 가능.

 

 

 

오늘은 모드시큐리티 일반적인 사항과 특징, 기능을 알아봤습니다.

 


 

4월 4일 업데이트

ModSecuriy 다운로드와 설치방법

ModSecurity를 설치하기 전에 먼저 아파치 버전을 확인해봅니다.

# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Jun 19 2018 15:45:13

저희 서버는 아파치 버전 2.2.15네요.

서버 버전이 2.X 대라서 Modsecurity도 2.X버전을 사용하기로 했습니다.

 

그전에 kisa에 보니 모드시큐리티2.x 를 설치하려면 필수 설치되어 있어야하는 프로그램들이 있습니다.

- PCRE (www.pcre.org 에서 다운로드) : Perl이란 프로그램언어와 호환을 위해 필요.

- libxml2 (www.xmlsoft.org에서 다운로드) : 이것도 펄과 관련

- unique_id : 모드시큐리티가 필터링을 할 수 있으려면 반드시 enable 되어 있어야 한다고 합니다.

아파치 컴파일시 되어있어야 하고 RPM 설치버전은 모듀로드를 확인하도록 합니다.

 

* Kisa의 문서가 오래된 것이라 현재 버전에선 이미 처리가 되어 있을 수 있습니다. 

 

* 실제로 저희 서버 버전엔 다른 버전의 modsecurity가 설치되어 있습니다만 현재 2.X 대 버전은 2.9.3을 내려받을 수 있습니다.

 

위 홈페이지에서 ModSecurity for Apache TAR/GZ (sha256) 파일을 다운로드 합니다.

 

서버에 접속합니다.

아래 내용은 카페24의 블로그를 참고해서 제가 사용하는 서버에 맞게 일부 수정했습니다.

# tar zxvf mod_security-2.7.3.tar.gz
.
.
.
mod_security-2.7.3/doc/modsecurity2-apache-reference.xml
mod_security-2.7.3/tools/
mod_security-2.7.3/tools/rules-updater.pl.in
mod_security-2.7.3/tools/README
mod_security-2.7.3/tools/rules-updater-example.conf
# 
# 
# cd mod_security-2.7.3
# cd apache2
# ./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
.
.
.
checking for strrchr... yes
checking for strstr... yes
checking for strtol... yes
configure: looking for Apache module support via DSO through APXS
configure: error: couldn't find APXS  <<<<< ----- configure시 이런 에러가 발생하면 하단의 명령어로 httpd-devel 패키지 설치
#
#
# yum install httpd-*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.daum.net
 * extras: ftp.daum.net
 * updates: ftp.daum.net
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.3-65.el5.centos.3 set to be updated
---> Package httpd-devel.i386 0:2.2.3-65.el5.centos.3 set to be updated
--> Processing Dependency: apr-util-devel for package: httpd-devel
--> Processing Dependency: apr-devel for package: httpd-devel
---> Package httpd-devel.x86_64 0:2.2.3-65.el5.centos.3 set to be updated
---> Package httpd-manual.x86_64 0:2.2.3-65.el5.centos.3 set to be updated
--> Running transaction check
---> Package apr-devel.x86_64 0:1.2.7-11.el5_6.5 set to be updated
---> Package apr-util-devel.x86_64 0:1.2.7-11.el5_5.2 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================
Package                    Arch               Version                           Repository           Size
==============================================================================
Installing:
 httpd-devel                i386               2.2.3-65.el5.centos.3             updates             153 k
 httpd-devel                x86_64             2.2.3-65.el5.centos.3             updates             153 k
 httpd-manual               x86_64             2.2.3-65.el5.centos.3             updates             817 k
Updating:
 httpd                      x86_64             2.2.3-65.el5.centos.3             updates             1.3 M
Installing for dependencies:
 apr-devel                  x86_64             1.2.7-11.el5_6.5                  base                238 k
 apr-util-devel             x86_64             1.2.7-11.el5_5.2                  base                 53 k

Transaction Summary
==============================================================================
Install       5 Package(s)
Upgrade       1 Package(s)

Total download size: 2.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): apr-util-devel-1.2.7-11.el5_5.2.x86_64.rpm                                   |  53 kB     00:00     
(2/6): httpd-devel-2.2.3-65.el5.centos.3.x86_64.rpm                                 | 153 kB     00:00     
(3/6): httpd-devel-2.2.3-65.el5.centos.3.i386.rpm                                   | 153 kB     00:00     
(4/6): apr-devel-1.2.7-11.el5_6.5.x86_64.rpm                                        | 238 kB     00:00     
(5/6): httpd-manual-2.2.3-65.el5.centos.3.x86_64.rpm                                | 817 kB     00:00     
(6/6): httpd-2.2.3-65.el5.centos.3.x86_64.rpm                                       | 1.3 MB     00:00     
-----------------------------------------------------------------------------------------------------------
Total                                                                      4.8 MB/s | 2.6 MB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating       : httpd                                                                               1/7 
  Installing     : apr-devel                                                                           2/7 
  Installing     : apr-util-devel                                                                      3/7 
  Installing     : httpd-manual                                                                        4/7 
  Installing     : httpd-devel                                                                         5/7 
  Installing     : httpd-devel                                                                         6/7 
  Cleanup        : httpd                                                                               7/7 

Installed:
  httpd-devel.i386 0:2.2.3-65.el5.centos.3             httpd-devel.x86_64 0:2.2.3-65.el5.centos.3         
  httpd-manual.x86_64 0:2.2.3-65.el5.centos.3         

Dependency Installed:
  apr-devel.x86_64 0:1.2.7-11.el5_6.5               apr-util-devel.x86_64 0:1.2.7-11.el5_5.2              

Updated:
  httpd.x86_64 0:2.2.3-65.el5.centos.3                                                                     

Complete!
[root@localhost /]#
[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
.
.
.
.
config.status: creating mlogc-src/mlogc-batch-load.pl
config.status: creating t/run-unit-tests.pl
config.status: creating t/run-regression-tests.pl
config.status: creating t/gen_rx-pm.pl
config.status: creating t/csv_rx-pm.pl
config.status: creating t/regression/server_root/conf/httpd.conf
config.status: creating ../tools/rules-updater.pl
config.status: creating mlogc-src/Makefile
config.status: creating mod_security2_config.h
#
# echo $?   <-- tip. 에러없이 정상적으로 configure 되었다면 echo $? 값이 0이 출력되어야 한다.
0
# make
.
.
생략
.
.
# make install
build/apxs-wrapper -i mod_security2.la
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' mod_security2.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp mod_security2.la /usr/lib64/httpd/modules/
cp .libs/mod_security2.so /usr/lib64/httpd/modules/mod_security2.so
cp .libs/mod_security2.lai /usr/lib64/httpd/modules/mod_security2.la
cp .libs/mod_security2.a /usr/lib64/httpd/modules/mod_security2.a
chmod 644 /usr/lib64/httpd/modules/mod_security2.a
ranlib /usr/lib64/httpd/modules/mod_security2.a
PATH="$PATH:/sbin" ldconfig -n /usr/lib64/httpd/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/lib64/httpd/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/lib64/httpd/modules/mod_security2.so
#
#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
#




출처: https://blog.cafe24.com/1969 [카페24 공식 블로그 인앤인]

이 방법이 일반적인 것 같은데, 좀 더 쉬운 방법이 있을 것 같아서 찾아봤습니다.

아래는 modsecurity 홈페이지에 나와 있는 방법입니다. 너무 간단하죠?

$ sudo yum install mod_security
$ sudo /etc/init.d/httpd restart

yum을 이용해서 설치하면 간단하다고 하는데 맞는 yum을 찾도록 합니다.

 

pkgs.org라는 홈페이지에서 rpm을 찾았습니다.

 

https://centos.pkgs.org/6/epel-x86_64/mod_security-2.7.3-5.el6.x86_64.rpm.html

 

mod_security-2.7.3-5.el6.x86_64.rpm CentOS 6 Download

/etc/httpd/conf.d/mod_security.conf /etc/httpd/modsecurity.d/ /etc/httpd/modsecurity.d/activated_rules/ /usr/lib64/httpd/modules/mod_security2.so /usr/share/doc/mod_security-2.7.3/CHANGES /usr/share/doc/mod_security-2.7.3/LICENSE /usr/share/doc/mod_securit

centos.pkgs.org

그리고 아래와 같은 순서로 진행하면 된다고 하는데요. 아래 페도라라고 되어 있는 것은 yum방식이 페도라에서 동작하는 거라 이를 centOS 에서도 가능하도록 해주는 것입니다. 그 후엔 rpm패키지를 받아서 인스톨하면 된다고 하네요. 웬만큼 설정이 다 되어 있어서 따로 건드릴 것이 없다고 하네요.

Download latest epel-release rpm from
http://download-ib01.fedoraproject.org/pub/epel/6/x86_64/
Install epel-release rpm:
# rpm -Uvh epel-release*rpm
Install mod_security rpm package:
# yum install mod_security

Installed: mod_security-2.7.3-5.el6.x86_64
Installed: mod_security_crs-2.2.6-3.el6.noarch

 

저희쪽은 설치된 내용을 확인하니 위와 같이 나옵니다.

 

* 진행과정중 서버관리자의 도움을 받았습니다.

 

공부를 하시는 분들은 참고만 하시구요. 위 방식으로는 바로 설치가 안될 수 있습니다. 서버 환경이 다 다르고 버전도 달라서요. 저 같은 경우도 위에 카페24 설정 방식이 가장 맞지만 버전이 다릅니다. 그래서 참고만 했구요. 최종적으론 결국 겁이 나서 최종 완료까지는 본 서버에 적용해보지 못했습니다; 

 

이글은 4월 4일 업데이트되었습니다.

반응형

댓글

Designed by JB FACTORY