-->

웹방화벽(WAF) ModSecurity 설치 및 sql인젝션 룰 설정

우분투(Ubuntu)에 리눅스 무료 웹방화벽(WAF)인 ModSecurity를 설치 및 설정해본다. 그리고 sql인젝션이 되는 취약한 웹페이지에서 ModSecurity에 sql인젝션 룰설정을 통해 인젝션을 방지할 수 있음을 보인다. 우분투 16.04 x64에서 진행했고 아파치는 Apache 2.4.18 버전이 설치되어있다.

 

- OS 환경 : 우분투 16.04 x64

- apache2 버전: Apache/2.4.18 (Ubuntu)

 

 

우분투에 웹방화벽(WAF) ModSecurity 설치

ModSecurity는 apt-get으로 간단히 설치할 수 있다.

 


# apt-get install libapache2-modsecurity -y

 

 

 

설치확인은 2가지 방식으로 할 수 있는데 apachectl -M 을 사용해 로드된 모듈 목록 중 security가 들어가는 모듈이 있는지 확인한다. 아래와 같이 security2_module (shared) 로 뜨면 제대로 설치가 된 것이다.

 

 

 

동일하게 로드된 모듈 목록을 확인하는 것은 같지만 phpinfo()를 통해 확인할 수도 있다. phpinfo() 함수를 실행하는 간단한 php 파일을 생성한 후 확인해보면 Loaded Modules 목록에 mod_security2가 로드된 것을 확인할 수 있다.

 

 

 

 

웹방화벽(WAF) ModSecurity 설정및 룰셋 설정

설치는 제대로 끝났으니 이제 몇가지 설정만 해주면 된다. 제일 먼저, /etc/modsecurirt/ 경로의 파일을 modsecurity.conf 파일로 복사를 해준다.

 


# cp /etc/modsecurity/modsecurity.conf-recommended

/etc/modsecurity/modsecurity.conf

 

 

 

그리고 /etc/modsecurity/modsecurity.conf 설정파일에서 SecRuleEngine DetectionOnly 부분을 아래와 같이 SecRuleEngine On 으로변경해준다.

 


SecRuleEngine DetectionOnly

-> SecRuleEngine On

 

 

 

현재까지의 변경사항에 문제가 없는지 apache2를 재시작해준다.

 

 

 

아무 이상이 없으면, 다시 /etc/modsecurity/modsecurity.conf 설정파일 하단에 아래와 같이 룰셋 설정을 추가해준다.

 


# ModSecurity Core Rule Set (CRS)
IncludeOptional /usr/share/modsecurity-crs/*.conf
IncludeOptional /usr/share/modsecurity-crs/activated_rules/*.conf

 

 

 

/usr/share/modsecurity-brs/base_rules 디렉토리로 가면 아래와 같이 기본적으로 제공되는 룰셋 설정파일들이 존재한다.

 

 

 

위 디렉토리에 존재하는 conf 파일 전부를 아래와 같이 /usr/share/modsecurity-crs/activated_rules/ 폴더로 전부 심볼릭 링크를 걸어준다. activated_rules 폴더에 설정파일이 존재해야 룰셋을 활성화시킬 수 있다. (전부 걸 필요없이 필요한 룰셋만 해주어도 된다. 밑에보면 나오지만 나의 경우도 아파치 재시작 시 특정 conf 파일에서 에러가 발생해서 결국 필요한 것 빼고 삭제했다).

 

 

 

여기까지하고 다시 apache2를 재시작해주는데 에러가 떠서 보니, 특정 설정파일 관련 에러가 지속적으로 발생했다.

 


root@ubuntu:/home/forest# systemctl restart apache2
Job for apache2.service failed because the control process exited with error code. 

See "systemctl status apache2.service" and "journalctl -xe" for details.


root@ubuntu:/home/forest# 
root@ubuntu:/home/forest# apache2ctl -t && sudo apache2ctl restart
AH00526: Syntax error on line 20 of /usr/share/modsecurity-

crs/activated_rules/modsecurity_crs_35_bad_robots.conf:

Error creating rule: Could not open phrase file "/usr/share/modsecurity-

crs/activated_rules/modsecurity_35_scanners.data": No such file or directory

Action '-t' failed.

 

 

 

그래서 일단은 아래 파일을 제외하곤 전부 삭제해주었다. 당연히 테스트할 sql인젝션 관련 conf 파일은 포함을 시켜준다 (modsecurity_crs_41_sql_injection_attacks.conf).

 

 

 

원인은 모르겠지만 에러가 발생한 conf 파일을 삭제해주니 apache2가 정상적으로 재시작되었다. 

 

 

 

sql인젝션 룰셋 적용 전후 확인

ModSecurity에 sql 룰셋을 적용하기 전에는 일부처 취약하게 만들어놓았기 때문에 아래와 같이 로그인폼에 <  1' or '1'='1  > 과 같은 sql 인젝션을 수행해 로그인에 성공할 수 있었다.

 

 

(로그인 성공으로 인한 세션값 생성)

 

 

 

 

하지만 ModSecurity에 sql 룰셋을 적용한 후에는 아래와 같이 일반적인 로그인은 정상적으로 동작하지만 인젝션을 하려 할 경우 에러 페이지가 뜨며 차단되는 것을 볼 수 있다.

 

 

참고자료
[1] https://malware.expert/tutorial/install-modsecurity-with-apache-on-ubuntu-16-04-lts/
[2] https://linuxhostsupport.com/blog/how-to-install-mod_security-and-mod_evasive-on-ubuntu-16-04/
[3] https://www.milesweb.com/hosting-faqs/install-and-configure-mod_security-on-ubuntu-16-04-server/

 

댓글

Designed by JB FACTORY