當(dāng)前位置:首頁(yè) > IT技術(shù) > Web編程 > 正文

Apache安全加固
2021-09-19 22:43:14

實(shí)驗(yàn)環(huán)境

root@NF:~# apache2 -version
Server version: Apache/2.4.29 (Ubuntu)
Server built:   2020-03-13T12:26:16
root@NF:~# cat /etc/issue
Ubuntu 18.04.3 LTS 
 l

關(guān)注官網(wǎng)更新公告

https://httpd.apache.org/security_report.html

以最小權(quán)限運(yùn)行Apache進(jìn)程

注意:本環(huán)境下的Apache默認(rèn)就是以www-data用戶運(yùn)行,默認(rèn)符合要求。

  1. 根據(jù)需要,為 Apache 服務(wù)創(chuàng)建用戶及用戶組。如果沒有設(shè)置用戶和組,則新建用戶,并在 Apache 配置文件中進(jìn)行指定。

    i. 創(chuàng)建 Apache 用戶組。

    groupadd apache

    ii. 創(chuàng)建 Apache 用戶并加入 Apache 用戶組。

    useradd apache –g apache

    iii. 將下面兩行設(shè)置參數(shù)加入 Apache 配置文件 apache2.conf 中:

     User apache
     Group apache
    
  2. 檢查 apache2.conf 配置文件中是否允許使用非專用賬戶(如 root 用戶)運(yùn)行 Apache 服務(wù)。

    默認(rèn)設(shè)置一般即符合要求。Linux 系統(tǒng)中默認(rèn)使用 apache 或者 nobody 用戶,Unix 系統(tǒng)默認(rèn)使用 daemon 用戶。

image

加固作用:

  • 以最小權(quán)限運(yùn)行中間件服務(wù),即使網(wǎng)站被getshell,也能減少影響程度。

擴(kuò)展閱讀:

查看和修改運(yùn)行 Apache 的用戶與用戶組 - 荒原之夢(mèng)

linux 下修改 apache 啟動(dòng)的所屬用戶和組 - 天涯雪

apache2為什么有多個(gè)進(jìn)程? - muru

禁用目錄瀏覽功能

編輯配置文件apache2.conf,指定網(wǎng)站根目錄添加Options FollowSymLinks參數(shù)。

<Directory /var/www/html>
	Options  FollowSymLinks
	AllowOverride None
	Require all granted
</Directory>

若要啟用目錄瀏覽功能,則是Options Indexes FollowSymLinks,同樣禁止目錄瀏覽功能除了刪除Indexes也可以在前面加個(gè)減號(hào),即Options -Indexes FollowSymLinks來表示。

image

加固作用:

  • 不展示目錄結(jié)構(gòu)信息,防止敏感文件泄露。

擴(kuò)展閱讀:

Apache Options Indexes FollowSymLinks詳解 - callie

AllowOverride參數(shù)詳解 - upupw

Apache的Order Allow,Deny 詳解 - 與時(shí)俱進(jìn)

Apache2.4使用require指令進(jìn)行訪問控制 - leoyu

啟用日志審計(jì)

apache2默認(rèn)啟用了錯(cuò)誤日志和訪問日志的記錄,可看配置文件apache.conf有無(wú)以下內(nèi)容。

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

LogFormat "%v:%p %h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" vhost_combined
LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

apache默認(rèn)日志路徑:/var/log/apache2

一條標(biāo)準(zhǔn)的訪問日志內(nèi)容如下:

192.168.56.1 - - [30/Mar/2020:16:34:56 +0800] "GET /test/index.php HTTP/1.1" 200 277 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
格式 含義
%h 遠(yuǎn)端主機(jī)(訪問網(wǎng)站的客戶端地址)
%l 遠(yuǎn)端登錄名,用了短橫代替
%u 遠(yuǎn)端用戶名,用了短橫代替
%t 時(shí)間,包括訪問的日期、時(shí)間、時(shí)區(qū)
%r 請(qǐng)求起始行,包括請(qǐng)求方法、訪問的文件路徑
%>s HTTP狀態(tài)碼
%O 響應(yīng)包數(shù)據(jù)大小,單位是字節(jié)
%{Referer}i 來源
%{User-Agent}i 遠(yuǎn)端主機(jī)瀏覽器的UA信息

