r/Python • • 13m ago

Discussion I wrote a multi-threaded malware scanner and real-time PC guard in Python: here's what I learned abo

Bastion is an open-source Windows security tool I built in Python (PySide6 GUI, packaged with PyInstaller). Some parts that might interest people here:

- Scanning ~1.9M files: parallel folder walkers feed a queue to 12 checker threads at below-normal priority. Hashing and YARA release the GIL, so threads actually help.

- The slow part was regex. Case-insensitive patterns on every script were ~4x slower than a plain substring check first, then the regex only if the word is there.

- Signature checks first: Authenticode/catalog checks via ctypes skip deep analysis for signed files, which is most of a PC.

- Archives are opened in memory with zip-bomb limits; nothing is ever written to disk.

- Script de-obfuscation: peeling Base64 (incl. UTF-16 and zlib), [char] codes, string concatenation and reversed strings before judging.

- Fun bug: it flagged its own source file, because un-reversing its own pattern list "revealed" hidden PowerShell. Fixed by comparing against the original text.

github / DRAGON12367

Source (MIT):

Happy to answer questions or take code review.

1 Upvotes

0 comments sorted by