{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "# Delete this cell to re-enable tracebacks\n", "import sys\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, TextLexer\n", "from pygments.formatters import HtmlFormatter\n", "from IPython.display import display, HTML\n", "from IPython.core.interactiveshell import InteractiveShell\n", "\n", "InteractiveShell.ast_node_interactivity = \"all\"\n", "\n", "def json_print(inpt):\n", " string = str(inpt)\n", " formatter = HtmlFormatter()\n", " if string[0] == '{':\n", " lexer = JsonLexer()\n", " else:\n", " lexer = TextLexer()\n", " return HTML('{}'.format(\n", " formatter.get_style_defs('.highlight'),\n", " highlight(string, lexer, formatter)))\n", "\n", "globals()['print'] = json_print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom STIX Content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Properties\n", "\n", "Attempting to create a STIX object with properties not defined by the specification will result in an error. Try creating an ``Identity`` object with a custom ``x_foo`` property:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "ExtraPropertiesError", "evalue": "Unexpected properties for Identity: (x_foo).", "output_type": "error", "traceback": [ "\u001b[0;31mExtraPropertiesError\u001b[0m\u001b[0;31m:\u001b[0m Unexpected properties for Identity: (x_foo).\n" ] } ], "source": [ "from stix2 import Identity\n", "\n", "Identity(name=\"John Smith\",\n", " identity_class=\"individual\",\n", " x_foo=\"bar\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a STIX object with one or more custom properties, pass them in as a dictionary parameter called ``custom_properties``:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "identity",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "identity--a4c49251-0ad1-44e6-8cfc-3dbd75e73fbd",\n",
       "    "created": "2020-06-24T18:29:07.107425Z",\n",
       "    "modified": "2020-06-24T18:29:07.107425Z",\n",
       "    "name": "John Smith",\n",
       "    "identity_class": "individual",\n",
       "    "x_foo": "bar"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "identity = Identity(name=\"John Smith\",\n", " identity_class=\"individual\",\n", " custom_properties={\n", " \"x_foo\": \"bar\"\n", " })\n", "print(identity.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, setting ``allow_custom`` to ``True`` will allow custom properties without requiring a ``custom_properties`` dictionary." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "identity",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "identity--50c33f36-362b-4815-9f97-f3c7f39aa691",\n",
       "    "created": "2020-06-24T18:29:15.435425Z",\n",
       "    "modified": "2020-06-24T18:29:15.435425Z",\n",
       "    "name": "John Smith",\n",
       "    "identity_class": "individual",\n",
       "    "x_foo": "bar"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "identity2 = Identity(name=\"John Smith\",\n", " identity_class=\"individual\",\n", " x_foo=\"bar\",\n", " allow_custom=True)\n", "print(identity2.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Likewise, when parsing STIX content with custom properties, pass ``allow_custom=True`` to [parse()](../api/stix2.parsing.rst#stix2.parsing.parse):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
bar\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import parse\n", "\n", "input_string = \"\"\"{\n", " \"type\": \"identity\",\n", " \"spec_version\": \"2.1\",\n", " \"id\": \"identity--311b2d2d-f010-4473-83ec-1edf84858f4c\",\n", " \"created\": \"2015-12-21T19:59:11Z\",\n", " \"modified\": \"2015-12-21T19:59:11Z\",\n", " \"name\": \"John Smith\",\n", " \"identity_class\": \"individual\",\n", " \"x_foo\": \"bar\"\n", "}\"\"\"\n", "identity3 = parse(input_string, allow_custom=True)\n", "print(identity3.x_foo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To remove a custom properties, use `new_version()` and set that property to `None`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "identity",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",\n",
       "    "created": "2015-12-21T19:59:11.000Z",\n",
       "    "modified": "2020-06-24T18:29:24.099Z",\n",
       "    "name": "John Smith",\n",
       "    "identity_class": "individual"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "identity4 = identity3.new_version(x_foo=None)\n", "print(identity4.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom STIX Object Types\n", "\n", "To create a custom STIX object type, define a class with the @[CustomObject](../api/v21/stix2.v21.sdo.rst#stix2.v21.sdo.CustomObject) decorator. It takes the type name and a list of property tuples, each tuple consisting of the property name and a property instance. Any special validation of the properties can be added by supplying an ``__init__`` function.\n", "\n", "Let's say zoo animals have become a serious cyber threat and we want to model them in STIX using a custom object type. Let's use a ``species`` property to store the kind of animal, and make that property required. We also want a property to store the class of animal, such as \"mammal\" or \"bird\" but only want to allow specific values in it. We can add some logic to validate this property in ``__init__``." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from stix2 import CustomObject, properties\n", "\n", "@CustomObject('x-animal', [\n", " ('species', properties.StringProperty(required=True)),\n", " ('animal_class', properties.StringProperty()),\n", "])\n", "class Animal(object):\n", " def __init__(self, animal_class=None, **kwargs):\n", " if animal_class and animal_class not in ['mammal', 'bird', 'fish', 'reptile']:\n", " raise ValueError(\"'%s' is not a recognized class of animal.\" % animal_class)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can create an instance of our custom ``Animal`` type." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "x-animal",\n",
       "    "spec_version": "2.1",\n",
       "    "id": "x-animal--c7dbda16-360a-4622-b9c7-91f0497167cc",\n",
       "    "created": "2020-06-24T18:33:29.856926Z",\n",
       "    "modified": "2020-06-24T18:33:29.856926Z",\n",
       "    "species": "lion",\n",
       "    "animal_class": "mammal"\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "animal = Animal(species=\"lion\",\n", " animal_class=\"mammal\")\n", "print(animal.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Trying to create an ``Animal`` instance with an ``animal_class`` that's not in the list will result in an error:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "'alien' is not a recognized class of animal.", "output_type": "error", "traceback": [ "\u001b[0;31mValueError\u001b[0m\u001b[0;31m:\u001b[0m 'alien' is not a recognized class of animal.\n" ] } ], "source": [ "Animal(species=\"xenomorph\",\n", " animal_class=\"alien\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Parsing custom object types that you have already defined is simple and no different from parsing any other STIX object." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
shark\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_string2 = \"\"\"{\n", " \"type\": \"x-animal\",\n", " \"id\": \"x-animal--941f1471-6815-456b-89b8-7051ddf13e4b\",\n", " \"created\": \"2015-12-21T19:59:11Z\",\n", " \"modified\": \"2015-12-21T19:59:11Z\",\n", " \"spec_version\": \"2.1\",\n", " \"species\": \"shark\",\n", " \"animal_class\": \"fish\"\n", "}\"\"\"\n", "animal2 = parse(input_string2)\n", "print(animal2.species)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, parsing custom object types which you have not defined will result in an error:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "ename": "ParseError", "evalue": "Can't parse unknown object type 'x-foobar'! For custom types, use the CustomObject decorator.", "output_type": "error", "traceback": [ "\u001b[0;31mParseError\u001b[0m\u001b[0;31m:\u001b[0m Can't parse unknown object type 'x-foobar'! For custom types, use the CustomObject decorator.\n" ] } ], "source": [ "input_string3 = \"\"\"{\n", " \"type\": \"x-foobar\",\n", " \"id\": \"x-foobar--d362beb5-a04e-4e6b-a030-b6935122c3f9\",\n", " \"created\": \"2015-12-21T19:59:11Z\",\n", " \"modified\": \"2015-12-21T19:59:11Z\",\n", " \"bar\": 1,\n", " \"baz\": \"frob\"\n", "}\"\"\"\n", "parse(input_string3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Cyber Observable Types\n", "\n", "Similar to custom STIX object types, use a decorator to create [custom Cyber Observable](../api/v21/stix2.v21.observables.rst#stix2.v21.observables.CustomObservable) types. Just as before, ``__init__()`` can hold additional validation, but it is not necessary." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "x-new-observable",\n",
       "    "id": "x-new-observable--fdb5fd26-533e-44f4-9463-e8ade73e08c0",\n",
       "    "a_property": "something",\n",
       "    "property_2": 10\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import CustomObservable\n", "\n", "@CustomObservable('x-new-observable', [\n", " ('a_property', properties.StringProperty(required=True)),\n", " ('property_2', properties.IntegerProperty()),\n", "])\n", "class NewObservable():\n", " pass\n", "\n", "new_observable = NewObservable(a_property=\"something\",\n", " property_2=10)\n", "print(new_observable.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Likewise, after the custom Cyber Observable type has been defined, it can be parsed." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
foobaz\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
5\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import ObservedData\n", "\n", "input_string4 = \"\"\"{\n", " \"type\": \"observed-data\",\n", " \"id\": \"observed-data--b67d30ff-02ac-498a-92f9-32f845f448cf\",\n", " \"spec_version\": \"2.1\",\n", " \"created_by_ref\": \"identity--f431f809-377b-45e0-aa1c-6a4751cae5ff\",\n", " \"created\": \"2016-04-06T19:58:16.000Z\",\n", " \"modified\": \"2016-04-06T19:58:16.000Z\",\n", " \"first_observed\": \"2015-12-21T19:00:00Z\",\n", " \"last_observed\": \"2015-12-21T19:00:00Z\",\n", " \"number_observed\": 50,\n", " \"objects\": {\n", " \"0\": {\n", " \"type\": \"x-new-observable\",\n", " \"a_property\": \"foobaz\",\n", " \"property_2\": 5\n", " }\n", " }\n", "}\"\"\"\n", "obs_data = parse(input_string4)\n", "print(obs_data.objects[\"0\"].a_property)\n", "print(obs_data.objects[\"0\"].property_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ID-Contributing Properties for Custom Cyber Observables\n", "STIX 2.1 Cyber Observables (SCOs) have deterministic IDs, meaning that the ID of a SCO is based on the values of some of its properties. Thus, if multiple cyber observables of the same type have the same values for their ID-contributing properties, then these SCOs will have the same ID. UUIDv5 is used for the deterministic IDs, using the namespace `\"00abedb4-aa42-466c-9c01-fed23315a9b7\"`. A SCO's ID-contributing properties may consist of a combination of required properties and optional properties.\n", "\n", "If a SCO type does not have any ID contributing properties defined, or all of the ID-contributing properties are not present on the object, then the SCO uses a randomly-generated UUIDv4. Thus, you can optionally define which of your custom SCO's properties should be ID-contributing properties. Similar to standard SCOs, your custom SCO's ID-contributing properties can be any combination of the SCO's required and optional properties.\n", "\n", "You define the ID-contributing properties when defining your custom SCO with the `CustomObservable` decorator. After the list of properties, you can optionally define the list of id-contributing properties. If you do not want to specify any id-contributing properties for your custom SCO, then you do not need to do anything additional.\n", "\n", "See the example below:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "type": "x-new-observable-2",\n",
       "    "id": "x-new-observable-2--cafee477-4edc-58fd-81c1-2e23e93f9326",\n",
       "    "a_property": "A property",\n",
       "    "property_2": 2000\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
{\n",
       "    "type": "x-new-observable-2",\n",
       "    "id": "x-new-observable-2--cafee477-4edc-58fd-81c1-2e23e93f9326",\n",
       "    "a_property": "A property",\n",
       "    "property_2": 3000\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
{\n",
       "    "type": "x-new-observable-2",\n",
       "    "id": "x-new-observable-2--2945b948-7361-5204-a630-31b828af920c",\n",
       "    "a_property": "A different property",\n",
       "    "property_2": 3000\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import CustomObservable\n", "\n", "@CustomObservable('x-new-observable-2', [\n", " ('a_property', properties.StringProperty(required=True)),\n", " ('property_2', properties.IntegerProperty()),\n", "], [\n", " 'a_property'\n", "])\n", "class NewObservable2():\n", " pass\n", "\n", "new_observable_a = NewObservable2(a_property=\"A property\", property_2=2000)\n", "print(new_observable_a.serialize(pretty=True))\n", "\n", "new_observable_b = NewObservable2(a_property=\"A property\", property_2=3000)\n", "print(new_observable_b.serialize(pretty=True))\n", "\n", "new_observable_c = NewObservable2(a_property=\"A different property\", property_2=3000)\n", "print(new_observable_c.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, `a_property` is the only id-contributing property. Notice that the ID for `new_observable_a` and `new_observable_b` is the same since they have the same value for the id-contributing `a_property` property." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Custom Cyber Observable Extensions\n", "\n", "Finally, custom extensions to existing Cyber Observable types can also be created. Just use the @[CustomExtension](../api/v21/stix2.v21.common.rst#stix2.v21.common.CustomExtension) decorator. Note that you must provide the Cyber Observable class to which the extension applies. Again, any extra validation of the properties can be implemented by providing an ``__init__()`` but it is not required. Let's say we want to make an extension to the ``File`` Cyber Observable Object:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "    "property1": "something",\n",
       "    "property2": 10\n",
       "}\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stix2 import CustomExtension\n", "\n", "@CustomExtension('x-new-ext', [\n", " ('property1', properties.StringProperty(required=True)),\n", " ('property2', properties.IntegerProperty()),\n", "])\n", "class NewExtension():\n", " pass\n", "\n", "new_ext = NewExtension(property1=\"something\",\n", " property2=10)\n", "print(new_ext.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once the custom Cyber Observable extension has been defined, it can be parsed." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
bla\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
50\n",
       "
\n" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_string5 = \"\"\"{\n", " \"type\": \"observed-data\",\n", " \"id\": \"observed-data--b67d30ff-02ac-498a-92f9-32f845f448cf\",\n", " \"spec_version\": \"2.1\",\n", " \"created_by_ref\": \"identity--f431f809-377b-45e0-aa1c-6a4751cae5ff\",\n", " \"created\": \"2016-04-06T19:58:16.000Z\",\n", " \"modified\": \"2016-04-06T19:58:16.000Z\",\n", " \"first_observed\": \"2015-12-21T19:00:00Z\",\n", " \"last_observed\": \"2015-12-21T19:00:00Z\",\n", " \"number_observed\": 50,\n", " \"objects\": {\n", " \"0\": {\n", " \"type\": \"file\",\n", " \"name\": \"foo.bar\",\n", " \"hashes\": {\n", " \"SHA-256\": \"35a01331e9ad96f751278b891b6ea09699806faedfa237d40513d92ad1b7100f\"\n", " },\n", " \"extensions\": {\n", " \"x-new-ext\": {\n", " \"property1\": \"bla\",\n", " \"property2\": 50\n", " }\n", " }\n", " }\n", " }\n", "}\"\"\"\n", "obs_data2 = parse(input_string5)\n", "print(obs_data2.objects[\"0\"].extensions[\"x-new-ext\"].property1)\n", "print(obs_data2.objects[\"0\"].extensions[\"x-new-ext\"].property2)" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }