Upgrade improve revise the following: import os
import threading
import requests
import dns.resolver
from bs4 import BeautifulSoup
from queue import Queue
import re
import socket
import tkinter as tk
from tkinter import messagebox

# List of common subdomains to check for
common_subdomains = ['www', 'ftp', 'mail', 'docs', 'files', 'blog']

# Define file extensions we're looking for
file_types = ['txt', 'csv', 'log', 'xml']

# Keywords to scan for in text files
keywords = [
    "credit card numbers", "debit card details", "CVV codes", "cardholder name", "PAN", "expired credit card", "payment card fraud", 
    "PCI-DSS", "bank account number", "routing number", "IBAN", "SWIFT code", "sort code", "bank transfer details", 
    "account balance", "banking credentials", "username password leak", "credential stuffing", "login information", 
    "account credentials", "password hash", "authentication token", "login history", "password manager leak", 
    "PayPal account details", "Venmo account", "Cash App details", "digital wallet access", "mobile payment credentials", 
    "e-wallet hack", "online payment leak", "private key", "wallet address", "crypto wallet leak", "cryptocurrency transaction", 
    "Bitcoin address", "Ethereum wallet", "crypto account hack", "crypto key exposure", "social security number leak", 
    "name address phone number", "identity theft", "personal data breach", "address history", "PII leak", "email and phone data", 
    "data scraping", "data breach", "leaked database", "data leak forum", "hacked data", "dark web leaks", "exposed database", 
    "compromised data", "breached credentials", "fraud detection", "unauthorized transaction", "fraudulent payment", 
    "account takeover", "identity verification", "credit card fraud prevention", "chargeback fraud"
]

# Function to create the directory to store found data
def create_directory(path):
    if not os.path.exists(path):
        os.makedirs(path)

# Function to resolve DNS and get IPs for subdomains
def get_dns_records(domain):
    try:
        resolver = dns.resolver.Resolver()
        resolver.nameservers = ['8.8.8.8']  # Google DNS
        result = resolver.resolve(domain, 'A')
        return [ip.address for ip in result]
    except dns.resolver.NoAnswer:
        return []

# Function to check for subdomains and resolve them
def check_subdomains(base_domain, queue, log_text):
    log_message(f"Resolving subdomains for {base_domain}...", log_text)
    subdomains = []
    for sub in common_subdomains:
        subdomain = f"{sub}.{base_domain}"
        ips = get_dns_records(subdomain)
        if ips:
            subdomains.append(subdomain)
            log_message(f"Found subdomain: {subdomain}", log_text)
    if not subdomains:
        log_message(f"No subdomains found for {base_domain}", log_text)
    return subdomains

# Function to get nearby domains based on the local IP
def get_nearby_domains(log_text):
    local_ip = socket.gethostbyname(socket.gethostname())  # Get local machine IP
    log_message(f"Local IP: {local_ip}", log_text)
    
    # Example logic for nearby domains (could be expanded for your ISP IP range)
    ip_parts = local_ip.split('.')  # Get the first three octets of the IP address
    base_domain = f"{ip_parts[0]}.{ip_parts[1]}.{ip_parts[2]}"
    log_message(f"Scanning nearby domain: {base_domain}.com", log_text)
    return f"{base_domain}.com"

# Function to download and check content for keywords
def check_for_keywords(file_url, save_path, queue, log_text):
    try:
        log_message(f"Checking file {file_url}...", log_text)
        response = requests.get(file_url)
        if response.status_code == 200:
            content = response.text.lower()
            for keyword in keywords:
                if re.search(r'\b' + re.escape(keyword.lower()) + r'\b', content):  # word boundaries for precise matching
                    filename = os.path.join(save_path, f"{os.path.basename(file_url)}")
                    with open(filename, 'w', encoding='utf-8') as f:
                        f.write(response.text)
                    match_message = f"Found '{keyword}' in {file_url}, saved to {filename}"
                    log_message(match_message, log_text)
                    queue.put(match_message)
        else:
            log_message(f"Failed to fetch {file_url}: {response.status_code}", log_text)
    except requests.exceptions.RequestException as e:
        log_message(f"Error fetching {file_url}: {e}", log_text)

