All projects

Zeus Scanner

A command-line tool that finds malware hidden in WordPress and PHP sites.

Built at Pressidium

What it is

  • A scanner that server operators point at a site’s files to find malicious PHP code.
  • Every file goes through several detectors: YARA rules, indicators of compromise matched on file names, hashes and content, a trained machine-learning model and an obfuscation check. Each finding carries a score and a severity, and a file is reported at its most severe finding.
  • The pipeline is Python; the heavy work runs in Rust extensions loaded straight into the Python process.
  • I built the first version around one main script, then rebuilt its core into a modular scanner made of small services.

The problem

Malicious code in a PHP site can be a known sample, a pattern a rule can match, or something obfuscated that only looks wrong in aggregate: unusual entropy, encoded strings, odd code structure. No single check sees all of that, so the scanner runs independent detectors over every file and adds up what they report.

That makes each file expensive. The model needs dozens of code features per file, entropy is computed over raw bytes, and a site has many files. I moved those hot paths into Rust and, in the rebuild, split the scan into services that share a cache keyed on file contents.

How I built it

  • Detectors are plugins behind a common interface, with a manager, loader, registry and executor. Each plugin declares a priority, so the IOC and YARA checks run first and the CPU-heavy model runs last.
  • Seven Rust crates, built with PyO3 and maturin, handle file discovery, file info, entropy, feature extraction, IOC matching, obfuscation analysis and log formatting.
  • The rebuild replaced the original main script with an orchestrator that delegates to services for discovery, file processing, execution, plugin coordination and caching.
  • Feature and entropy results go into bounded caches, a TTL cache and an LRU cache of 2,000 entries each, shared by the model and obfuscation plugins. The cache key is the path plus a SHA-256 prefix of the contents, so a file that changes gets fresh features.
  • File info comes from Rust: one buffered pass computes the MD5, SHA-1 and SHA-256 hashes the IOC and YARA rules match on, and files over 10 MB are held in memory only up to their first megabyte.
  • It can scan only the files touched in recent git history, the last few days or the last few commits, which keeps routine checks short. The git query also runs in Rust.
  • Settings come from a TOML file validated with pydantic, with CLI flags on top.

The interesting parts

Rust where the time goes

The Rust extensions load into the Python process as native modules, so a call into Rust is a function call, not a subprocess. Directory discovery walks the tree with walkdir and stats files in parallel with Rayon. Entropy counts byte frequencies with atomic counters, using a Rayon parallel iterator over each 32 KB buffer. Feature extraction exposes every metric as its own function, plus one that returns all of them for a file in a single call, so the model crosses the Python and Rust boundary once per file instead of once per feature. Its regular expressions are compiled once, on first use, and reused for every file.

Features for the model

The classifier is a pretrained model loaded with joblib and chosen from the command line; random forest, LightGBM and stacked ensembles were trained for it. It is fed code features that Rust extracts from each file: entropy at several levels, compression ratio, nesting depth, naming style, comment density, base64 and hex-encoded strings, and uses of request globals, file functions and error suppression. The Python training tools compute a wider set and include a variance threshold and a Pearson and Spearman correlation ranking for pruning redundant features; the scanner computes only the ones the model uses at inference.

Paths the scanner will not follow

A scanner that reads whatever it is pointed at has to be careful about where paths lead. Entries in a file list and file names reported by git are canonicalized in Rust and must resolve inside the base directory, which catches ../ tricks and symlinks; anything outside is dropped and logged as a security warning. The directory walk does not follow symlinks. Before a file is read, the processor checks that it is a regular file the scanner can read, and refuses symlinks that resolve into system locations such as /etc, /proc or /sys.

How it runs

  • It ships as a Docker image. A small wrapper builds the image on first use, rejects arguments containing shell metacharacters, mounts the target into the container, runs the scan with CSV output and copies the log back with container paths rewritten.
  • Results go to the terminal, to CSV, to JSON or to a log file, with severity thresholds set in the configuration.
  • A build script compiles each Rust crate with maturin for x86-64-v2, and CI commits the built extensions back with a marker that stops the commit from starting another pipeline, so the scanner runs without a Rust toolchain.