1. 程式人生 > >8.6 11.25-11.27

8.6 11.25-11.27

grace oca match tty 9.png expire head mis color

11.25 配置防盜鏈

背景:

一個免費論壇,所有人可以在該論壇上申請免費的站點,用戶運營網站不需要購買服務器或維護程序,只需要在免費的論壇平臺上申請一個論壇,申請後用戶自主運營網站;

問題:

從一段時間開始,服務器的流量劇烈增長

分析:

定位大流量的服務器,然後抓包分析,發現日誌無異常但流量中存在大量圖片信息,且對應的包中的referer信息固定(即用戶從固定的某站點跳轉到了本站點);

發現了一個可疑站點,該站點的服務器在臺灣,屬於×××網站,放在論壇站點的圖片屬於×××圖片;

我們的論壇站點可以免費無限制的上傳圖片,對方將圖片放在論壇站點,並在另一個網站引用,即對方將黃圖放在了我們論壇的服務器上,服務器在國內,圖片訪問較快,而對方的網站主體放在了臺灣,用戶訪問時獲得的×××圖片實際上是從在國內的我方論壇的服務器發出的;

對於我們沒有意義,流量上升,但用戶不會增加;

操作:

1 刪除所有×××

2 配置防盜鏈(跳轉訪問本站點時若referer信息不是認識的referer則不允許訪問)

圖片訪問地址為a域名,b域名引用了a域名的圖片,這樣不被允許;

指定圖片只能在a域名,則圖片可以被控制在我們的服務器上;

第三方站點無法引用;

配置防盜鏈:

