How rasta-mouse/threatcheck works
What this project is (type + purpose)
ThreatCheck is a Windows/.NET Framework console application (Visual Studio .sln / .csproj) used to identify the exact byte ranges in an input file/script that trigger detection by a chosen “engine”:
• Microsoft Defender (on-disk/binary scanning)
• AMSI (script/content scanning via the Windows AMSI interface)
Architecturally it’s a single-process CLI tool with a small internal “plugin-like” scanner abstraction (Scanner) and two concrete implementations (DefenderScanner, AmsiScanner). It also optionally downloads input from a URL.
• C4 Context (tool + external dependencies),
• C4 Container (single executable + OS services),
Read the file tree as architecture signals
Top-level:
• ThreatCheck.sln / ThreatCheck/ThreatCheck.csproj → single Visual Studio solution + one main project.
• Program.cs → CLI entry point; argument parsing; orchestration.
• Scanners/ folder:
• Scanner.cs → base class / interface for scanning engines (core extensibility seam)
• AmsiScanner.cs → AMSI integration
• DefenderScanner.cs → Defender integration
• NativeMethods.cs → P/Invoke boundary (calls into Windows APIs; likely AMSI functions and other native calls)
• Helpers.cs → shared utilities (byte slicing, formatting, maybe splitting algorithm helpers, IO)
• App.config → runtime config (often .NET Framework settings)
• Properties/AssemblyInfo.cs → assembly metadata (not important for runtime architecture)
• Orchestrator (Program)
• Input acquisition (file vs URL)
• Analysis loop (splitting / pinpointing bad bytes)
• Scanning abstraction + implementations
• Native boundary (AMSI + Windows/Defender)
• Presentation (console output)
From README behavior:
• Accepts input from disk or URL
• Scans using Defender or AMSI
• Splits input repeatedly until it finds the smallest/precise flagged range
• Outputs offsets + hex dump to console
• User / Operator (invokes tool)
• Filesystem (reads local target file)
• Network / Remote HTTP server (if --url is used)
• Windows Defender scanning capability (engine external to the process)
• Windows AMSI subsystem (external to the process; invoked through native APIs)
• Optionally: Windows OS boundary (because AMSI + Defender are OS-integrated services)
CLI/Orchestrator
• Program.cs
• Responsibilities:
• Parse args (--engine, --file, --url, --type)
• Choose scanner implementation
• Kick off analysis loop
• Handle success/failure exit codes
Input Acquisition
• (Likely in Helpers.cs and/or Program.cs)
• Two subcomponents:
• FileReader: reads bytes from disk
• UrlDownloader: downloads bytes over HTTP
Analysis Engine (Byte Range Minimizer)
• (Often implemented across Program.cs + Helpers.cs)
• Responsibilities:
• Split content (binary/script) into segments
• Iteratively test segments against the chosen scanner
• Converge on “bad bytes end offset” (and potentially start offset)
• Produce result: offsets + offending byte sequence
Scanning Interface
• Scanners/Scanner.cs
• Responsibilities:
• Defines contract like Scan(byte[] buffer, FileType type) returning “clean/flagged” + message
Concrete Scanners
• Scanners/DefenderScanner.cs
• Calls into Defender scanning mechanism (external)
• Scanners/AmsiScanner.cs
• Calls into AMSI via native methods (external)
Native Interop Layer
• NativeMethods.cs
• Responsibilities:
• P/Invoke declarations
• Marshaling buffers/strings
• Strict boundary: “managed ↔ native Windows APIs”
Utilities
• Helpers.cs
• Responsibilities:
• Byte slicing, encoding conversions (especially for Script mode, likely UTF-16LE as shown)
• Hex dump helper
• Common constants
• Program → Input Acquisition: “get target bytes”
• Program → Analysis Engine: “analyze(bytes, type, scanner)”
• Analysis Engine → Scanner (interface): “scan(candidateBuffer)”
• Scanner → (DefenderScanner or AmsiScanner): “engine-specific scan”
• AmsiScanner → NativeMethods → AMSI (external): “AmsiScanBuffer”
• DefenderScanner → Defender (external): “submit/scan content”
• Analysis Engine → CustomConsole: “print offsets + hexdump”
Architectural patterns/design principles to highlight (specific to ThreatCheck)
• Strategy pattern for scanning engines:
• Scanner = strategy interface
• DefenderScanner / AmsiScanner = concrete strategies
• Explicit boundary around native/system calls:
• NativeMethods.cs isolates platform coupling
• Pipeline-like processing:
• Acquire input → Normalize/encode (esp. Script) → Split/minimize → Scan → Report
• Single responsibility separation (even in a small tool):
• Orchestration vs scanning vs interop vs output formatting
Of the following (explicitly) in the drawing:
Labels
• Component name
• File/class (e.g., “AmsiScanner (Scanners/AmsiScanner.cs)”)
• Responsibility (short phrase)
• Data type: “byte[]”, “string/script text”, “scan verdict”
• Mode: “Bin vs Script”
• Protocol where relevant: “HTTP GET”, “Win32 API (P/Invoke)”
Container: single-process ThreatCheck.exe (.NET) + OS services + IO boundaries
3. Component (most important): map every file above into components and show the scanning/minimization loop
• Candidate slice → scan → if flagged then shrink slice → repeat → result
That loop is the essence of ThreatCheck’s behavior and is what differentiates it from a trivial “scan a file” tool—so it must be visually central.
• Defender and AMSI are shown as external systems.
• Scanner abstraction is shown, with two implementations.
• Both input modes (file + URL) appear.
• Script vs Bin type is represented (either as a decision node or an annotation on the analysis pipeline).
• Output includes offset + hexdump (Presentation component).