找回密码
 立即注册
查看: 129|回复: 0

代码防cc源码

[复制链接]

154

积分

0

金钱

154

金币

实习版主

积分
154
    发表于 2024-9-12 10:55:39 | 显示全部楼层 |阅读模式
    1. <?php
    2. session_start();

    3. $ip_address = $_SERVER['REMOTE_ADDR'];
    4. $blacklist = file('blacklist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    5. $whitelist = file('whitelist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    6. $access_log = file('access_log.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    7. // 检查是否在黑名单中
    8. if (in_array($ip_address, $blacklist)) {
    9.     header('HTTP/1.1 403 Forbidden');
    10.     exit('<h1>403 Forbidden: 您的IP地址已被列入黑名单。</h1>');
    11. }

    12. // 检查是否已经在白名单中
    13. $whitelisted = false;
    14. foreach ($whitelist as $entry) {
    15.     list($ip, $timestamp) = explode('|', $entry);
    16.     if ($ip === $ip_address) {
    17.         $whitelisted = true;
    18.         break;
    19.     }
    20. }

    21. if (!$whitelisted) {
    22.     // 记录访问
    23.     file_put_contents('access_log.txt', $ip_address . '|' . time() . PHP_EOL, FILE_APPEND);

    24.     // 检查访问次数
    25.     $recent_attempts = 0;
    26.     $one_minute_ago = time() - 60;
    27.     foreach ($access_log as $log_entry) {
    28.         list($log_ip, $log_time) = explode('|', $log_entry);
    29.         if ($log_ip === $ip_address && $log_time > $one_minute_ago) {
    30.             $recent_attempts++;
    31.         }
    32.     }

    33.     $qps_threshold = 20; // QPS阈值
    34.     if ($recent_attempts > $qps_threshold) {
    35.         file_put_contents('blacklist.txt', $ip_address . PHP_EOL, FILE_APPEND);
    36.         header('HTTP/1.1 403 Forbidden');
    37.         exit('<h1>403 Forbidden: 请求次数过多。您的IP地址已被列入黑名单。</h1>');
    38.     }
    39. }

    40. // 处理验证码验证
    41. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    42.     if (isset($_POST['captcha_code']) && $_POST['captcha_code'] === $_SESSION['captcha_code']) {
    43.         file_put_contents('whitelist.txt', $ip_address . '|' . time() . PHP_EOL, FILE_APPEND);
    44.         header('Location: index.php'); // 验证成功后重定向到本页以清除表单提交
    45.         exit;
    46.     } else {
    47.         $message = '验证码验证失败。';
    48.     }
    49. }

    50. // 生成验证码
    51. $captcha_code = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 6);
    52. $_SESSION['captcha_code'] = $captcha_code;
    53. ?>

    54. <!DOCTYPE html>
    55. <html lang="zh">
    56. <head>
    57.     <meta charset="UTF-8">
    58.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    59.     <title>访问保护页面</title>
    60.     <style>
    61.         body {
    62.             font-family: Arial, sans-serif;
    63.             text-align: center;
    64.             background-color: #f4f4f4;
    65.             padding: 50px;
    66.         }
    67.         h1 {
    68.             color: #333;
    69.         }
    70.         .container {
    71.             background: #fff;
    72.             padding: 20px;
    73.             border-radius: 5px;
    74.             box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    75.             display: inline-block;
    76.         }
    77.         img {
    78.             margin: 10px 0;
    79.         }
    80.         input[type="text"] {
    81.             padding: 10px;
    82.             margin: 10px 0;
    83.             border: 1px solid #ccc;
    84.             border-radius: 5px;
    85.         }
    86.         button {
    87.             padding: 10px 20px;
    88.             background-color: #28a745;
    89.             border: none;
    90.             color: #fff;
    91.             border-radius: 5px;
    92.             cursor: pointer;
    93.         }
    94.         button:hover {
    95.             background-color: #218838;
    96.         }
    97.         .message {
    98.             margin-top: 20px;
    99.             color: #d9534f;
    100.         }
    101.     </style>
    102. </head>
    103. <body>
    104.     <div class="container">
    105.         <?php if (!$whitelisted): ?>
    106.         <h1>请输入验证码以访问内容</h1>
    107.         <form method="POST">
    108.             <label>验证码:</label>
    109.             <img src="captcha.php" alt="验证码">
    110.             <input type="text" name="captcha_code" required>
    111.             <button type="submit">提交</button>
    112.         </form>
    113.         <?php if (isset($message)): ?>
    114.             <p class="message"><?php echo htmlspecialchars($message); ?></p>
    115.         <?php endif; ?>
    116.         <?php else: ?>
    117.         <p>您已通过验证,可以访问页面内容。</p>
    118.         <!-- 这里放置需要保护的页面内容 -->
    119.         <?php endif; ?>
    120.     </div>
    121. </body>
    122. </html>
    复制代码
    代码现在将会在检测到黑名单中的IP时,直接返回HTTP 403状态码作用不是很大

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|天天站

    GMT+8, 2024-10-24 02:17 , Processed in 0.087868 second(s), 19 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.