Build a Python Link Checker That Unmasks Shortened URLs Before You Click


A shortened link is a blindfold. You cannot see where it goes until you click it, and by then it may be too late. Attackers know this, which is why phishing messages are full of short links.

The fix is simple: follow the redirect chain without opening the page in a browser. A short Python script can do that in seconds, and it doubles as a great first security tool to build yourself.

How it works

When you request a short URL, the server answers with a redirect — a “go here instead” instruction. Your script follows each redirect and reports the final destination, without ever rendering the page or running any scripts on it. You see the truth before your browser touches it.

The code

Save this as linkcheck.py:

import sys
import requests
def unmask(url):
    try:
        response = requests.head(url, allow_redirects=True, timeout=10)
    except requests.exceptions.RequestException as error:
        print(f"Could not reach the link: {error}")
        return
    print(f"Original link: {url}")
    print(f"Final destination: {response.url}")
    if response.history:
        print("\nRedirect chain:")
        for step in response.history:
            print(f"  {step.status_code} -> {step.url}")
    else:
        print("\nNo redirects — the link goes straight there.")
    suspicious = ["@", "xn--", ".tk", ".zip", ".mov"]
    hits = [flag for flag in suspicious if flag in response.url]
    if hits:
        print(f"\nWarning: final URL contains {hits} — treat with extra caution.")
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python linkcheck.py <url>")
    else:
        unmask(sys.argv[1])

Run it like this:

python linkcheck.py https://bit.ly/example

What to look for in the result

  • A different domain than expected. A link claiming to be your bank that lands on a random domain is a phishing attempt, full stop.
  • Long redirect chains. One or two hops is normal. Five or six is a sign someone is trying to hide the destination.
  • Lookalike domains. paypa1.com or arnazon.com are not the real sites, however convincing they look at a glance.
  • Odd top-level domains. Phishing pages love cheap or unusual endings like .zip and .mov.

Make it a habit

Paste any link from an unexpected email or message into this script before clicking. It takes five seconds and it breaks the attacker’s main trick: hiding the destination.

Two limits to keep in mind. First, this script checks where a link goes, not whether the destination itself is safe — a direct link to a malicious site will still resolve normally, so combine this with common sense and your browser’s warnings. Second, only use this tool on links you received or on your own systems. Following redirects is harmless, but do not use it to hammer someone else’s server with requests.

Small tools like this are the heart of practical security: they do one job, they do it well, and they change your behaviour for the better.

Comments

Popular Posts