Build a Python Website Change Monitor That Alerts You When Your Site Is Defaced


A defaced website is embarrassing enough. What makes it worse is that most owners find out from a visitor, days later. Attackers love that gap. Today we will build a Python monitor that watches your own website, hashes its pages, and alerts you the moment anything changes — before your users ever see the damage.

How It Works

The idea is simple: fetch a page, hash its content, and store the hash. Next run, fetch it again and compare. If the hash changed, something on the page changed — an edit you made, or an attacker. Only monitor sites you own or have written permission to test.

The Code

import hashlib
import json
import os
import urllib.request

STATE_FILE = "monitor_state.json"

PAGES = [
    "https://example.com/",
    "https://example.com/about.html",
]

def fetch_hash(url):
    req = urllib.request.Request(url, headers={"User-Agent": "SiteMonitor/1.0"})
    with urllib.request.urlopen(req, timeout=15) as resp:
        data = resp.read()
    return hashlib.sha256(data).hexdigest(), len(data)

def load_state():
    if os.path.exists(STATE_FILE):
        with open(STATE_FILE) as f:
            return json.load(f)
    return {}

def save_state(state):
    with open(STATE_FILE, "w") as f:
        json.dump(state, f, indent=2)

def main():
    state = load_state()
    changed = []
    for url in PAGES:
        try:
            digest, size = fetch_hash(url)
        except Exception as e:
            print(f"[!] Could not fetch {url}: {e}")
            continue
        if url in state and state[url] != digest:
            changed.append(url)
            print(f"[!] CHANGED: {url} (was {state[url][:12]}, now {digest[:12]})")
        elif url not in state:
            print(f"[+] Baseline saved for {url}")
        state[url] = digest
    save_state(state)
    if changed:
        print(f"\nALERT: {len(changed)} page(s) changed. Check them now!")

if __name__ == "__main__":
    main()

How to Run It

Save it as monitor.py, put your own site's URLs in the PAGES list, and run it once to save the baseline hashes. Run it again after editing a page and it will report the change — that is expected. The value is in the runs where you changed nothing but the hash moved anyway.

Make It Smarter

  • Ignore dynamic content. Pages with timestamps, ads or rotating banners change hash every fetch. Strip those sections before hashing, or hash only a stable part of the page, like the main content div.
  • Run it on a schedule. A cron job every 15 minutes turns this script into a real monitor: */15 * * * * python3 /home/anony/monitor.py >> /var/log/site-monitor.log 2>&1.
  • Add real alerts. Print is fine for testing, but for production send an email, a Telegram message, or a webhook to your chat app when changed is non-empty. A monitor nobody reads is decoration.
  • Watch more than the homepage. Attackers often deface a single page or inject hidden spam links into a footer. Monitor every public page, or at least your most visited ones.

What to Do When It Fires

Do not just restore the page and move on. A changed page means someone modified your site. Check your server logs, look for new files, change your credentials, and find how they got in — otherwise they will do it again tomorrow. The alert is the start of incident response, not the end of it.

You just built a defensive security tool that real teams pay for. Keep it running, and your site will never sit defaced for days again.

Comments

Popular Posts