#!/usr/bin/env python3 """mask-windows.py -- show where live-credential body windows sit in a file, without printing them. Reads the credential from /.sah (never from argv, never echoed), finds every maximal run of body characters inside the named file, and prints, for each line that carries one: the line number, the offset into the body, the run length, and the surrounding text with the run replaced by dots. The point is that a fragment must never be printed in order to be located -- the incident this family of tools exists for was a fragment printed while looking for it. python3 mask-windows.py --state [--floor 3] [--context 60] """ import argparse import os import sys def main(): ap = argparse.ArgumentParser() ap.add_argument("path") ap.add_argument("--state", required=True) ap.add_argument("--floor", type=int, default=3) ap.add_argument("--context", type=int, default=60) a = ap.parse_args() tok = open(os.path.join(a.state, ".sah"), encoding="utf-8").read().strip() body = tok.split("_", 1)[1] if "_" in tok else tok text = open(a.path, encoding="utf-8", errors="replace").read() # every contiguous run of body characters at least `floor` long, and where it starts in the body runs = [] for L in range(len(body), a.floor - 1, -1): for off in range(0, len(body) - L + 1): w = body[off:off + L] n = text.count(w) if n: runs.append((off, L, n, w)) # keep only maximal runs (a run that is contained in a longer one is not a separate finding) keep = [] for off, L, n, w in runs: if not any(o <= off and off + L <= o + K for o, K, _, _ in runs if K > L): keep.append((off, L, n, w)) print("file %s, %d bytes; %d maximal body run(s) at floor %d" % (a.path, len(text), len(keep), a.floor)) for off, L, n, w in sorted(keep, key=lambda r: (-r[1], r[0])): print(" body offset %d, length %d, %d occurrence(s)" % (off, L, n)) for i, line in enumerate(text.splitlines(), 1): if w in line: j = 0 while True: k = line.find(w, j) if k < 0: break lo = max(0, k - a.context) hi = min(len(line), k + L + a.context) ctx = (line[lo:k] + "." * L + line[k + L:hi]).replace("\r", "") print(" line %d: ...%s..." % (i, ctx)) j = k + L return 0 if __name__ == "__main__": sys.exit(main())