import sqlite3

# Function to create the vulnerabilities table
def create_vulnerabilities_table():
    vulnerability_connection = sqlite3.connect("vulnerabilities.db")
    vulnerability_cursor = vulnerability_connection.cursor()

    # Create the 'vulnerabilities' table with columns for details
    vulnerability_cursor.execute('''CREATE TABLE IF NOT EXISTS vulnerabilities
                               (id INTEGER PRIMARY KEY AUTOINCREMENT,
                                protocol TEXT,
                                source TEXT,
                                destination TEXT,
                                length INTEGER,
                                vulnerability_info TEXT)''')
    vulnerability_connection.commit()
    vulnerability_connection.close()

# Function to detect vulnerabilities and store them in a database
def detect_vulnerabilities():
    # Connect to the SQLite database containing captured packets
    connection = sqlite3.connect("captured_packets.db")
    cursor = connection.cursor()

    # Connect to the vulnerabilities database
    vulnerability_connection = sqlite3.connect("vulnerabilities.db")
    vulnerability_cursor = vulnerability_connection.cursor()

    # Protocol to check for vulnerability
    target_protocol = 'DATA'

    # Query to retrieve packets with the target protocol
    query = "SELECT protocol, source, destination, length FROM packets WHERE protocol = ?"
    cursor.execute(query, (target_protocol,))
    packets = cursor.fetchall()

    for packet in packets:
        protocol, source, destination, length = packet
        vulnerability_info = f"Vulnerability detected: Protocol is '{protocol}', Source: {source}, Destination: {destination}, Length: {length}"
        vulnerability_query = "INSERT INTO vulnerabilities (protocol, source, destination, length, vulnerability_info) VALUES (?, ?, ?, ?, ?)"
        vulnerability_cursor.execute(vulnerability_query, (protocol, source, destination, length, vulnerability_info))

    vulnerability_connection.commit()
    connection.close()
    vulnerability_connection.close()

# Call the function to create the 'vulnerabilities' table
create_vulnerabilities_table()

# Call the function to detect 'DATA' protocol as a vulnerability and store it
detect_vulnerabilities()
