{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "# Delete this cell to re-enable tracebacks\n", "import sys\n", "import traceback\n", "ipython = get_ipython()\n", "\n", "def hide_traceback(exc_tuple=None, filename=None, tb_offset=None,\n", " exception_only=False, running_compiled_code=False):\n", " etype, value, tb = sys.exc_info() \n", " value.__cause__ = None # suppress chained exceptions\n", " return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))\n", "\n", "ipython.showtraceback = hide_traceback" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "# JSON output syntax highlighting\n", "from __future__ import print_function\n", "from pygments import highlight\n", "from pygments.lexers import JsonLexer\n", "from pygments.formatters import HtmlFormatter\n", "from IPython.display import HTML\n", "\n", "original_print = print\n", "\n", "def json_print(inpt):\n", " string = str(inpt)\n", " if string[0] == '{':\n", " formatter = HtmlFormatter()\n", " return HTML('{}'.format(\n", " formatter.get_style_defs('.highlight'),\n", " highlight(string, JsonLexer(), formatter)))\n", " else:\n", " original_print(inpt)\n", "\n", "print = json_print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating STIX Content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating STIX Domain Objects\n", "\n", "To create a STIX object, provide keyword arguments to the type's constructor:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "indicator",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",\n",
       "    "created": "2020-06-24T15:04:40.048932Z",\n",
       "    "modified": "2020-06-24T15:04:40.048932Z",\n",
       "    "name": "File hash for malware variant",\n",
       "    "pattern": "[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",\n",
       "    "pattern_type": "stix",\n",
       "    "pattern_version": "2.1",\n",
       "    "valid_from": "2020-06-24T15:04:40.048932Z"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import Indicator\n", "\n", "indicator = Indicator(name=\"File hash for malware variant\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " pattern_type=\"stix\")\n", "print(indicator.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Certain required attributes of all objects will be set automatically if not provided as keyword arguments:\n", "\n", "- If not provided, ``type`` will be set automatically to the correct type. You can also provide the type explicitly, but this is not necessary:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "indicator2 = Indicator(type='indicator',\n", " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Passing a value for ``type`` that does not match the class being constructed will cause an error:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "InvalidValueError", "evalue": "Invalid value for Indicator 'type': must equal 'indicator'.", "output_type": "error", "traceback": [ "\u001b[0;31mInvalidValueError\u001b[0m\u001b[0;31m:\u001b[0m Invalid value for Indicator 'type': must equal 'indicator'.\n" ] } ], "source": [ "indicator3 = Indicator(type='xxx',\n", " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- If not provided, ``id`` will be generated randomly. If you provide an\n", " ``id`` argument, it must begin with the correct prefix:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "ename": "InvalidValueError", "evalue": "Invalid value for Indicator 'id': must start with 'indicator--'.", "output_type": "error", "traceback": [ "\u001b[0;31mInvalidValueError\u001b[0m\u001b[0;31m:\u001b[0m Invalid value for Indicator 'id': must start with 'indicator--'.\n" ] } ], "source": [ "indicator4 = Indicator(id=\"campaign--63ce9068-b5ab-47fa-a2cf-a602ea01f21a\",\n", " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "MissingPropertiesError", "evalue": "No values for required properties for Indicator: (pattern, pattern_type).", "output_type": "error", "traceback": [ "\u001b[0;31mMissingPropertiesError\u001b[0m\u001b[0;31m:\u001b[0m No values for required properties for Indicator: (pattern, pattern_type).\n" ] } ], "source": [ "indicator = Indicator()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, the required ``valid_from`` attribute on Indicators will be set to the current time if not provided as a keyword argument.\n", "\n", "Once created, the object acts like a frozen dictionary. Properties can be accessed using the standard Python dictionary syntax:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'File hash for malware variant'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indicator['name']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or access properties using the standard Python attribute syntax:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'File hash for malware variant'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indicator.name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Warning**\n", "\n", "Note that there are several attributes on these objects used for method names. Accessing those will return a bound method, not the attribute value.\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Attempting to modify any attributes will raise an error:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'Indicator' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31mTypeError\u001b[0m\u001b[0;31m:\u001b[0m 'Indicator' object does not support item assignment\n" ] } ], "source": [ "indicator['name'] = \"This is a revised name\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "ImmutableError", "evalue": "Cannot modify 'name' property in 'Indicator' after creation.", "output_type": "error", "traceback": [ "\u001b[0;31mImmutableError\u001b[0m\u001b[0;31m:\u001b[0m Cannot modify 'name' property in 'Indicator' after creation.\n" ] } ], "source": [ "indicator.name = \"This is a revised name\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To update the properties of an object, see the [Versioning](versioning.ipynb) section." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creating a Malware object follows the same pattern:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "malware",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6",\n",
       "    "created": "2020-06-24T14:53:20.156644Z",\n",
       "    "modified": "2020-06-24T14:53:20.156644Z",\n",
       "    "name": "Poison Ivy",\n",
       "    "is_family": false\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import Malware\n", "\n", "malware = Malware(name=\"Poison Ivy\",\n", " is_family=False)\n", "print(malware.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "You can see the full list of SDO classes [here](../api/v21/stix2.v21.sdo.rst)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Relationships\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "relationship",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "relationship--2f6a8785-e27b-487e-b870-b85a2121502d",\n",
       "    "created": "2020-06-24T15:05:18.250605Z",\n",
       "    "modified": "2020-06-24T15:05:18.250605Z",\n",
       "    "relationship_type": "indicates",\n",
       "    "source_ref": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",\n",
       "    "target_ref": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import Relationship\n", "\n", "relationship = Relationship(relationship_type='indicates',\n", " source_ref=indicator.id,\n", " target_ref=malware.id)\n", "print(relationship.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "relationship",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "relationship--d43ec245-5496-44f4-8732-3131380435de",\n",
       "    "created": "2020-06-24T15:05:47.705352Z",\n",
       "    "modified": "2020-06-24T15:05:47.705352Z",\n",
       "    "relationship_type": "indicates",\n",
       "    "source_ref": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",\n",
       "    "target_ref": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "relationship2 = Relationship(indicator, 'indicates', malware)\n", "print(relationship2.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Bundles\n", "\n", "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:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "bundle",\n",
       "    "id": "bundle--177c6477-2dee-43d5-b4c9-8b7f3f5ec517",\n",
       "    "objects": [\n",
       "        {\n",
       "            "type": "indicator",\n",
       "            "spec_version": "2.1",\n",
       "            "id": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",\n",
       "            "created": "2020-06-24T15:04:40.048932Z",\n",
       "            "modified": "2020-06-24T15:04:40.048932Z",\n",
       "            "name": "File hash for malware variant",\n",
       "            "pattern": "[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']",\n",
       "            "pattern_type": "stix",\n",
       "            "pattern_version": "2.1",\n",
       "            "valid_from": "2020-06-24T15:04:40.048932Z"\n",
       "        },\n",
       "        {\n",
       "            "type": "malware",\n",
       "            "spec_version": "2.1",\n",
       "            "id": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6",\n",
       "            "created": "2020-06-24T14:53:20.156644Z",\n",
       "            "modified": "2020-06-24T14:53:20.156644Z",\n",
       "            "name": "Poison Ivy",\n",
       "            "is_family": false\n",
       "        },\n",
       "        {\n",
       "            "type": "relationship",\n",
       "            "spec_version": "2.1",\n",
       "            "id": "relationship--2f6a8785-e27b-487e-b870-b85a2121502d",\n",
       "            "created": "2020-06-24T15:05:18.250605Z",\n",
       "            "modified": "2020-06-24T15:05:18.250605Z",\n",
       "            "relationship_type": "indicates",\n",
       "            "source_ref": "indicator--a862ff86-68d9-42e5-8095-cd80c040e112",\n",
       "            "target_ref": "malware--389c934c-258c-44fb-ae4b-14c6c12270f6"\n",
       "        }\n",
       "    ]\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import Bundle\n", "\n", "bundle = Bundle(indicator, malware, relationship)\n", "print(bundle.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Cyber Observable References\n", "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.\n", "\n", "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:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "ipv4-addr",\n",
       "    "id": "ipv4-addr--dc63603e-e634-5357-b239-d4b562bc5445",\n",
       "    "value": "177.60.40.7",\n",
       "    "resolves_to_refs": [\n",
       "        "mac-addr--43f380fd-37c6-476d-8643-60849bf9240e"\n",
       "    ],\n",
       "    "spec_version": "2.1"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import IPv4Address\n", "\n", "ip4 = IPv4Address(\n", " value=\"177.60.40.7\",\n", " resolves_to_refs=[\"mac-addr--43f380fd-37c6-476d-8643-60849bf9240e\"]\n", ")\n", "\n", "print(ip4.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or we could create the MACAddress object(s) beforehand and then pass them in:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "ipv4-addr",\n",
       "    "id": "ipv4-addr--dc63603e-e634-5357-b239-d4b562bc5445",\n",
       "    "value": "177.60.40.7",\n",
       "    "resolves_to_refs": [\n",
       "        "mac-addr--f72d7d00-86bd-5cd2-8c86-52f7a83bef62",\n",
       "        "mac-addr--875ad625-177b-5c2a-9101-d44b0ad55938"\n",
       "    ],\n",
       "    "spec_version": "2.1"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import MACAddress\n", "\n", "mac_addr_a = MACAddress(value=\"a1:b2:c3:d4:e5:f6\")\n", "mac_addr_b = MACAddress(value=\"a7:b8:c9:d0:e1:f2\")\n", "\n", "ip4_valid_refs = IPv4Address(\n", " value=\"177.60.40.7\",\n", " resolves_to_refs=[mac_addr_a.id, mac_addr_b.id]\n", ")\n", "\n", "print(ip4_valid_refs.serialize(pretty=True))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.0a6" } }, "nbformat": 4, "nbformat_minor": 2 }