การปิดชื่อ NGINX และ VERSION ที่ตอบกลับมาจากเครื่องให้บริการ

เมื่อเกิดการประเมินช่องโหว่บางครั้งเครื่องให้บริการที่ติดตั้ง Nginx เพื่อให้บริการเว็บไซต์หรือ Port หมายเลขอื่น ๆ ที่เกี่ยวข้องกับ HTTP response ตอบกลับค่า nginx และเปิดเผยเลข version ต่าง ๆ นั้น สามารถปิดได้หรือแก้เป็นค่าที่ต้องการได้ โดยบทความนี้กล่าวถึงวิธีการปิดชื่อ Server และ เลข Version ของ โดยทดลองติดตั้ง NGINX บน KALI จากนั้นเริ่มใช้ วิธีการแก้ปัญหาเปิดเผยเลข Version สุดท้ายเป็นวิธี การแก้ปัญหาเปิดเผยคำว่า NGINX ส่วนสุดท้ายเป็นวิธีการแก้ปัญหาเมื่อเจอ Error ต่าง ๆ และอ้างอิงเพิ่มเติมเพื่อค้นหาข้อมูล

อ้างอิงจากการทำ VA และ Pentest

Warunyou Sunpachit และ Boonperm Mark
Cybersecurity consultant

ทดลองติดตั้ง NGINX บน Kali เพื่อการทดลอง

ใช้คำสั่งดังนี้

sudo apt update
sudo apt install nginx

ตัวอย่างใช้คำสั่ง update

┌──(root💀kali)-[~]
└─# sudo apt update
Get:1 http://mirror.kku.ac.th/kali kali-rolling InRelease [30.6 kB]
Get:2 http://mirror.kku.ac.th/kali kali-rolling/main amd64 Packages [18.7 MB]
Get:3 http://mirror.kku.ac.th/kali kali-rolling/main amd64 Contents (deb) [43.3 MB]                                 
Get:4 http://mirror.kku.ac.th/kali kali-rolling/contrib amd64 Packages [111 kB]                                    
Get:5 http://mirror.kku.ac.th/kali kali-rolling/contrib amd64 Contents (deb) [161 kB]                              
Get:6 http://mirror.kku.ac.th/kali kali-rolling/non-free amd64 Packages [234 kB]                                  
Get:7 http://mirror.kku.ac.th/kali kali-rolling/non-free amd64 Contents (deb) [897 kB]                                                                       
Fetched 63.4 MB in 21s (2,978 kB/s)                                                                                                                          
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
1271 packages can be upgraded. Run 'apt list --upgradable' to see them.

ตัวอย่างใช้คำสั่งติดตั้ง nginx

ตรวจสอบข้อมูล HTTP ที่ตอบกลับจากเครื่องให้บริการ

ทดลองตรวจสอบ HTTP response message พบ header: nginx/1.22.0

┌──(root💀kali)-[~]
└─# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Sat, 29 Oct 2022 07:44:17 GMT
Content-Type: text/html
Content-Length: 10701
Last-Modified: Thu, 02 Sep 2021 02:08:25 GMT
Connection: keep-alive
ETag: "61303219-29cd"
Accept-Ranges: bytes

ทดลองใช้ Nessus พบ Version ตามข้างล่าง

วิธีการแก้ปัญหาเปิดเผยเลข Version

ค้นหาไฟล์ nginx.conf

┌──(root💀kali)-[/etc/nginx]
└─# ls -l nginx.conf                                                                                                    127 ⨯
-rw-r--r-- 1 root root 1447 Aug 19  2020 nginx.conf

แก้ค่าปรับแต่ง

หา #server_tokens off;

เอา # ออก หรือใช้ค่าดังรูป

ทำการ restart server

Restart server ในกรณี Server ถูกเปิดใช้งานอยู่

sudo systemctl restart nginx

ตรวจสอบผล

ไม่พบ Version แล้วใน Nmap

