詳述Linux系統中Apache網頁與安全優化(二)

網頁緩存

配置網頁的緩存時間

  • 通過mod_ expire模塊配置Apache,使網頁能在客戶端瀏覽器緩存一段時間,以避免重複請求
  • 啓用mod_ expire模塊後,會自動生成頁面頭部信息中的Expires標籤和Cache-Control標籤,從而降低客戶端的訪問頻率和次數,達到減少不必要的流量和增加訪問速度的目的

啓用網頁緩存功能步驟

  • 查看是否安裝mod_ expire模塊
  • 修改配置文件啓用緩存功能
  • 抓包測試

查看是否安裝了mod_ expire模塊

  • /usr/local/apache/bin/apachectl -t -D DUMP_MODULES
  • 如果輸出中沒有expires_module (static), 則說明編譯時沒有安裝mod_expires

如果沒有安裝則要重新編譯安裝

  • ./configure --enable-expires...
  • make && make install

修改httpd.conf配置文件

  • 啓用mod_ expires模塊, 並設置http協議下任意格式的文檔均60秒後過期
    • <lfModule mod_expires.c>
      ExpiresActive On
      ExpiresDefault "access plus 60 seconds"
      </lfModule>

重啓httpd服務

再次訪問測試網站,使用抓包工具Fiddler進行數據抓取分析

配置實例

  • 在此處我將接着上一篇實驗繼續做,在上一篇實驗中,手工編譯安裝Apache服務時已經安裝過expires模塊,這裏我直接測試
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
...//省略部分內容...
#LoadModule logio_module modules/mod_logio.so
LoadModule env_module modules/mod_env.so
LoadModule expires_module modules/mod_expires.so     //找到此模塊,去掉#開啓模塊
LoadModule headers_module modules/mod_headers.so
#LoadModule unique_id_module modules/mod_unique_id.so
...//省略部分內容...
<IfModule mod_expires.c>          //在文件末尾處添加以下條目,對expires模塊進行配置
  ExpiresActive On                //開啓功能                     
  ExpiresDefault" access plus 50 seconds"       //設置緩存時間
</IfModule>
:wq                       //保存退出
[root@localhost ~]# apachectl -t    //驗證語法格式
Syntax OK                   //語法正常     
[root@localhost ~]# service httpd stop      //停止HTTP服務
[root@localhost ~]# service httpd start     //啓動HTTP服務
[root@localhost ~]# /usr/local/httpd/bin/apachectl -t -D DUMP_MODULES |grep "expires" 
//檢測模塊是否開啓
 expires_module (shared)  //成功開啓
[root@localhost ~]# netstat -ntap | grep 80    //查看端口是否開啓
tcp        0      0 192.168.144.133:80      0.0.0.0:*               LISTEN      47752/httpd  
  • 在客戶機中訪問網頁,使用抓包工具看緩存模塊是否開啓
    詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)

Apache安全優化

配置防盜鏈

  • 防盜鏈就是防止別人的網站代碼裏面盜用服務器的圖片、文件、視頻等相關資源
  • 如果別人盜用網站的這些靜態資源,明顯的是會增大服務器的帶寬壓力
  • 所以作爲網站的維護人員,要杜絕我們服務器的靜態資源被其他網站盜用

配置Apache實現防盜鏈

  • 檢查Apache是否安裝了mod_rewrite模塊

    • /usr/local/apache/bin/apachectl -t -D DUMP_ MODULES
    • 如果輸出中沒有rewrite_module (static), 則說明編譯時沒有安裝mod_ rewrite模塊
  • 如果沒有安裝則要重新編譯安裝
    • ./configure --enable-rewrite...
    • make && make install

配置規則變量說明

  • %{HTTP_ REFERER}: 瀏覽header中的鏈接字段,存放一-個鏈接的URL,代表是從哪個鏈接訪問所需的網頁
  • !^:不以後面的字符串開頭
  • .*$: 以任意字符結尾
  • NC:不區分大寫
  • R:強制跳轉

規則匹配說明

  • RewriteEngine On: 打開網頁重寫功能
  • RewriteCond: 設置匹配規則
  • RewriteRule: 設置跳轉動作

規則匹配

  • 如果相應變量的值匹配所設置的規則,則逐條往下處理;如果不匹配,則往後的規則不再匹配

配置操作演示

  • 修改配置文件啓用防盜鏈功能並設置規則:
    • RewriteEngine On
    • RewriteCond %{HTTP_ REFERER} !^http://test.com/.*$ [NC]*
    • RewriteCond %{HTTP_ REFERER} !^http://test.com$ [NC]
    • RewriteCond %{HTTP_ REFERER} !^http://www.test.com/.*$ [NC]*
    • RewriteCond %{HTTP_ REFERER} !^http://www.test.com$ [NC]
    • RewriteRule .*\.(gifljipg|swf)$ http://www.test.com/error.html [R,NC]

配置實例

  • 首先安裝DNS服務,並配置DNS服務,我們這裏是同域名訪問網頁;在前面手工編譯安裝Apache服務時已經安裝過放掉連模塊插件mod_rewrite,直接進入HTTP主著配置文件進行配置。
[root@localhost ~]# yum install bind -y
已加載插件:fastestmirror, langpacks
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00 
...//省略部分內容...
已安裝:
  bind.x86_64 32:9.11.4-9.P2.el7                                                

