2018-06-20 09:29:08 +02:00
{
"cells": [
2018-06-25 11:28:40 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Initializing your environment\n",
"\n",
"## Installation as PyMISP user\n",
"\n",
"The quick and dirty way:\n",
"\n",
"```bash\n",
"sudo pip3 install pymisp\n",
"```\n",
"\n",
"The clean approach as user:\n",
"\n",
"```bash\n",
"pip3 install --user pymisp\n",
"```\n",
"\n",
"## Installation as PyMISP developer (recommended for this session)\n",
"\n",
"\n",
"```bash\n",
"git clone https://github.com/MISP/PyMISP.git\n",
"\n",
"cd PyMISP\n",
"\n",
"virtualenv -p python3 pymisp-env\n",
"source pymisp-env/bin/activate\n",
"\n",
"pip install -e . \n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setting up of jupyter\n",
"\n",
"**We assume you're in a virtual environment**\n",
"\n",
"If you want to follow along this workshop on your computer, this is the way to go:\n",
"\n",
"\n",
"```bash\n",
2018-11-22 14:29:07 +01:00
"pip3 install jupyter\n",
2018-06-25 11:28:40 +02:00
"cd docs/tutorial\n",
"jupyter-notebook\n",
"```"
]
},
2018-06-20 09:29:08 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using the PyMISP objects\n",
2018-06-25 11:28:40 +02:00
"\n",
2019-09-26 11:03:03 +02:00
"This page aims to give recommendations about how to efficiently use the `pymisp` library.\n",
2018-06-25 11:28:40 +02:00
"\n",
"It is strongly recommended (read \"don't do anything else, please\") to use the library this way and never, ever modify the python dictionary you get by loading the json blob you receive from the server.\n",
"\n",
2019-09-26 11:03:03 +02:00
"This library is made in a way to hide as much as the complexity as possible and we're happy to improve it is there is something missing."
2018-06-25 11:28:40 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## MISPEvent\n",
"\n",
"`MISPEvent` is the main class to use when you want to create/update events on a MISP instance."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPEvent\n",
"\n",
"event = MISPEvent()\n",
"\n",
"event.info = 'This is my new MISP event' # Required\n",
"event.distribution = 0 # Optional, defaults to MISP.default_event_distribution in MISP config\n",
"event.threat_level_id = 2 # Optional, defaults to MISP.default_event_threat_level in MISP config\n",
"event.analysis = 1 # Optional, defaults to 0 (initial analysis)\n",
"\n",
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Tag Event\n",
"\n",
"First example of helper aiming to make your life easier."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event.add_tag('tlp:white')\n",
"\n",
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Set the Event date\n",
"\n",
"\n",
2019-09-26 11:03:03 +02:00
"The date can be in many different formats. This helper makes sure it normalizes it in a way that will be understood by your MISP instance."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# As text\n",
"event.set_date('2018-04-13')\n",
"print('Simple', event.date)\n",
2018-06-25 11:28:40 +02:00
"\n",
"# Some weird text format (anything supported by dateparse will work)\n",
2018-06-20 09:29:08 +02:00
"event.set_date('Sat Oct 11 00:13:46 2017')\n",
2018-06-25 11:28:40 +02:00
"print('Messy', event.date)\n",
2018-06-20 09:29:08 +02:00
"\n",
2018-06-25 11:28:40 +02:00
"# datetime.date\n",
2018-06-20 09:29:08 +02:00
"from datetime import date\n",
"d = date.today()\n",
"print(type(d))\n",
"event.set_date(d)\n",
"print(event.date)\n",
"\n",
2019-09-26 11:03:03 +02:00
"# datetime.datetime => MISP expects a day, so the hour will be dropped.\n",
2018-06-20 09:29:08 +02:00
"from datetime import datetime\n",
"d = datetime.now()\n",
"print(type(d))\n",
"event.set_date(d)\n",
"print(event.date)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Add Attribute to event\n",
"\n",
2019-09-26 11:03:03 +02:00
"More useful things: adding attributes to an event.\n",
2018-06-25 11:28:40 +02:00
"\n",
"Attributes have a bunch of parameters you can pass (if you feel like it). If you don't pass them, they'll be automatically set depending on their sane defaults.\n",
"\n",
"The parameters are the following:\n",
"* **type** (required)\n",
"* **value** (required)\n",
"* **category**: [see default](https://github.com/MISP/PyMISP/blob/master/pymisp/data/describeTypes.json)\n",
"* **to_ids**: [see default](https://github.com/MISP/PyMISP/blob/master/pymisp/data/describeTypes.json)\n",
"* **distribution**: defaults to inherit from parent (event or object)\n",
"* **disable_correlation**: true for a normal attribute, fallback to the value defined in the template object if relevant\n",
"* **data**: only for malware-sample or attachment, BytesIO object of the file. If it is a malware, the sample is decrypted in memory"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attribute = event.add_attribute('ip-dst', '8.8.8.8') # Minimal parameters\n",
"\n",
"print(type(attribute))\n",
"\n",
"print(attribute.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Set parameters (inline)\n",
"\n",
"This is the was to pass other parameters"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"attribute_second = event.add_attribute('ip-dst', '8.8.8.9', disable_correlation=True)\n",
2018-06-20 09:29:08 +02:00
"\n",
2018-06-25 11:28:40 +02:00
"print(attribute_second.to_json())"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Modify existing attribute\n",
"\n",
"Every parameter can be modified in a pythonic way."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"attribute.to_ids = False\n",
2018-06-20 09:29:08 +02:00
"\n",
2018-06-25 11:28:40 +02:00
"print(attribute.to_json())"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tag Attribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"# Using the list of attributes in the event\n",
"event.attributes[0].add_tag('tlp:green')\n",
"\n",
"# ... or the variable we got from `add_attribute`\n",
2018-06-20 10:05:47 +02:00
"attribute_second.add_tag('tlp:amber')\n",
2018-06-20 09:29:08 +02:00
"\n",
"print(attribute_second.to_json())"
]
},
2019-04-11 23:14:16 +02:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(event.to_json())"
]
},
2018-06-20 09:29:08 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Soft delete attribute\n",
"\n",
2019-09-26 11:03:03 +02:00
"**Important note**: the default approach to *delete* on MISP is to do a soft delete (meaning the attribute is not displayed on the default view on MISP). The reason we do it this way is that it allows to push *delete* updates to instances we synchronize with.\n",
2018-06-25 11:28:40 +02:00
"\n",
"The delete method will set the default parameter of the attribute to `True`."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attribute.delete()\n",
"print(attribute.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Mark event as published\n",
"\n",
"Same idea: you can set the published flag from the api"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event.publish()\n",
"print(event.published)"
]
},
2019-04-11 23:14:16 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MISPAttribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attr_type = 'ip-dst'\n",
"value = '1.1.1.1'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPAttribute\n",
"\n",
"# Attribute data already defined\n",
"attribute = MISPAttribute()\n",
"attribute.type = attr_type\n",
"attribute.value = value\n",
"print(attribute)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# An attribute can also be loaded directly from a JSON\n",
"json = '''{\n",
" \"type\": \"ip-dst\",\n",
" \"value\": \"127.0.0.1\",\n",
" \"category\": \"Network activity\",\n",
" \"to_ids\": false\n",
" }'''\n",
"\n",
"attribute = MISPAttribute()\n",
"attribute.from_json(json)\n",
"print(attribute)"
]
},
2018-06-25 11:28:40 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MISPObject\n",
"\n",
"Objects in MISP are a way to group attributes together in a way that makes sense. The objects are based on templates that are bundled in the library itself.\n",
"\n",
"**Note**: you can use your own templates, we will see how later"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPObject\n",
"\n",
"circl_attr = event.add_attribute('ip-dst', '149.13.33.14')\n",
"\n",
"\n",
"misp_object = MISPObject('domain-ip', standalone=False, default_attributes_parameters=circl_attr)\n",
"# Notes: \n",
"# * standalone: this object will be attached to a MISPEvent, so the references will be in the dump\n",
2018-06-25 11:28:40 +02:00
"# * default_attributes_parameters: keep parameters from a MISPAttribute (useful when expanding a existing one) \n",
2018-06-20 09:29:08 +02:00
"misp_object.comment = 'My Fancy new object'\n",
"\n",
"obj_attr = misp_object.add_attribute('domain', value='circl.lu')\n",
"obj_attr.add_tag('tlp:green')\n",
"misp_object.add_attribute('ip', value='149.13.33.14')\n",
"misp_object.add_attribute('first-seen', value='2018-04-11')\n",
"misp_object.add_attribute('last-seen', value='2018-06-11')\n",
"\n",
"event.add_object(misp_object)\n",
"print(event.to_json())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-04-11 23:14:16 +02:00
"## Short version to add an object to a MISPEvent\n",
2018-06-25 11:28:40 +02:00
"\n",
"You can also add the object directly in a misp event this way"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"from pymisp import MISPObject\n",
2018-06-20 09:29:08 +02:00
"\n",
2018-06-25 11:28:40 +02:00
"misp_object = event.add_object(name='domain-ip', comment='My Fancy new object, in one line')\n",
"\n",
"obj_attr = misp_object.add_attribute('domain', value='circl.lu')\n",
"obj_attr.add_tag('tlp:green')\n",
"misp_object.add_attribute('ip', value='149.13.33.14')\n",
"misp_object.add_attribute('first-seen', value='2018-04-11')\n",
"misp_object.add_attribute('last-seen', value='2018-06-11')\n",
2019-04-11 23:14:16 +02:00
"\n",
"misp_object.add_attributes('ip', {'value': '10.8.8.8', 'to_ids': False}, '10.9.8.8')\n",
"\n",
"\n",
2018-11-06 15:28:07 +01:00
"misp_object.add_reference(obj_attr.uuid, 'related-to', 'Expanded with passive DNS entry')\n",
2018-06-25 11:28:40 +02:00
"\n",
"print(event.to_json())\n"
2018-06-20 09:29:08 +02:00
]
},
{
2018-06-25 11:28:40 +02:00
"cell_type": "markdown",
2018-06-20 09:29:08 +02:00
"metadata": {},
"source": [
2019-04-11 23:14:16 +02:00
"# Helpers for MISPObjects \n",
2018-06-25 11:28:40 +02:00
"\n",
"For some objects, we have helpers in order to make your life easier. The most relevant example is the file object: when you have a file to push on MISP, there are plenty of indicators you can extract at once, and it is pretty simple to automate, so we made it a oneliner.\n",
"\n",
"**Note**: This requires a few more dependencies to get the full power of the script: \n",
"* `lief` to extract indicators out of PE/ELF/MachO files, and soon Android binaries.\n",
"* `python-magic` to get the mime type\n",
"* `pydeep` to compute the ssdeep of the binary whenever possible\n",
"\n",
"\n",
"```bash\n",
"pip install lief python-magic git+https://github.com/kbandla/pydeep.git\n",
"```"
2018-06-20 09:29:08 +02:00
]
},
{
2018-06-25 11:28:40 +02:00
"cell_type": "code",
"execution_count": null,
2018-06-20 09:29:08 +02:00
"metadata": {},
2018-06-25 11:28:40 +02:00
"outputs": [],
2018-06-20 09:29:08 +02:00
"source": [
2018-06-25 11:28:40 +02:00
"from pymisp.tools import FileObject\n",
"\n",
"file_obj = FileObject(filepath='../../tests/viper-test-files/test_files/EICAR.com', standalone=False)\n",
"print(file_obj.to_json())"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"event.add_object(file_obj)\n",
"print(event.to_json())"
2018-06-20 09:29:08 +02:00
]
},
2018-06-26 06:01:48 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-04-11 23:14:16 +02:00
"### Excel support \n",
"\n",
"(okay, CSV, but that's the same thing, right?)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash \n",
"\n",
"cat ../../tests/csv_testfiles/valid_fieldnames.csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash \n",
"\n",
"cat ../../tests/csv_testfiles/invalid_fieldnames.csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp.tools import CSVLoader\n",
"from pymisp import MISPEvent\n",
"from pathlib import Path\n",
"\n",
"csv1 = CSVLoader(template_name='file', csv_path=Path('../../tests/csv_testfiles/valid_fieldnames.csv'))\n",
"event = MISPEvent()\n",
"event.info = 'Test event from CSV loader'\n",
"for o in csv1.load():\n",
" event.add_object(**o)\n",
"\n",
"print(event.to_json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event = MISPEvent()\n",
"event.info = 'Test event from CSV loader'\n",
"csv2 = CSVLoader(template_name='file', csv_path=Path('../../tests/csv_testfiles/invalid_fieldnames.csv'),\n",
" fieldnames=['SHA1', 'fileName', 'size-in-bytes'], has_fieldnames=True)\n",
"\n",
"for o in csv2.load():\n",
" event.add_object(**o)\n",
" \n",
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generic helper\n",
2018-06-26 06:01:48 +02:00
"\n",
2019-09-26 11:03:03 +02:00
"This helper is meant to be used when you already have a script that does the mapping between your own code, and the MISPObject template."
2018-06-26 06:01:48 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp.tools import GenericObjectGenerator\n",
"\n",
"attributeAsDict = [{'script': ':(){ :|:& };:', 'comment': 'Forkbomb'},\n",
" {'language': {'value': 'Bash', 'to_ids': False, 'disable_correlation': True}}, \n",
" {'filename': {'value': 'forkbomb.sh', 'to_ids': True}},\n",
" {'state': 'Malicious'}]\n",
"\n",
"\n",
"misp_object = GenericObjectGenerator('script', strict=True)\n",
"misp_object.generate_attributes(attributeAsDict)\n",
"\n",
"print(misp_object.to_json())"
]
},
2019-04-11 23:14:16 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User defined objects"
]
},
2018-06-26 06:01:48 +02:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp.tools import GenericObjectGenerator\n",
"\n",
"attributeAsDict = [{'MyCoolAttribute': {'value': 'critical thing', 'type': 'text'}}, \n",
" {'MyCoolerAttribute': {'value': 'even worse', 'type': 'text'}}]\n",
"\n",
"\n",
"misp_object = GenericObjectGenerator('my-cool-template', strict=True) # This is supposed to fail due to the strict parameter\n",
"misp_object.generate_attributes(attributeAsDict)\n",
"\n",
"print(misp_object.to_json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp.tools import GenericObjectGenerator\n",
"\n",
"attributeAsDict = [{'MyCoolAttribute': {'value': 'critical thing', 'type': 'text'}}, \n",
" {'MyCoolerAttribute': {'value': 'even worse', 'type': 'text'}}]\n",
"\n",
"\n",
"misp_object = GenericObjectGenerator('my-cool-template')\n",
"misp_object.generate_attributes(attributeAsDict)\n",
"\n",
"print(misp_object.to_json())"
]
},
2018-06-20 09:29:08 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Use locally defined objet templates\n",
"\n",
"**Important**: The path you pass as parameter for `misp_objects_path_custom` needs to contain a directory equals to the value of the parameter `name` (same structure as the content of the `misp-object` repository)\n"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"user_defined_obj = MISPObject(name='test_object_template', strict=True, misp_objects_path_custom='../../tests/mispevent_testfiles')\n",
2018-06-20 09:29:08 +02:00
"\n",
2018-06-25 11:28:40 +02:00
"user_defined_obj.add_attribute('member3', value='foo')\n",
"user_defined_obj.add_attribute('member1', value='baz')\n",
2018-06-20 09:29:08 +02:00
"\n",
2018-06-25 11:28:40 +02:00
"print(user_defined_obj.to_json())"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Playing with a malware sample\n",
"\n",
"The data you receive out of the JSON dump from a MISP instance is a base64 encoded zip with `infected` as a password. The zip file contains 2 files, one containing the original file name of the uploaded file, and the other one is the binary.\n",
"\n",
"This is pretty much a pain to use as-is.\n",
"\n",
"So there is an helper for that!\n"
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"sample = file_obj.get_attributes_by_relation('malware-sample')[0]\n",
"print(sample)\n",
"print('File name --->', sample.malware_filename)\n",
"print(sample.malware_binary)\n",
"print('Content of the malware (in bytes) ----->', sample.malware_binary.getvalue())"
2018-06-20 09:29:08 +02:00
]
},
2018-06-20 10:05:47 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Use lief to extract indicators out of binaries\n",
"\n",
"An other cool helper: one liner to whom you can pass the path to a binary, if it is supported by `lief` (PE/ELF/Mach-o), you get the the file object, a PE, ELF, or Mach-o object, and the relevant sections.\n",
"\n",
"If it is anything else, it will just generate the the file object.\n"
2018-06-20 10:05:47 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"from pymisp.tools import make_binary_objects\n",
"\n",
"file_obj, bin_obj, sections = make_binary_objects(filepath='../../tests/viper-test-files/test_files/whoami.exe', standalone=False)\n",
"\n",
"event.add_object(file_obj)\n",
"if bin_obj: \n",
" event.add_object(bin_obj)\n",
" for s in sections:\n",
" event.add_object(s)"
2018-06-20 10:05:47 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## References\n",
"\n",
"The references are also set by default by this method."
2018-06-20 10:05:47 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-06-25 11:28:40 +02:00
"print(bin_obj.uuid)\n",
"print(bin_obj.references[0].to_json())\n",
"print(event.to_json())"
2018-06-20 10:05:47 +02:00
]
},
2018-06-20 09:29:08 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Dump valid MISP Event ready to push to MISP\n",
"\n",
"We've been using `to_json` a lot. The thing you should know is that every python MISP objects have this method, and it **always** returns a valid json blob you can send to MISP."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"# Update an existing MISPEvent\n",
"\n",
"We were creating new events, but you will also want to update an existing one."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPEvent\n",
"\n",
"existing_event = MISPEvent()\n",
"existing_event.load_file('../../tests/mispevent_testfiles/existing_event.json')\n",
"\n",
"print(existing_event.attributes[0])\n",
"print(existing_event.attributes[0].tags)\n",
"print(existing_event.attributes[0].timestamp)\n",
"print(existing_event.attributes[0].to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-06-25 11:28:40 +02:00
"## Edit, removes the timestamp when exporting\n",
"\n",
2019-09-26 11:03:03 +02:00
"If you tried to edit an event manually, and never got the updates on the instance, it is probably because the timestamps weren't updated/removed. Or you removed them all, and adding a single tag was making every attributes as new.\n",
2018-06-25 11:28:40 +02:00
"\n",
"PyMISP got you covered."
2018-06-20 09:29:08 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"existing_event.attributes[0].add_tag('tlp:white')\n",
"print(existing_event.attributes[0].to_json())"
]
2018-06-21 00:49:03 +02:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-04-11 23:14:16 +02:00
"# Specific use-cases"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change creator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPOrganisation\n",
"orgc = MISPOrganisation()\n",
"orgc.name = 'bazbaz'\n",
"orgc.id = 15\n",
"orgc.uuid = '5888a98d-a7e8-4183-94bb-4d19950d210f'\n",
"# NOTE: Pushing this object will only work if the user has sync right (if not, the orgc key will be ignored)\n",
"event.Orgc = orgc\n",
"\n",
"print(event.to_json())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-09-26 11:03:03 +02:00
"# Getting the API key (automatically generated on the training VM)"
2019-04-11 23:14:16 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"\n",
"api_file = Path('apikey')\n",
"if api_file.exists():\n",
" misp_url = 'http://127.0.0.1'\n",
" misp_verifycert = False\n",
" with open(api_file) as f:\n",
" misp_key = f.read().strip()\n",
" print(misp_key)\n",
"else:\n",
" print(\"Unable to find the api key\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize variables if you run the notebook locally"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The URL of the MISP instance to connect to\n",
2019-09-16 18:30:53 +02:00
"misp_url = 'https://127.0.0.1:8443/'\n",
2019-04-11 23:14:16 +02:00
"# Can be found in the MISP web interface under \n",
"# http://+MISP_URL+/users/view/me -> Authkey\n",
2019-09-16 18:30:53 +02:00
"misp_key = 'd6OmdDFvU3Seau3UjwvHS1y3tFQbaRNhJhDX0tjh'\n",
2019-04-11 23:14:16 +02:00
"# Should PyMISP verify the MISP certificate\n",
"misp_verifycert = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import ExpandedPyMISP, PyMISP\n",
"\n",
"misp = ExpandedPyMISP(misp_url, misp_key, misp_verifycert)\n",
"misp_old = PyMISP(misp_url, misp_key, misp_verifycert)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Full example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## New API\n",
"\n",
"Returns MISPEvent."
2018-06-21 00:49:03 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPEvent, MISPObject\n",
"\n",
"event = MISPEvent()\n",
"event.info = 'This is my new MISP event' # Required\n",
"event.distribution = 0 # Optional, defaults to MISP.default_event_distribution in MISP config\n",
"event.threat_level_id = 2 # Optional, defaults to MISP.default_event_threat_level in MISP config\n",
"event.analysis = 1 # Optional, defaults to 0 (initial analysis)\n",
"\n",
"mispObject = MISPObject('file')\n",
"mispObject.add_attribute('filename', type='filename',\n",
2018-06-26 06:01:48 +02:00
" value='filename.exe',\n",
" Tag=[{'name': 'tlp:amber'}])\n",
"\n",
2018-06-21 00:49:03 +02:00
"event.add_object(mispObject)\n",
"\n",
2019-04-11 23:14:16 +02:00
"print(misp)\n",
2019-09-16 18:30:53 +02:00
"existing_event = misp.add_event(event, pythonify=True)\n",
2019-04-11 23:14:16 +02:00
"print(existing_event)\n",
2018-06-26 06:01:48 +02:00
"mispObject = MISPObject('file')\n",
"mispObject.add_attribute('filename', type='filename',\n",
" value='filename2.exe',\n",
" Tag=[{'name': 'tlp:white'}])\n",
"\n",
"existing_event.add_object(mispObject)\n",
"print(existing_event.to_json())\n",
"\n",
"res = misp.update(existing_event)\n",
"existing_event = MISPEvent()\n",
"existing_event.load(res)\n",
2018-06-21 00:49:03 +02:00
"print(existing_event.to_json())"
]
2018-06-25 11:28:40 +02:00
},
2019-04-11 23:14:16 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Old API\n",
"\n",
"Returns plain JSON"
]
},
2018-06-25 11:28:40 +02:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPEvent, MISPObject\n",
"\n",
"event = MISPEvent()\n",
"event.info = 'This is my new MISP event' # Required\n",
"event.distribution = 0 # Optional, defaults to MISP.default_event_distribution in MISP config\n",
"event.threat_level_id = 2 # Optional, defaults to MISP.default_event_threat_level in MISP config\n",
"event.analysis = 1 # Optional, defaults to 0 (initial analysis)\n",
"\n",
"mispObject = MISPObject('file')\n",
"mispObject.add_attribute('filename', type='filename',\n",
2019-04-11 23:14:16 +02:00
" value='filename.exe',\n",
" Tag=[{'name': 'tlp:amber'}])\n",
2018-06-25 11:28:40 +02:00
"\n",
2019-04-11 23:14:16 +02:00
"event.add_object(mispObject)\n",
2018-06-25 11:28:40 +02:00
"\n",
2019-04-11 23:14:16 +02:00
"print(misp)\n",
2018-06-25 11:28:40 +02:00
"res = misp.add_event(event)\n",
2019-04-11 23:14:16 +02:00
"print(res)\n",
"existing_event = MISPEvent()\n",
"existing_event.load(res)\n",
"mispObject = MISPObject('file')\n",
"mispObject.add_attribute('filename', type='filename',\n",
" value='filename2.exe',\n",
" Tag=[{'name': 'tlp:white'}])\n",
"\n",
"existing_event.add_object(mispObject)\n",
"print(existing_event.to_json())\n",
"\n",
"res = misp.update(existing_event)\n",
2018-06-25 11:28:40 +02:00
"existing_event = MISPEvent()\n",
"existing_event.load(res)\n",
"print(existing_event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-04-11 23:14:16 +02:00
"# Interacting with a MISP instance"
2018-06-25 11:28:40 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-04-11 23:14:16 +02:00
"## Creating An Event"
2018-06-25 11:28:40 +02:00
]
},
{
2019-04-11 23:14:16 +02:00
"cell_type": "markdown",
"metadata": {},
"source": [
"### Directly"
]
},
{
"cell_type": "code",
"execution_count": null,
2018-06-25 11:28:40 +02:00
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"misp.toggle_global_pythonify() # Returns PyMISP objects whenever possible, allows to skip pythonify\n",
"\n",
"event = misp.add_event({'distribution': 1, \"threat_level_id\": 1, \"analysis\": 1, 'info':\"Event from notebook\"})\n",
2019-04-11 23:14:16 +02:00
"print(\"Event id: %s\" % event.id)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event = misp_old.new_event(distribution=1,\n",
" threat_level_id=1,\n",
" analysis=1,\n",
" info=\"Event from notebook\")\n",
"print(\"Event id: %s\" % event['Event']['id'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using the MISPEvent constructor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPEvent\n",
2018-06-25 11:28:40 +02:00
"\n",
2019-04-11 23:14:16 +02:00
"event_obj = MISPEvent()\n",
"event_obj.distribution = 1\n",
"event_obj.threat_level_id = 1\n",
"event_obj.analysis = 1\n",
"event_obj.info = \"Event from notebook 2\"\n",
"event = misp.add_event(event_obj)\n",
"event_id = event.id\n",
"print(\"Event id: %s\" % event_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fetching an event"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"event_id = 9"
2019-04-11 23:14:16 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Fetch by ID\n",
"event = misp.get_event(event_id)\n",
"print(event)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Fetch by ID\n",
"event = misp_old.get_event(event_id)\n",
"print(event)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add an attribute to an event"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Directly"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attr_type = \"ip-src\"\n",
"value = \"8.8.8.8\"\n",
"category = \"Network activity\"\n",
"to_ids = False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Oldish API"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"proposal = False\n",
"updated_event = misp.add_named_attribute(event_id,\n",
" attr_type,\n",
" value,\n",
" category=category,\n",
" to_ids=to_ids,\n",
" proposal=proposal)\n",
"print(updated_event)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Cleaner way"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"value = \"9.8.8.8\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPAttribute\n",
"\n",
"# Attribute data already defined\n",
"attribute = MISPAttribute()\n",
"attribute.type = attr_type\n",
"attribute.value = value\n",
"attribute.category = category\n",
"attribute.to_ids = to_ids\n",
"\n",
"attribute_to_change = misp.add_attribute(event_id, attribute)\n",
"print(attribute_to_change.id, attribute_to_change)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Propose new Attribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPAttribute\n",
"\n",
"attr_type = \"ip-src\"\n",
"value = \"10.8.8.8\"\n",
"category = \"Network activity\"\n",
"to_ids = False\n",
"\n",
"# Attribute data already defined\n",
"attribute = MISPAttribute()\n",
"attribute.type = attr_type\n",
"attribute.value = value\n",
"attribute.category = category\n",
"attribute.to_ids = to_ids\n",
"\n",
"proposal = misp.add_attribute_proposal(event_id, attribute)\n",
"print(proposal.id, proposal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Other things on proposals"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"proposal = misp.get_attribute_proposal(1)\n",
2019-04-11 23:14:16 +02:00
"print(proposal.to_json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"proposal = misp.accept_attribute_proposal(1)\n",
2019-04-11 23:14:16 +02:00
"print(proposal)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"proposal = misp.discard_attribute_proposal(2)\n",
2019-04-11 23:14:16 +02:00
"print(proposal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Propose change to attribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPShadowAttribute\n",
"\n",
"proposal = MISPShadowAttribute()\n",
"proposal.type = 'ip-dst'\n",
"proposal.category = 'External analysis'\n",
"proposal.to_ids = False\n",
"\n",
"attribute = misp.update_attribute_proposal(attribute_to_change.id, proposal)\n",
"print(attribute.to_json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attribute = misp.update_attribute_proposal(attribute_to_change.id, {'to_ids': False, 'comment': \"This is crap\"})\n",
"print(attribute.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Update existing event"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPAttribute, MISPObject\n",
"\n",
"attr_type = \"ip-src\"\n",
"value = \"20.8.8.8\"\n",
"category = \"Network activity\"\n",
"to_ids = False\n",
"\n",
"# Attribute data already defined\n",
"attribute = MISPAttribute()\n",
"attribute.type = attr_type\n",
"attribute.value = value\n",
"attribute.category = category\n",
"attribute.to_ids = to_ids\n",
"\n",
"# New Python 3.6 API\n",
"event = misp.get(event_id)\n",
"\n",
"## Add the attribute to the event\n",
"event.add_attribute(**attribute)\n",
"event.add_attribute(type='domain', value='circl.lu', disable_correlation=True)\n",
"\n",
"mispObject = MISPObject('file')\n",
"mispObject.add_attribute('filename', type='filename',\n",
" value='filename2.exe',\n",
" Tag=[{'name': 'tlp:white'}])\n",
"\n",
"event.add_object(mispObject)\n",
"\n",
"## Push the updated event to MISP\n",
"event_dict = misp.update_event(event)\n",
"print(event_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sightings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.sighting(value=event.attributes[1].value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.sighting_list(event.attributes[1].id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Direct call, no validation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"misp.direct_call('attributes/add/9', {'type': 'ip-dst', 'value': '8.11.8.8'})"
2019-04-11 23:14:16 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.direct_call('events')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Admin Stuff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_sharing_groups()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_users_list()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-09-16 18:30:53 +02:00
"misp.add_user({'email': 'bar@foo.de'})"
2019-04-11 23:14:16 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Organisations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_organisations_list()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Roles"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_roles_list()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Feeds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_feeds_list()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.cache_feeds_all()"
2018-06-25 11:28:40 +02:00
]
2019-09-16 18:30:53 +02:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2018-06-20 09:29:08 +02:00
}
],
"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",
2019-09-11 14:20:55 +02:00
"version": "3.7.3"
2018-06-20 09:29:08 +02:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}