Build a Python Ransomware Canary: Tripwire Files That Alert You the Moment Encryption Starts
Ransomware does not encrypt your whole drive instantly. It walks through your folders file by file — and that gives you a window to catch it. A canary file is a fake document placed where ransomware will find it early. It should never change. The moment it does, something is encrypting your files, and your monitor should shout.
This project runs on your own machine only, and it is purely defensive: it watches your files and alerts you. It harms nothing and touches no other system.
How it works
- Drop a few canary files (DO_NOT_DELETE_canary.txt) in your Documents, Desktop and shared folders.
- Record their hashes.
- Check the hashes every few seconds. If a hash changes — or a file disappears — raise the alarm immediately.
The code
import hashlibimport osimport timefrom datetime import datetimeCANARIES = [ os.path.expanduser("~/Documents/DO_NOT_DELETE_canary.txt"), os.path.expanduser("~/Desktop/DO_NOT_DELETE_canary.txt"),]def file_hash(path): with open(path, "rb") as f: return hashlib.sha256(f.read()).hexdigest()def setup_canaries(): for path in CANARIES: if not os.path.exists(path): with open(path, "w") as f: f.write("CANARY FILE - if this changes, something is encrypting your files.\n") return {p: file_hash(p) for p in CANARIES}def alert(message): stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(f"[!] {stamp} ALERT: {message}") with open("canary_alerts.log", "a") as log: log.write(f"{stamp} - {message}\n")def main(): baseline = setup_canaries() print(f"Watching {len(baseline)} canary files. Press Ctrl+C to stop.") while True: for path, old_hash in baseline.items(): if not os.path.exists(path): alert(f"Canary DELETED: {path} - possible ransomware activity!") elif file_hash(path) != old_hash: alert(f"Canary MODIFIED: {path} - possible ransomware activity!") time.sleep(5)if __name__ == "__main__": main()
Make it actually useful
- Put canaries where encryption starts. Ransomware usually hits Documents, Desktop, Pictures and network shares first. Cover all of them.
- Do not rely on the alert alone. The moment a canary trips, the correct response is to disconnect the machine from the network and power it down — then investigate from a clean machine. An alert you ignore is worse than no alert.
- Add a real notification. Swap the print for an email, a Telegram message or a webhook to your phone. Five seconds of delay is fine; five hours is not.
- Keep a copy of the script off the machine. If ransomware encrypts your scripts folder too, you lose the tool that warned you.
- Test it. Edit a canary file yourself and confirm the alert fires. A defense you never tested is a hope, not a defense.
Canary files will not stop ransomware — nothing simple will. But they buy you the one thing that matters most in an attack: minutes of warning while your files are still mostly intact. Build it today, in your own lab first, and it costs you nothing to keep running.



Comments
Post a Comment