CentOS 7, Debian 8 및 Ubuntu 16.04에 Nginx 용 ModSecurity를 ​​설치하는 방법

ModSecurity는 다양한 웹 애플리케이션의 잠재적 취약성을 대상으로하는 다양한 사이버 공격으로부터 Apache, Nginx 및 IIS를 보호하는 데 유용한 오픈 소스 웹 애플리케이션 방화벽 (WAF) 모듈입니다.

이 기사에서는 CentOS 7, Debian 8 및 Ubuntu 16.04에 Nginx 용 ModSecurity를 ​​설치하고 구성합니다.

전제 조건

  • - - 날짜 CentOS는 7, 데비안 8, 우분투 16.04 64 비트 설치.
  • 로 로그인 중 root입니다.

1 단계 : 시스템 업데이트

다음은 이 가이드 , 서버의 커널과 최신 버전의 패키지를 업데이트합니다.

2 단계 : 종속성 설치

Nginx 및 ModSecurity를 ​​성공적으로 컴파일하려면 다음과 같이 여러 소프트웨어 패키지를 설치해야합니다.

a) CentOS 7 :

yum groupinstall -y "Development Tools"
yum install -y httpd httpd-devel pcre pcre-devel libxml2 libxml2-devel curl curl-devel openssl openssl-devel
shutdown -r now

b) 데비안 8 또는 우분투 16.04 :

apt-get install -y git build-essential libpcre3 libpcre3-dev libssl-dev libtool autoconf apache2-dev libxml2-dev libcurl4-openssl-dev automake pkgconf

3 단계 : ModSecurity 컴파일

Nginx 마스터 브랜치 용 ModSecurity에보고 된 여러 가지 불안정성 때문에 현재로서는 nginx_refactoring가능한 한 최신 버전의 브랜치 를 사용하는 것이 공식적으로 권장됩니다 .

nginx_refactoringNginx 용 ModSecurity 브랜치를 다운로드하십시오 .

cd /usr/src
git clone -b nginx_refactoring https://github.com/SpiderLabs/ModSecurity.git

컴파일 모드

a) CentOS 7 :

cd ModSecurity
sed -i '/AC_PROG_CC/a\AM_PROG_CC_C_O' configure.ac
sed -i '1 i\AUTOMAKE_OPTIONS = subdir-objects' Makefile.am
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make

참고 : sed위 의 두 명령은 최신 automake 버전을 사용할 때 경고 메시지를 방지하는 데 사용됩니다.

b) 데비안 8 또는 우분투 16.04 :

cd ModSecurity
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make

4 단계 : Nginx 컴파일

Nginx 1.10.3작성 당시의 최신 Nginx 릴리스를 다운로드하여 보관 해제하십시오 .

cd /usr/src
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz && rm -f nginx-1.10.3.tar.gz

a) CentOS 7 :

먼저 Nginx 전용 사용자 nginx와 전용 그룹 을 생성해야합니다 nginx.

groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx

그런 다음 ModSecurity 및 SSL 모듈을 활성화하면서 Nginx를 컴파일하십시오.

cd nginx-1.10.3/
./configure --user=nginx --group=nginx --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
make
make install

Nginx의 기본 사용자를 수정하십시오.

sed -i "s/#user  nobody;/user nginx nginx;/" /usr/local/nginx/conf/nginx.conf

b) 데비안 8 또는 우분투 16.04 :

먼저 기존 사용자 www-data와 기존 그룹을 사용해야합니다 www-data.

그런 다음 ModSecurity 및 SSL 모듈을 활성화하면서 Nginx를 컴파일하십시오.

cd nginx-1.10.3/
./configure --user=www-data --group=www-data --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
make
make install

Nginx의 기본 사용자를 수정하십시오.

sed -i "s/#user  nobody;/user www-data www-data;/" /usr/local/nginx/conf/nginx.conf

Nginx가 성공적으로 설치되면 관련 파일은 다음 위치에 있습니다.

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

다음을 사용하여 설치를 테스트 할 수 있습니다.

/usr/local/nginx/sbin/nginx -t

아무 것도 잘못되면 출력은 다음과 같아야합니다

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

편의를 위해 Nginx에 대한 시스템 단위 파일을 설정할 수 있습니다.

cat <<EOF>> /lib/systemd/system/nginx.service
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillStop=/usr/local/nginx/sbin/nginx -s stop

