Automatic Cloudflare "Under Attack Mode" activation based on server CPU load monitoring. Perfect for protecting your websites during DDoS attacks or traffic spikes.
- 🤖 Automatic Protection: Automatically enables Cloudflare's "Under Attack Mode" when CPU load is high
- 📊 Smart CPU Monitoring: Uses multiple methods (top, /proc/stat, load average) for accurate measurements
- 📈 Averaging Algorithm: Prevents false positives with CPU load averaging over multiple samples
- ⏱️ Configurable Thresholds: Set custom CPU thresholds and duration timers
- 🌐 Multi-Zone Support: Protect multiple domains/zones simultaneously
- 📱 Telegram Notifications: Get instant alerts with detailed information
- 🔄 Auto-Recovery: Automatically disables protection when load returns to normal
- 🚫 Anti-Spam: Built-in notification cooldown to prevent message spam
- 📝 Detailed Logging: Comprehensive logs for monitoring and debugging
- 👥 Multi-User Mode: Separate script for per-user CPU monitoring (shared hosting)
| Script | Use Case |
|---|---|
cf_protection.sh |
Single server monitoring based on total CPU load |
cf_protection_multi.sh |
Multi-user/multi-zone monitoring with individual API tokens |
- Linux server with
/proc/statsupport - Bash shell
curlandawkcommands- Cloudflare account with API access
- (Optional) Telegram bot for notifications
git clone https://github.com/and-ri/cloudflare-auto-protection.git
cd cloudflare-auto-protectionEdit cf_protection.sh and set your credentials:
# Cloudflare Configuration
CF_API_TOKEN="your-cloudflare-api-token"
# Add your Zone IDs
CF_ZONE_IDS=(
"your_first_zone_id_here"
"your_second_zone_id_here"
)
# Telegram Settings (optional)
TELEGRAM_BOT_TOKEN="your-telegram-bot-token"
TELEGRAM_CHAT_ID="your-telegram-chat-id"
TELEGRAM_ENABLED=truechmod +x cf_protection.sh./cf_protection.sh --test-telegramAdd to crontab to run every minute:
crontab -eAdd this line:
* * * * * /path/to/cf_protection.sh
| Parameter | Default | Description |
|---|---|---|
CPU_THRESHOLD |
80 | CPU load percentage threshold to trigger protection |
CPU_SAMPLES |
3 | Number of measurements to average for stability |
HIGH_LOAD_DURATION |
120 | Seconds of high load before activating (2 minutes) |
COOLDOWN_DURATION |
300 | Seconds of normal load before deactivating (5 minutes) |
- Go to Cloudflare Dashboard
- Click on your profile → API Tokens
- Click Create Token
- Use Edit zone security template or create custom with:
- Zone → Zone Settings → Edit
- Zone → Zone → Read
- Copy the token to
CF_API_TOKEN
- Go to your domain in Cloudflare Dashboard
- Scroll down in the Overview tab
- Find Zone ID in the right sidebar
- Copy it to
CF_ZONE_IDSarray
- Talk to @BotFather on Telegram
- Send
/newbotand follow instructions - Copy the bot token to
TELEGRAM_BOT_TOKEN - Start a chat with your bot
- Get your chat ID:
curl "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates" - Copy
chat.idtoTELEGRAM_CHAT_ID
┌─────────────────────┐
│ Monitor CPU Load │
│ (every minute) │
└──────────┬──────────┘
│
▼
┌─────────────┐
│ CPU > 80%? │
└──────┬──────┘
│
┌──────▼───────┐ ┌────────────────┐
│ Yes: Wait │────────▶│ After 2 min: │
│ 2 minutes │ │ Enable Attack │
│ │ │ Mode │
└──────────────┘ └────────┬───────┘
│ │
│ ▼
┌──────▼───────┐ ┌────────────────┐
│ No: Is │ │ Monitor CPU │
│ Attack │────────▶│ Wait for │
│ Mode ON? │ │ normalization │
└──────────────┘ └────────┬───────┘
│
┌────────▼────────┐
│ After 5 min: │
│ Disable Attack │
│ Mode │
└─────────────────┘
- Normal State: Medium security level
- High Load Detected: Warning notification sent
- After 2 minutes of high load: Under Attack Mode enabled
- After 5 minutes of normal load: Protection disabled
You'll receive notifications for:
🚨 CLOUDFLARE UNDER ATTACK MODE ACTIVATED
🖥 Server: your-server
🌐 Domains: example.com, site.com
📊 Current CPU: 95%
📈 Average CPU: 92%
⚠️ Threshold: 80%
🛡 Mode: Under Attack Mode
⏰ Time: 2025-10-27 14:30:00
✅ CLOUDFLARE UNDER ATTACK MODE DISABLED
🖥 Server: your-server
🌐 Domains: example.com, site.com
📊 Current CPU: 45%
📈 Average CPU: 48%
✓ Status: Normalized
🔓 Mode: Medium Security
⏰ Time: 2025-10-27 14:35:00
⚠️ WARNING: High CPU Load
🖥 Server: your-server
🌐 Domains: example.com, site.com
📊 Current CPU: 85%
📈 Average CPU: 83%
⏱ Status: Countdown started
⏰ Time: 2025-10-27 14:28:00
Logs are stored in /var/tmp/cf_protection/protection.log
Example log output:
[2025-10-27 14:28:00] CPU: 85% | Average: 83% | Method: top | Cores: 4 | Load: 3.2 | Zones: 2
[2025-10-27 14:28:00] ⚠ Average CPU load exceeds 80%
[2025-10-27 14:28:00] → High load countdown started
[2025-10-27 14:30:00] ⚠ Activating Cloudflare Under Attack Mode...
[2025-10-27 14:30:01] → Changing security level to 'under_attack' for 2 zone(s)...
[2025-10-27 14:30:02] ✓ example.com (abc123...) - success
[2025-10-27 14:30:03] ✓ site.com (def456...) - success
[2025-10-27 14:30:03] ✓ All zones updated successfully
Test the script manually:
./cf_protection.shTest Telegram notifications:
./cf_protection.sh --test-telegramRun every 2 minutes:
*/2 * * * * /path/to/cf_protection.sh
Run every 30 seconds:
* * * * * /path/to/cf_protection.sh
* * * * * sleep 30; /path/to/cf_protection.sh
You can run this script on multiple servers protecting the same zones. The script handles concurrent changes gracefully.
The script maintains state in /var/tmp/cf_protection/:
high_load_start- When high load detection startedunder_attack_enabled- Flag indicating protection is activenormal_load_start- When normal load detection startedcpu_history- Recent CPU measurementslast_telegram_notification_*- Anti-spam timestampsprotection.log- Activity log
The cf_protection_multi.sh script is designed for shared hosting environments or scenarios where you need to monitor CPU usage per system user with individual Cloudflare zones and API tokens.
Edit cf_protection_multi.sh and configure:
# Telegram Configuration
TELEGRAM_BOT_TOKEN="YOUR_BOT_TOKEN_HERE"
TELEGRAM_CHAT_ID="YOUR_CHAT_ID_HERE"
TELEGRAM_ENABLED=true
# Cloudflare Configuration - Add zones and tokens
declare -A ZONES
declare -A TOKENS
# Website 1
ZONES[username1]="ZONE_ID_1"
TOKENS[username1]="API_TOKEN_1"
# Website 2
ZONES[username2]="ZONE_ID_2"
TOKENS[username2]="API_TOKEN_2"| Parameter | Default | Description |
|---|---|---|
CPU_THRESHOLD |
20 | Per-user CPU threshold percentage |
HIGH_LOAD_DURATION |
120 | Seconds before activating protection |
COOLDOWN_DURATION |
300 | Seconds of normal load before deactivating |
- Monitors CPU usage for each configured system user via
ps aux - Each user has independent protection state
- Uses individual Cloudflare API tokens per zone
- Singleton enforcement prevents concurrent script runs
- State files stored in
/var/tmp/cf_protection_multi/
Per-user state files in /var/tmp/cf_protection_multi/:
{user}_high_load_start- High load detection timestamp{user}_active- Protection active flag{user}_normal_start- Normalization countdown startprotection.log- Combined activity log
* * * * * /path/to/cf_protection_multi.shCheck permissions:
chmod +x cf_protection.shCheck required commands:
which curl awk- Verify bot token and chat ID
- Run test:
./cf_protection.sh --test-telegram - Check if
TELEGRAM_ENABLED=true - Check logs for error messages
- Check CPU threshold matches your server load
- Verify Zone IDs are correct
- Check API token has correct permissions
- Review logs:
tail -f /var/tmp/cf_protection/protection.log
Increase thresholds:
CPU_THRESHOLD=90 # Higher CPU threshold
CPU_SAMPLES=5 # More samples for averaging
HIGH_LOAD_DURATION=180 # Longer wait time (3 minutes)Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
This script modifies your Cloudflare security settings. Test thoroughly in a development environment before deploying to production. The authors are not responsible for any issues caused by using this script.
- DDoS Protection: Automatically enable protection during attacks
- Traffic Spikes: Handle sudden legitimate traffic increases
- Resource Management: Protect server during high load
- Automated Security: Set-and-forget protection for multiple sites
- Server Monitoring: Get alerts about unusual server behavior
If you find this project useful, please give it a star! ⭐
- Create an issue for bugs or feature requests
- Pull requests are always welcome
- Share your use case and improvements
Made with ❤️ for the security and performance of your websites