The `stix2-patterns` library provides a software tool for checking the syntax of Cyber Threat Intelligence (CTI) STIX Pattern expressions, used within STIX Indicators to describe conditions indicating cyber threat activity. It is currently at version 2.1.2 and actively maintained with regular updates addressing bug fixes and improvements.
pip install stix2-patternsVerified import paths — ran on the pinned version, not inferred.
The primary way to use the library programmatically is via the `run_validator` function. It takes a STIX pattern string and returns a list of errors encountered during parsing. An empty list indicates a valid pattern.
Upgrade your Python environment to 3.10 or later.
Review and re-test complex STIX patterns with the updated validator, especially those pushing the boundaries of the STIX Patterning Specification.
Be aware of the STIX specification version you are targeting. The `cti-pattern-validator` repo's `stix2patterns.test` directories contain `spec_examples.txt` for both `v20` and `v21`, which can serve as a reference.
If you need to evaluate patterns against STIX data, use `pip install cti-pattern-matcher` in conjunction with `stix2-patterns`.
Review the STIX Patterning Language specification (e.g., STIX 2.1 Part 4: Patterns) to correct the syntax of your STIX pattern.
Use `stix2patterns` for imports, for example: `from stix2patterns.v21.pattern import Pattern` or `from stix2patterns.validator import validate`.
Import the `validate` function specifically from `stix2patterns.validator`: `from stix2patterns.validator import validate`.
Ensure the input pattern is a string: `pattern_str = "[file:hashes.'MD5' = 'd41d8cd98f00b204e9800998ecf8427e']"; p = Pattern(pattern_str)`.