2018-06-26 06:01:48 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using the API to interact with a remote MISP instance\n",
"\n",
"You can fetch a VM from here: https://www.circl.lu/misp-images/latest/, or connect to your dev instance.\n",
"\n",
"This box needs to be run in order to connect to the MISP instance and run the subsequent commands."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-11-22 14:29:07 +01:00
"from pymisp import ExpandedPyMISP, MISPEvent, MISPAttribute\n",
2018-06-26 06:01:48 +02:00
"\n",
"# The URL of the MISP instance to connect to\n",
"misp_url = 'http://127.0.0.1:8080'\n",
"# Can be found in the MISP web interface under \n",
"# http://+MISP_URL+/users/view/me -> Authkey\n",
2019-02-28 19:49:32 +01:00
"misp_key = 'aJAmQQoBhVL5jqUDSucIkPrEYIbFyW0wwQnxyBfc'\n",
2018-06-26 06:01:48 +02:00
"# Should PyMISP verify the MISP certificate\n",
2018-11-06 15:28:07 +01:00
"misp_verifycert = False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting the API key (automatically generated on the trainig VM)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
2018-06-26 06:01:48 +02:00
"\n",
2018-11-06 15:28:07 +01:00
"api_file = Path('apikey')\n",
"if api_file.exists():\n",
2018-11-06 17:53:10 +01:00
" misp_url = 'http://127.0.0.1'\n",
" misp_verifycert = False\n",
2018-11-06 15:28:07 +01:00
" with open(api_file) as f:\n",
" misp_key = f.read().strip()\n",
" print(misp_key)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Initialize PyMISP"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-11-22 14:29:07 +01:00
"misp = ExpandedPyMISP(misp_url, misp_key, misp_verifycert)"
2018-06-26 06:01:48 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Get the last events\n",
"\n",
"There are multiple definition for *last* in MISP.\n",
"\n",
"## Last *published* events"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"response = misp.search(publish_timestamp='2d')\n",
2018-06-26 06:01:48 +02:00
"\n",
2019-02-28 19:49:32 +01:00
"print (response)\n",
2018-06-26 06:01:48 +02:00
"events = []\n",
2019-02-28 19:49:32 +01:00
"for event in response:\n",
2018-06-26 06:01:48 +02:00
" me = MISPEvent()\n",
" me.load(event)\n",
" events.append(me)\n",
2018-11-22 14:29:07 +01:00
" \n",
2018-06-26 06:01:48 +02:00
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-11-22 14:29:07 +01:00
"events = misp.search(publish_timestamp=['120m', '100m'], pythonify=True)\n",
2018-06-26 06:01:48 +02:00
"\n",
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Last *updated* events"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"ts = int(datetime.now().timestamp())\n",
"\n",
"response = misp.search(timestamp=ts-36000)\n",
"\n",
"events = []\n",
2019-02-28 19:49:32 +01:00
"for event in response:\n",
2018-06-26 06:01:48 +02:00
" me = MISPEvent()\n",
" me.load(event)\n",
" events.append(me)\n",
"\n",
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## On an interval"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2018-11-22 14:29:07 +01:00
"from datetime import datetime\n",
2018-06-26 06:01:48 +02:00
"ts = int(datetime.now().timestamp())\n",
"\n",
2018-11-22 14:29:07 +01:00
"events = misp.search(timestamp=[ts-3600, ts], pythonify=True)\n",
2018-06-26 06:01:48 +02:00
"\n",
"for e in events:\n",
2018-11-22 14:29:07 +01:00
" print(e)"
2018-06-26 06:01:48 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Get the last attributes\n",
"\n",
"## Last *published* attributes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"response = misp.search(controller='attributes', publish_timestamp='1d')\n",
2018-06-26 06:01:48 +02:00
"\n",
"attributes = []\n",
2019-02-28 19:49:32 +01:00
"for attribute in response['Attribute']:\n",
2018-06-26 06:01:48 +02:00
" ma = MISPAttribute()\n",
" ma.from_dict(**attribute)\n",
" attributes.append(ma)\n",
"\n",
"for a in attributes:\n",
" print(a.event_id, a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"response = misp.search(controller='attributes', publish_timestamp=['2d', '1h'])\n",
2018-06-26 06:01:48 +02:00
"\n",
"attributes = []\n",
2019-02-28 19:49:32 +01:00
"for attribute in response['Attribute']:\n",
2018-06-26 06:01:48 +02:00
" ma = MISPAttribute()\n",
" ma.from_dict(**attribute)\n",
" attributes.append(ma)\n",
"\n",
"for a in attributes:\n",
" print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Last *updated* attributes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"ts = int(datetime.now().timestamp())\n",
"\n",
"response = misp.search(controller='attributes', timestamp=ts - 36000)\n",
"\n",
"attributes = []\n",
2019-02-28 19:49:32 +01:00
"for attribute in response['Attribute']:\n",
2018-06-26 06:01:48 +02:00
" ma = MISPAttribute()\n",
" ma.from_dict(**attribute)\n",
" attributes.append(ma)\n",
"\n",
"for a in attributes:\n",
" print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fast search at index event level\n",
"\n",
"You have multiple ways to search for different values in MISP. Searching in the medadata of the events is very fast and if generally the recommended approach if your query returns lots of events."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = misp.search_index(eventinfo='Cobalt Strike')\n",
"\n",
"events = []\n",
2019-02-28 19:49:32 +01:00
"for event in response:\n",
2018-06-26 06:01:48 +02:00
" me = MISPEvent()\n",
" me.from_dict(**event)\n",
" events.append(me)\n",
"\n",
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('No attributes are in the event', events[0].attributes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"response = misp.search_index(tags='malware_classification:malware-category=\"Ransomware\"')\n",
2018-06-26 06:01:48 +02:00
"\n",
"events = []\n",
2019-02-28 19:49:32 +01:00
"for event in response:\n",
2018-06-26 06:01:48 +02:00
" me = MISPEvent()\n",
" me.from_dict(**event)\n",
" events.append(me)\n",
"\n",
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"response = misp.search_index(timestamp='1h')\n",
"\n",
"events = []\n",
2019-02-28 19:49:32 +01:00
"for event in response:\n",
2018-06-26 06:01:48 +02:00
" me = MISPEvent()\n",
" me.from_dict(**event)\n",
" events.append(me)\n",
"\n",
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"events[0].id"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event = MISPEvent()\n",
2019-02-28 19:49:32 +01:00
"#event.load(misp.get(events[0].id))\n",
"print (misp.get(events[0].id))\n",
"#print(event.to_json())"
2018-06-26 06:01:48 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Search indicators"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"complex_query = misp.build_complex_query(or_parameters=['59.157.4.2', 'hotfixmsupload.com', '8.8.8.8'])\n",
2018-11-22 14:29:07 +01:00
"events = misp.search(value=complex_query, pythonify=True)\n",
2018-06-26 06:01:48 +02:00
"\n",
"for e in events:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sightings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"misp.sighting(value=e.attributes[1].value)"
2018-06-26 06:01:48 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2019-02-28 19:49:32 +01:00
"misp.sighting_list(e.attributes[1].id)"
2018-06-26 06:01:48 +02:00
]
},
{
"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": [
"misp.add_user('bar@foo.de', 1, 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_organisations_list()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"misp.get_roles_list()"
]
},
{
"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()"
]
},
{
"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",
2018-11-20 01:39:20 +01:00
"version": "3.6.7"
2018-06-26 06:01:48 +02:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}