配置防盗链、访问控制Directory、访问控制FilesMatch

11.25 配置防盗链

盗链的定义
此内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接在自己的有广告有利益的页面上向最终用户提供此内容。 常常是一些名不见经传的小网站来盗取一些有实力的大网站的地址(比如一些音乐、图片、软件的下载地址)然后放置在自己的网站中,通过这种方法盗取大网站的空间和流量。
为什么会产生盗链
一般浏览有一个重要的现象就是一个完整的页面并不是一次全部传送到客户端的。如果请求的是一个带有许多图片和其它信息的页面,那么最先的一个Http请求被传送回来的是这个页面的文本,然后通过客户端的浏览器对这段文本的解释执行,发现其中还有图片,那么客户端的浏览器会再发送一条Http请求,当这个请求被处理后那么这个图片文件会被传送到客户端,然后浏览器会将图片安放到页面的正确位置,就这样一个完整的页面也许要经过发送多条Http请求才能够被完整的显示。基于这样的机制,就会产生一个问题,那就是盗链问题:就是一个网站中如果没有起页面中所说的信息,例如图片信息,那么它完全可以将这个图片的连接到别的网站。这样没有任何资源的网站利用了别的网站的资源来展示给浏览者,提高了自己的访问量,而大部分浏览者又不会很容易地发现,这样显然,对于那个被利用了资源的网站是不公平的。一些不良网站为了不增加成本而扩充自己站点内容,经常盗用其他网站的链接。一方面损害了原网站的合法利益,另一方面又加重了服务器的负担。

  • 通过限制referer来实现防盗链的功能
  • 编辑配置
    #配置文件增加如下内容
        <Directory /data/wwwroot/123.com>
            #定义白名单
        SetEnvIfNoCase Referer "http://123.com" local_ref
        SetEnvIfNoCase Referer "http://aaa.com" local_ref
                #第一个空名单
        #SetEnvIfNoCase Referer "^$" local_ref  //空的
                #定义规则
        <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">   //对这些设置做一个防盗链
                 #定义访问顺序的
            Order Allow,Deny
            Allow from env=local_ref
        </filesmatch>
    </Directory>

检查、加载配置文件

[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl -t
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl graceful
#curl 测试
[root@taoyuan ~]# curl -x127.0.0.1:80 123.com/qq.png -I
HTTP/1.1 200 OK
Date: Mon, 25 Dec 2017 14:29:44 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Last-Modified: Thu, 21 Dec 2017 13:47:55 GMT
ETag: "193e-560d9f26db8c0"
Accept-Ranges: bytes
Content-Length: 6462
Content-Type: image/png

[root@taoyuan ~]# curl -e "http://www.qq.com/123.txt" -x127.0.0.1:80 123.com/qq.png -I
HTTP/1.1 403 Forbidden
Date: Mon, 25 Dec 2017 14:32:56 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

[root@taoyuan ~]# curl -e "http://123.com/123.txt" -x127.0.0.1:80 123.com/qq.png -I
HTTP/1.1 200 OK
Date: Mon, 25 Dec 2017 14:33:13 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Last-Modified: Thu, 21 Dec 2017 13:47:55 GMT
ETag: "193e-560d9f26db8c0"
Accept-Ranges: bytes
Content-Length: 6462
Content-Type: image/png

11.26 访问控制Directory

  • 核心配置文件内容
    以限制一个目录的形式访问
    
    #格式
    <Directory /data/wwwroot/123.com/admin/>  //必须写绝对路径
       Order deny,allow  //执行顺序,执行过程中都会从头到尾
       Deny from all
       Allow from 127.0.0.1
    </Directory>
    #放到上面的防盗链的上面,防止冲突

#检查与加载
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@taoyuan ~]#

#在123.com目录下创建一个目录
[root@taoyuan ~]# mkdir /data/wwwroot/123.com/admin
[root@taoyuan ~]# ls !$
ls /data/wwwroot/123.com/admin

#创建一个index.php文件
[root@taoyuan ~]# touch /data/wwwroot/123.com/admin/index.php
[root@taoyuan ~]# vim /data/wwwroot/123.com/admin/index.php
[root@taoyuan ~]# cat !$
cat /data/wwwroot/123.com/admin/index.php
<?php echo "123123"; ?>


* curl 测试状态码为403则被限制访问了

#127.0.0.1为源ip,也是目标ip,即自己为自己通讯,所以能访问
[root@taoyuan ~]# curl -x127.0.0.1:80 123.com/admin/index.php -I
HTTP/1.1 200 OK
Date: Mon, 25 Dec 2017 14:47:42 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8

#查看的结果
[root@taoyuan ~]# curl -x127.0.0.1:80 123.com/admin/index.php
123123[root@taoyuan ~]#

#源ip不是127.0.0.1,所以状态码为403
[root@taoyuan ~]# curl -x192.168.0.10:80 123.com/admin/index.php -I
HTTP/1.1 403 Forbidden
Date: Mon, 25 Dec 2017 14:49:12 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1


# 11.27 访问控制FilesMatch
* 核心配置文件内容
以限制一个文件的形式
<Directory /data/wwwroot/123.com>
   <FilesMatch "admin.php(.*)">
     Order deny,allow
     Deny from all
     Allow from 127.0.0.1
   </FilesMatch>
</Directory>

#检测与加载
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@taoyuan ~]# /usr/local/apache2.4/bin/apachectl graceful

测试


#现在是404 由于没有对目录进行限制
[root@taoyuan ~]# curl -x192.168.0.10:80 123.com/admin/shisf -I
HTTP/1.1 404 Not Found
Date: Mon, 25 Dec 2017 14:59:19 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

#通过修改链接 结果显示为 403 
[root@taoyuan ~]# curl -x192.168.0.10:80 'http://123.com/admin.php?shisf' -I
HTTP/1.1 403 Forbidden
Date: Mon, 25 Dec 2017 15:01:32 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

#用127.0.0.1 结果为404 表示允许访问
[root@taoyuan ~]# curl -x127.0.0.1:80 'http://123.com/admin.php?shisf' -I
HTTP/1.1 404 Not Found
Date: Mon, 25 Dec 2017 15:03:12 GMT
Server: Apache/2.4.28 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

转载于:https://blog.51cto.com/3622288/2054595

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值