Security Monitoring Script Pangolin
Table Of Contents
- Security Monitoring Script
- Features
- Network Traffic Monitoring
- System Security Monitoring
- Reporting Capabilities
- Prerequisites
- Installation
- Configuration
- Discord Webhook Setup
- Customizable Parameters
- Usage
- Starting the Monitor
- Stopping the Monitor
- Log Files
- Troubleshooting
- Common Issues and Solutions
- Error Logging
- Security Considerations
- Customization
Security Monitoring Script #
A security monitoring solution that tracks network traffic, system health, and security events on Linux systems, with automated reporting to Discord. This script is designed to help system administrators maintain awareness of their system’s security status and network activity, particularly for servers running services like Pangolin, Gerbil, and other web services.
Features
The script provides continuous monitoring of several critical system aspects:
Network Traffic Monitoring
- HTTPS traffic (Port 443)
- HTTP traffic (Port 80)
- WireGuard traffic (Port 51820)
- Active connection tracking
- Detailed connection state information
System Security Monitoring
- Failed SSH login attempts
- System load statistics
- Memory usage tracking
- Disk space utilization
- Active connection counts
Reporting Capabilities
- Real-time Discord notifications
- Local log file generation
- Automatic message chunking for Discord’s limits
- Rate-limited API calls to prevent throttling
- Error logging and handling
Prerequisites
The script requires the following components to be installed on your system:
- bash wrap shell (version 4.0 or higher)
ss
command (usually part of iproute2 package)curl
for Discord API communicationgrep
for log analysis- Standard Unix utilities (
date
,awk
, etc.)
To install missing prerequisites on Debian/Ubuntu systems:
sudo apt updatesudo apt install iproute2 curl grep coreutils
For Red Hat/CentOS systems:
sudo yum updatesudo yum install iproute curl grep coreutils
Installation
- Download the script:
curl -O https://gist.githubusercontent.com/hhftechnology/38e10e051af2c0313dbb364bdf67eeb0/raw/0bdcc175756d996d7c3beed15824fc0822e05fd9/monitor.sh
- Make the script executable:
chmod +x monitor.sh
- Create the log directory:
sudo mkdir -p /var/log/security-monitorsudo chown $(whoami):$(whoami) /var/log/security-monitor
Configuration
Discord Webhook Setup
- Open your Discord server settings
- Navigate to Integrations → Webhooks
- Click “Create Webhook”
- Choose a name and channel for the webhook
- Copy the webhook URL
- Edit the script and replace
YOUR_DISCORD_WEBHOOK_URL
with your webhook URL:
DISCORD_WEBHOOK="https://discord.com/api/webhooks/your-webhook-url"
Customizable Parameters
The script includes several configurable parameters at the top:
LOG_DIR="/var/log/security-monitor" # Location of log filesTEMP_FILE="/tmp/security_report.txt" # Temporary file for report generationCHECK_INTERVAL=300 # Monitoring interval in seconds
Modify these values according to your needs. For example, to check every minute, set CHECK_INTERVAL=60
.
Usage
Starting the Monitor
Run in the foreground:
./monitor.sh
Run in the background:
nohup ./monitor.sh > monitor.log 2>&1 &
Stopping the Monitor
Find the process ID:
ps aux | grep monitor.sh
Stop the script:
kill <process_id>
Log Files
The script generates several log files:
- Daily monitoring logs:
/var/log/security-monitor/monitoring_YYYY-MM-DD.log
- Error logs:
/var/log/security-monitor/error.log
Log files follow the format:
=== Traffic Monitoring Report 2025-01-22 14:30:00 ===Port 443 (HTTPS) Traffic:[Connection details]
Port 80 (HTTP) Traffic:[Connection details]
Port 51820 (WireGuard) Traffic:[Connection details]
Security Report[System statistics and security information]
Troubleshooting
Common Issues and Solutions
-
Discord messages not sending:
- Verify your webhook URL is correct
- Check network connectivity
- Review error.log for specific error messages
-
Missing command errors:
- Install missing prerequisites using package manager
- Verify PATH includes required commands
- Check permissions for command execution
-
Permission denied errors:
- Ensure proper permissions on log directory
- Run with sudo if accessing protected log files
- Check file ownership and permissions
Error Logging
The script logs errors to /var/log/security-monitor/error.log
. Common error messages include:
- Discord API errors
- Command execution failures
- File access permission issues
Security Considerations
- The script requires access to system logs and network information
- Discord webhook URL should be kept private
- Consider running the script as a non-root user with minimal required permissions
- Monitor the script’s resource usage, especially on busy systems
- Review logged data regularly to ensure sensitive information is not exposed
Customization
The script can be customized in several ways:
-
Monitoring additional ports: Add new port monitoring in the main loop:
Terminal window custom_traffic=$(ss -tn state established "( dport = :YOUR_PORT or sport = :YOUR_PORT )")format_traffic "YOUR_PORT (DESCRIPTION)" "$custom_traffic" >> "$TEMP_FILE" -
Additional security checks: Modify the check_security function to include custom checks:
Terminal window check_security() {# Add your custom checks herelocal custom_check=$(your_command_here)} -
Custom formatting: Modify the format_traffic function to change output formatting:
Terminal window format_traffic() {# Customize your output format}