第十九课时预习笔记

配置防盗链

防盗链,就是不让别人盗用你网站上的资源,这个资源,通常指的是图片、视频、歌曲、文档等。

 

referer的概念

你通过A网站的一个页面http://a.com/a.html 里面的链接去访问B网站的一个页面http://b.com/b.html ,那么这个B网站页面的referer就是http://a.com/a.html。 也就是说,一个referer其实就是一个网址。

 

1.配置防盗链

[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful

 

参考配置文件内容如下:

<Directory /data/wwwroot/111.com>

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

        SetEnvIfNoCase Referer "http://111.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>

 

解释说明:

首先定义允许访问链接的referer,其中^$为空referer,当直接在浏览器里输入图片地址去访问它时,它的referer就为空。然后又使用filesmatch来定义需要保护的文件类型,访问txt、doc、mp3、zip、rar、jpg、gif、png格式的文件,当访问这样的类型文件时就会被限制。

 

修改后示例如下图:

第十九课时预习笔记

 

 

2.测试网页访问

浏览器访问:http://111.com/qq.png

在其它网站上链接这个网址,还是打不开。

第十九课时预习笔记第十九课时预习笔记

 

然后在虚拟主机配置文件里把第三方站点加入到白名单

[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

 

在下面图片上把第三方站点网址加入到白名单,然后保存退出重新加载配置。

第十九课时预习笔记

 

在点击链接http://111.com/qq.png 访问就可以了,这就是referer,如下图

第十九课时预习笔记

 

3.使用curl测试

[[email protected] 111.com]# curl -x127.0.0.1:80 111.com/qq.png -I

HTTP/1.1 200 OK

Date: Thu, 21 Dec 2017 13:41:38 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Last-Modified: Thu, 21 Dec 2017 06:18:31 GMT

ETag: "91ab-560d3ab3fbbc0"

Accept-Ranges: bytes

Content-Length: 37291

Cache-Control: max-age=86400

Expires: Fri, 22 Dec 2017 13:41:38 GMT

Content-Type: image/png

 

[[email protected] 111.com]# curl -e "http://wwww.qq.com/123.txt" -x127.0.0.1:80 111.com/qq.png -I  //使用-e来定义referer,这个referer一定要以http://开头,否则不管用。

HTTP/1.1 403 Forbidden

Date: Thu, 21 Dec 2017 13:42:17 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

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

 

[[email protected] 111.com]# curl -e "http://111.com/123.txt" -x127.0.0.1:80 111.com/qq.png -I

HTTP/1.1 200 OK

Date: Thu, 21 Dec 2017 13:42:34 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Last-Modified: Thu, 21 Dec 2017 06:18:31 GMT

ETag: "91ab-560d3ab3fbbc0"

Accept-Ranges: bytes

Content-Length: 37291

Cache-Control: max-age=86400

Expires: Fri, 22 Dec 2017 13:42:34 GMT

Content-Type: image/png

 

 

访问控制Directory

对于一些比较重要的网站内容,除了可以使用用户认证限制访问之外,还可以通过其他一些方法做到限制,比如可以限制IP,也可以限制user_agent,限制IP指的是限制访问网站的来源IP,而限制user_agent,通常用来限制恶意或者不正常的请求。

 

1.修改虚拟主机配置:

[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

 

核心配置文件内容如下:

  <Directory /data/wwwroot/www.123.com/admin/>

        Order deny,allow

        Deny from all

        Allow from 127.0.0.1

    </Directory>

 

配置示例图:

第十九课时预习笔记

 

解释说明:

使用<Directory>来指定要限制访问的目录,order定义控制顺序,哪个在前面就先匹配哪个规则,在本例中deny在前面,所以要先匹配Deny from all,这样所有的来源IP都会被限制,然后匹配Allow from 127.0.0.1,这样又允许了127.0.0.1这个IP。最终的效果是,只允许来源IP为127.0.0.1的访问。

验证如下:

 

[[email protected] 111.com]# mkdir /data/wwwroot/111.com/admin/ //创建admin目录,模拟网站后台

[[email protected] 111.com]# ls

123.php  admin  index.php  qq.png

[[email protected] 111.com]# touch /data/wwwroot/111.com/admin/index.php //在后台目录下面创建文件

[[email protected] 111.com]# ls admin/

index.php

[[email protected] 111.com]# vim admin/index.php //并写入内容

[[email protected] 111.com]# cat admin/index.php 

123456789

[[email protected] 111.com]# curl -x127.0.01:80 111.com/admin/index.php -I

HTTP/1.1 200 OK

Date: Mon, 25 Dec 2017 02:34:14 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Cache-Control: max-age=0

Expires: Mon, 25 Dec 2017 02:34:14 GMT

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

 

[[email protected] 111.com]# curl -x127.0.01:80 111.com/admin/index.php

123456789

[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful //加载配置

[[email protected] 111.com]# curl -x172.16.111.100:80 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>

[[email protected] 111.com]# curl -x127.0.01:80 111.com/admin/index.php

123456789

[[email protected] 111.com]# tail /usr/local/apache2.4/logs/111.com-access_20171225.log  //查看日志

127.0.0.1 - - [25/Dec/2017:10:34:14 +0800] "HEAD HTTP://111.com/admin/index.php HTTP/1.1" 200 - "-" "curl/7.29.0"

127.0.0.1 - - [25/Dec/2017:10:34:30 +0800] "GET HTTP://111.com/admin/index.php HTTP/1.1" 200 10 "-" "curl/7.29.0"

172.16.111.100 - - [25/Dec/2017:10:36:54 +0800] "GET HTTP://111.com/admin/index.php HTTP/1.1" 403 224 "-" "curl/7.29.0"

127.0.0.1 - - [25/Dec/2017:10:37:33 +0800] "GET HTTP://111.com/admin/index.php HTTP/1.1" 200 10 "-" "curl/7.29.0"

[[email protected] 111.com]# curl -x127.0.0.1:80 http://111.com/admin/adfafdafdas -I

HTTP/1.1 404 Not Found

Date: Mon, 25 Dec 2017 02:54:48 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

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

 

[[email protected] 111.com]# curl -x172.16.111.100:80 http://111.com/admin/adfafdafdas -I

HTTP/1.1 403 Forbidden

Date: Mon, 25 Dec 2017 02:54:59 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

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

 

解释说明:

本机有两个IP,一个是172.16.111.100,一个是127.0.0.1,通过这两个IP都可以访问到站点.而来源分别为172.16.111.110和127.0.0.1,其实和本机IP是一样的,curl测试状态码为403则被限制访问了。

 

使用windowo浏览器访问示例图

第十九课时预习笔记

 

 

[[email protected] 111.com]# tail /usr/local/apache2.4/logs/111.com-access_20171225.log  //windows浏览器访问日志

172.16.111.1 - - [25/Dec/2017:11:00:37 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

172.16.111.1 - - [25/Dec/2017:11:00:39 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

172.16.111.1 - - [25/Dec/2017:11:00:39 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

172.16.111.1 - - [25/Dec/2017:11:00:40 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

 

解释说明:

浏览器访问提示Forbidden,其实就是403,再来看日志,可以查看到对应的来源IP为172.16.111.1,希望不要把来源IP和本机IP搞混了,前面实验中之所以本机IP和来源IP一样,就是因为它相当于自己访问自己,而后面用浏览器访问,相当于拿windows

 

 

访问控制FilesMatch

针对某个文件来做限制。

[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

 

核心配置文件内容

<Directory /data/wwwroot/www.123.com>

    <FilesMatch  "admin.php(.*)">

        Order deny,allow

        Deny from all

        Allow from 127.0.0.1

    </FilesMatch>

</Directory>

 

[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t //检测语法

Syntax OK

[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful //加载配置

 

配置示例图:

第十九课时预习笔记

 限制user_agent

user_agent可以理解为浏览器标识
user_agent为什么可以做访问控制呢?
背景:比如网站受到cc***。***的人通过软件或者肉机,想***某个网站的时候,把所有的肉机发动起来,让它们同时访问一个站点。但是cc***往往有一个特征,就是user_agent一致的,访问地址一致。访问速度快,每秒N次

核心配置文件内容
<IfModule mod_rewrite.c>
RewriteEngine on

 php相关配置

查看php配置文件位置
/usr/local/php/bin/php -i|grep -i "loaded configuration file" 
或者phpinfo();
但是有些情况下使用-i是不准的。
如果想找的更准。比如找11.com,那么可以在11.com的根目录下创建phpinfo()文件。浏览器访问它。查看配置文件

cp -r /usr/local/src/php-5.6.30/php.ini-production /usr/local/php/etc/php.ini
vi /usr/local/php/etc/php.ini //搜索disable_functions

date.timezone //定义时区
date.timezone =Asia/shanghai

disable_functions //安全函数(禁止掉这些比较危险的函数)
eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close 
在生产环境中会把phpinfo禁掉。因为有时候不小心写了一个phpinfo文件,上传上去被***发现。能够看到系统的目录。不安全
eval:一句话***使用了该函数如果把这个函数禁了,即使上传了***也没办法解析

日志相关:
error_log, log_errors, display_errors, error_reporting

display_errors = On //会把错误信息直接显示在浏览器上

(生产环境该参数改成Off,为了避免暴露信息)但是有一个问题,就是页面什么都不显示(白页)。那么还需要配置错误日志。
log_errors = On //表示错误日志打开状态
error_log = /tmp/php_errors.log //定义错误日志所在位置
还需要定义error_log的级别。如果级别很高的话,很严谨的话,只会记录一些比较严峻的错误,不太严峻的就不记录,比如警告等、所以放松些
error_reporting //定义日志的级别
生产环境使用
E_ALL & ~E_NOTICE (Show all errors, except for notices)

模拟:把phpinfo函数禁掉。
1.phpinfo放入disable_functions里
2.display_errors = Off
3.log_errors = On
4.error_log = /tmp/php_errors.log
5./usr/local/apache2.4/bin/apachectl -t && graceful
6.ll /tmp/
srwxrwxrwx. 1 mysql mysql 0 12月 11 18:39 mysql.sock
-rw-r--r--. 1 daemon daemon 290 12月 11 21:23 php_errors.log
7.cat /tmp/php_errors.log 
[11-Dec-2017 21:23:49 Asia/shanghai] PHP Warning: phpinfo() has been disabled for security reasons in /data/wwwroot/discuz2/lsx/1.php on line 2

php_errors.log的属主实际是httpd的属主daemon。因为这个日志是以daemon这个进程的身份创建的。当你定义了一个日志,但是日志始终没有生成,就要看一下定义的目录有没写权限,而且写文件的进程是这个进程。

为了保守起见,可以把这个文件创建好/tmp/php_errors.log,在授权777权限
grep error_log /usr/local/php/etc/php.ini
error_log = /tmp/php_errors.log
;error_log = syslog

第十九课时预习笔记