┌──(root💀kali)-[~]
└─# nmap -A localhost                            
Starting Nmap 7.91 ( https://nmap.org ) at 2022-10-28 22:01 +07
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000048s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 999 closed ports
PORT   STATE SERVICE VERSION
80/tcp open  http    nginx
|_http-title: Apache2 Debian Default Page: It works
Device type: general purpose
Running: Linux 2.6.X
OS CPE: cpe:/o:linux:linux_kernel:2.6.32
OS details: Linux 2.6.32
Network Distance: 0 hops

ไม่พบ Version แล้ว ใน Nessus

ทดลองใช้ CURL ก็ไม่พบแล้ว

──(root💀kali)-[~]
└─# curl -I localhost                                                                                                                                                                     7 ⨯
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 29 Oct 2022 07:41:38 GMT
Content-Type: text/html
Content-Length: 10701
Last-Modified: Thu, 02 Sep 2021 02:08:25 GMT
Connection: keep-alive
ETag: "61303219-29cd"
Accept-Ranges: bytes

วิธีการแก้ปัญหาเปิดเผยคำว่า NGINX

ติดตั้ง Dynamic module

ให้ check version ที่ Link ข้างล่างของ Nginx ที่เราติดตั้ง

┌──(root💀kali)-[~]
└─# nginx -v         
nginx version: nginx/1.22.0

จากนั้นติดตั้งให้ตรง Version ของ Nginx

wget http://nginx.org/download/nginx-1.22.0.tar.gz

ผลดังนี้

──(root💀kali)-[~/Desktop]
└─# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-28 22:25:54--  http://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’

nginx-1.22.0.tar.gz                  100%[====================================================================>]   1.02M   545KB/s    in 1.9s    

2022-10-28 22:25:56 (545 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]

แตกไฟล์

tar -xvzf nginx-1.22.0.tar.gz

ดู module ที่ติดตั้ง

──(root💀kali)-[~/Desktop]
└─# nginx -V
nginx version: nginx/1.22.0
built with OpenSSL 3.0.5 5 Jul 2022
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-YD8tFJ/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module

ดาวน์โหลดและติดตั้ง nginx-with-headers-more module

https://github.com/openresty/headers-more-nginx-module

git clone https://github.com/openresty/headers-more-nginx-module.git

──(root💀kali)-[~/Desktop]
└─# git clone https://github.com/openresty/headers-more-nginx-module.git
Cloning into 'headers-more-nginx-module'...
remote: Enumerating objects: 1453, done.
remote: Counting objects: 100% (49/49), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 1453 (delta 28), reused 30 (delta 16), pack-reused 1404
Receiving objects: 100% (1453/1453), 523.46 KiB | 1.01 MiB/s, done.
Resolving deltas: 100% (789/789), done.

ติดตั้งตรวจสอบจะพบว่าอยู่ที่ path นี้

/root/Desktop/headers-more-nginx-module/

┌──(root💀kali)-[~/Desktop]
└─# ls -l headers-more-nginx-module 
total 40
-rw-r--r-- 1 root root  1468 Oct 29 12:28 config
-rw-r--r-- 1 root root 18711 Oct 29 12:28 README.markdown
drwxr-xr-x 2 root root  4096 Oct 29 12:28 src
drwxr-xr-x 2 root root  4096 Oct 29 12:28 t
drwxr-xr-x 2 root root  4096 Oct 29 12:28 util
-rw-r--r-- 1 root root  2488 Oct 29 12:28 valgrind.suppress

เข้าไปที่ Folder ติดตั้ง nginx

cd nginx-1.22.0

ตั้งค่าโดยใช้คำสั่งด้านโดยเอาข้อมูลจากคำสั่ง nginx -V command แปะเข้าไป

./configure  --add-dynamic-module=/root/Desktop/headers-more-nginx-module/  <--with... จากคำสั่ง nginx -V command>

ตัวอย่าง

./configure  --add-dynamic-module=/root/Desktop/headers-more-nginx-module/  --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-YD8tFJ/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module

พบปัญหา Error

Error: the HTTP rewrite module requires the PCRE library

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

Ubantu: แก้ปัญหาโดยการลง Lib ดังนี้

sudo apt-get install libpcre3-dev

Error: SSL modules require the OpenSSL library.

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

Ubantu: : แก้ปัญหาโดยการลง Lib ดังนี้

sudo apt-get install libssl-dev

ถ้าไม่ติดปัญหาให้ดำเนินการสร้าง Header Module ดังนี้

แสดงผลได้ตามนี้

Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/share/nginx"
  nginx binary file: "/usr/share/nginx/sbin/nginx"
  nginx modules path: "/usr/lib/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/body"
  nginx http proxy temporary files: "/var/lib/nginx/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi"
  nginx http uwsgi temporary files: "/var/lib/nginx/uwsgi"
  nginx http scgi temporary files: "/var/lib/nginx/scgi"

ใช้คำสั่ง make

make

เสร็จเรียบร้อย

sed -e "s|%%PREFIX%%|/usr/share/nginx|" \
        -e "s|%%PID_PATH%%|/run/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/root/Desktop/nginx-1.22.0'

เมื่อติดตั้งเสร็จ จะพบ module อยู่ใน Folder

ให้ copy ไปใส่ /usr/lib/nginx/modules/ โดยใช้คำสั่งดังนี้

sudo cp ngx_http_headers_more_filter_module.so /usr/lib/nginx/modules/

ผลลัพธ์ดังนี้

┌──(root💀kali)-[~/Desktop/nginx-1.22.0/objs]
└─# sudo cp ngx_http_headers_more_filter_module.so /usr/lib/nginx/modules/
                                                                                                                                                  
┌──(root💀kali)-[~/Desktop/nginx-1.22.0/objs]
└─# ls -l /usr/lib/nginx/modules/ngx_http_headers_more_filter_module.so
-rwxr-xr-x 1 root root 231824 Oct 29 14:19 /usr/lib/nginx/modules/ngx_http_headers_more_filter_module.so

ตอนนี้เรามี headers-more-nginx-module พร้อมใช้งาน ให้ไปที่ nginx.conf

ในกรณีต้องการแก้ไขความ: ให้แก้ค่าตามต้องการดังนี้

more_set_headers ‘Server: itselectlab’;

Restart server อีกครั้ง

sudo service nginx restart
sudo service nginx start

ถ้าพบปัญหา Error

┌──(root💀kali)-[~]
└─# nginx -t                                                                                                                                              
nginx: [emerg] unknown directive "more_set_headers" in /etc/nginx/nginx.conf:21
nginx: configuration file /etc/nginx/nginx.conf test failed

ให้ติดตั้งแล้วลอง Restart อีกครั้ง

 sudo apt-get install nginx-extras
 service nginx restart

จะพบ Server: itselectlab

──(root💀kali)-[~]
└─# curl -I localhost
HTTP/1.1 200 OK
Date: Sat, 29 Oct 2022 07:59:47 GMT
Content-Type: text/html
Content-Length: 10701
Last-Modified: Thu, 02 Sep 2021 02:08:25 GMT
Connection: keep-alive
ETag: "61303219-29cd"
Server: itselectlab
Accept-Ranges: bytes

ในกรณีต้องไม่แสดง Header: ให้แก้ค่าตามต้องการดังนี้

more_clear_headers Server;

ลอง Restart อีกครั้งจะพบว่าจะไม่พบ Header Server อีกเลย

──(root💀kali)-[~]
└─# service nginx restart
                                                                                                                                                                     
┌──(root💀kali)-[~]
└─# curl -I localhost    
HTTP/1.1 200 OK
Date: Sat, 29 Oct 2022 08:04:51 GMT
Content-Type: text/html
Content-Length: 10701
Last-Modified: Thu, 02 Sep 2021 02:08:25 GMT
Connection: keep-alive
ETag: "61303219-29cd"
Accept-Ranges: bytes

คำสั่งอื่น ๆ ที่น่าสนใจ

┌──(root💀kali)-[~]
└─# sudo service nginx status 
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
     Active: active (running) since Sat 2022-10-29 15:04:19 +07; 15min ago
       Docs: man:nginx(8)
    Process: 38511 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 38512 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 38513 (nginx)
      Tasks: 5 (limit: 7079)
     Memory: 11.1M
        CPU: 43ms
     CGroup: /system.slice/nginx.service
             ├─38513 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─38514 nginx: worker process
             ├─38515 nginx: worker process
             ├─38516 nginx: worker process
             └─38517 nginx: worker process

Oct 29 15:04:19 kali systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 29 15:04:19 kali systemd[1]: Started A high performance web server and a reverse proxy server.

อ้างอิง

  1. https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
  2. https://www.cyberciti.biz/faq/star-stop-restart-apache2-webserver/
  3. https://dev.to/rshiva/how-to-nginx-remove-version-and-name-50ek
  4. https://blog.cpming.top/p/nginx-http-rewrite-module-requires-pcre
  5. https://stackoverflow.com/questions/3016956/how-do-i-install-the-openssl-libraries-on-ubuntu
  6. https://serverfault.com/questions/214242/can-i-hide-all-server-os-info