Creating STIX Content

Creating STIX Domain Objects

To create a STIX object, provide keyword arguments to the type’s constructor:

[15]:
from stix2 import Indicator

indicator = Indicator(name="File hash for malware variant",
                      pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",
                      pattern_type="stix")
print(indicator.serialize(pretty=True))
[15]:
{
    "type": "indicator",
    "spec_version": "2.1",
    "id": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",
    "created": "2020-06-24T15:04:40.048932Z",
    "modified": "2020-06-24T15:04:40.048932Z",
    "name": "File hash for malware variant",
    "pattern": "[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",
    "pattern_type": "stix",
    "pattern_version": "2.1",
    "valid_from": "2020-06-24T15:04:40.048932Z"
}

Certain required attributes of all objects will be set automatically if not provided as keyword arguments:

  • If not provided, type will be set automatically to the correct type. You can also provide the type explicitly, but this is not necessary:
[16]:
indicator2 = Indicator(type='indicator',
                       pattern_type="stix",
                       pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']")

Passing a value for type that does not match the class being constructed will cause an error:

[17]:
indicator3 = Indicator(type='xxx',
                       pattern_type="stix",
                       pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']")
InvalidValueError: Invalid value for Indicator 'type': must equal 'indicator'.

  • If not provided, id will be generated randomly. If you provide an id argument, it must begin with the correct prefix:
[18]:
indicator4 = Indicator(id="campaign--63ce9068-b5ab-47fa-a2cf-a602ea01f21a",
                       pattern_type="stix",
                       pattern="[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']")
InvalidValueError: Invalid value for Indicator 'id': must start with 'indicator--'.

For indicators, pattern and pattern_type are required and cannot be set automatically. Trying to create an indicator that is missing one of these properties will result in an error:

[8]:
indicator = Indicator()
MissingPropertiesError: No values for required properties for Indicator: (pattern, pattern_type).

However, the required valid_from attribute on Indicators will be set to the current time if not provided as a keyword argument.

Once created, the object acts like a frozen dictionary. Properties can be accessed using the standard Python dictionary syntax:

[9]:
indicator['name']
[9]:
'File hash for malware variant'

Or access properties using the standard Python attribute syntax:

[10]:
indicator.name
[10]:
'File hash for malware variant'

Warning

Note that there are several attributes on these objects used for method names. Accessing those will return a bound method, not the attribute value.

Attempting to modify any attributes will raise an error:

[11]:
indicator['name'] = "This is a revised name"
TypeError: 'Indicator' object does not support item assignment

[12]:
indicator.name = "This is a revised name"
ImmutableError: Cannot modify 'name' property in 'Indicator' after creation.

To update the properties of an object, see the Versioning section.

Creating a Malware object follows the same pattern:

[14]:
from stix2 import Malware

malware = Malware(name="Poison Ivy",
                  is_family=False)
print(malware.serialize(pretty=True))
[14]:
{
    "type": "malware",
    "spec_version": "2.1",
    "id": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6",
    "created": "2020-06-24T14:53:20.156644Z",
    "modified": "2020-06-24T14:53:20.156644Z",
    "name": "Poison Ivy",
    "is_family": false
}

As with indicators, the type, id, created, and modified properties will be set automatically if not provided. For Malware objects, the is_family property must be provided.

You can see the full list of SDO classes here.

Creating Relationships

STIX 2 Relationships are separate objects, not properties of the object on either side of the relationship. They are constructed similarly to other STIX objects. The type, id, created, and modified properties are added automatically if not provided. Callers must provide the relationship_type, source_ref, and target_ref properties.

[19]:
from stix2 import Relationship

relationship = Relationship(relationship_type='indicates',
                            source_ref=indicator.id,
                            target_ref=malware.id)
print(relationship.serialize(pretty=True))
[19]:
{
    "type": "relationship",
    "spec_version": "2.1",
    "id": "relationship--2f6a8785-e27b-487e-b870-b85a2121502d",
    "created": "2020-06-24T15:05:18.250605Z",
    "modified": "2020-06-24T15:05:18.250605Z",
    "relationship_type": "indicates",
    "source_ref": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",
    "target_ref": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6"
}

The source_ref and target_ref properties can be either the ID’s of other STIX objects, or the STIX objects themselves. For readability, Relationship objects can also be constructed with the source_ref, relationship_type, and target_ref as positional (non-keyword) arguments:

[20]:
relationship2 = Relationship(indicator, 'indicates', malware)
print(relationship2.serialize(pretty=True))
[20]:
{
    "type": "relationship",
    "spec_version": "2.1",
    "id": "relationship--d43ec245-5496-44f4-8732-3131380435de",
    "created": "2020-06-24T15:05:47.705352Z",
    "modified": "2020-06-24T15:05:47.705352Z",
    "relationship_type": "indicates",
    "source_ref": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",
    "target_ref": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6"
}

Creating Bundles

STIX Bundles can be created by passing objects as arguments to the Bundle constructor. All required properties (type, id, and spec_version) will be set automatically if not provided, or can be provided as keyword arguments:

[21]:
from stix2 import Bundle

bundle = Bundle(indicator, malware, relationship)
print(bundle.serialize(pretty=True))
[21]:
{
    "type": "bundle",
    "id": "bundle--177c6477-2dee-43d5-b4c9-8b7f3f5ec517",
    "objects": [
        {
            "type": "indicator",
            "spec_version": "2.1",
            "id": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",
            "created": "2020-06-24T15:04:40.048932Z",
            "modified": "2020-06-24T15:04:40.048932Z",
            "name": "File hash for malware variant",
            "pattern": "[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",
            "pattern_type": "stix",
            "pattern_version": "2.1",
            "valid_from": "2020-06-24T15:04:40.048932Z"
        },
        {
            "type": "malware",
            "spec_version": "2.1",
            "id": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6",
            "created": "2020-06-24T14:53:20.156644Z",
            "modified": "2020-06-24T14:53:20.156644Z",
            "name": "Poison Ivy",
            "is_family": false
        },
        {
            "type": "relationship",
            "spec_version": "2.1",
            "id": "relationship--2f6a8785-e27b-487e-b870-b85a2121502d",
            "created": "2020-06-24T15:05:18.250605Z",
            "modified": "2020-06-24T15:05:18.250605Z",
            "relationship_type": "indicates",
            "source_ref": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",
            "target_ref": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6"
        }
    ]
}

Creating Cyber Observable References

Cyber Observable Objects have properties that can reference other Cyber Observable Objects. In order to create those references, either supply the ID string of the object being referenced, or pass in the object itself.

For example, the IPv4Address object has a resolves_to_refs property which must hold a list of references to MACAddress objects. We could specify the id string:

[22]:
from stix2 import IPv4Address

ip4 = IPv4Address(
    value="177.60.40.7",
    resolves_to_refs=["mac-addr--43f380fd-37c6-476d-8643-60849bf9240e"]
)

print(ip4.serialize(pretty=True))
[22]:
{
    "type": "ipv4-addr",
    "id": "ipv4-addr--dc63603e-e634-5357-b239-d4b562bc5445",
    "value": "177.60.40.7",
    "resolves_to_refs": [
        "mac-addr--43f380fd-37c6-476d-8643-60849bf9240e"
    ],
    "spec_version": "2.1"
}

Or we could create the MACAddress object(s) beforehand and then pass them in:

[23]:
from stix2 import MACAddress

mac_addr_a = MACAddress(value="a1:b2:c3:d4:e5:f6")
mac_addr_b = MACAddress(value="a7:b8:c9:d0:e1:f2")

ip4_valid_refs = IPv4Address(
    value="177.60.40.7",
    resolves_to_refs=[mac_addr_a.id, mac_addr_b.id]
)

print(ip4_valid_refs.serialize(pretty=True))
[23]:
{
    "type": "ipv4-addr",
    "id": "ipv4-addr--dc63603e-e634-5357-b239-d4b562bc5445",
    "value": "177.60.40.7",
    "resolves_to_refs": [
        "mac-addr--f72d7d00-86bd-5cd2-8c86-52f7a83bef62",
        "mac-addr--875ad625-177b-5c2a-9101-d44b0ad55938"
    ],
    "spec_version": "2.1"
}