Build a Python Phishing URL Detector: Check Links Before You Click


Phishing doesn't start with a hack — it starts with a click. A link in a message, an email, a comment. Today we'll build a Python tool that checks a link before you open it: a URL scorer that flags the classic phishing tricks in seconds.

How It Works

Phishing links share a set of tells. Our script checks seven and assigns a risk score:

  1. No HTTPS — real login pages use encryption; many phishing pages don't.
  2. IP address instead of a domain — http://185.22.41.9/login is almost never legitimate.
  3. Too many subdomains — login.paypal.com.attacker.com looks like PayPal, but the real domain is attacker.com.
  4. Urgent keywords — verify, secure, account, prize are bait words.
  5. High-abuse TLDs — free domains like .tk, .xyz, and .top dominate phishing.
  6. The @ trick — in http://paypal.com@evil.com, everything before the @ is ignored; the real host is evil.com.
  7. Absurd length — long URLs hide the real destination.

The Code

import re
import ipaddress
from urllib.parse import urlparse

SUSPICIOUS_WORDS = ["login", "verify", "secure", "account", "update",
                    "confirm", "bank", "wallet", "free", "prize"]
RISKY_TLDS = {".tk", ".ml", ".ga", ".cf", ".gq", ".xyz", ".top", ".buzz"}

def score_url(raw):
    url = raw if raw.startswith(("http://", "https://")) else "http://" + raw
    parsed = urlparse(url)
    host = parsed.hostname or ""
    score, reasons = 0, []

    # 1. No HTTPS
    if parsed.scheme != "https":
        score += 2
        reasons.append("No HTTPS — data is sent in plain text.")

    # 2. IP address instead of a domain name
    try:
        ipaddress.ip_address(host)
        score += 3
        reasons.append("Host is a raw IP address, not a domain.")
    except ValueError:
        pass

    # 3. Too many subdomains
    if len(host.split(".")) > 3:
        score += 2
        reasons.append("Too many subdomains — classic lookalike trick.")

    # 4. Suspicious keywords
    hits = [w for w in SUSPICIOUS_WORDS if w in url.lower()]
    if hits:
        score += 2
        reasons.append(f"Urgent keywords found: {', '.join(hits)}.")

    # 5. Risky or free TLD
    if any(host.endswith(t) for t in RISKY_TLDS):
        score += 2
        reasons.append("Domain uses a high-abuse TLD.")

    # 6. @ in the URL hides the real host
    if "@" in url:
        score += 3
        reasons.append("'@' in URL — the real host is after the @.")

    # 7. Very long URL
    if len(url) > 75:
        score += 1
        reasons.append("Unusually long URL.")

    verdict = "SAFE-LOOKING" if score <= 2 else "SUSPICIOUS" if score <= 5 else "HIGH RISK"
    return verdict, score, reasons

if __name__ == "__main__":
    link = input("Paste a link to check: ").strip()
    verdict, score, reasons = score_url(link)
    print(f"\nVerdict: {verdict} (score {score}/15)")
    for r in reasons:
        print(" -", r)
    if score > 2:
        print("Do NOT open it. Verify through the official app or website instead.")

Try It

Save it as urlcheck.py and run python3 urlcheck.py. Your bank's real site should score low; something like http://secure-login-verify.tk/paypal@evil.com lights up every check.

What the Score Means

  • 0–2: SAFE-LOOKING — no obvious red flags, but stay alert.
  • 3–5: SUSPICIOUS — be cautious; verify through the official app.
  • 6+: HIGH RISK — do not open it. Report it as phishing.

Level Up

  • Add a lookalike-domain check with edit distance (difflib) against popular brands.
  • Query Google Safe Browsing or VirusTotal for a real reputation verdict.
  • Shortened URLs can't be judged until expanded — use requests with allow_redirects=True and score the final destination.

One Warning: this tool checks links you receive — never use it to visit live phishing pages to "test" them. Check the link, then close the tab. The safest click is the one never made.

 

Comments

Popular Posts