[Apache2] 修改 HTTP 回應標頭欄位中的 Server 欄位

伺服器資訊包含類型(IIS、Apache 或 Nginx)與版本是一個很重要的資訊,當駭客想針對一個網頁應用程式下手時,必定先蒐集這些情報,再看有沒有尚未修復的漏洞可以利用。因此,若不想使這些資訊這麼透明,我們可以採取一些手段。這篇文章以 Apache2 為例子,相信不同類型的伺服器的概念都是一樣的。

(環境:Ubuntu20.04LTS)
我們分為兩個階段,第一個階段說明如何從 HTTP 回應標頭(HTTP Response Header)來蒐集伺服器資訊,讓讀者心裡有個底為什麼接下來要這樣改;第二個階段就是去修改 HTTP 回應頭欄位中的 Server 欄位,這裡會採取兩個方式,一個是使其只剩下「Apache」,版本與作業系統資訊不會告知,第二個是將Server欄位改為自己想要的輸出顯示,可以連「Apache」都不顯示。
  1. 從 HTTP 回應標頭(HTTP Response Header)來蒐集伺服器資訊
    1. 前置說明
      • 伺服器 IP 為localhost
      • 在當前目錄下創建檔案「message」,內容如下
        HEAD / HTTP/1.1
        Host: localhost
                  		
        這裡特別秀出16進位,注意每一行後都有「\r\n」,也就是 0x0d0a,最後一個空白行「\r\n」可別忘了!因為 linux 的換行只有「\n」,如何加上「\r」要請讀者自行研究
        00000000: 4845 4144 202f 2048 5454 502f 312e 310d  HEAD / HTTP/1.1.
        00000010: 0a48 6f73 743a 2031 3237 2e30 2e30 2e31  .Host: 127.0.0.1
        00000020: 3a38 300d 0a0d 0a                        :80....
    2. 使用NetCat來取得資訊
      user@pc:~$ nc localhost 80 < message
      HTTP/1.1 200 OK
      Date: Thu, 03 Sep 2020 01:37:03 GMT
      Server: Apache/2.4.41 (Ubuntu)
      Last-Modified: Thu, 03 Sep 2020 01:24:28 GMT
      ETag: "2aa6-5ae5e9d0acf52"
      Accept-Ranges: bytes
      Content-Length: 10918
      Vary: Accept-Encoding
      Content-Type: text/html
    3. 從上個步驟的結果(紅字),可以知道現在的伺服器是什麼,版本是什麼,甚至!運作在什麼作業系統上,這些都是當這些服務被發現有漏洞時,對我們不利的點!因此,要如何隱藏這個資訊是我們該學的,雖然隱藏起來不能說就沒有漏洞,只是攻方在沒有這些情報下會比較難鎖定,若能拖延到補丁出現,受到攻擊的機率就降低了!
  2. 修改 HTTP 回應頭欄位中的 Server 欄位
    1. 前置說明
      • 修改的方法有兩種,一種是修改 *.conf 檔;另一種是修改原始碼,再自行編譯。
      • 修改 *.conf 檔:用 apt-get 安裝的 Apache 只能用這個方法,優點是快速,缺點是至少還會留下「Apache」的資訊,讓使用者知道這是 Apache 。
      • 自行編譯:優點是可以完全改為自己想顯示的文字,缺點是要自行編譯,若要使 Apache 更新,還要再編譯一次,無法像 apt-get 安裝的那樣直接更新。
    2. 修改 *.conf 檔,這裡使用的 Apache 版本為 2.4.41,不同版本所要修改的conf檔與其所在位置可能不同
      1. 修改 security.conf ,其位置在「/etc/apache2/conf-available/security.conf」,將內容中的 ServerTokens 所對應的值改為 Prod
        user@pc:~$ sudo vim /etc/apache2/conf-available/security.conf
        #
        # ServerTokens
        # This directive configures what you return as the Server HTTP response
        # Header. The default is 'Full' which sends information about the OS-Type
        # and compiled in modules.
        # Set to one of:  Full | OS | Minimal | Minor | Major | Prod
        # where Full conveys the most information, and Prod the least.
        #ServerTokens Minimal
        ServerTokens Prod
        #ServerTokens Full
      2. 重啟 Apache 服務,並且再做一次資訊蒐集,可以發現版本與 OS 的資訊已被隱蔽
        user@pc:~$ sudo /etc/init.d/apache2 restart
        user@pc:~$ nc localhost 80 < message
        HTTP/1.1 200 OK
        Date: Thu, 03 Sep 2020 07:50:50 GMT
        Server: Apache
        Last-Modified: Thu, 03 Sep 2020 07:25:19 GMT
        ETag: "2aa6-5ae63a79037be"
        Accept-Ranges: bytes
        Content-Length: 10918
        Vary: Accept-Encoding
        Content-Type: text/html
      3. 這個方法最多只能只能遮蔽到這個程度,知道你是 Apache,但版本與 OS 無法從此處得知。
    3. 自行編譯
      1. 先到官網上選擇要下載的版本(選tar.gz),並下載。
      2. 編譯步驟官網上都有教學,但我們要修改原始碼,所以還是會細講一下,先按照它所說的解壓縮並進到資料。其中的 httpd-NN 就是妳下載的版本啦!
        $ gzip -d httpd-NN.tar.gz #解壓gz
        $ tar xvf httpd-NN.tar    #解壓tar
        $ cd httpd-NN             #進到目錄下
      3. 開啟「httpd-NN/include/ap_release.h」找到「#define AP_SERVER_BASEPRODUCT "Apache"」,將 Apache 改為想要顯示在 Server 欄位的文字,僅需修改此處就可以往下個步驟
        user@pc:~/Downloads/httpd-2.4.46 $ sudo vim ./include/ap_release.h
        #define AP_SERVER_BASEVENDOR "Apache Software Foundation"
        #define AP_SERVER_BASEPROJECT "Apache HTTP Server"
        #define AP_SERVER_BASEPRODUCT "Server field masked!"
      4. 產出 Makefile ,在這步驟可能會遇到相依套件沒裝的問題「apr、apr-util與prec」,所以先安裝相關套件,網路上很多都教下載這些套件的原碼再 compile,但我認為若可以 apt-get 就盡量使用它,因為它可以用 update/upgrade更新版本!管理上方便許多
        user@pc:~/Downloads/httpd-2.4.46 $ sudo apt-get install gcc libapr1 libapr1-dev libaprutil1 libaprutil1-dev libpcre3 libpcre3-dev
        user@pc:~/Downloads/httpd-2.4.46 $ ./configure      #若看官網會看到--prefix=PREFIX,PREFIX是想安裝Apache的地方
                                                            #這裡不指定的話,就是安裝在預設路徑/usr/local/apache2
                                                            #沒甚麼特別需求就留給預設路徑即可
      5. 上一步若正常運作,就會產出 Makefile,現在就可以來編譯然後安裝啦。安裝完預設是關閉服務的,記得開啟嘿!
        user@pc:~/Downloads/httpd-2.4.46 $ make                                           #編譯
        user@pc:~/Downloads/httpd-2.4.46 $ sudo make install                              #安裝
        user@pc:~/Downloads/httpd-2.4.46 $ sudo /usr/local/apache2/bin/apachectl -k start #開啟服務
      6. 開啟服務後,檢查 Server 欄位是否被修改了,可以看到的確是剛修改的內容「Server field masked!」,但是版本與 OS 的資訊還在,要改的東西還沒完,請繼續下一步!
        user@pc:~$ nc localhost 80 < message
        HTTP/1.1 200 OK
        Date: Thu, 03 Sep 2020 09:29:14 GMT
        Server: Server field masked!/2.4.46 (Unix)
        Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
        ETag: "2d-432a5e4a73a80"
        Accept-Ranges: bytes
        Content-Length: 45
        Content-Type: text/html
      7. 跟第一個修改方法很類似,要去修改一個 *.conf 檔,這裡為「/usr/local/apache2/conf/extra/httpd-default.conf」,將內容中的 ServerTokens 所對應的值改為 Prod
        user@pc:~$ sudo vim /usr/local/apache2/conf/extra/httpd-default.conf
        # ServerTokens
        # This directive configures what you return as the Server HTTP response
        # Header. The default is 'Full' which sends information about the OS-Type
        # and compiled in modules.
        # Set to one of:  Full | OS | Minor | Minimal | Major | Prod
        # where Full conveys the most information, and Prod the least.
        #
        ServerTokens Prod
        
      8. 還沒完喔!還要修改一個 *.conf 檔,這裡為「/usr/local/apache2/conf/httpd.conf」,將內容中「#Include conf/extra/httpd-default.conf」前的井字號拿掉, 確保上一步的「httpd-default.conf」設定會被吃到
        # Various default settings
        Include conf/extra/httpd-default.conf
      9. 重啟服務,檢查 Server 欄位,可以看到的只剩下剛修改的內容「Server field masked!」完全無法從Server欄位得知這是什麼伺服器,更狠一點,也可以改為「Microsoft-IIS/10.0」來混淆視聽
        user@pc:~$ sudo /usr/local/apache2/bin/apachectl -k restart #重啟服務
        user@pc:~$ nc localhost 80 < message
        HTTP/1.1 200 OK
        Date: Thu, 03 Sep 2020 09:49:28 GMT
        Server: Server field masked!
        Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
        ETag: "2d-432a5e4a73a80"
        Accept-Ranges: bytes
        Content-Length: 45
        Content-Type: text/html

留言

這個網誌中的熱門文章