傅令江的光影色彩世界
留住记忆的点滴
  • 首页
  • 文学
    • 诗词鉴赏
    • 美文共读
    • 原创
  • 编程
    • php
    • asp
    • .net
    • VB
    • C/C++
    • 易语言
    • js
    • 其他
    • 逆向
  • 运维
    • windows
    • linux
  • 光影色彩
    • 电影
    • 音乐
  • 科技
    • 互联网
    • 手机
  • 生活
    • 情感
  • 微语
11月82022

WordPress – 5秒盾防CC(PHP通用代码)

作者:傅令江   发布:2022-11-8 10:54   分类:php   阅读:2980次   评论:0条  

简介

这是个类似Cloudflare的防cc功能,利用的是五秒盾的原理,经过检测得到cookie的浏览器才能正常的访问你的网站了,当然了,面对数以万计的肉鸡,这代码还是无能为力的,所以说做人就要低调,别到处装逼,到时候被打了惨的还是自己,下面分享给需要的同学。

使用教程新建文件

首先建个PHP文件cc.php代码如下:


<?php
define('SYSTEM_ROOT', dirname(preg_replace('@\\(.*\\(.*$@', '', preg_replace('@\\(.*\\(.*$@', '', __FILE__))) . '/');
session_start();
date_default_timezone_set('Asia/Shanghai');
header('Content-Type: text/html; charset=UTF-8');
function getspider($useragent=''){
  if(CC_Defender==2)return false;
  if(!$useragent){$useragent = $_SERVER['HTTP_USER_AGENT'];}
$useragent=strtolower($useragent);
  if (strpos($useragent, 'baiduspider') !== false){return 'baiduspider';}
  if (strpos($useragent, 'googlebot') !== false){return 'googlebot';}
  if (strpos($useragent, 'soso') !== false){return 'soso';}
  if (strpos($useragent, 'bing') !== false){return 'bing';}
  if (strpos($useragent, 'yahoo') !== false){return 'yahoo';}
  if (strpos($useragent, 'sohu-search') !== false){return 'Sohubot';}
  if (strpos($useragent, 'sogou') !== false){return 'sogou';}
  if (strpos($useragent, 'youdaobot') !== false){return 'YoudaoBot';}
  if (strpos($useragent, 'yodaobot') !== false){return 'YodaoBot';}
  if (strpos($useragent, 'robozilla') !== false){return 'Robozilla';}
  if (strpos($useragent, 'msnbot') !== false){return 'msnbot';}
  if (strpos($useragent, 'lycos') !== false){return 'Lycos';}
  if (strpos($useragent, 'ia_archiver') !== false || strpos($useragent, 'iaarchiver') !== false){return 'alexa';}
  if (strpos($useragent, 'archive.org_bot') !== false){return 'Archive';} 
  if (strpos($useragent, 'robozilla') !== false){return 'Robozilla';} 
  if (strpos($useragent, 'sitebot') !== false){return 'SiteBot';} 
  if (strpos($useragent, 'mj12bot') !== false){return 'MJ12bot';} 
  if (strpos($useragent, 'gosospider') !== false){return 'gosospider';} 
  if (strpos($useragent, 'gigabot') !== false){return 'Gigabot';} 
  if (strpos($useragent, 'yrspider') !== false){return 'YRSpider';} 
  if (strpos($useragent, 'gigabot') !== false){return 'Gigabot';} 
  if (strpos($useragent, 'jikespider') !== false){return 'jikespider';} 
  if (strpos($useragent, 'addsugarspiderbot') !== false){return 'AddSugarSpiderBot';/*非常少*/} 
  if (strpos($useragent, 'testspider') !== false){return 'TestSpider';} 
  if (strpos($useragent, 'etaospider') !== false){return 'EtaoSpider';} 
  if (strpos($useragent, 'wangidspider') !== false){return 'WangIDSpider';} 
  if (strpos($useragent, 'foxspider') !== false){return 'FoxSpider';} 
  if (strpos($useragent, 'docomo') !== false){return 'DoCoMo';} 
  if (strpos($useragent, 'yandexbot') !== false){return 'YandexBot';} 
  if (strpos($useragent, 'ezooms') !== false){return 'Ezooms';/*个人*/} 
  if (strpos($useragent, 'sinaweibobot') !== false){return 'SinaWeiboBot';} 
  if (strpos($useragent, 'catchbot') !== false){return 'CatchBot';} 
  if (strpos($useragent, 'surveybot') !== false){return 'SurveyBot';} 
  if (strpos($useragent, 'dotbot') !== false){return 'DotBot';} 
  if (strpos($useragent, 'purebot') !== false){return 'Purebot';} 
  if (strpos($useragent, 'ccbot') !== false){return 'CCBot';} 
  if (strpos($useragent, 'mlbot') !== false){return 'MLBot';} 
  if (strpos($useragent, 'adsbot-google') !== false){return 'AdsBot-Google';}
  if (strpos($useragent, 'ahrefsbot') !== false){return 'AhrefsBot';}
  if (strpos($useragent, 'spbot') !== false){return 'spbot';}
  if (strpos($useragent, 'augustbot') !== false){return 'AugustBot';}
  return false;
}

