8.6任务

11.25 配置防盗链

早期,09年,有免费的论坛可以帮你申请自己的论坛。

某段时间流量飙升,带宽增加。查到某服务器后开始抓包。虽然日志中没有图片,但是抓包发现了大量的图片,且Referer是固定的。

就是这个站点,服务器放在了台湾,是非法站点,利用了免费的论坛可以无限上传图片,就导致了站点虽然在A地,但是图片是使用的B地免费论坛的带宽。这样的流量于免费的论坛无益。

这样解决方案就是删除图片,配置防盗链,防盗链的作用就是使某些文件只能在特定的站点打开。

原理就是利用Referer控制。

以下是防盗链的配置,对于txt、doc等文件访问的时候必须要经过白名单。filesmatch并不区分大小写。

    <Directory /usr/local/apache2.4/docs/dummy-host2.example.com>
        SetEnvIfNoCase Referer "http://www.123.com" local_ref
        SetEnvIfNoCase Referer "http://dummy-host2.example.com" local_ref
        #SetEnvIfNoCase Referer "^$" local_ref
        <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
            Order Allow,Deny
            Allow from env=local_ref
        </filesmatch>
    </Directory>

配置好重启服务,访问我们的图片时候,就会出现403错误。

[root@localhost: ~]# curl -x127.0.0.1:80 dummy-host2.example.com/tiger.jpg -I
HTTP/1.1 403 Forbidden
Date: Sun, 05 Aug 2018 15:57:30 GMT
Server: Apache/2.4.32 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

在论坛中做一个测试帖子,

之后跳转

添加白名单后跳转。

SetEnvIfNoCase Referer "http://ask.apelearn.com" local_ref

设置空referer也可以访问就把之前注释打开。

curl使用-e可以指定referer,要以http://开头

[root@localhost: ~]# curl -e "http://www.123.com" -x127.0.0.1:80 dummy-host2.example.com/tiger.jpg -I
HTTP/1.1 200 OK
Date: Sun, 05 Aug 2018 16:10:45 GMT
Server: Apache/2.4.32 (Unix) PHP/5.6.30
Last-Modified: Tue, 18 Mar 2014 09:49:30 GMT
ETag: "76bc5f-4f4de73d37e80"
Accept-Ranges: bytes
Content-Length: 7781471
Cache-Control: max-age=86400
Expires: Mon, 06 Aug 2018 16:10:45 GMT
Content-Type: image/jpeg

11.26 访问控制Directory

当网站有核心配置文件,只想让内部人员使用,用户认证当然是可以的,但是还会有泄露密码的危险。

我们可以设置一层,只有白名单IP才被允许。

    <Directory /usr/local/apache2.4/docs/dummy-host2.example.com>
        Order deny,allow #按照顺序执行deny和allow的代码,这两行是都会被执行完的,以最后状态为主
        Deny from all
        Allow from 127.0.0.1
    </Directory>

这层代码最好放置在最前端。

[root@localhost: ~]# mkdir /usr/local/apache2.4/docs/dummy-host2.example.com/admin
[root@localhost: ~]# echo -e "<?php \n\techo \"admin2\" \n?>" > /usr/local/apache2.4/docs/dummy-host2.example.com/admin/index.php

之后我们来做测试,注意这里的-x指定的是目标ip

[root@localhost: ~]# curl -x127.0.0.1:80 dummy-host2.example.com/admin/index.php
admin2[root@localhost: ~]# curl -x192.168.244.128:80 dummy-host2.example.com/admin/index<!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@localhost: ~]# tail -5 /usr/local/apache2.4/logs/dummy-host2.example.com-access_2018080 
dummy-host2.example.com-access_20180805.log
dummy-host2.example.com-access_20180806.log
[root@localhost: ~]# tail -5 /usr/local/apache2.4/logs/dummy-host2.example.com-access_20180806.log 
127.0.0.1 - - [06/Aug/2018:00:26:37 +0800] "GET HTTP://dummy-host2.example.com/admin/index.php HTTP/1.1" 200 6 "-" "curl/7.29.0"
192.168.244.128 - - [06/Aug/2018:00:27:30 +0800] "GET HTTP://dummy-host2.example.com/admin/index.php HTTP/1.1" 403 224 "-" "curl/7.29.0"

如果我们把配置文件中的order改变了顺序,那么无论什么都是403禁止,因为deny后面的语句是禁止所有访问。

11.27 访问控制FilesMatch

我们不想对目录做访问控制的话,可以选择对文件进行访问控制。

    <Directory /usr/local/apache2.4/docs/dummy-host2.example.com>
        <FilesMatch  "admin.php(.*)">
            Order deny,allow
            Deny from all
            Allow from 127.0.0.1
        </FilesMatch>
    </Directory>

后面的通配符为了匹配对网站的传参。

[root@localhost: ~]# curl -x192.168.244.128:80 dummy-host2.example.com/admin.php?$1=1
<!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.php
on this server.<br />
</p>
</body></html>
[root@localhost: ~]# curl -x127.0.0.1:80 dummy-host2.example.com/admin.php?$1=1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

几种限制ip的方法

apache 自定义header

apache的keepalive和keepalivetimeout

转载于:https://my.oschina.net/u/3866688/blog/1922951

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值