作爲依賴被安裝:
  bind-export-libs.x86_64 32:9.11.4-9.P2.el7                                    

作爲依賴被升級:
  bind-libs.x86_64 32:9.11.4-9.P2.el7                                           
  bind-libs-lite.x86_64 32:9.11.4-9.P2.el7                                      
  bind-license.noarch 32:9.11.4-9.P2.el7                                        
  bind-utils.x86_64 32:9.11.4-9.P2.el7                                          
  dhclient.x86_64 12:4.2.5-77.el7.centos                                        
  dhcp-common.x86_64 12:4.2.5-77.el7.centos                                     
  dhcp-libs.x86_64 12:4.2.5-77.el7.centos                                       

完畢!
[root@localhost ~]# vim /etc/named.conf
...//省略部分內容...
options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };
...//省略部分內容...
:wq
[root@localhost ~]# vim /etc/named.rfc1912.zones
...//省略部分內容...
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};
...//省略部分內容...
:wq
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133
[root@localhost named]# systemctl start named   //啓動DNS服務
  • 這裏我們先不開啓盜鏈功能,現在客戶機中訪問網頁圖片,然後在開啓一臺win 7客戶機,安裝http服務,盜鏈圖片,看是否可以盜鏈
    詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)詳述Linux系統中Apache網頁與安全優化(二)
  • 在Linux系統中配置防盜鏈模塊
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf   //編輯主配置文件
...//省略部分內容...
#LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so     //找到此條,並開啓此條目

<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
...//省略部分內容...
<Directory "/usr/local/httpd/htdocs">           //在此標籤下添加防盜鏈條目
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
    RewriteEngine On               //開啓防盜鏈功能
    RewriteCond %{HTTP_REFERER} !^http://kgc.com/.*$ [NC]   
    RewriteCond %{HTTP_REFERER} !^http://kgc.com$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http:// www.kgc.com/$ [NC]
    RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png
 </Directory>
 ...//省略部分內容...
 :wq
 [root@localhost ~]# cd /mnt               //進入掛載目錄
[root@localhost mnt]# ls       //查看是否有準備好的防盜鏈圖片
apr-1.6.2.tar.gz       cronolog-1.6.2-14.el7.x86_64.rpm  httpd-2.4.29.tar.bz2  mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz  Discuz_X2.5_SC_UTF8.zip           LAMP-php5.6.txt       nginx-1.12.0.tar.gz
awstats-7.6.tar.gz     error.png                         miao.jpg              php-5.6.11.tar.bz2
[root@localhost mnt]# cp error.png /usr/local/httpd/htdocs/   //將防盜鏈圖片複製入http站點目錄
[root@localhost mnt]# cd /usr/local/httpd/htdocs/  //進入站點目錄
[root@localhost htdocs]# ls         //查看
error.png  index.html  miao.jpg      //成功複製
[root@localhost htdocs]# systemctl start httpd   //重啓網絡服務
  • 再次在客戶機中訪問網頁,看防盜鏈功能是否開啓
    詳述Linux系統中Apache網頁與安全優化(二)

    隱藏版本信息

Apache隱藏版本信息

  • Apache的版本信息,透露了一定的漏洞信息,從而給網站帶來安全隱患
  • 生產環境中要配置Apache隱藏版本信息
  • 可使用Fiddler抓包工具分析

配置Apache隱藏版本信息

  • 將主配置文件httpd.conf以下行註釋去掉

    • #Include conf/extra/httpd-default.conf
  • 修改httpd-default.conf文件兩個地方
  • ServerTokens Full修改爲Server Tokens Prod
  • ServersSignature On修改爲ServersSignature Off

重啓httpd服務,訪問網站,抓包測試

配置實例

  • 現在客戶機中使用抓包工具,看看是否會顯示版本信息
    詳述Linux系統中Apache網頁與安全優化(二)
  • 進入HTTP配置文件,更改信息
[root@localhost htdocs]# vim /usr/local/httpd/conf/httpd.conf 
...//省略部分內容...
#Include conf/extra/httpd-dav.conf

# Various default settings
Include conf/extra/httpd-default.conf     //找到此條目,並去掉註釋

# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
...//省略部分內容...
:wq             //保存退出
[root@localhost htdocs]# cd /usr/local/httpd/conf/extra/     //進入目錄
[root@localhost extra]# ls           
httpd-autoindex.conf  httpd-info.conf       httpd-mpm.conf                 httpd-userdir.conf
httpd-dav.conf        httpd-languages.conf  httpd-multilang-errordoc.conf  httpd-vhosts.conf
httpd-default.conf    httpd-manual.conf     httpd-ssl.conf                 proxy-html.conf
[root@localhost extra]# vim httpd-default.conf     //編輯配置文件
...//省略部分內容...
# Set to one of:  Full | OS | Minor | Minimal | Major | Prod
# where Full conveys the most information, and Prod the least.
#
ServerTokens Prod          //找到此條目,並更改Full爲Prod

#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory 
# listings, mod_status and mod_info output etc., but not CGI generated 
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of:  On | Off | EMail
# 
ServerSignature Off                   //並確定此處是否爲關閉,默認爲關閉

#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
:wq              //保存退出
[root@localhost extra]# systemctl restart httpd.service   //重啓服務
  • 再次在客戶機中測試抓包,看是否還顯示版本信息
    詳述Linux系統中Apache網頁與安全優化(二)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章