วิธีการติดตั้ง ModSecurity สำหรับ Nginx บน CentOS 7, Debian 8 และ Ubuntu 16.04

ModSecurity เป็นโมดูลโอเพนซอร์สของเว็บแอพพลิเคชันไฟร์วอลล์ (WAF) ซึ่งเหมาะสำหรับการปกป้อง Apache, Nginx, และ IIS จากการโจมตีทางไซเบอร์ต่างๆที่กำหนดเป้าหมายช่องโหว่ที่อาจเกิดขึ้นในแอปพลิเคชันเว็บต่างๆ

ในบทความนี้เราจะติดตั้งและกำหนดค่า ModSecurity สำหรับ Nginx บน CentOS 7, Debian 8 และ Ubuntu 16.04

ข้อกำหนดเบื้องต้น

  • การติดตั้งล่าสุดของ CentOS 7, Debian 8 หรือ Ubuntu 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) บน Debian 8 หรือ Ubuntu 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

เนื่องจากมีความไม่เสถียรหลายอย่างที่รายงานเกี่ยวกับ ModSecurity สำหรับ Nginx master branch ดังนั้นในตอนนี้จึงขอแนะนำอย่างเป็นทางการให้ใช้nginx_refactoringสาขาเวอร์ชันล่าสุดเมื่อใดก็ตามที่เป็นไปได้

ดาวน์โหลดnginx_refactoringสาขาของ ModSecurity สำหรับ Nginx:

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

รวบรวม ModSecurity:

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) บน Debian 8 หรือ Ubuntu 16.04:

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

ขั้นตอนที่ 4: คอมไพล์ Nginx

ดาวน์โหลดและยกเลิกการเก็บถาวร Nginx ที่เสถียรล่าสุดซึ่งเป็นNginx 1.10.3ตอนที่เขียน:

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

จากนั้นคอมไพล์ Nginx ในขณะที่เปิดใช้งาน ModSecurity และโมดูล SSL:

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) บน Debian 8 หรือ Ubuntu 16.04:

ครั้งแรกที่คุณควรใช้ใช้ที่มีอยู่และกลุ่มที่มีอยู่www-datawww-data

จากนั้นคอมไพล์ Nginx ในขณะที่เปิดใช้งาน ModSecurity และโมดูล SSL:

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

เพื่อความสะดวกของคุณคุณสามารถตั้งค่าไฟล์ systemd unit สำหรับ 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 ทั้งหมดในowasp-modsecurity-crs/rules/ไดเรกทอรี ถ้าคุณต้องการใช้กฎแบบเลือกอย่างเดียวคุณควรลบ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 (ชุดกฎหลัก):

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) บน Ubuntu 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ไฟล์อย่างระมัดระวัง



Leave a Comment

วิธีการติดตั้ง Directus 6.4 CMS บน CentOS 7 LAMP VPS

วิธีการติดตั้ง Directus 6.4 CMS บน CentOS 7 LAMP VPS

เรียนรู้วิธีการติดตั้ง Directus 6.4 CMS บน CentOS 7; ระบบการจัดการเนื้อหา Headless ที่มีความยืดหยุ่นสูงและปลอดภัย

ตั้งค่า Nginx บน Ubuntu เพื่อสตรีมวิดีโอสด HLS

ตั้งค่า Nginx บน Ubuntu เพื่อสตรีมวิดีโอสด HLS

เรียนรู้วิธีการตั้งค่า Nginx บน Ubuntu สำหรับการสตรีมวิดีโอสด HLS ด้วยคำแนะนำที่ชัดเจนและเป็นประโยชน์

การสำรองข้อมูลยอดนิยมด้วย Percona XtraBackup บนแอพ WordPress แบบคลิกเดียว

การสำรองข้อมูลยอดนิยมด้วย Percona XtraBackup บนแอพ WordPress แบบคลิกเดียว

เรียนรู้การใช้ Percona XtraBackup สำหรับการสำรองข้อมูลด้วยวิธีที่เป็นระบบและง่ายดายบน WordPress ออนไลน์ของคุณ

ReactOS: นี่คืออนาคตของ Windows หรือไม่?

ReactOS: นี่คืออนาคตของ Windows หรือไม่?

ReactOS ซึ่งเป็นโอเพ่นซอร์สและระบบปฏิบัติการฟรีพร้อมเวอร์ชันล่าสุดแล้ว สามารถตอบสนองความต้องการของผู้ใช้ Windows ยุคใหม่และล้ม Microsoft ได้หรือไม่? มาหาข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบเก่านี้ แต่เป็นประสบการณ์ OS ที่ใหม่กว่ากัน

AI สามารถต่อสู้กับการโจมตีของแรนซัมแวร์ที่เพิ่มขึ้นได้หรือไม่

AI สามารถต่อสู้กับการโจมตีของแรนซัมแวร์ที่เพิ่มขึ้นได้หรือไม่

การโจมตีของ Ransomware กำลังเพิ่มขึ้น แต่ AI สามารถช่วยจัดการกับไวรัสคอมพิวเตอร์ตัวล่าสุดได้หรือไม่? AI คือคำตอบ? อ่านที่นี่รู้ว่า AI boone หรือ bane

เชื่อมต่อผ่าน WhatsApp Desktop App 24*7

เชื่อมต่อผ่าน WhatsApp Desktop App 24*7

ในที่สุด Whatsapp ก็เปิดตัวแอพเดสก์ท็อปสำหรับผู้ใช้ Mac และ Windows ตอนนี้คุณสามารถเข้าถึง Whatsapp จาก Windows หรือ Mac ได้อย่างง่ายดาย ใช้ได้กับ 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 เครื่องมือดึงข้อมูลเชิงพาณิชย์ของ Big Data

13 เครื่องมือดึงข้อมูลเชิงพาณิชย์ของ Big Data

13 เครื่องมือดึงข้อมูลเชิงพาณิชย์ของ Big Data

ระบบไฟล์บันทึกคืออะไรและทำงานอย่างไร

ระบบไฟล์บันทึกคืออะไรและทำงานอย่างไร

คอมพิวเตอร์ของเราจัดเก็บข้อมูลทั้งหมดในลักษณะที่เรียกว่าระบบไฟล์บันทึก เป็นวิธีการที่มีประสิทธิภาพที่ช่วยให้คอมพิวเตอร์สามารถค้นหาและแสดงไฟล์ได้ทันทีที่คุณกดค้นหาhttps://wethegeek.com/?p=94116&preview=true