nginx 버전 : 1.12.0
Naxsi 관련 자료를 찾아보면 Ubuntu위주의 글이 많다.
찾아본 바로는 Ubuntu 14.04버전이 나올 당시 nginx-naxsi 패키지가 제공되었고 Ubuntu기반으로 편리하게 패키지를 다운받아 설치해서 사용해왔던것 같다.
특히 ELK스택을 기반으로 하는 naxsi-ui패키지는 naxsi 사이트가 Github로 옮기면서 없어졌고 '사용자들에게 숙제로 남긴다'면서 더이상의 패키지 자료는 제공하지 않는 모양이다.
naxsi는 nginx를 설치할 때 추가해주어야 하는 모듈이고 nginx를 컴파일 설치했다고 해서 DB나 php까지 컴파일 설치할 필요는 없다는 걸 확인했다.
mariaDB나 php는 rpm으로 설치해도 웹서버를 사용하는 데에는 큰 지장이 없었다.
1. 기존에 설치 했던 nginx는 제거
2. 최신 버전의 nginx 다운로드
naxsi 모듈을 추가해야 하므로 컴파일을 통해 설치
wget http://nginx.org/download/nginx-1.12.0.tar.gz
3. naxsi 소스파일 다운로드
wget https://github.com/nbs-system/naxsi/archive/master.zip
4. 압축해제
5. naxsi 파일 위치 변경
6. nginx 디렉터리로 이동
7. nginx 초기 설정
./configure
--conf-path=/usr/local/nginx/conf/nginx.conf \
--add-module=/usr/local/naxsi/naxsi_src/ \
--error-log-path=/var/log/nginx/error.log \
--http-client-body-temp-path=/usr/local/nginx/body \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi \
--http-log-path=/var/log/nginx/access.log \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--lock-path=/var/run/nginx.lock \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_addition_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_stub_status_module \
--with-http_v2_module \
--without-mail_pop3_module \
--without-mail_smtp_module \
--without-mail_imap_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--sbin-path=/usr/sbin/nginx \
--prefix=/usr/local/nginx
8. 빌드 및 설치
9. 데몬 스크립트 작성
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /var/run/nginx.pid
# user: nginx
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf "
lockfile=/var/run/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
10. 스크립트 실행파일에 권한 부여
11. nginx 실행
12. naxsi 룰 복사
13. naxsi.rules 작성
LearningMode는 주석처리
14. nginx 설정파일 수정
1) http 블록
naxsi_core.rules를 추가해준다.
2) server 블록
위에서 작성한 naxsi.rules를 추가해주고 naxsi로그 경로를 정해준다
15. 설정파일 수정 후 재시작 해준다.
16. 정상적인 요청에 대한 응답값 확인
17. 비정상적인 요청에 대한 응답 확인
설정파일에 요청에 대한 거부는 403 return으로 설정해 놓았기 때문에 아래에 요청에 대해 403 Forbidden 에러를 확인 할 수 있다.
※LearningMode를 실행했을 때는??
LearningMode를 실행하지 않았을 때는 403 에러 메시지가 출력됐으나 LearningMode로 Naxsi 방화벽이 동작하게 되면 404에러메시지를 출력한다 (Not Found)
18. naxsi 에러 로그 확인
naxsi는 whitelist 기반의 WAF이기 때문에 Github에서는 Learning Mode를 추천하고 있다.
naxsi 디렉터리
1. naxsi_config
naxsi_core.rules에는 XSS, SQL인젝션, RFI에 관한 규칙이 규정되어 있고 각 항목별로 카운터 값을 지정해 놓는다.
2. nxapi
nxapi 디렉터리에는 키바나 대쉬보드 관련 코드가 있고 json이나 nxtool.py가 있다.
naxsi 로그를 참조하여 nxtool.py로 whitelist를 작성하여 허용목록 규칙을 nginx에 적용하는 방식으로 동작하는 것 같다.
지금은 naxsi가 동작하는지만 확인했는데 naxsi의 규칙을 입맛에 맞게 바꾸고 LearningMode로 동작시켜 로그 기반으로whitelist를 작성하는데 까지는 시간이 다소 걸릴 것 같다.
nginx, naxsi 넘나 어려운것
0 댓글