一、限定某个目录禁止解析php
并不是所有的网站目录是需要解析php的,比如上传图片的目录等。禁止解析php能够防止被攻击者以php的方式搞破坏。
1、配置虚拟主机
[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www/123.com" ServerName 123.com ErrorLog "logs/123.com-error_log" CustomLog "logs/123.com-access_log" common <Directory /data/www/123.com/upload> php_admin_flag engine off ##upload目录中的php不解析 <FilesMatch (.*)\.php(.*)> Order allow,deny deny from all ##所有.php文件拒绝访问 </FilesMatch> </Directory> </VirtualHost>
2、检查重新加载
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful [[email protected] ~]# ls /data/www/123.com/upload 1.php upload [[email protected] ~]# cat /data/www/123.com/upload/1.php <?php echo "123.com"; ?> [[email protected] ~]# cat /data/www/123.com/upload/upload <?php echo "123.com"; ?>
3、测试验证
[[email protected] ~]# curl -x127.0.0.1:80 123.com/1.php 123.com [[email protected] ~]# curl -x127.0.0.1:80 123.com/upload/1.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 /upload/1.php on this server.<br /> </p> </body></html> [[email protected] ~]# curl -x127.0.0.1:80 123.com/upload/upload <?php echo "123.com"; ?>
二、限制user_agent
User Agent意为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
cc攻击是我们经常见到的最常见的一种攻击,它是黑客利用所能利用的肉鸡去高频访问你的站点,导致站点不可以被正常的用户所浏览。在攻击的时候有一个规律的特征,user_agent是一致的。通过对user_agent做限制,可以解决这个问题。
1、配置虚拟主机
[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www/123.com" ServerName 123.com ErrorLog "logs/123.com-error_log" CustomLog "logs/123.com-access_log" common <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] ##匹配curl的访问 OR=或关系 RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] ##NC=忽略大小写 RewriteRule .* - [F] ##F:Forbidden 禁止 </IfModule> </VirtualHost>
2、检查重新加载
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
3、测试验证
[[email protected] ~]# curl -x127.0.0.1:80 123.com <!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 / on this server.<br /> </p> </body></html> [[email protected] ~]# curl -A "360Sec" -x127.0.0.1:80 123.com ##-A指定user-agent 123.com [[email protected] ~]# curl -A "baidu.com" -x127.0.0.1:80 123.com <!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 / on this server.<br /> </p> </body></html>
时间: 2024-10-13 20:36:55