Build a Python Phishing URL Detector: Spot Fake Login Links With Simple Heuristics (Full Code)


Phishing doesn’t usually look like a Hollywood hack. It looks like a normal email with a normal-looking link — except the link goes somewhere it shouldn’t. The good news: most phishing URLs leave fingerprints. Too many subdomains, a brand name in the wrong part of the address, an IP address where a domain should be, a newly registered domain pretending to be a bank. This post builds a small Python tool that scores a URL against those fingerprints and flags the suspicious ones. It’s a learning project and a genuinely useful first line of defence for your own inbox — not a replacement for a real security product, but a great way to understand how detection actually works.

How the Detector Works

The tool checks five simple signals, each worth points. A high total score means “treat this link with suspicion”:

  1. IP address instead of a domain — real companies don’t send you to http://185.220.101.5/login. That’s +3 points.
  2. Too many subdomains — secure.paypal.com.login-verify.net hides the real domain at the end. More than 3 dots in the hostname is +2.
  3. Suspicious keywords — words like verify, secure-login, update-account or confirm in the URL are common phishing bait. +1 each.
  4. A brand name in the wrong place — if paypal appears in the URL but the actual domain isn’t paypal.com, that’s +3.
  5. URL shortener — bit.ly, tinyurl.com and friends hide the destination entirely. +2.

The Code

from urllib.parse import urlparse
import ipaddress
import re
SUSPICIOUS_WORDS = ["verify", "secure-login", "update-account",
"confirm", "login-now", "account-suspended"]
SHORTENERS = ["bit.ly", "tinyurl.com", "t.co", "goo.gl",
"is.gd", "cutt.ly", "shorturl.at"]
BRANDS = ["paypal", "google", "amazon", "microsoft", "apple",
"netflix", "facebook", "instagram", "binance"]
def score_url(url):
score = 0
reasons = []
parsed = urlparse(url)
host = parsed.netloc.lower()
# 1. IP address instead of a domain
try:
ipaddress.ip_address(host.split(":")[0])
score += 3
reasons.append("Host is a raw IP address, not a domain")
except ValueError:
pass
# 2. Too many subdomains
if host.count(".") > 3:
score += 2
reasons.append("Unusually many subdomains")
# 3. Suspicious keywords
for word in SUSPICIOUS_WORDS:
if word in url.lower():
score += 1
reasons.append(f"Suspicious keyword found: {word}")
# 4. Brand name in the wrong place
domain = host.split(":")[0]
for brand in BRANDS:
if brand in url.lower() and not domain.endswith(brand + ".com"):
score += 3
reasons.append(f"Brand '{brand}' used but domain is {domain}")
break
# 5. URL shortener
if any(s in host for s in SHORTENERS):
score += 2
reasons.append("URL shortener hides the real destination")
return score, reasons
def verdict(score):
if score >= 5:
return "HIGH RISK — do not click, verify through the official app"
if score >= 3:
return "SUSPICIOUS — check carefully before clicking"
return "Looks okay — stay alert anyway"
if __name__ == "__main__":
test_urls = [
"https://www.paypal.com/signin",
"http://secure.paypal.com.login-verify.net/verify-account",
"https://bit.ly/3xK9mQ2",
]
for url in test_urls:
score, reasons = score_url(url)
print(f"\n{url}\nScore: {score} — {verdict(score)}")
for r in reasons:
print(f" - {r}")

Try It Yourself

Save the file as phishing_detector.py and run it with python3 phishing_detector.py. The fake PayPal link scores high for exactly the reasons a human would flag it: the real domain is login-verify.net, not PayPal, and it uses the bait word verify. The real PayPal link scores zero. The shortened link scores 2 — not proof of anything bad, but a reminder that you can’t judge a link you can’t see.

How to Make It Better

  • Check domain age. A domain registered yesterday that claims to be your bank is a red flag. You can query WHOIS data with Python’s whois library.
  • Follow redirects. A short link that lands on a lookalike domain is worse than a short link that lands on the real site. Use requests with allow_redirects=True and score the final URL.
  • Check against blocklists. Google Safe Browsing and PhishTank publish lists of known phishing URLs — comparing against them turns a heuristic toy into a real detector.

The Takeaway

Phishing works because people click before they think. A tool like this doesn’t replace thinking — it slows you down just enough to think. The real skill is the habit: before you click a link in an email or message, read the actual domain. The brand name that matters is the one at the end, right before the .com — everything before it is decoration.

Only test systems you own or have written permission to test.

Comments

Popular Posts