Add docs (Jupyter) for custom content

stix2.1
Chris Lenk 2017-09-21 09:34:02 -04:00
parent 21d978acc8
commit d54ac6e231
2 changed files with 533 additions and 0 deletions

532
docs/custom.ipynb Normal file
View File

@ -0,0 +1,532 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"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",
" return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))\n",
"\n",
"ipython.showtraceback = hide_traceback"
]
},
{
"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": 2,
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"x_foo\": \"bar\",\n",
" \"type\": \"identity\",\n",
" \"id\": \"identity--1c4d65f9-cd62-4ee0-8fa7-f2686e5226bb\",\n",
" \"created\": \"2017-09-21T13:11:37.863Z\",\n",
" \"modified\": \"2017-09-21T13:11:37.863Z\",\n",
" \"name\": \"John Smith\",\n",
" \"identity_class\": \"individual\"\n",
"}\n"
]
}
],
"source": [
"identity = Identity(name=\"John Smith\",\n",
" identity_class=\"individual\",\n",
" custom_properties={\n",
" \"x_foo\": \"bar\"\n",
" })\n",
"print(identity)"
]
},
{
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"x_foo\": \"bar\",\n",
" \"type\": \"identity\",\n",
" \"id\": \"identity--d31a136a-49f6-44e6-b503-03f359349d4d\",\n",
" \"created\": \"2017-09-21T13:11:39.777Z\",\n",
" \"modified\": \"2017-09-21T13:11:39.777Z\",\n",
" \"name\": \"John Smith\",\n",
" \"identity_class\": \"individual\"\n",
"}\n"
]
}
],
"source": [
"identity2 = Identity(name=\"John Smith\",\n",
" identity_class=\"individual\",\n",
" x_foo=\"bar\",\n",
" allow_custom=True)\n",
"print(identity2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Likewise, when parsing STIX content with custom properties, pass ``allow_custom=True`` to ``parse()``:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bar\n"
]
}
],
"source": [
"from stix2 import parse\n",
"\n",
"input_string = \"\"\"{\n",
" \"type\": \"identity\",\n",
" \"id\": \"identity--311b2d2d-f010-5473-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": [
"### Custom STIX Object Types\n",
"\n",
"To create a custom STIX object type, define a class with the ``@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": 4,
"metadata": {
"collapsed": true
},
"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": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"type\": \"x-animal\",\n",
" \"id\": \"x-animal--47ee8f49-d76c-4221-a924-c9e6662f78c9\",\n",
" \"created\": \"2017-09-21T13:11:45.416Z\",\n",
" \"modified\": \"2017-09-21T13:11:45.416Z\",\n",
" \"species\": \"lion\",\n",
" \"animal_class\": \"mammal\"\n",
"}\n"
]
}
],
"source": [
"animal = Animal(species=\"lion\",\n",
" animal_class=\"mammal\")\n",
"print(animal)"
]
},
{
"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": 9,
"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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"shark\n"
]
}
],
"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",
" \"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": 11,
"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 types. Just as before, ``__init__()`` can hold additional validation, but it is not necessary."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"type\": \"x-new-observable\",\n",
" \"a_property\": \"something\",\n",
" \"property_2\": 10\n",
"}\n"
]
}
],
"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)"
]
},
{
"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": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foobaz\n",
"5\n"
]
}
],
"source": [
"from stix2 import ObservedData\n",
"\n",
"input_string4 = \"\"\"{\n",
" \"type\": \"observed-data\",\n",
" \"id\": \"observed-data--b67d30ff-02ac-498a-92f9-32f845f448cf\",\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": {
"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`` 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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"property1\": \"something\",\n",
" \"property2\": 10\n",
"}\n"
]
}
],
"source": [
"from stix2 import File, CustomExtension\n",
"\n",
"@CustomExtension(File, '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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once the custom Cyber Observable extension has been defined, it can be parsed."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bla\n",
"50\n"
]
}
],
"source": [
"input_string5 = \"\"\"{\n",
" \"type\": \"observed-data\",\n",
" \"id\": \"observed-data--b67d30ff-02ac-498a-92f9-32f845f448cf\",\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 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -16,6 +16,7 @@ Welcome to stix2's documentation!
parsing
versioning
markings
custom
datastore_api
roadmap
contributing