if($_GET['rand'] && $_SESSION['rand_session']!=$_GET['rand']){
  @header('Content-Type: text/html; charset=UTF-8');
exit('<b>浏览器不支持COOKIE或者不正常访问!</b>');
}
if(!$_SESSION['rand_session'] && $nosecu!=true){
  if(!getspider()){
    $rand_session=md5(uniqid().rand(1,1000));
  $_SESSION['rand_session']=$rand_session;
    exit("<!DOCTYPE HTML>
    <html>
    <head>
    <meta charset=\"UTF-8\"/>
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\" />

    <title>In inspection...</title>
   <script> var i = 5; 
  var intervalid; 
  intervalid = setInterval(\"fun()\", 1000); 
function fun() { 
if (i == 0) { 
window.location.href = \"?{$_SERVER['QUERY_STRING']}&rand={$rand_session}\"; 
clearInterval(intervalid); 
} 
document.getElementById(\"mes\").innerHTML = i; 
i--; 
} 
</script> 
<style>
  html, body {width: 100%; height: 100%; margin: 0; padding: 0;}
    body {background-color: #ffffff; font-family: Helvetica, Arial, sans-serif; font-size: 100%;}
    h1 {font-size: 1.5em; color: #404040; text-align: center;}
    p {font-size: 1em; color: #404040; text-align: center; margin: 10px 0 0 0;}
    #spinner {margin: 0 auto 30px auto; display: block;}
    .attribution {margin-top: 20px;}
  </style>
  </head>
<body>
  <table width=\"100%\" height=\"100%\" cellpadding=\"20\">
    <tr>
      <td align=\"center\" valign=\"middle\">
    <noscript><h2>请打开浏览器的javascript,然后刷新浏览器</h2></noscript>
  <h1><span data-translate=\"checking_browser\">浏览器安全检查中...</span></h1>
    <p data-translate=\"process_is_automatic\"></p>
    <p data-translate=\"allow_5_secs\">还剩 <span id=\"mes\">5</span> 秒</p>
  </div>
</div>
  </td>
    </tr>
</table></body></html>");}}



WordPress 上传到主题/template/cc.php ;

调用代码

PHP通用调用,在头部或主要文件内文件加入:

<?php include 'cc.php'; ?>



WordPress调用,主题/header.php第一行添加代码:

<?php get_template_part( 'template/cc' ); ?>

建议

可以把代码调用至404错误页,对于单IP刷新CC不存在页面有很好的防御效果!

首次访问网站检测浏览器cookie通过才能正常的访问你的网站,搜索引擎蜘蛛已经过滤,不影响收录,还有未加上的可以自己补充。平时没攻击的时候建议不要开启,影响网站体验,配合《Nginx lua waf 简单防御CC攻击》效果更佳。

补充

使用方法

2019/07/02 新增脚本运用,根据系统负载来开启/关闭5秒盾,新建check_5.sh:

#!/bin/bash
# author: yunloc
# website: https://yunloc.com
 
if [ ! -f "status.txt" ];then
echo "" > status.txt
else
status=$(cat status.txt)
fi
now=$(date +%s)
time=$(date +%s -r status.txt)
load=$(cat /proc/loadavg | colrm 5)
echo "当前负载:$load"
newtime=`expr $now - $time`
if [[ $load <1.2 ]] && [[ $status -eq 1 ]] && [[ $newtime >600 ]]  
then
cResult=$(/bin/cp /home/wwwroot/yunloc.com/wp-content/themes/begin/header_5_on.php /home/wwwroot/yunloc.com/wp-content/themes/begin/header.php)
echo $cResult
echo "负载低于 1.2,当前已开盾超过 10 分钟($newtime 秒),尝试关盾"
echo 0 > status.txt
elif [[ $load <1.2 ]]
then
echo "负载低于 1.2,不做任何改变,$newtime 秒"
exit
else
cResult=$(/bin/cp /home/wwwroot/yunloc.com/wp-content/themes/begin/header_5_off.php /home/wwwroot/yunloc.com/wp-content/themes/begin/header.php)
echo $cResult
echo "负载高于 1.2,开启防御规则"
echo 1 > status.txt
fi


设置定时任务,每1分钟检查一次:

*/1 * * * * /root/check_5.sh

新建两个文件:

/home/wwwroot/yunloc.com/wp-content/themes/begin/pheader_5_off.php

(首行加入WordPress调用代码)开启防御后把文件写入header.php

/home/wwwroot/yunloc.com/wp-content/themes/begin/header_5_on.php

原理

当系统负载达到1.2后自动开启,小于1.2后600秒关闭。

关于负载一般可以简单的设置为你的核心数*1.2 比如你CPU是双核的,可以设置为2.4。









本文固定链接: https://www.fulingjiang.cn/php/288.html

blogger
该日志由 傅令江 于2022-11-8 10:54 Tuesday发表在 php 分类下。
版权所有:《傅令江的光影色彩世界》 → 《WordPress – 5秒盾防CC(PHP通用代码)》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:
上一篇::openai给的ionCube 解密代码,应该是老版本可以这样
下一篇:我高中最好的朋友今天猝死了-伤心得不行

热门文章

  • 兄弟二周年祭

相关文章

  • php记录日志,达到文件大小,自动新建文件
  • php复制文件,目录不存在就创建
  • php直接下载文件,不在浏览器中打开,多个
  • 正则表达式的口诀及常用的正则
  • zencart 模板常用文件说明以及快速模板制作
取消回复

发表评论

亲,头像对么?

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


  • 日历

  • 存档

    • 2024年10月(1)
    • 2023年2月(1)
    • 2022年11月(1)
    • 2022年10月(10)
    • 2022年9月(13)
    • 2022年8月(2)
    • 2022年7月(14)
    • 2022年6月(2)
    • 2022年5月(8)
    • 2022年4月(7)
    • 2022年3月(13)
    • 2022年2月(2)
    • 2022年1月(9)
    • 2021年12月(2)
    • 2021年11月(4)
    • 2021年10月(2)
    • 2021年9月(6)
    • 2021年7月(4)
    • 2021年6月(3)
    • 2021年5月(3)
    • 2021年4月(11)
    • 2021年3月(13)
    • 2021年2月(2)
    • 2021年1月(1)
    • 2020年12月(1)
    • 2020年4月(5)
    • 2019年9月(1)
    • 2019年8月(1)
    • 2019年5月(3)
    • 2018年3月(1)
    • 2017年10月(1)
    • 2016年7月(1)
    • 2016年4月(1)
    • 2015年12月(1)
    • 2015年11月(3)
    • 2015年9月(1)
    • 2015年8月(10)
    • 2015年7月(1)
    • 2015年6月(1)
    • 2015年4月(1)
    • 2015年3月(3)
    • 2015年2月(8)
    • 2015年1月(4)
    • 2014年12月(1)
    • 2014年11月(27)
    • 2014年10月(13)
    • 2014年9月(14)
    • 2014年8月(26)
    • 2014年7月(21)
  • 最新评论

    • 令狐江:
      喜欢这首歌是因为可以引起共鸣!
  • 链接

    • 演讲稿网
    • Recollect
    • 演讲稿
    • 祁阳人生活网
    • 我爱演讲稿网
  • 搜索

  • 标签

      函数 自定义方法 SEO 分页 分页函数 分页方法 nginx重新的一些规则
  • 分类

    • 文学(0)
    • 编程(0)
    • 运维(0)
    • 光影色彩(0)
    • 科技(0)
    • 生活(0)
    • 诗词鉴赏(3)
    • 美文共读(1)
    • 原创(10)
    • php(111)
    • asp(1)
    • .net(0)
    • VB(0)
    • C/C++(0)
    • 易语言(0)
    • js(8)
    • 其他(9)
    • 逆向(2)
    • windows(11)
    • linux(121)
    • 电影(0)
    • 音乐(1)
    • 互联网(4)
    • 手机(0)
    • 情感(2)
  • 最新文章热门文章随机文章

    • 兄弟二周年祭
    • openai给的ionCube 解密代码,应该是老版本可以这样
    • WordPress – 5秒盾防CC(PHP通用代码)
    • 我高中最好的朋友今天猝死了-伤心得不行
    • Linux系统中 systemd-journaldCPU占用异常的解决方法
    • SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法
    • 解决Linux读写nfs共享盘速度慢的问题
    • php 获取302跳转后的地址
    • 让vsftp显示隐藏文件的办法,比如显示 .htaccess
    • 添加自签名https证书到centos系统信任的问题
    • Windows客户端内网穿透工具frpc安装及使用教程
    • 添加自签名https证书到centos系统信任的问题
    • 如何在php中获取curl请求的请求头信息及响应头信息
    • 关于centos6 yum源不可用问题解决办法
    • 宝塔历史版本集合,适合自己手动升级
Copyright © 2001-2025 傅令江的光影色彩世界. Powered by www.fulingjiang.cn ICP备案:京ICP备14015190号-5