[root@hyc-01-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/111.com"

ServerName 111.com

ServerAlias www.example.com

<Directory /data/wwwroot/111.com/*>

AllowOverride AuthConfig

AuthName "111.com user auth"

AuthType Basic

AuthUserFile /data/.htpasswd

require valid-user

</Directory>

<Directory /data/wwwroot/111.com>

SetEnvIfNoCase Referer "http://111.com" local_ref

設置該referer為白名單

SetEnvIfNoCase Referer "http://aaa.com" local_ref

#SetEnvIfNoCase Referer "^$" local_ref 將空referer設為白名單

<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)"> .txt/.doc等這樣的訪問設置為白名單,即針對此類訪問設置防盜鏈

Order Allow,Deny 行為:allow,後deny

Allow from env=local_ref 允許以上設置的三個站點即allow,其余deny

</filesmatch>

<Directory>

<IfModule mod_expires.c>

ExpiresActive on

ExpiresByType image/gif "access plus 1 days"

ExpiresByType text/css "now plus 2 hours"

測試:

[root@hyc-01-01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@hyc-01-01 ~]# /usr/local/apache2.4/bin/apachectl graceful

referer(請求baidu.png時的信息中沒有攜帶referer

技術分享圖片

refererhttp://ask.apelearn.com

技術分享圖片

技術分享圖片

curl測試時指定referer信息:

[root@hyc-01-01 111.com]# curl -e "http://ttt.com/tty" -x 192.168.31.129:80 111.com/baidu.png -I

HTTP/1.1 403 Forbidden

Date: Tue, 07 Aug 2018 11:30:50 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1

-e指定訪問時攜帶的referer信息

[root@hyc-01-01 111.com]# curl -e "http://111.com/tty" -x 192.168.31.129:80 111.com/baidu.png -I

HTTP/1.1 200 OK

Date: Tue, 07 Aug 2018 11:32:08 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Last-Modified: Wed, 06 Jun 2018 16:15:03 GMT

ETag: "105e0-56dfb781defc0"

Accept-Ranges: bytes

Content-Length: 67040

Cache-Control: max-age=86400

Expires: Wed, 08 Aug 2018 11:32:08 GMT

Content-Type: image/png

[root@hyc-01-01 111.com]# curl -e "http://aaa.com/tty" -x 192.168.31.129:80 111.com/baidu.png -I

HTTP/1.1 200 OK

Date: Tue, 07 Aug 2018 11:33:50 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Last-Modified: Wed, 06 Jun 2018 16:15:03 GMT

ETag: "105e0-56dfb781defc0"

Accept-Ranges: bytes

Content-Length: 67040

Cache-Control: max-age=86400

Expires: Wed, 08 Aug 2018 11:33:50 GMT

Content-Type: image/png

curl命令中指定referer時一定要以http://開頭

11.26 訪問控制Directory

問題:

部分http的內容(網頁、圖片等)僅針對公司內部,對外不開放,做用戶認證可能會泄露賬號密碼,存在安全隱患

解決方案:

限制ip訪問指定的內容,只有白名單的ip才能訪問

比如假設公司公網ip固定,則可以將公司的固定公網ip加入白名單,其他公網ip訪問將受到限制

被限制的ip為源ip

配置:

[root@hyc-01-01 logs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<Directory /data/wwwroot/111.com/admin/>

Order deny,allow

執行規則時無論是否匹配到對應的ip都會從頭到尾匹配完所有規則,再根據最後匹配的規則決定allowdeny

Order後先寫deny,後寫allow,則執行規則時會先執行deny的規則,後執行allow,執行allowdeny的順序與order有關,與denyallow的排列順序無關;

比如這裏先執行deny,此時127.0.0.1deny,後執行allow127.0.0.1又被allow,則最終127.0.0.1allow

Deny from all

Allow from 127.0.0.1

</Directory>

[root@hyc-01-01 logs]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@hyc-01-01 logs]# /usr/local/apache2.4/bin/apachectl graceful

測試:

創建測試內容:

[root@hyc-01-01 admin]# vim index.php

[root@hyc-01-01 admin]# ls

index.php

[root@hyc-01-01 admin]# pwd

/data/wwwroot/111.com/admin

測試:

[root@hyc-01-01 admin]# curl -x 192.168.31.129:80 111.com/admin/index.php -I

HTTP/1.1 403 Forbidden

Date: Tue, 07 Aug 2018 13:15:25 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1

[root@hyc-01-01 admin]# curl -x 127.0.0.1:80 111.com/admin/index.php -I

HTTP/1.1 200 OK

Date: Tue, 07 Aug 2018 13:15:40 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Cache-Control: max-age=0

Expires: Tue, 07 Aug 2018 13:15:40 GMT

Content-Type: text/html; charset=UTF-8

[root@hyc-01-01 logs]# tail 111.com-access_20180807.log

192.168.31.129 - - [07/Aug/2018:21:15:25 +0800] "HEAD HTTP://111.com/admin/index.php HTTP/1.1" 403 - "-" "curl/7.29.0"

127.0.0.1 - - [07/Aug/2018:21:15:40 +0800] "HEAD HTTP://111.com/admin/index.php HTTP/1.1" 200 - "-" "curl/7.29.0"

192.168.31.1 - - [07/Aug/2018:21:20:09 +0800] "GET /admin/index.php HTTP/1.1" 403 224 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)" 192.168.31.129127.0.0.1192.168.31.1均為源ip

403為限制訪問,404為找不到內容

11.27 訪問控制FilesMatch

不匹配目錄,匹配某個網頁執行策略

操作:

[root@hyc-01-01 logs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<Directory /data/wwwroot/111.com/admin/>

<FilesMatch index.php(.*)>

Order deny,allow

Deny from all

Allow from 127.0.0.1

</FilesMatch>

</Directory>

測試:

[root@hyc-01-01 admin]# curl -x192.168.31.129:80 'http://111.com/admin/index.php'

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>403 Forbidden</title> 訪問被拒絕

</head><body>

<h1>Forbidden</h1>

<p>You don't have permission to access /admin/index.php

on this server.<br />

</p>

</body></html>

[root@hyc-01-01 admin]# curl -x192.168.31.129:80 'http://111.com/admin/aaa.php'

12345qwert 訪問正常

[root@hyc-01-01 admin]# curl -x127.0.0.1:80 'http://111.com/admin/index.php' -I

HTTP/1.1 200 OK 根據策略,127.0.0.1被允許訪問http://111.com/admin/index.php

Date: Tue, 07 Aug 2018 14:15:22 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Cache-Control: max-age=0

Expires: Tue, 07 Aug 2018 14:15:22 GMT

Content-Type: text/html; charset=UTF-8


8.6 11.25-11.27