Skip to main content

ThreatCheck-Py

Started as a python port of Rasta-Mouse's ThreatCheck.

A tool to find AV signatures in files.

Requirements

  • Python 3.8 or higher
  • Windows OS (for Defender and AMSI scanners)

Supported Scanners

File Scanners

  • Defender
  • Amsi
  • Clamav

Process Scanners:

  • Yara

Installation

From PyPi

pip install threatcheck-py

From Source

git clone https://github.com/ruvolof/threatcheck-py.git
cd threatcheck-py
pip install -e .

Usage

Command Line Options

$ threatcheck --help
usage: threatcheck [-h] [-e {defender,amsi,clamav,yara}] (-f FILE | -u URL | -d DIRECTORY | -p PID) [-r RULES] [--debug] [--version]

Identify AV signatures in files

options:
  -h, --help            show this help message and exit
  -e, --engine {defender,amsi,clamav,yara}
                        Scanning engine (default: defender)
  -f, --file FILE       Analyze a file on disk
  -u, --url URL         Analyze a file from a URL
  -d, --directory DIRECTORY
                        Analyze all files in a directory
  -p, --pid PID         Analyze a process by PID
  -r, --rules RULES     Path to YARA rules directory. Will recursively search for all .yar and .yara files.
  --debug               Enable debug output
  --version             show program's version number and exit

As a Python library

All scanners share a common interface: instantiate one, then call analyze(file_bytes=...) (or analyze(pid=...) for YARA) and inspect the returned ScanResult.

from pathlib import Path
from threatcheck import DefenderScanner, ScanStatus

scanner = DefenderScanner()
result = scanner.analyze(file_bytes=Path('sample.exe').read_bytes())

if result.malicious:
    print(f'Threat: {result.signature}')
    print(f'Bad bytes end at offset 0x{result.end_offset:X}')
elif result.status == ScanStatus.ERROR:
    print(f'Scan failed: {result.error_message}')
else:
    print('Clean')

AmsiScanner and ClamAVScanner use the same pattern. YaraScanner takes a rules_path and can scan a running process as well as file bytes:

from threatcheck import YaraScanner

scanner = YaraScanner(rules_path='/path/to/rules')
result = scanner.analyze(pid=1234)

for match in result.matches or []:
    print(f'Rule: {match.rule}')
    for s in match.strings:
        print(f'  {s.identifier} @ 0x{s.offset:X}: {s.matched_data!r}')

ScanResult exposes:

  • status — a ScanStatus (NO_THREAT_FOUND, THREAT_FOUND, FILE_NOT_FOUND, TIMEOUT, ERROR)
  • maliciousTrue when status == THREAT_FOUND
  • successTrue when the scan completed (threat found or not)
  • signature — signature name, when the engine reports one (Defender, ClamAV)
  • matches — list of YaraMatch when using YaraScanner
  • offending_bytes / end_offset — populated by split scanners (Defender, AMSI, ClamAV) that bisect the file to locate bad bytes
  • error_message — set when status == ERROR

Defender Test Environment Setup

It is recommended to force autosubmission of samples to never send them, or the amount of notification can be overwhelming.

Method 1: PowerShell

Open and administrative PowerShell session and type the following:

Set-MpPreference -SubmitSamplesConsent 2

Method 2: Group Policies

This can be forced through group policies:

  1. Open Local Group Policy Editor (gpedit.msc)
  2. Navigate to Computer Configuration > Administrative Templates > Windows Components > Microsoft Defender Antivirus > MAPS
  3. Double click on "Send file samples when further analysis is required"
  4. Set it to "Enabled" and in the bottom-left panel select "Never send"

Exceptions

All other Defender settings can be left on as long:

  1. There's a path exception in place for the original location of your samples (otherwise real-time protection will prevent the initial loading of the files).
  2. (Maybe?) The original location has to be on your C drive. In my experience Defender ignores exception for external drives and I run in problem 1 above.

Credits

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threatcheck_py-0.4.0.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threatcheck_py-0.4.0-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file threatcheck_py-0.4.0.tar.gz.

File metadata

  • Download URL: threatcheck_py-0.4.0.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for threatcheck_py-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8db71d69410e80c34326154427e61c7544854a726bb119fc13ed37fdf3858b41
MD5 27858f6f60772e48977665013db87a0c
BLAKE2b-256 81446472cdba445a00dfea8487642816200d83ef8f92b8cdc540d7caf36b59a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for threatcheck_py-0.4.0.tar.gz:

Publisher: python-publish.yml on ruvolof/threatcheck-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file threatcheck_py-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: threatcheck_py-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for threatcheck_py-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1dfb759778c0d1ae5b47b0e8a44a200581b0ca50085190bfa31e595183082d5d
MD5 fd99d175f8bfbc5c9b6a19a990ea2385
BLAKE2b-256 b558df2a6deb7995c6db67a6c778592a0a058124323a604b6175b6b2104ec91b

See more details on using hashes here.

Provenance

The following attestation bundles were made for threatcheck_py-0.4.0-py3-none-any.whl:

Publisher: python-publish.yml on ruvolof/threatcheck-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page