KillMode=process
Restart=on-failure
RestartSec=42s

PrivateTmp=true
LimitNOFILE=200000

[Install]
WantedBy=multi-user.target
EOF

다음과 같이 Nginx를 시작 / 중지 / 다시 시작할 수 있습니다.

systemctl start nginx.service
systemctl stop nginx.service
systemctl restart nginx.service

4 단계 : ModSecurity 및 Nginx 구성

4.1 Nginx 구성 :

vi /usr/local/nginx/conf/nginx.conf

세그먼트 내에서 다음 세그먼트를 찾으십시오 http {}.

location / {
    root   html;
    index  index.html index.htm;
}

아래 줄을 location / {}세그먼트에 삽입하십시오 .

ModSecurityEnabled on;
ModSecurityConfig modsec_includes.conf;
#proxy_pass http://localhost:8011;
#proxy_read_timeout 180s;

최종 결과는 다음과 같아야합니다.

location / {
    ModSecurityEnabled on;
    ModSecurityConfig modsec_includes.conf;
    #proxy_pass http://localhost:8011;
    #proxy_read_timeout 180s;
    root   html;
    index  index.html index.htm;
}

저장하고 종료하십시오 :

:wq!

참고 : 위의 Nginx 구성은 리버스 프록시가 아닌 웹 서버로 Nginx를 사용하기위한 샘플 구성 일뿐입니다. Nginx를 리버스 프록시로 사용하는 경우 #마지막 두 줄에서 문자를 제거하고 적절히 수정하십시오.

4.2 /usr/local/nginx/conf/modsec_includes.conf:

