SSH Security Monitor is a Python-based security tool designed to analyze SSH authentication logs, detect failed login attempts, identify suspicious IP addresses, and generate comprehensive daily security reports.
Perfect for learning defensive cybersecurity (Blue Team) operations and log analysis techniques.
- โจ Features
- ๐ฏ Objectives
- ๐ ๏ธ Technologies
- โ๏ธ Installation
- ๐ Usage
- ๐ Output Examples
- ๐ Project Structure
- ๐ How It Works
- ๐ก๏ธ Security Best Practices
- ๐ License
- ๐ค Author
- ๐ SSH log parsing from
data/sample_auth.log - ๐จ Failed login attempt detection with IP tracking
- ๐ Suspicious IP identification based on threshold rules (>3 attempts)
- โ Successful connection monitoring
- ๐ Automated daily security reports in
reports/daily_report.txt - ๐ IP blocking simulation with
iptables(dry-run mode) - ๐ Statistical analysis of authentication patterns
- ๐จ Clean terminal output with organized results
This project aims to:
- Parse and analyze SSH authentication logs from
data/sample_auth.log - Detect and count failed connection attempts per IP address
- Identify suspicious IPs with multiple failures (threshold: >3 attempts)
- List successful connections with occurrence counts
- Generate automated reports for daily security review in
reports/daily_report.txt - Simulate IP blocking using
iptables(test mode) - Provide actionable insights for system administrators
| Component | Technology | Purpose |
|---|---|---|
| Language | Python 3 | Core scripting and log parsing |
| Modules | re, os, subprocess, collections |
Pattern matching, file operations, system integration |
| System | Linux (Ubuntu/Debian/Kali) | Target operating system |
| Version Control | Git & GitHub | Source code management |
| Firewall | iptables |
IP blocking capabilities (simulated) |
- Linux-based OS (Ubuntu 20.04+, Debian, Kali Linux)
- Python 3.x installed
- Root/sudo access (for reading system logs and iptables)
- Git for cloning the repository
# Clone the repository
git clone https://github.com/YasserBouchaal/ssh-security-monitor.git
cd ssh-security-monitor
# Verify project structure
ls -la
# Install Python (if not already installed)
sudo apt update
sudo apt install python3 python3-pip -y
# Install dependencies (optional, uses built-in modules)
pip3 install -r requirements.txt
# (Optional) Copy system SSH logs for analysis
sudo cp /var/log/auth.log data/sample_auth.logThe main script is located in the src/ directory. Run it from the project root:
# Navigate to src directory
cd src/
# Run the monitor
python3 main.py- Reads logs from
data/sample_auth.log - Analyzes failed and successful authentication attempts
- Displays results in the terminal
- Generates report in
reports/daily_report.txt - Simulates blocking suspicious IPs (dry-run mode)
The script currently runs with default settings. To customize:
# Edit main.py to change:
# - Log file path: "data/sample_auth.log"
# - Threshold for suspicious IPs: count > 3
# - Dry-run mode: dry_run=True (change to False to actually block IPs)=== SSH Security Monitor ===
[Connexions รฉchouรฉes]
192.168.1.15 โ 7 fois
10.0.0.44 โ 1 fois
[Connexions rรฉussies]
192.168.1.20 โ 1 fois
[SIMULATION] Would block IP: 192.168.1.15 (7 attempts)
Rapport gรฉnรฉrรฉ : reports/daily_report.txt
File: reports/daily_report.txt
=== SSH Security Monitor - Rapport Quotidien ===
Date : 2025-10-31 04:17:02
[Connexions รฉchouรฉes]
192.168.1.15 โ 7 fois
10.0.0.44 โ 1 fois
[Connexions rรฉussies]
192.168.1.20 โ 1 fois
ssh-security-monitor/
โ
โโโ src/
โ โโโ main.py # Main entry point - orchestrates analysis
โ โโโ log_parser.py # Log parsing and IP detection functions
โ โโโ report_generator.py # Report generation module
โ โโโ ip_blocker.py # IP blocking simulation with iptables
โ
โโโ data/
โ โโโ sample_auth.log # Sample SSH authentication logs
โ
โโโ reports/
โ โโโ daily_report.txt # Generated security reports
โ
โโโ requirements.txt # Python dependencies (built-in modules)
โโโ README.md # Project documentation
โโโ LICENSE # MIT License
โโโ .gitignore # Git ignore rules
Functions:
read_local_log(filepath)- Reads SSH log file line by linedetect_failed_ips(logs)- Extracts IPs with failed authentication attemptsdetect_successful_ips(logs)- Extracts IPs with successful logins
Detection Patterns:
# Failed authentication patterns
"Failed password for"
"Invalid user"
"Connection closed by authenticating user"
# Successful authentication
"Accepted password for"
"Accepted publickey for"Workflow:
- Load logs from
data/sample_auth.log - Parse failed and successful authentication attempts
- Display results in terminal
- Generate daily report
- Identify suspicious IPs (>3 failed attempts)
- Simulate IP blocking (dry-run mode)
Threshold Logic:
# IPs with more than 3 failed attempts are flagged
ips_to_block = {ip: count for ip, count in failed_ips.items() if count > 3}Output:
- Creates/updates
reports/daily_report.txt - Includes timestamp, failed attempts, and successful connections
- Formatted for easy reading and archival
Simulation Mode:
# Dry-run mode (default) - only prints what would be blocked
block_multiple_ips(ips_to_block, dry_run=True)
# Active mode (use with caution)
block_multiple_ips(ips_to_block, dry_run=False)iptables Command Used:
# Example: blocking an IP
sudo iptables -A INPUT -s 192.168.1.15 -j DROP-
Change default SSH port (from 22 to custom port)
sudo nano /etc/ssh/sshd_config # Change: Port 2222 sudo systemctl restart sshd -
Disable root login
# In /etc/ssh/sshd_config PermitRootLogin no -
Use key-based authentication
# Generate SSH key pair ssh-keygen -t rsa -b 4096 # Disable password authentication PasswordAuthentication no
-
Implement fail2ban for automatic blocking
sudo apt install fail2ban sudo systemctl enable fail2ban -
Regular log monitoring
# Add to crontab for daily analysis 0 8 * * * cd /path/to/ssh-security-monitor/src && python3 main.py
-
Keep SSH updated
sudo apt update && sudo apt upgrade openssh-server
- โ Change default port (22 โ custom)
- โ Disable root login
- โ Enforce key-based authentication
- โ Limit authentication attempts (MaxAuthTries 3)
- โ Set idle timeout (ClientAliveInterval 300)
- โ Use strong passwords/passphrases (20+ characters)
- โ Enable two-factor authentication (2FA)
- โ Whitelist allowed users (AllowUsers)
- โ Monitor logs regularly
- โ Update software frequently
This project is licensed under the MIT License.
Yasser Bouchaal
Cybersecurity Student | Blue Team Enthusiast
- ๐ Specialization: Defensive Security & Log Analysis
- ๐ผ LinkedIn: linkedin.com/in/yasser-bouchaal
- ๐ GitHub: @YasserBouchaal
- ๐ง Contact: yasser.bouchaal@example.com
Project created as part of network security and defensive cybersecurity learning.
Contributions are welcome! To contribute:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Planned features for future versions:
- ๐ Real-time log monitoring with file watchers
- ๐ Web dashboard using Flask for visualization
- ๐๏ธ SQLite database for historical data storage
- ๐ง Email alerts for critical security events
- ๐ Advanced statistics and trend analysis
- ๐ Integration with fail2ban for automated blocking
- ๐ GeoIP lookup for IP location tracking
- ๐ฑ Mobile notifications via Telegram/Discord
- ๐ SSH Best Practices Guide
- ๐ Linux Log Analysis Tutorial
- ๐ Fail2ban Documentation
- ๐ Python Regex Guide
- ๐ iptables Tutorial
โ ๏ธ Warning: This tool is for educational and defensive security purposes only. Always ensure you have proper authorization before analyzing system logs or blocking IP addresses.
๐ก Tip: Test the IP blocking functionality in a controlled environment before deploying to production. Use dry-run mode by default.
๐ Privacy: Never share raw log files or IP addresses publicly without proper anonymization.
๐ Learning: This project is designed for educational purposes to help students understand log analysis, pattern matching, and network security concepts.
# 1. Clone and setup
git clone https://github.com/YasserBouchaal/ssh-security-monitor.git
cd ssh-security-monitor
# 2. Add your log file (or use sample)
sudo cp /var/log/auth.log data/sample_auth.log
# 3. Run the monitor
cd src/
python3 main.py
# 4. Check the report
cat ../reports/daily_report.txtStatus: โ
Functional & Tested
Version: 1.0.0
Last Updated: October 2024
Next Release: v1.1.0 (planned - web dashboard integration)
๐ฏ Goal Achieved: Functional SSH Security Monitoring System!
Made with โค๏ธ for the cybersecurity community