Skip to content

yasserbouchaal/ssh-security-monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ” SSH Security Monitor โ€” Log Analysis & Threat Detection

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.


๐Ÿ“‹ Table of Contents


โœจ Features

  • ๐Ÿ” 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

๐ŸŽฏ Objectives

This project aims to:

  1. Parse and analyze SSH authentication logs from data/sample_auth.log
  2. Detect and count failed connection attempts per IP address
  3. Identify suspicious IPs with multiple failures (threshold: >3 attempts)
  4. List successful connections with occurrence counts
  5. Generate automated reports for daily security review in reports/daily_report.txt
  6. Simulate IP blocking using iptables (test mode)
  7. Provide actionable insights for system administrators

๐Ÿ› ๏ธ Technologies

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)

โš™๏ธ Installation

Prerequisites

  • 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

Quick Setup

# 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.log

๐Ÿš€ Usage

Running the Monitor

The 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

What Happens When You Run It:

  1. Reads logs from data/sample_auth.log
  2. Analyzes failed and successful authentication attempts
  3. Displays results in the terminal
  4. Generates report in reports/daily_report.txt
  5. Simulates blocking suspicious IPs (dry-run mode)

Command Options

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)

๐Ÿ“Š Output Examples

Terminal Output

=== 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

Generated Report Format

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

๐Ÿ“ Project Structure

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

๐Ÿ” How It Works

1. Log Parsing (log_parser.py)

Functions:

  • read_local_log(filepath) - Reads SSH log file line by line
  • detect_failed_ips(logs) - Extracts IPs with failed authentication attempts
  • detect_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"

2. Main Analysis (main.py)

Workflow:

  1. Load logs from data/sample_auth.log
  2. Parse failed and successful authentication attempts
  3. Display results in terminal
  4. Generate daily report
  5. Identify suspicious IPs (>3 failed attempts)
  6. 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}

3. Report Generation (report_generator.py)

Output:

  • Creates/updates reports/daily_report.txt
  • Includes timestamp, failed attempts, and successful connections
  • Formatted for easy reading and archival

4. IP Blocking (ip_blocker.py)

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

๐Ÿ›ก๏ธ Security Best Practices

Recommendations

  1. Change default SSH port (from 22 to custom port)

    sudo nano /etc/ssh/sshd_config
    # Change: Port 2222
    sudo systemctl restart sshd
  2. Disable root login

    # In /etc/ssh/sshd_config
    PermitRootLogin no
  3. Use key-based authentication

    # Generate SSH key pair
    ssh-keygen -t rsa -b 4096
    
    # Disable password authentication
    PasswordAuthentication no
  4. Implement fail2ban for automatic blocking

    sudo apt install fail2ban
    sudo systemctl enable fail2ban
  5. Regular log monitoring

    # Add to crontab for daily analysis
    0 8 * * * cd /path/to/ssh-security-monitor/src && python3 main.py
  6. Keep SSH updated

    sudo apt update && sudo apt upgrade openssh-server

SSH Hardening Checklist

  • โœ… 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

๐Ÿ“„ License

This project is licensed under the MIT License.


๐Ÿ‘ค Author

Yasser Bouchaal
Cybersecurity Student | Blue Team Enthusiast

Project created as part of network security and defensive cybersecurity learning.


๐Ÿค Contributing

Contributions are welcome! To contribute:

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ”ฎ Future Enhancements

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

๐Ÿ“š Additional Resources


๐Ÿ“ Important Notes

โš ๏ธ 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.


โšก Quick Start Guide

# 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.txt

๐ŸŽฏ Project Status

Status: โœ… 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

About

๐Ÿ”‘๐Ÿ›ก๏ธ๐Ÿ–ฅ๏ธ Tool to monitor SSH login attempts and detect suspicious IPs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages