#!/usr/bin/env python3 """Builds a canary project with FAKE secrets (assembled at runtime, so this file itself holds no key-shaped strings) and checks that scan.py finds every planted item and none of the decoys. Usage: python3 -I make_canary.py """ import json import os import subprocess import sys J = "".join T = sys.argv[1] if len(sys.argv) > 1 else sys.exit(__doc__) os.makedirs(os.path.join(T, "src"), exist_ok=True) os.makedirs(os.path.join(T, "config"), exist_ok=True) # planted: (rule expected, file, content line) planted = [ ("openai-key", "src/app.py", 'OPENAI = "' + J(["sk-", "proj-", "Q9fLx2Wm7RtYc4Vb8NnKpZa1Ds6Ge3Hj0Uo5Ii"]) + '"'), ("anthropic-key", "src/app.py", 'KEY2 = "' + J(["sk-", "ant-", "api03-Zk8Lm2Nq7Rv4Tx9Wc1Yb6Hf3Jd5Gs0Ae"]) + '"'), ("aws-access-key", "src/app.py", 'aws = "' + J(["AKIA", "Z7Q2M4X9W1R6T3Y5"]) + '"'), ("telegram-bot-token", "src/app.py", 'BOT = "' + J(["7349182650", ":AA", "Gq8Lz2Wm4Xc7Vb1Nn9Kp3Rt6Yh0Ud5Ej"]) + '"'), ("password-assignment", "src/app.py", 'password = "' + J(["Fr1tz!", "Haus2024"]) + '"'), ("url-credentials", "src/app.py", 'DB = "postgres://admin:' + J(["S3hr", "Geheim99"]) + '@db.intern:5432/app"'), ("github-token", "config/settings.json", '"Token": "' + J(["gh", "p_", "Ab3Cd5Ef7Gh9Ij1Kl3Mn5Op7Qr9St1Uv3Wx5"]) + '",'), ("email", "config/settings.json", '"Contact": "max.mustermann' + "@" + 'gmx.de",'), ("phone", "config/settings.json", '"Phone": "+49 851 1234567",'), ("iban", "config/settings.json", '"Bank": "DE89 3704 0044 0532 0130 00",'), ("private-ip", "config/settings.json", '"Server": "192.168.178.23",'), ("google-api-key", ".env", "GOOGLE=" + J(["AIza", "SyD8kL2mN4pQ6rS8tU0vW2xY4zA6bC8dE0f"])), ("private-key", "config/deploy_key", J(["-----BEGIN ", "OPENSSH PRIVATE KEY-----"])), ("home-path", "src/notes.md", "log: /Users/maxi/projects/app/log.txt"), ("rapidapi-key", "src/Api.java", 'String k = "' + J(["5f2a9c81d4", "msh", "7b3e6a90c1d2", "p1a4f3", "jsn", "8e2d6c0b9a71"]) + '";'), ("key-in-header-or-call", "src/Api.java", 'req.header("x-' + 'rapidapi-key", "' + J(["Hk29Lm4Np6", "Qr8St0Uv2Wx"]) + '");'), ("property-secret", "config/application.properties", "spring.mail.password=" + J(["Mx7!qe", "Lp29zT"])), ] decoys = [ ("src/app.py", 'api_key = os.environ["API_KEY"]'), ("src/app.py", 'password = "changeme"'), ("config/settings.json", '"Sample": "user@example.com",'), ("config/settings.json", '"Docs": "https://example.com",'), ("src/notes.md", "Version 1.2.3.4 released"), ("src/notes.md", "Set your password in the settings page."), ("config/application.properties", "spring.mail.password=${MAIL_PASSWORD}"), ("src/Api.java", 'req.header("Content-Type", "application/json");'), ] files = {} for _, f, line in planted: files.setdefault(f, []).append(line) for f, line in decoys: files.setdefault(f, []).append(line) for f, lines in files.items(): with open(os.path.join(T, f), "w") as fh: fh.write("\n".join(lines) + "\n") scan = os.path.join(os.path.dirname(os.path.abspath(__file__)), "scan.py") out = os.path.join(T, "..", os.path.basename(T.rstrip("/")) + "-result.json") subprocess.run([sys.executable, "-I", scan, T, "--json", out], capture_output=True) res = json.load(open(out))["findings"] hit = {(f["rule"], f["file"]) for f in res} missed = [(r, f) for r, f, _ in planted if (r, f) not in hit] planted_keys = {(r, f) for r, f, _ in planted} decoy_lines = {(f, i + 1) for f, lines in files.items() for i, l in enumerate(lines) if (f, l) in set(decoys)} false_pos = [f for f in res if (f["file"], f["line"]) in decoy_lines] print(f"planted {len(planted)} found {len(planted) - len(missed)} missed {missed}") print(f"decoys {len(decoys)} false positives {[(f['rule'], f['file'], f['line']) for f in false_pos]}") extra = [f for f in res if (f["rule"], f["file"]) not in planted_keys and f not in false_pos] print(f"other findings: {[(f['rule'], f['file'], f['line']) for f in extra]}") sys.exit(1 if missed or false_pos else 0)