PyMISP/docs/tutorial/FullOverview.ipynb

1504 lines
37 KiB
Plaintext

{
"cells": [
{
"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",
"pip3 install jupyter\n",
"cd docs/tutorial\n",
"jupyter-notebook\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using the PyMISP objects\n",
"\n",
"This page aims to give recommendations about how to efficiently use the `pymisp` library.\n",
"\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",
"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."
]
},
{
"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."
]
},
{
"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": [
"## Tag Event\n",
"\n",
"First example of helper aiming to make your life easier."
]
},
{
"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": [
"## Set the Event date\n",
"\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# As text\n",
"event.set_date('2018-04-13')\n",
"print('Simple', event.date)\n",
"\n",
"# Some weird text format (anything supported by dateparse will work)\n",
"event.set_date('Sat Oct 11 00:13:46 2017')\n",
"print('Messy', event.date)\n",
"\n",
"# datetime.date\n",
"from datetime import date\n",
"d = date.today()\n",
"print(type(d))\n",
"event.set_date(d)\n",
"print(event.date)\n",
"\n",
"# datetime.datetime => MISP expects a day, so the hour will be dropped.\n",
"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": [
"## Add Attribute to event\n",
"\n",
"More useful things: adding attributes to an event.\n",
"\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"
]
},
{
"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": [
"## Set parameters (inline)\n",
"\n",
"This is the way to pass other parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attribute_second = event.add_attribute('ip-dst', '8.8.8.9', disable_correlation=True)\n",
"\n",
"print(attribute_second.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Modify existing attribute\n",
"\n",
"Every parameter can be modified in a pythonic way."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attribute.to_ids = False\n",
"\n",
"print(attribute.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tag Attribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 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",
"attribute_second.add_tag('tlp:amber')\n",
"\n",
"print(attribute_second.to_json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Soft delete attribute\n",
"\n",
"**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",
"\n",
"The delete method will set the default parameter of the attribute to `True`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attribute.delete()\n",
"print(attribute.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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)"
]
},
{
"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)"
]
},
{
"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"
]
},
{
"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",
"# * default_attributes_parameters: keep parameters from a MISPAttribute (useful when expanding a existing one) \n",
"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": [
"## Short version to add an object to a MISPEvent\n",
"\n",
"You can also add the object directly in a misp event this way"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPObject\n",
"\n",
"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",
"\n",
"misp_object.add_attributes('ip', {'value': '10.8.8.8', 'to_ids': False}, '10.9.8.8')\n",
"\n",
"\n",
"misp_object.add_reference(obj_attr.uuid, 'related-to', 'Expanded with passive DNS entry')\n",
"\n",
"print(event.to_json())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## New first/last seen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPObject\n",
"\n",
"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",
"\n",
"misp_object.first_seen = '2018-04-11'\n",
"misp_object.last_seen = '2018-06-11T23:27:40.23356+07:00'\n",
"\n",
"print(misp_object.last_seen)\n",
"\n",
"misp_object.add_attributes('ip', {'value': '10.8.8.8', 'to_ids': False}, '10.9.8.8')\n",
"\n",
"\n",
"misp_object.add_reference(obj_attr.uuid, 'related-to', 'Expanded with passive DNS entry')\n",
"\n",
"print(event.to_json(indent=2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Helpers for MISPObjects \n",
"\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",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event.add_object(file_obj)\n",
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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",
"\n",
"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."
]
},
{
"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())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User defined objects"
]
},
{
"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())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use locally defined object 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"user_defined_obj = MISPObject(name='test_object_template', strict=True, misp_objects_path_custom='../../tests/mispevent_testfiles')\n",
"\n",
"user_defined_obj.add_attribute('member3', value='foo')\n",
"user_defined_obj.add_attribute('member1', value='baz')\n",
"\n",
"print(user_defined_obj.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"The references are also set by default by this method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(bin_obj.uuid)\n",
"print(bin_obj.references[0].to_json())\n",
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generate a feed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import MISPEvent, MISPOrganisation\n",
"from pymisp.tools import feed_meta_generator\n",
"from pathlib import Path\n",
"import json\n",
"\n",
"out_dir = Path('feed_test')\n",
"out_dir.mkdir(exist_ok=True)\n",
"\n",
"org = MISPOrganisation()\n",
"org.name = \"Test Org\"\n",
"org.uuid = \"972360d2-2c96-4004-937c-ba010d03f925\"\n",
"\n",
"event = MISPEvent()\n",
"\n",
"event.info = 'This is my new MISP event for a feed'\n",
"event.distribution = 1\n",
"event.Orgc = org\n",
"event.add_attribute('ip-dst', \"8.8.8.8\")\n",
"\n",
"feed_event = event.to_feed()\n",
"\n",
"with (out_dir / f'{event.uuid}.json').open('w') as f:\n",
" json.dump(feed_event, f)\n",
"\n",
"\n",
"feed_meta_generator(out_dir)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ls feed_test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!cat feed_test/manifest.json\n",
"\n",
"!echo ''\n",
"\n",
"!cat feed_test/hashes.csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!rm feed_test/*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Update an existing MISPEvent\n",
"\n",
"We were creating new events, but you will also want to update an existing one."
]
},
{
"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": [
"## Edit, removes the timestamp when exporting\n",
"\n",
"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",
"\n",
"PyMISP got you covered."
]
},
{
"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())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 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": [
"# Getting the API key (automatically generated on the training VM)"
]
},
{
"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",
"misp_url = 'https://127.0.0.1:8443/'\n",
"# Can be found in the MISP web interface under \n",
"# http://+MISP_URL+/users/view/me -> Authkey\n",
"misp_key = 'd6OmdDFvU3Seau3UjwvHS1y3tFQbaRNhJhDX0tjh'\n",
"# Should PyMISP verify the MISP certificate\n",
"misp_verifycert = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymisp import PyMISP\n",
"\n",
"misp = 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."
]
},
{
"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",
" value='filename.exe',\n",
" Tag=[{'name': 'tlp:amber'}])\n",
"\n",
"event.add_object(mispObject)\n",
"\n",
"print(misp)\n",
"existing_event = misp.add_event(event, pythonify=True)\n",
"print(existing_event)\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_event(existing_event)\n",
"existing_event = MISPEvent()\n",
"existing_event.load(res)\n",
"print(existing_event.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interacting with a MISP instance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating An Event"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Directly"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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",
"print(\"Event id: %s\" % 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",
"\n",
"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, pythonify=True)\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": [
"event_id = 9"
]
},
{
"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": [
"##### 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, pythonify=True)\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": [
"proposal = misp.get_attribute_proposal(1)\n",
"print(proposal.to_json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"proposal = misp.accept_attribute_proposal(1)\n",
"print(proposal)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"proposal = misp.discard_attribute_proposal(2)\n",
"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": [
"misp.direct_call('attributes/add/9', {'type': 'ip-dst', 'value': '8.11.8.8'})"
]
},
{
"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.sharing_groups()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.users()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.add_user({'email': 'bar@foo.de'})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Organisations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.organisations()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Roles"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.roles()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Feeds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.feeds(pythonify=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.cache_feeds_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}