fabriziosalmi / blacklists

Hourly updated domains blacklist 🚫
https://github.com/fabriziosalmi/blacklists/releases/download/latest/blacklist.txt
GNU General Public License v3.0
141 stars 6 forks source link

Services > Alerting & Notifications #20

Closed fabriziosalmi closed 1 year ago

fabriziosalmi commented 1 year ago

Certainly! Creating an alerting and notification service involves several steps:

  1. User Registration: Collect user information, including domain or IP and their contact details.
  2. Database: Store the user data.
  3. Blacklist Checking Service: Periodically check the stored domains/IPs against blacklists.
  4. Notification System: Notify users if their domain or IP appears on a blacklist.

Given the requirements, I'll outline a high-level solution using Python, SQLite as a database, and the SMTP library to send email alerts:

  1. Setting Up the Database:

    First, let's create a script to set up a SQLite database:

    # setup_db.py
    import sqlite3
    
    con = sqlite3.connect('domains.db')
    cursor = con.cursor()
    
    # Create the table
    cursor.execute('''
    CREATE TABLE users (
        id INTEGER PRIMARY KEY,
        domain TEXT NOT NULL,
        email TEXT NOT NULL
    )
    ''')
    
    con.commit()
    con.close()

    Run this script to create the SQLite database:

    python setup_db.py
  2. User Registration Script:

    Register domains and their respective user's email.

    # register_domain.py
    import sqlite3
    
    def register(domain, email):
        con = sqlite3.connect('domains.db')
        cursor = con.cursor()
    
        cursor.execute("INSERT INTO users (domain, email) VALUES (?, ?)", (domain, email))
    
        con.commit()
        con.close()
    
    if __name__ == "__main__":
        domain = input("Enter the domain/IP to monitor: ")
        email = input("Enter your email: ")
        register(domain, email)
        print(f"Monitoring {domain} for {email}.")
  3. Blacklist Checking and Notification:

    This script will check the blacklists and send an email notification:

    # monitor_blacklist.py
    import sqlite3
    import requests
    import smtplib
    from email.message import EmailMessage
    
    BLACKLIST_URL = "https://get.domainsblacklists.com/blacklist.txt"
    
    def get_blacklist():
        response = requests.get(BLACKLIST_URL)
        if response.status_code == 200:
            return set(response.text.splitlines())
        return set()
    
    def send_email(recipient, domain):
        msg = EmailMessage()
        msg.set_content(f'Your domain {domain} is on the blacklist!')
        msg['Subject'] = 'Blacklist Alert!'
        msg['From'] = 'your_email@gmail.com'
        msg['To'] = recipient
    
        # Authenticate with your SMTP server and send email
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.login('your_email@gmail.com', 'your_password')  # Use a more secure method for production
        server.send_message(msg)
        server.quit()
    
    if __name__ == "__main__":
        con = sqlite3.connect('domains.db')
        cursor = con.cursor()
        cursor.execute("SELECT domain, email FROM users")
        users = cursor.fetchall()
    
        blacklist = get_blacklist()
    
        for domain, email in users:
            if domain in blacklist:
                print(f"Alert! {domain} found in blacklist. Notifying {email}.")
                send_email(email, domain)

    Note: For the email service, we're using Gmail's SMTP server. Remember to enable "Less secure app access" for your Gmail account to allow sending emails from the script. In a production environment, consider using dedicated email services and not hardcoding credentials.

Now, to run the service:

  1. Set up the database: python setup_db.py
  2. Register domains: python register_domain.py
  3. Periodically run the monitoring script: python monitor_blacklist.py

For a real-world service, you'd want to incorporate more features such as error handling, user verification, a web-based interface, better security for password handling, and scheduling tools to automate the blacklist monitoring.

fabriziosalmi commented 1 year ago

maybe actions instead