# Simple example (Python makes this easy!)
from collections import deque # This automatically keeps only the last 60 items
recent_traffic = deque(maxlen=60) # Every second, we add new data
recent_traffic.append(current_visitors) # Old data automatically disappears!
# Simple example (Python makes this easy!)
from collections import deque # This automatically keeps only the last 60 items
recent_traffic = deque(maxlen=60) # Every second, we add new data
recent_traffic.append(current_visitors) # Old data automatically disappears!
# Simple example (Python makes this easy!)
from collections import deque # This automatically keeps only the last 60 items
recent_traffic = deque(maxlen=60) # Every second, we add new data
recent_traffic.append(current_visitors) # Old data automatically disappears!
import statistics # Collect 30 minutes of data
traffic_data = [12, 15, 13, 14, 16, 11, 12, ...] # Calculate your "normal"
average = statistics.mean(traffic_data) # Example: 13.5
variation = statistics.stdev(traffic_data) # Example: 2.1
import statistics # Collect 30 minutes of data
traffic_data = [12, 15, 13, 14, 16, 11, 12, ...] # Calculate your "normal"
average = statistics.mean(traffic_data) # Example: 13.5
variation = statistics.stdev(traffic_data) # Example: 2.1
import statistics # Collect 30 minutes of data
traffic_data = [12, 15, 13, 14, 16, 11, 12, ...] # Calculate your "normal"
average = statistics.mean(traffic_data) # Example: 13.5
variation = statistics.stdev(traffic_data) # Example: 2.1
z-score = (current traffic - normal traffic) / variation
z-score = (current traffic - normal traffic) / variation
z-score = (current traffic - normal traffic) / variation
normal = 13.5 visitors/second
variation = 2.1
current = 30 visitors/second z_score = (30 - 13.5) / 2.1
# Result: 7.86 β Definitely an attack! if z_score > 3.0: print("π¨ ATTACK DETECTED!")
normal = 13.5 visitors/second
variation = 2.1
current = 30 visitors/second z_score = (30 - 13.5) / 2.1
# Result: 7.86 β Definitely an attack! if z_score > 3.0: print("π¨ ATTACK DETECTED!")
normal = 13.5 visitors/second
variation = 2.1
current = 30 visitors/second z_score = (30 - 13.5) / 2.1
# Result: 7.86 β Definitely an attack! if z_score > 3.0: print("π¨ ATTACK DETECTED!")
# Update your system
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">update && -weight: 600;">sudo -weight: 500;">apt -weight: 500;">upgrade -y # Install Docker (the easy way!)
-weight: 500;">curl -fsSL https://get.-weight: 500;">docker.com -o get--weight: 500;">docker.sh
-weight: 600;">sudo sh get--weight: 500;">docker.sh # Install Docker Compose
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install -weight: 500;">docker-compose -y # Install Python
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install python3 python3--weight: 500;">pip -weight: 500;">git -y # Create your project folder
mkdir ~/hng-stage3
cd ~/hng-stage3
# Update your system
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">update && -weight: 600;">sudo -weight: 500;">apt -weight: 500;">upgrade -y # Install Docker (the easy way!)
-weight: 500;">curl -fsSL https://get.-weight: 500;">docker.com -o get--weight: 500;">docker.sh
-weight: 600;">sudo sh get--weight: 500;">docker.sh # Install Docker Compose
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install -weight: 500;">docker-compose -y # Install Python
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install python3 python3--weight: 500;">pip -weight: 500;">git -y # Create your project folder
mkdir ~/hng-stage3
cd ~/hng-stage3
# Update your system
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">update && -weight: 600;">sudo -weight: 500;">apt -weight: 500;">upgrade -y # Install Docker (the easy way!)
-weight: 500;">curl -fsSL https://get.-weight: 500;">docker.com -o get--weight: 500;">docker.sh
-weight: 600;">sudo sh get--weight: 500;">docker.sh # Install Docker Compose
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install -weight: 500;">docker-compose -y # Install Python
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install python3 python3--weight: 500;">pip -weight: 500;">git -y # Create your project folder
mkdir ~/hng-stage3
cd ~/hng-stage3
hng-stage3/
βββ -weight: 500;">docker-compose.yml # Starts all our services
βββ nginx/
β βββ nginx.conf # Web server config
βββ detector/
β βββ main.py # Main program
β βββ monitor.py # Watches the logs
β βββ baseline.py # Calculates "normal"
β βββ detector.py # Spots attacks
β βββ blocker.py # Blocks bad IPs
β βββ notifier.py # Sends Slack alerts
β βββ dashboard.py # Web dashboard
β βββ config.yaml # Your settings
β βββ requirements.txt # Python packages needed
hng-stage3/
βββ -weight: 500;">docker-compose.yml # Starts all our services
βββ nginx/
β βββ nginx.conf # Web server config
βββ detector/
β βββ main.py # Main program
β βββ monitor.py # Watches the logs
β βββ baseline.py # Calculates "normal"
β βββ detector.py # Spots attacks
β βββ blocker.py # Blocks bad IPs
β βββ notifier.py # Sends Slack alerts
β βββ dashboard.py # Web dashboard
β βββ config.yaml # Your settings
β βββ requirements.txt # Python packages needed
hng-stage3/
βββ -weight: 500;">docker-compose.yml # Starts all our services
βββ nginx/
β βββ nginx.conf # Web server config
βββ detector/
β βββ main.py # Main program
β βββ monitor.py # Watches the logs
β βββ baseline.py # Calculates "normal"
β βββ detector.py # Spots attacks
β βββ blocker.py # Blocks bad IPs
β βββ notifier.py # Sends Slack alerts
β βββ dashboard.py # Web dashboard
β βββ config.yaml # Your settings
β βββ requirements.txt # Python packages needed
# Every second, check how many people visited
def count_visitors(): # Read the log file # Count new entries # Return the number return visitor_count
# Every second, check how many people visited
def count_visitors(): # Read the log file # Count new entries # Return the number return visitor_count
# Every second, check how many people visited
def count_visitors(): # Read the log file # Count new entries # Return the number return visitor_count
def calculate_baseline(data): average = sum(data) / len(data) # Calculate how much traffic varies variation = calculate_standard_deviation(data) return { 'mean': average, 'stddev': variation }
def calculate_baseline(data): average = sum(data) / len(data) # Calculate how much traffic varies variation = calculate_standard_deviation(data) return { 'mean': average, 'stddev': variation }
def calculate_baseline(data): average = sum(data) / len(data) # Calculate how much traffic varies variation = calculate_standard_deviation(data) return { 'mean': average, 'stddev': variation }
def is_attack(current_traffic, baseline): z_score = (current_traffic - baseline['mean']) / baseline['stddev'] # Check z-score rule if z_score > 3.0: return True # Check 5x rule if current_traffic > (baseline['mean'] * 5): return True return False
def is_attack(current_traffic, baseline): z_score = (current_traffic - baseline['mean']) / baseline['stddev'] # Check z-score rule if z_score > 3.0: return True # Check 5x rule if current_traffic > (baseline['mean'] * 5): return True return False
def is_attack(current_traffic, baseline): z_score = (current_traffic - baseline['mean']) / baseline['stddev'] # Check z-score rule if z_score > 3.0: return True # Check 5x rule if current_traffic > (baseline['mean'] * 5): return True return False
def block_ip(ip_address): # Add firewall rule to block this IP os.system(f"iptables -A INPUT -s {ip_address} -j DROP") print(f"π« Blocked {ip_address}")
def block_ip(ip_address): # Add firewall rule to block this IP os.system(f"iptables -A INPUT -s {ip_address} -j DROP") print(f"π« Blocked {ip_address}")
def block_ip(ip_address): # Add firewall rule to block this IP os.system(f"iptables -A INPUT -s {ip_address} -j DROP") print(f"π« Blocked {ip_address}")
import requests def send_slack_alert(message): webhook_url = "YOUR_SLACK_WEBHOOK_URL" payload = {"text": message} requests.post(webhook_url, json=payload)
import requests def send_slack_alert(message): webhook_url = "YOUR_SLACK_WEBHOOK_URL" payload = {"text": message} requests.post(webhook_url, json=payload)
import requests def send_slack_alert(message): webhook_url = "YOUR_SLACK_WEBHOOK_URL" payload = {"text": message} requests.post(webhook_url, json=payload)
# Install the testing tool
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2-utils -y # Send normal traffic (10 requests/second for 60 seconds)
ab -n 600 -c 1 -t 60 http://localhost/
# Install the testing tool
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2-utils -y # Send normal traffic (10 requests/second for 60 seconds)
ab -n 600 -c 1 -t 60 http://localhost/
# Install the testing tool
-weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2-utils -y # Send normal traffic (10 requests/second for 60 seconds)
ab -n 600 -c 1 -t 60 http://localhost/
-weight: 500;">docker logs hng-detector -f
-weight: 500;">docker logs hng-detector -f
-weight: 500;">docker logs hng-detector -f
# Send 100 requests/second for 60 seconds
ab -n 6000 -c 10 -t 60 http://localhost/
# Send 100 requests/second for 60 seconds
ab -n 6000 -c 10 -t 60 http://localhost/
# Send 100 requests/second for 60 seconds
ab -n 6000 -c 10 -t 60 http://localhost/
# Make sure the web server is running
-weight: 500;">docker ps # Test by visiting your site
-weight: 500;">curl http://localhost/ # Check if logs are being created
-weight: 500;">docker exec hng-nginx tail /var/log/nginx/hng-access.log
# Make sure the web server is running
-weight: 500;">docker ps # Test by visiting your site
-weight: 500;">curl http://localhost/ # Check if logs are being created
-weight: 500;">docker exec hng-nginx tail /var/log/nginx/hng-access.log
# Make sure the web server is running
-weight: 500;">docker ps # Test by visiting your site
-weight: 500;">curl http://localhost/ # Check if logs are being created
-weight: 500;">docker exec hng-nginx tail /var/log/nginx/hng-access.log
# Make sure the detector has permission to use the firewall
-weight: 500;">docker inspect hng-detector | grep -i privileged
# Should show: "Privileged": true # If not, add this to your -weight: 500;">docker-compose.yml:
# privileged: true
# network_mode: "host"
# Make sure the detector has permission to use the firewall
-weight: 500;">docker inspect hng-detector | grep -i privileged
# Should show: "Privileged": true # If not, add this to your -weight: 500;">docker-compose.yml:
# privileged: true
# network_mode: "host"
# Make sure the detector has permission to use the firewall
-weight: 500;">docker inspect hng-detector | grep -i privileged
# Should show: "Privileged": true # If not, add this to your -weight: 500;">docker-compose.yml:
# privileged: true
# network_mode: "host"
# Check if the port is open on GCP
# Go to: VPC Network β Firewall β allow-dashboard-8080 # Test from your computer:
-weight: 500;">curl http://YOUR_SERVER_IP:8080/api/metrics
# Check if the port is open on GCP
# Go to: VPC Network β Firewall β allow-dashboard-8080 # Test from your computer:
-weight: 500;">curl http://YOUR_SERVER_IP:8080/api/metrics
# Check if the port is open on GCP
# Go to: VPC Network β Firewall β allow-dashboard-8080 # Test from your computer:
-weight: 500;">curl http://YOUR_SERVER_IP:8080/api/metrics - Watches the door - Keeps track of everyone visiting your site
- Learns the pattern - Figures out what "normal" traffic looks like
- Spots the troublemakers - Detects when something fishy is happening
- Takes action - Blocks suspicious visitors automatically
- Sends you alerts - Notifies you on Slack when there's trouble
- Shows a dashboard - Gives you a live view of what's happening - We track how many people visited in the last 60 seconds
- Every second, we add new data and -weight: 500;">remove the oldest
- This gives us a "rolling" view of recent activity - Watch traffic for 30 minutes
- Calculate the average (mean)
- Calculate how much it varies (standard deviation) - Normal traffic: 13 visitors/second Β± 2
- This means 11-15 is totally normal
- But 50 visitors/second? That's suspicious! - Z-score of 0 = Perfectly normal
- Z-score of 1-2 = A bit high, but okay
- Z-score of 3+ = ALERT! Something's wrong! - A Google Cloud account (free tier works!)
- A Slack account (to receive alerts)
- Basic knowledge of: Running commands in terminal
What Docker is (even just a basic idea)
Python basics (if statements, loops)
- Running commands in terminal
- What Docker is (even just a basic idea)
- Python basics (if statements, loops) - Running commands in terminal
- What Docker is (even just a basic idea)
- Python basics (if statements, loops) - Go to Google Cloud Console
- Create a new VM (virtual machine): Name: hng-stage3 Type: e2-medium (2 CPU, 4GB RAM) Disk: Ubuntu 22.04 LTS, 20GB Firewall: Allow HTTP and HTTPS traffic
- Name: hng-stage3
- Type: e2-medium (2 CPU, 4GB RAM)
- Disk: Ubuntu 22.04 LTS, 20GB
- Firewall: Allow HTTP and HTTPS traffic
- Click "Create" and wait a minute - Name: hng-stage3
- Type: e2-medium (2 CPU, 4GB RAM)
- Disk: Ubuntu 22.04 LTS, 20GB
- Firewall: Allow HTTP and HTTPS traffic - Go to VPC Network β Firewall
- Create a new rule called allow-dashboard-8080
- Allow TCP port 8080 from anywhere (0.0.0.0/0) - Detector spots the unusual traffic
- Calculates a high z-score
- Blocks the IP address
- Sends you a Slack alert
- Shows the attack on your dashboard - Live traffic graph - See requests per second
- Baseline tracker - Your "normal" traffic pattern
- Recent alerts - What attacks were detected
- Blocked IPs - Who's been banned - How real security systems work - It's not magic, just math!
- The power of baselines - Understanding "normal" helps you spot "abnormal"
- Why automation matters - Blocking attacks manually would be impossible
- Docker makes deployment easy - Everything runs in containers
- Monitoring is crucial - You can't fix what you can't see - π Python Documentation
- π³ Docker Getting Started
- π Slack Webhooks Guide