cat <<EOF>> /usr/local/nginx/conf/modsec_includes.conf
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/*.conf
EOF

참고 : 위의 구성은 owasp-modsecurity-crs/rules/디렉토리의 모든 OWASP ModSecurity 핵심 규칙을 적용합니다 . 선택적 규칙 만 적용하려면 include owasp-modsecurity-crs/rules/*.conf행을 제거한 다음 4.5 단계 후에 필요한 정확한 규칙을 지정해야합니다.

4.3 ModSecurity 구성 파일 가져 오기 :

cp /usr/src/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
cp /usr/src/ModSecurity/unicode.mapping /usr/local/nginx/conf/

4.4 /usr/local/nginx/conf/modsecurity.conf파일 수정

sed -i "s/SecRuleEngine DetectionOnly/SecRuleEngine On/" /usr/local/nginx/conf/modsecurity.conf

4.5 OWASP ModSecurity CRS (Core Rule Set) 파일 추가 :

cd /usr/local/nginx/conf
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
cd owasp-modsecurity-crs
mv crs-setup.conf.example crs-setup.conf
cd rules
mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

5 단계 : ModSecurity 테스트

Nginx를 시작하십시오 :

systemctl start nginx.service

외부 액세스를 허용하려면 포트 80을여십시오.

a) CentOS 7 :

firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --reload

b) 데비안 8 :

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
touch /etc/iptables
iptables-save > /etc/iptables

c) 우분투 16.04에서 :

ufw allow OpenSSH
ufw allow 80
ufw default deny
ufw enable    

웹 브라우저가 다음을 가리 키도록하십시오.

http://203.0.113.1/?param="><script>alert(1);</script>

grep다음과 같이 오류 메시지를 가져 오는 데 사용하십시오 .

grep error /usr/local/nginx/logs/error.log

출력에는 다음과 유사한 몇 가지 오류 메시지가 포함되어야합니다.

2017/02/15 14:07:54 [error] 10776#0: [client 104.20.23.240] ModSecurity: Warning. detected XSS using libinjection. [file "/usr/local/nginx/conf/owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf"] [line "56"] [id "941100"] [rev "2"] [msg "XSS Attack Detected via libinjection"] [data "Matched Data:  found within ARGS:param: \x22><script>alert(1);</script>"] [severity "CRITICAL"] [ver "OWASP_CRS/3.0.0"] [maturity "1"] [accuracy "9"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-xss"] [tag "OWASP_CRS/WEB_ATTACK/XSS"] [tag "WASCTC/WASC-8"] [tag "WASCTC/WASC-22"] [tag "OWASP_TOP_10/A3"] [tag "OWASP_AppSensor/IE1"] [tag "CAPEC-242"] [hostname ""] [uri "/index.html"] [unique_id "ATAcAcAkucAchGAcPLAcAcAY"]

그게 다야. 보다시피, ModSecurity 모듈은 기본 동작 정책에 따라이 공격을 성공적으로 기록했습니다. 더 많은 사용자 정의 설정을 확인하려면주의 깊게 편집 및 검토하시기 바랍니다 /usr/local/nginx/conf/modsecurity.conf/usr/local/nginx/conf/owasp-modsecurity-crs/crs-setup.conf파일을.

댓글 남기기

NTLite를 사용하여 업데이트로 Windows 사용자 지정 ISO를 만드는 방법

NTLite를 사용하여 업데이트로 Windows 사용자 지정 ISO를 만드는 방법

NTLite를 사용하여 Vultr 시스템과 호환되는 Windows 사용자 지정 ISO를 만드는 방법을 단계별로 설명합니다. 최신 드라이버와 업데이트 통합을 포함한 완벽 가이드.

Vultr에서 BGP 구성

Vultr에서 BGP 구성

Vultr BGP 설정을 위한 단계별 튜토리얼. ASN 할당부터 IP 라우팅 구성, BIRD 데몬 설정, FreeBSD 특이사항 및 실전 문제 해결 팁 7가지 포함한 최종 가이드

WsgiDAV를 사용하여 Debian 10에 WebDAV 배포

WsgiDAV를 사용하여 Debian 10에 WebDAV 배포

WebDAV (Web Distributed Authoring and Versioning)를 이용한 원격 파일 작성을 위한 프레임워크로, WsgiDAV 설치 및 구성 방법을 안내합니다.

데비안 7에 Mailcow 설치

데비안 7에 Mailcow 설치

Mailcow는 DoveCot, Postfix 및 기타 여러 오픈 소스 패키지를 기반으로 하는 경량 메일 서버입니다. 데비안에서 Mailcow 설치 방법을 안내합니다.

AI가 랜섬웨어 공격의 증가와 싸울 수 있습니까?

AI가 랜섬웨어 공격의 증가와 싸울 수 있습니까?

랜섬웨어 공격이 증가하고 있지만 AI가 최신 컴퓨터 바이러스를 처리하는 데 도움이 될 수 있습니까? AI가 답인가? AI boone 또는 bane인지 여기에서 읽으십시오.

ReactOS: 이것이 Windows의 미래입니까?

ReactOS: 이것이 Windows의 미래입니까?

오픈 소스이자 무료 운영 체제인 ReactOS가 최신 버전과 함께 제공됩니다. 현대 Windows 사용자의 요구 사항을 충족하고 Microsoft를 무너뜨릴 수 있습니까? 이 구식이지만 더 새로운 OS 환경에 대해 자세히 알아보겠습니다.

WhatsApp 데스크톱 앱 24*7을 통해 연결 유지

WhatsApp 데스크톱 앱 24*7을 통해 연결 유지

Whatsapp은 마침내 Mac 및 Windows 사용자용 데스크톱 앱을 출시했습니다. 이제 Windows 또는 Mac에서 Whatsapp에 쉽게 액세스할 수 있습니다. Windows 8 이상 및 Mac OS 10.9 이상에서 사용 가능

AI는 어떻게 프로세스 자동화를 다음 단계로 끌어올릴 수 있습니까?

AI는 어떻게 프로세스 자동화를 다음 단계로 끌어올릴 수 있습니까?

인공 지능이 소규모 회사에서 어떻게 인기를 얻고 있으며 어떻게 인공 지능이 성장할 가능성을 높이고 경쟁자가 우위를 점할 수 있는지 알아보려면 이 기사를 읽으십시오.

macOS Catalina 10.15.4 추가 업데이트로 인해 해결보다 더 많은 문제가 발생함

macOS Catalina 10.15.4 추가 업데이트로 인해 해결보다 더 많은 문제가 발생함

최근 Apple은 문제를 해결하기 위해 macOS Catalina 10.15.4 추가 업데이트를 출시했지만 이 업데이트로 인해 Mac 컴퓨터의 브릭킹으로 이어지는 더 많은 문제가 발생하는 것 같습니다. 자세히 알아보려면 이 기사를 읽어보세요.

빅데이터의 13가지 상용 데이터 추출 도구

빅데이터의 13가지 상용 데이터 추출 도구

빅데이터의 13가지 상용 데이터 추출 도구