話說時間過得真快,轉眼間又收到上半年度局端的伺服器「弱點管制表」通報,其中當然免不了那些PHP/Apache需要升級項目,但本次多了三個新弱點項目,做個紀錄日後就能SOP處理!
弱點名稱:備份檔案洩漏
弱點概述:It is possible to retrieve file backups from the remote web server.
弱點名稱:Web 伺服器的目錄/資料夾可被瀏覽
弱點概述:Some directories on the remote web server are browsable.
弱點名稱:Web 應用程式具有點擊劫持(ClickJacking)的弱點
弱點概述:The remote web server may fail to mitigate a class of web application vulnerabilities.
這三個弱點可以一次解決,只需要找到 HTTPS vhost,也就是 <IfModule mod_ssl.c> 裡的 <VirtualHost *:443>。
該檔案通常由 certbot 自動產生,檔案名稱多為:000-default-le-ssl.conf(通常位於 sites-available 資料夾中,並連結到 sites-enabled 啟用)
sudo nano /etc/apache2/sites-available/000-default-le-ssl.conf 打開編輯
<IfModule mod_ssl.c>
<VirtualHost *:443>
略........................
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
#防護設定加在DocumentRoot 後面
#這段解決Web 伺服器的目錄/資料夾可被瀏覽問題
<Directory /var/www/html>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
#這段解決備份檔案洩漏
<FilesMatch "(?i)(~$|\.bak$|\.old$|\.orig$|\.save$|\.backup$|\.tmp$|\.temp$|\.swp$|\.swo$|\.sql$|\.zip$|\.tar$|\.tgz$|\.gz$|\.bz2$|\.xz$|\.7z$|\.rar$|\.log$|\.env$|\.ini$|\.conf$)">
Require all denied
</FilesMatch>
#這段解決Web 應用程式具有點擊劫持(ClickJacking)的弱點
<IfModule mod_headers.c>
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Content-Security-Policy "frame-ancestors 'self';"
</IfModule>
略........................
</VirtualHost>
</IfModule>
然後執行
sudo a2enmod headers
sudo apache2ctl configtest
sudo systemctl reload apache2
最後驗證
curl -I https://您的網址.tw/
返回HTTP/1.1 200 OK
curl -I https://您的網址.tw/uploads/
返回HTTP/1.0 404 Not Found
curl -I https://您的網址.tw/uploads/test.sql (臨時放一個 test.sql 測試檔,測試用 test.sql 驗證完成後請立即刪除。)
返回HTTP/1.1 403 Forbidden
這樣就一次解決(敏感檔案、目錄瀏覽、iframe 防護)等弱點問題,有需要的朋友參考看看。
弱點名稱:備份檔案洩漏
弱點概述:It is possible to retrieve file backups from the remote web server.
弱點名稱:Web 伺服器的目錄/資料夾可被瀏覽
弱點概述:Some directories on the remote web server are browsable.
弱點名稱:Web 應用程式具有點擊劫持(ClickJacking)的弱點
弱點概述:The remote web server may fail to mitigate a class of web application vulnerabilities.
這三個弱點可以一次解決,只需要找到 HTTPS vhost,也就是 <IfModule mod_ssl.c> 裡的 <VirtualHost *:443>。
該檔案通常由 certbot 自動產生,檔案名稱多為:000-default-le-ssl.conf(通常位於 sites-available 資料夾中,並連結到 sites-enabled 啟用)
sudo nano /etc/apache2/sites-available/000-default-le-ssl.conf 打開編輯
<IfModule mod_ssl.c>
<VirtualHost *:443>
略........................
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
#防護設定加在DocumentRoot 後面
#這段解決Web 伺服器的目錄/資料夾可被瀏覽問題
<Directory /var/www/html>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
#這段解決備份檔案洩漏
<FilesMatch "(?i)(~$|\.bak$|\.old$|\.orig$|\.save$|\.backup$|\.tmp$|\.temp$|\.swp$|\.swo$|\.sql$|\.zip$|\.tar$|\.tgz$|\.gz$|\.bz2$|\.xz$|\.7z$|\.rar$|\.log$|\.env$|\.ini$|\.conf$)">
Require all denied
</FilesMatch>
#這段解決Web 應用程式具有點擊劫持(ClickJacking)的弱點
<IfModule mod_headers.c>
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Content-Security-Policy "frame-ancestors 'self';"
</IfModule>
略........................
</VirtualHost>
</IfModule>
然後執行
sudo a2enmod headers
sudo apache2ctl configtest
sudo systemctl reload apache2
最後驗證
curl -I https://您的網址.tw/
返回HTTP/1.1 200 OK
curl -I https://您的網址.tw/uploads/
返回HTTP/1.0 404 Not Found
curl -I https://您的網址.tw/uploads/test.sql (臨時放一個 test.sql 測試檔,測試用 test.sql 驗證完成後請立即刪除。)
返回HTTP/1.1 403 Forbidden
這樣就一次解決(敏感檔案、目錄瀏覽、iframe 防護)等弱點問題,有需要的朋友參考看看。
工作心的撰寫:徐嘉裕 Neil hsu
留言
張貼留言