# Function to crawl and check for files in directories
def crawl_and_check_files(url, save_path, queue, log_text):
    try:
        log_message(f"Scanning {url} for links to files...", log_text)
        response = requests.get(url)
        if response.status_code == 200:
            soup = BeautifulSoup(response.text, 'html.parser')
            links = soup.find_all('a', href=True)
            for link in links:
                href = link['href']
                if any(href.endswith(ext) for ext in file_types):
                    full_url = f"{url}/{href}" if not href.startswith('http') else href
                    threading.Thread(target=check_for_keywords, args=(full_url, save_path, queue, log_text)).start()
        else:
            log_message(f"Failed to access {url}: {response.status_code}", log_text)
    except requests.exceptions.RequestException as e:
        log_message(f"Error accessing {url}: {e}", log_text)

# Function to crawl DNS-resolved subdomains and look for files
def crawl_subdomains_and_find_files(subdomains, save_path, queue, log_text):
    log_message("Starting subdomain crawl...", log_text)
    for subdomain in subdomains:
        crawl_and_check_files(f"http://{subdomain}", save_path, queue, log_text)

# Function to start the crawling process in a separate thread
def start_crawling(base_domain, save_path, queue, log_text):
    log_message(f"Starting crawl for base domain {base_domain}...", log_text)
    subdomains = check_subdomains(base_domain, queue, log_text)
    if subdomains:
        crawl_subdomains_and_find_files(subdomains, save_path, queue, log_text)

# Function to log messages to the GUI
def log_message(message, log_text):
    log_text.insert(tk.END, message + "\n")
    log_text.yview(tk.END)  # Scroll to the end of the log

# GUI Setup using Tkinter
def start_gui():
    # Create directory for storing found data
    save_path = "D:/SpiderCrawl"
    create_directory(save_path)
    
    # Setting up Tkinter window
    window = tk.Tk()
    window.title("SpiderCrawl")
    window.geometry("600x400")

    # Create a queue to get updates from threads
    queue = Queue()

    def start_crawl():
        # Automatically get nearby domain based on IP
        base_domain = get_nearby_domains(log_text)  # Get domain from IP resolution
        threading.Thread(target=start_crawling, args=(base_domain, save_path, queue, log_text), daemon=True).start()
        status_label.config(text=f"Crawling {base_domain} for sensitive data...", fg="green")
        log_message(f"Started crawling {base_domain}...", log_text)

    def update_gui():
        while not queue.empty():
            match_message = queue.get()
            matches_text.insert(tk.END, match_message + "\n")
            matches_text.yview(tk.END)
        window.after(100, update_gui)  # Check for updates every 100ms

    # UI components
    tk.Label(window, text="Starting automatic crawl based on local network...").pack(pady=10)
    crawl_button = tk.Button(window, text="Start Crawling", command=start_crawl)
    crawl_button.pack(pady=10)
    status_label = tk.Label(window, text="Status: Starting crawl", fg="red")
    status_label.pack(pady=10)

    # Text area to display matches
    matches_label = tk.Label(window, text="Matches Found:")
    matches_label.pack(pady=5)
    matches_text = tk.Text(window, height=10, width=70)
    matches_text.pack(pady=10)

    # Text area for logs
    log_label = tk.Label(window, text="Log:")
    log_label.pack(pady=5)
    log_text = tk.Text(window, height=10, width=70)
    log_text.pack(pady=10)

    # Start the GUI update loop
    window.after(100, update_gui)  # Start checking for matches immediately
    window.mainloop()

# Start the GUI
if __name__ == "__main__":
    start_gui()