2011年1月11日 星期二

Secure Apache Web Server

1. Secure Apache Web Server

    要保護 Apache Web Server ,除了可以使用一般的存取控制及適當設定Apache Web Server 的設定檔『/etc/httpd/conf/httpd.conf』中的安全相關設定外,還可以使用FirewallSELinux 來增強 Apache Web Server 的安全性。


2. Firewall


    先確認一下Firewall是否已開放 WWW(HTTPport 80)及 Secure WWW(HTTPSport 443)。

    在GUI 執行[System]-->[Security Level and Firewall] 或在TUI執行system-config-securitylevel』命令啟動『Security Level Configuration』。





    確認或開放WWW(HTTPport 80)Secure WWW(HTTPSport 443) 的防火牆規 則設定。    
    
    也可以在TUI執行『iptables -L』命令檢視防火牆規則。
[root@lab2 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere


Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere


Chain OUTPUT (policy ACCEPT)
target prot opt source destination


Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT esp -- anywhere anywhere
ACCEPT ah -- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:https
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
[root@lab2 ~]#

    確認下列兩行防火牆規則已存在於防火牆規則清單中。
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:https
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
3. 確認SELinux 的現行模式

  接著確認 SELinux的現行運作模式是否為『Enforcing』。


 在GUI 執行[System]-->[Security Level and Firewall] 或在TUI執行
system-config-securitylevel』命令啟動『Security Level Configuration』,檢視 SELinux 的現行模式。

 確認SELinux 的模式為『Enforcing』。


    或在 TUI 執行『getenforce』命令檢視SELinux 模式設定值。
[root@lab2 ~]# getenforce
Enforcing
4.Apache Web Server 主要目錄及檔案的security contexts    



    使用『ls -dZ』檢視『/etc/httpd/』、『/etc/httpd/conf/』、
/etc/httpd/conf.d/』、『 /var/www/html/』等目錄的security contexts
[root@lab2 ~]# ls -dZ /etc/httpd/ /etc/httpd/conf/ /etc/httpd/conf.d/ /var/www/html/
drwxr-xr-x root root system_u:object_r:httpd_config_t /etc/httpd/
drwxr-xr-x root root system_u:object_r:httpd_config_t /etc/httpd/conf/
drwxr-xr-x root root system_u:object_r:httpd_config_t /etc/httpd/conf.d/
drwxr-xr-x root root system_u:object_r:httpd_sys_content_t /var/www/html/
    整理如下表,
DirectoryName
Identify
Role
Typr
/etc/httpd
system_u
object_r
httpd_config_t
/etc/httpd/conf
system_u
object_r
httpd_config_t
/etc/httpd/conf.d/
system_u
object_r
httpd_config_t
/var/www/html
system_u
object_r
httpd_sys_content_t

    先在『/var/www/html』目錄中建立『index.html』。
[root@lab2 ~]# touch /var/www/html/index.html

    使用『ls -lZ』檢視『/etc/httpd/conf/httpd.conf』、 /var/www/html/index.html』等檔案的security contexts
[root@lab2 ~]# ls -Z /etc/httpd/conf/httpd.conf /var/www/html/index.html
-rw-r--r-- root root system_u:object_r:httpd_config_t /etc/httpd/conf/httpd.conf
-rw-r--r-- root root user_u:object_r:httpd_sys_content_t /var/www/html/index.html
    整理如下表,
FileName
Identify
Role
Typr
/etc/httpd/conf/httpd.conf
system_u
object_r
httpd_config_t
/var/www/html/index.html
system_u
object_r
httpd_sys_content_t
5. security contexts 設定錯誤時如何修正


    security contexts 設定錯誤導致設定檔或網頁無法被存取時可以使用 chcon』及『restorecon』進行修正。


7. 修正個人網頁存放目錄及個人網頁檔的security contexts 設定錯誤


    a.修改 「/etc/httpd/conf/httpd.conf」的下列Directive,並重新啟動 httpd
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
#UserDir disable
加上井號
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disable" line above, and uncomment
# the following line instead:
#
#UserDir public_html
UserDir html
去除井號,設定使用者個人網頁目錄名稱
</IfModule>


    b. 建立個人網頁放置目錄
[max@lab2 ~]$ mkdir html

    c. 建立個人網頁
[max@lab2 ~]$ touch html/index.html
[max@lab2 ~]$ echo "Privat Test Page" >> html/index.html

    d. 檢視個人網頁

    出現無檢視權限的錯誤


    e. 檢視個人網頁放置目錄權限設定

[max@lab2 ~]$ ls -ld html/
drwxrwxr-- 2 max max 4096 Jan 11 03:48 html/
[max@lab2 ~]$ ls -l html/index.html
-rw-rw-r-- 1 max max 17 Jan 11 03:49 html/index.html

    個人網頁放置目錄的other 無『x』權限,所以無法存取個人網頁。

    f. 在家目錄及個人網頁放置目錄增加other的『x』權限
[[max@lab2 ~]$ ls -ld ~/ ~/html
drwx------ 19 max max 4096 Jan 11 03:47 /home/max/
drwxrwxr-- 2 max max 4096 Jan 11 03:48 /home/max/html
[max@lab2 ~]$ chmod o+x ~/ ~/html
[max@lab2 ~]$ ls -ld ~/ ~/html
drwx-----x 19 max max 4096 Jan 11 03:47 /home/max/
drwxrwxr-x 2 max max 4096 Jan 11 03:48 /home/max/html
    但仍是出現無存取權限的錯誤



    g. 檢視個人網頁放置目錄及個人網頁的security contexts 設定
[max@lab2 ~]$ ls -dZ html/
drwxrwxr-x max max user_u:object_r:user_home_t html/
[max@lab2 ~]$ ls -Z html/index.html
-rw-rw-r-- max max user_u:object_r:user_home_t html/index.html

h. 修正個人網頁放置目錄及個人網頁的security contexts 設定
[max@lab2 ~]$ chcon -R --referenc=/var/www/html/ ~/html/
[max@lab2 ~]$ ls -dZ html/
drwxrwxr-x max max system_u:object_r:httpd_sys_content_t html/
[max@lab2 ~]$ ls -Z html/index.html
-rw-rw-r-- max max system_u:object_r:httpd_sys_content_t html/index.html

i. 這樣就看得到個人網頁了


沒有留言:

張貼留言

Powered By Blogger