Spaces:
Sleeping
Sleeping
Download app.py from SpectroGuard-CySe/SpectroGuard: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/spaces/SpectroGuard-CySe/SpectroGuard/resolve/main/app.py
- Command line
-
hf download hf://spaces/SpectroGuard-CySe/SpectroGuard/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SpectroGuard-CySe/SpectroGuard/resolve/main/app.py
10.7 kB
| import os | |
| import re | |
| import math | |
| import pickle | |
| import joblib | |
| from urllib.parse import urlparse | |
| from collections import defaultdict, Counter | |
| from difflib import SequenceMatcher | |
| # Socket fallback for local DNS checking | |
| import socket | |
| import datetime | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from flask import Flask, request, jsonify, render_template | |
| from flask_cors import CORS | |
| # --- SAFE WHOIS IMPORT --- | |
| try: | |
| import whois | |
| except ImportError: | |
| whois = None | |
| # ========================================== | |
| # ๐ ๏ธ DYNAMIC PATHS FOR CLOUD DEPLOYMENT | |
| # ========================================== | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| TEMPLATE_DIR = os.path.join(BASE_DIR, 'Web') if os.path.exists(os.path.join(BASE_DIR, 'Web')) else os.path.join(BASE_DIR, 'templates') | |
| MODEL_PATH = os.path.join(BASE_DIR, 'Web', 'Models') if os.path.exists(os.path.join(BASE_DIR, 'Web', 'Models')) else os.path.join(BASE_DIR, 'Models') | |
| app = Flask(__name__, template_folder=TEMPLATE_DIR) | |
| CORS(app) | |
| # ========================================== | |
| # ๐ง MODEL CLASSES & DEFINITIONS | |
| # ========================================== | |
| class MarkovPasswordModel: | |
| def __init__(self): | |
| self.n = 3 | |
| self.transitions = defaultdict(Counter) | |
| def score_password(password: str): | |
| if not password: | |
| return 0, 'Too Short' | |
| # ๐ ๏ธ HIGH-FIDELITY HYBRID FALLBACK ALGORITHM | |
| # If file loader fails due to structural environment guards, calculate real complexity dynamically | |
| if not markov_model or not hasattr(markov_model, 'transitions') or len(markov_model.transitions) == 0: | |
| length = len(password) | |
| pool = 0 | |
| if re.search(r'[a-z]', password): pool += 26 | |
| if re.search(r'[A-Z]', password): pool += 26 | |
| if re.search(r'[0-9]', password): pool += 10 | |
| if re.search(r'[^a-zA-Z0-9]', password): pool += 32 | |
| if pool == 0: pool = 1 | |
| entropy = length * math.log2(pool) | |
| score = (entropy / 80) * 100 | |
| score = max(5, min(score, 100)) | |
| if length < 6: | |
| score = min(score, 25) | |
| elif length < 8: | |
| score = min(score, 45) | |
| labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')] | |
| label = next((l for s, l in labels if score < s), 'Very Strong') | |
| return round(score, 2), label | |
| # Standard Markov Calculation if loaded successfully | |
| order, text = 2, '<^^' + password + '<' | |
| log_prob, count, vocab = 0.0, 0, 150 | |
| for i in range(order, len(text)): | |
| ctx = text[i - order:i] | |
| nx = text[i] | |
| tot = sum(markov_model.transitions[ctx].values()) | |
| freq = markov_model.transitions[ctx].get(nx, 0) | |
| prob = (freq + 1) / (tot + vocab) if tot > 0 else 1 / vocab | |
| log_prob += math.log2(prob) | |
| count += 1 | |
| if count == 0: | |
| return 0, 'Too Short' | |
| avg_ll = log_prob / count | |
| score = (avg_ll - (-3.5)) / ((-8.0) - (-3.5)) * 100 | |
| score = max(0, min(score, 100)) | |
| labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')] | |
| label = next((l for s, l in labels if score < s), 'Very Strong') | |
| return round(score, 2), label | |
| def load_model_file(filename): | |
| path = os.path.join(MODEL_PATH, filename) | |
| if not os.path.exists(path): | |
| print(f"[WARNING] Cloud model file not found at: {path}") | |
| return None | |
| try: | |
| return joblib.load(path) | |
| except Exception as e: | |
| print(f"[ERROR/JOBLIB] Direct load failed: {e}") | |
| try: | |
| with open(path, "rb") as f: | |
| return pickle.load(f) | |
| except Exception as e2: | |
| print(f"[ERROR/PICKLE] Fallback read failed: {e2}") | |
| return None | |
| # --- MODELS LOADING --- | |
| password_model = load_model_file("markov_password_model.joblib") | |
| markov_model = password_model | |
| phishing_svm_model = load_model_file("phishing_svm_model.pkl") | |
| url_data = load_model_file("phishing_model_url.pkl") | |
| url_legitimacy_model = url_data["model"] if isinstance(url_data, dict) else url_data | |
| print("[SYSTEM] Cloud-ready configuration loaded.") | |
| # ========================================== | |
| # ๐ TYPO DETECTION | |
| # ========================================== | |
| TOP_TARGET_DOMAINS = [ | |
| "google.com", "facebook.com", "amazon.com", "paypal.com", | |
| "microsoft.com", "apple.com", "netflix.com", "instagram.com", | |
| "twitter.com", "linkedin.com", "github.com", "yahoo.com" | |
| ] | |
| def get_domain(url): | |
| parsed = urlparse(url if url.startswith("http") else "http://" + url) | |
| return parsed.netloc.lower().replace("www.", "") | |
| def similarity(a, b): | |
| return SequenceMatcher(None, a, b).ratio() | |
| def detect_typo(domain): | |
| best_match = None | |
| best_score = 0 | |
| for legit in TOP_TARGET_DOMAINS: | |
| sim = similarity(domain, legit) | |
| if sim > best_score: | |
| best_score = sim | |
| best_match = legit | |
| return best_match, best_score | |
| def extract_url_features(url: str): | |
| parsed = urlparse(url if url.startswith("http") else "http://" + url) | |
| domain = parsed.netloc.lower().replace("www.", "") | |
| feats = [len(url), len(re.findall(r"\d", url)), url.count('.'), domain.count('-'), | |
| 1 if parsed.scheme == "https" else 0] | |
| while len(feats) < 18: feats.append(0) | |
| return feats | |
| # ========================================== | |
| # ๐ ๏ธ ROBUST FEATURE EXTRACTION | |
| # ========================================== | |
| def extract_real_features(url): | |
| try: | |
| parsed = urlparse(url if url.startswith("http") else "http://" + url) | |
| domain = parsed.netloc | |
| url_length = len(url) | |
| num_subdomains = domain.count(".") | |
| has_ip = 1 if re.match(r"\d+\.\d+\.\d+\.\d+", domain) else 0 | |
| has_https = 1 if parsed.scheme == "https" else 0 | |
| num_special = len(re.findall(r"[!@#$%^&*(),?\":{}|<>]", url)) | |
| url_depth = parsed.path.count("/") | |
| try: | |
| socket.gethostbyname(domain) | |
| dns_exists = 1 | |
| except: | |
| return None | |
| age_days = 0 | |
| if whois: | |
| try: | |
| domain_info = whois.whois(domain) | |
| creation_date = domain_info.creation_date | |
| if isinstance(creation_date, list): | |
| creation_date = creation_date[0] | |
| if creation_date: | |
| age_days = (datetime.datetime.now() - creation_date).days | |
| except: | |
| age_days = 0 | |
| try: | |
| response = requests.get(url, timeout=4) | |
| html = response.text | |
| soup = BeautifulSoup(html, "html.parser") | |
| except: | |
| return None | |
| forms = soup.find_all("form") | |
| iframes = soup.find_all("iframe") | |
| scripts = soup.find_all("script", src=True) | |
| has_login_form = 0 | |
| for form in forms: | |
| if form.find("input", {"type": "password"}): | |
| has_login_form = 1 | |
| break | |
| iframe_count = len(iframes) | |
| external_scripts = 0 | |
| for script in scripts: | |
| src = script.get("src") | |
| if src and domain not in src: | |
| external_scripts += 1 | |
| ssl_valid = 1 if parsed.scheme == "https" else 0 | |
| features = [ | |
| url_length, num_subdomains, has_ip, 0, num_special, has_https, url_depth, | |
| 0, 0, len(domain), age_days, ssl_valid, dns_exists, 1, 1, | |
| external_scripts, has_login_form, 0, iframe_count, 0, 0 | |
| ] | |
| return features | |
| except: | |
| return None | |
| def check_blacklist(url): | |
| try: | |
| test_url = f"https://transparencyreport.google.com/transparencyreport/api/v3/safebrowsing/status?site={url}" | |
| r = requests.get(test_url, timeout=4) | |
| if "unsafe" in r.text.lower(): | |
| return 1 | |
| return 0 | |
| except: | |
| return 0 | |
| # ========================================== | |
| # ๐ฃ๏ธ ROUTES | |
| # ========================================== | |
| def index(): return render_template('Index.html') | |
| def url_page(): return render_template('url-checker.html') | |
| def phishing_page(): return render_template('phishing.html') | |
| def password_checker_page(): return render_template('password-checker.html') | |
| def hash_page(): return render_template('hash.html') | |
| def analysis_page(): return render_template('Analysis.html') | |
| # ========================================== | |
| # ๐ API ENDPOINTS | |
| # ========================================== | |
| def check_url(): | |
| if not url_legitimacy_model: | |
| return jsonify({"error": "URL legitimacy model missing"}), 500 | |
| url = request.get_json().get("url", "") | |
| domain = get_domain(url) | |
| prob = url_legitimacy_model.predict_proba([extract_url_features(url)])[0][1] | |
| ml_score = (1 - prob) * 100 | |
| matched_domain, sim_score = detect_typo(domain) | |
| typo_penalty = 0 | |
| is_typo = False | |
| if sim_score > 0.75 and domain != matched_domain: | |
| is_typo = True | |
| typo_penalty = sim_score * 50 | |
| final_score = min(ml_score + typo_penalty, 100) | |
| status = "Not Safe" if is_typo or final_score < 75 else "Safe" | |
| return jsonify({ | |
| "score": round(final_score, 2), | |
| "status": status, | |
| "typo_detected": is_typo, | |
| "matched_domain": matched_domain if is_typo else None, | |
| "similarity": round(sim_score, 2) | |
| }) | |
| def check_phishing(): | |
| if not phishing_svm_model: | |
| return jsonify({"error": "Phishing SVM model missing"}), 500 | |
| url = request.get_json().get("url", "") | |
| model = phishing_svm_model['pipeline'] | |
| features = extract_real_features(url) | |
| if features is None: | |
| return jsonify({"score": 0, "status": "Website Not Found"}) | |
| blacklist_flag = check_blacklist(url) | |
| probs = model.predict_proba([features])[0] | |
| phishing_prob = probs[1] * 100 | |
| final_score = 100 if blacklist_flag else phishing_prob | |
| domain = get_domain(url) | |
| matched_domain, sim_score = detect_typo(domain) | |
| if sim_score > 0.75 and domain != matched_domain: | |
| final_score += 30 | |
| final_score = min(final_score, 100) | |
| return jsonify({ | |
| "score": round(final_score, 2), | |
| "status": "Not Safe" if final_score > 50 else "Safe" | |
| }) | |
| def route_score(): | |
| data = request.get_json() | |
| s, lbl = score_password(data.get('password', '')) | |
| return jsonify({'score': s, 'label': lbl}) | |
| if __name__ == '__main__': | |
| port = int(os.environ.get("PORT", 5000)) | |
| app.run(host='0.0.0.0', port=port) |