The quick and dirty way:
sudo pip3 install pymisp
The clean approach as user:
pip3 install --user pymisp
git clone https://github.com/MISP/PyMISP.git
cd PyMISP
virtualenv -p python3 pymisp-env
source pymisp-env/bin/activate
pip install -e .
We assume you're in a virtual environment
If you want to follow along this workshop on your computer, this is the way to go:
pip install jupyter
cd docs/tutorial
jupyter-notebook
This page aims to give recommandations about how to efficiently use the pymisp
library.
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.
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 someting missing.
MISPEvent
is the main class to use when you want to create/update events on a MISP instance.
from pymisp import MISPEvent
event = MISPEvent()
event.info = 'This is my new MISP event' # Required
event.distribution = 0 # Optional, defaults to MISP.default_event_distribution in MISP config
event.threat_level_id = 2 # Optional, defaults to MISP.default_event_threat_level in MISP config
event.analysis = 1 # Optional, defaults to 0 (initial analysis)
print(event.to_json())
First example of helper aiming to make your life easier.
event.add_tag('tlp:white')
print(event.to_json())
The date can be in many different formats. This helper makes sure it normalises it in a way that will be understood by your MISP instance.
# As text
event.set_date('2018-04-13')
print('Simple', event.date)
# Some weird text format (anything supported by dateparse will work)
event.set_date('Sat Oct 11 00:13:46 2017')
print('Messy', event.date)
# datetime.date
from datetime import date
d = date.today()
print(type(d))
event.set_date(d)
print(event.date)
# datetime.datetime => MISP expects a day, so the hour will be droped.
from datetime import datetime
d = datetime.now()
print(type(d))
event.set_date(d)
print(event.date)
More usefull things: adding attributes to an event.
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.
The parameters are the following:
attribute = event.add_attribute('ip-dst', '8.8.8.8') # Minimal parameters
print(type(attribute))
print(attribute.to_json())
This is the was to pass other parameters
attribute_second = event.add_attribute('ip-dst', '8.8.8.9', disable_correlation=True)
print(attribute_second.to_json())
Every parameter can be modified in a pythonic way.
attribute.to_ids = False
print(attribute.to_json())
# Using the list of attributes in the event
event.attributes[0].add_tag('tlp:green')
# ... or the variable we got from `add_attribute`
attribute_second.add_tag('tlp:amber')
print(attribute_second.to_json())
Important note: the default approach to delete on MISP is to do a soft delete (meaning the attribue 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.
The delete method will set the default parameter of the attribute to True
.
attribute.delete()
print(attribute.to_json())
Same idea: you can set the published flag from the api
event.publish()
print(event.published)
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.
Note: you can use your own templates, we will see how later
from pymisp import MISPObject
circl_attr = event.add_attribute('ip-dst', '149.13.33.14')
misp_object = MISPObject('domain-ip', standalone=False, default_attributes_parameters=circl_attr)
# Notes:
# * standalone: this object will be attached to a MISPEvent, so the references will be in the dump
# * default_attributes_parameters: keep parameters from a MISPAttribute (useful when expanding a existing one)
misp_object.comment = 'My Fancy new object'
obj_attr = misp_object.add_attribute('domain', value='circl.lu')
obj_attr.add_tag('tlp:green')
misp_object.add_attribute('ip', value='149.13.33.14')
misp_object.add_attribute('first-seen', value='2018-04-11')
misp_object.add_attribute('last-seen', value='2018-06-11')
event.add_object(misp_object)
print(event.to_json())
You can also add the object directly in a misp event this way
from pymisp import MISPObject
misp_object = event.add_object(name='domain-ip', comment='My Fancy new object, in one line')
obj_attr = misp_object.add_attribute('domain', value='circl.lu')
obj_attr.add_tag('tlp:green')
misp_object.add_attribute('ip', value='149.13.33.14')
misp_object.add_attribute('first-seen', value='2018-04-11')
misp_object.add_attribute('last-seen', value='2018-06-11')
misp_object.add_reference(circl_attr.uuid, 'related-to', 'Expanded with passive DNS entry')
print(event.to_json())
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.
Note: This requires a few more dependencies to get the full power of the script:
lief
to extract indicators out of PE/ELF/MachO files, and soon Android binaries.python-magic
to get the mime typepydeep
to compute the ssdeep of the binary whenever possiblepip install lief python-magic git+https://github.com/kbandla/pydeep.git
from pymisp.tools import FileObject
file_obj = FileObject(filepath='../../tests/viper-test-files/test_files/EICAR.com', standalone=False)
print(file_obj.to_json())
event.add_object(file_obj)
print(event.to_json())
This helper is meant to be used when you alreadu have a script that does the mapping between your own code, and the MISPObject template.
from pymisp.tools import GenericObjectGenerator
attributeAsDict = [{'script': ':(){ :|:& };:', 'comment': 'Forkbomb'},
{'language': {'value': 'Bash', 'to_ids': False, 'disable_correlation': True}},
{'filename': {'value': 'forkbomb.sh', 'to_ids': True}},
{'state': 'Malicious'}]
misp_object = GenericObjectGenerator('script', strict=True)
misp_object.generate_attributes(attributeAsDict)
print(misp_object.to_json())
from pymisp.tools import GenericObjectGenerator
attributeAsDict = [{'MyCoolAttribute': {'value': 'critical thing', 'type': 'text'}},
{'MyCoolerAttribute': {'value': 'even worse', 'type': 'text'}}]
misp_object = GenericObjectGenerator('my-cool-template', strict=True) # This is supposed to fail due to the strict parameter
misp_object.generate_attributes(attributeAsDict)
print(misp_object.to_json())
from pymisp.tools import GenericObjectGenerator
attributeAsDict = [{'MyCoolAttribute': {'value': 'critical thing', 'type': 'text'}},
{'MyCoolerAttribute': {'value': 'even worse', 'type': 'text'}}]
misp_object = GenericObjectGenerator('my-cool-template')
misp_object.generate_attributes(attributeAsDict)
print(misp_object.to_json())
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)
user_defined_obj = MISPObject(name='test_object_template', strict=True, misp_objects_path_custom='../../tests/mispevent_testfiles')
user_defined_obj.add_attribute('member3', value='foo')
user_defined_obj.add_attribute('member1', value='baz')
print(user_defined_obj.to_json())
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.
This is pretty much a pain to use as-is.
So there is an helper for that!
sample = file_obj.get_attributes_by_relation('malware-sample')[0]
print(sample)
print('File name --->', sample.malware_filename)
print(sample.malware_binary)
print('Content of the malware (in bytes) ----->', sample.malware_binary.getvalue())
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.
If it is anything else, it will just generate the the file object.
from pymisp.tools import make_binary_objects
file_obj, bin_obj, sections = make_binary_objects(filepath='../../tests/viper-test-files/test_files/whoami.exe', standalone=False)
event.add_object(file_obj)
if bin_obj:
event.add_object(bin_obj)
for s in sections:
event.add_object(s)
The references are also set by default by this method.
print(bin_obj.uuid)
print(bin_obj.references[0].to_json())
print(event.to_json())
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.
print(event.to_json())
We were creating new events, but you will also want to update an existing one.
from pymisp import MISPEvent
existing_event = MISPEvent()
existing_event.load_file('../../tests/mispevent_testfiles/existing_event.json')
print(existing_event.attributes[0])
print(existing_event.attributes[0].tags)
print(existing_event.attributes[0].timestamp)
print(existing_event.attributes[0].to_json())
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 makting every attributes as new.
PyMISP got you covered.
existing_event.attributes[0].add_tag('tlp:white')
print(existing_event.attributes[0].to_json())
from pymisp import MISPEvent, MISPObject
from pymisp import PyMISP
event = MISPEvent()
event.info = 'This is my new MISP event' # Required
event.distribution = 0 # Optional, defaults to MISP.default_event_distribution in MISP config
event.threat_level_id = 2 # Optional, defaults to MISP.default_event_threat_level in MISP config
event.analysis = 1 # Optional, defaults to 0 (initial analysis)
mispObject = MISPObject('file')
mispObject.add_attribute('filename', type='filename',
value='filename.exe',
Tag=[{'name': 'tlp:amber'}])
event.add_object(mispObject)
# The URL of the MISP instance to connect to
misp_url = 'http://127.0.0.1:8080'
# Can be found in the MISP web interface under
# http://+MISP_URL+/users/view/me -> Authkey
misp_key = 'yB8DMS8LkfYYpcVX8bN2v7xwDZDMp4bpW0sNqNGj'
# Should PyMISP verify the MISP certificate
misp_verifycert = False
misp = PyMISP(misp_url, misp_key, misp_verifycert)
res = misp.add_event(event)
existing_event = MISPEvent()
existing_event.load(res)
mispObject = MISPObject('file')
mispObject.add_attribute('filename', type='filename',
value='filename2.exe',
Tag=[{'name': 'tlp:white'}])
existing_event.add_object(mispObject)
print(existing_event.to_json())
res = misp.update(existing_event)
existing_event = MISPEvent()
existing_event.load(res)
print(existing_event.to_json())
from pymisp import MISPEvent, MISPObject
from pymisp import PyMISP
event = MISPEvent()
event.info = 'This is my new MISP event' # Required
event.distribution = 0 # Optional, defaults to MISP.default_event_distribution in MISP config
event.threat_level_id = 2 # Optional, defaults to MISP.default_event_threat_level in MISP config
event.analysis = 1 # Optional, defaults to 0 (initial analysis)
mispObject = MISPObject('file')
mispObject.add_attribute('filename', type='filename',
value='filename.exe',
Tag=[{'name':'tlp:amber'}])
event.add_object(mispObject)
# The URL of the MISP instance to connect to
misp_url = 'http://127.0.0.1:8080'
# Can be found in the MISP web interface under
# http://+MISP_URL+/users/view/me -> Authkey
misp_key = 'yB8DMS8LkfYYpcVX8bN2v7xwDZDMp4bpW0sNqNGj'
# Should PyMISP verify the MISP certificate
misp_verifycert = False
misp = PyMISP(misp_url, misp_key, misp_verifycert)
res = misp.add_event(event)
existing_event = MISPEvent()
existing_event.load(res)
print(existing_event.to_json())
from pymisp import MISPOrganisation
orgc = MISPOrganisation()
orgc.name = 'bazbaz'
orgc.id = 15
orgc.uuid = '5888a98d-a7e8-4183-94bb-4d19950d210f'
# NOTE: Pushing this object will only work if the user has sync right (if not, the orgc key will be ignored)
event.Orgc = orgc
print(event.to_json())