加固作用:

  • 記錄訪問信息,提供溯源證據(jù)。
  • 幫助開發(fā)者排查問題。

擴(kuò)展閱讀:

Apache日志詳解 - long9617

限制特定目錄文件執(zhí)行權(quán)限

編輯配置文件apache2.conf,根據(jù)業(yè)務(wù)需求添加下面內(nèi)容。

<Directory "/var/www/html/upload">
	<FilesMatch ".(php|php3)$">
		Order allow,deny
		Deny from all
	</FilesMatch>
</Directory>

image

加固作用:

  • 通過禁止訪問來阻止一些非法文件的執(zhí)行(惡意攻擊者通過任意文件上傳漏洞,往往會(huì)上傳一些可執(zhí)行文件,如木馬文件,從而拿到webshell)。

擴(kuò)展閱讀:

apache禁止訪問文件或目錄執(zhí)行權(quán)限、禁止運(yùn)行腳本PHP文件的設(shè)置方法(轉(zhuǎn))

自定義錯(cuò)誤頁(yè)面

編輯配置文件apache2.conf,添加下面內(nèi)容

ErrorDocument 403 /custom403.html
ErrorDocument 404 /custom404.html
ErrorDocument 500 /custom500.html

其中customxxx.html為要設(shè)置的錯(cuò)誤頁(yè)面,需提前寫好放網(wǎng)站根目錄下。

root@NF:~# ls -lah /var/www/html/ | grep html
-rw-r--r--  1 ubuntu ubuntu  688 1月  19 15:00 403.html
-rw-r--r--  1 ubuntu ubuntu  685 1月  19 14:57 404.html
-rw-r--r--  1 ubuntu ubuntu  665 1月  19 15:01 500.html

image

加固作用:

  • 防止默認(rèn)報(bào)錯(cuò)頁(yè)面泄露一些敏感信息(開發(fā)框架、數(shù)據(jù)庫(kù)語(yǔ)句、物理路徑、內(nèi)網(wǎng)IP等)。

擴(kuò)展閱讀:

APACHE_自定義404錯(cuò)誤頁(yè)面 - epsilon1

隱藏Apache版本號(hào)

編輯配置文件apache2.conf,添加下面內(nèi)容

ServerSignature Off
ServerTokens Prod

image

加固作用:

  • 防止中間件版本信息泄露。

限制和允許特定IP訪問

編輯配置文件apache2.conf,添加下面內(nèi)容。

若使用IP白名單,則根據(jù)業(yè)務(wù)需求添加下面內(nèi)容

<Directory "/var/www/html/test">
	Options All
	AllowOverride None
	Order Deny,Allow
	Deny From all
	Allow From 192.168.1.0/24 192.168.3.0/24
	Allow From 127.0.0.1
</Directory>

若使用IP黑名單,則根據(jù)業(yè)務(wù)需求添加下面內(nèi)容

<Directory "/var/www/html/test">
	Options All
	AllowOverride None
	Order Deny,Allow
	Deny From 192.168.1.0/24 192.168.3.0/24
	Deny From 192.168.56.1
</Directory>

image

加固作用:

  • 使訪問受控。

擴(kuò)展閱讀:

Apache中限制和允許特定IP訪問 - we will rock you

擴(kuò)展閱讀

http://httpd.apache.org/docs/current/zh-cn/
https://blog.51cto.com/ww123/1639424
https://www.cnblogs.com/xiaozi/p/10117715.html
https://www.jianshu.com/p/a8bab3f50c7b
https://bbs.ichunqiu.com/thread-35736-1-1.html
http://www.defvul.com/apache/
https://www.alibabacloud.com/help/zh/faq-detail/52981.htm
https://blog.csdn.net/my98800/article/details/51740389

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >