STIX 2 Python Library

3.0.2 · active · verified Sun Apr 12

The stix2 Python library provides APIs for serializing and de-serializing STIX 2 JSON content, enabling users to produce, consume, and process cyber threat intelligence. It supports multiple versions of the STIX 2 Technical Specification, currently STIX 2.1. The library, currently at version 3.0.2, is actively maintained by the OASIS Cyber Threat Intelligence (CTI) Technical Committee, with releases typically driven by STIX specification updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a STIX Indicator object, serialize it to pretty-printed JSON, and parse a STIX JSON string back into a Python object. It uses the `Indicator` class for object creation and `serialize` and `parse` functions for data interchange.

from stix2 import Indicator, parse, serialize
from datetime import datetime, timezone

# Create a STIX Indicator object
indicator = Indicator(
    name="File hash for malware variant",
    indicator_types=["malicious-activity"],
    pattern_type="stix",
    pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",
    valid_from=datetime.now(timezone.utc)
)

# Serialize the STIX object to JSON
serialized_indicator = serialize(indicator, pretty=True)
print("\nSerialized Indicator:")
print(serialized_indicator)

# Parse a STIX JSON string back into a Python object
json_string = '''{
    "type": "indicator",
    "spec_version": "2.1",
    "id": "indicator--dbcbd659-c927-4f9a-994f-0a2632274394",
    "created": "2017-09-26T23:33:39.829Z",
    "modified": "2017-09-26T23:33:39.829Z",
    "name": "Another malware hash",
    "indicator_types": ["malicious-activity"],
    "pattern_type": "stix",
    "pattern_version": "2.1",
    "pattern": "[file:hashes.md5 = 'abcdef1234567890abcdef1234567890']",
    "valid_from": "2017-09-26T23:33:39.829952Z"
}'''
parsed_indicator = parse(json_string)
print("\nParsed Indicator Name:", parsed_indicator.name)

view raw JSON →