From af636812fd8d7da0c7293e80713a8c1fd7a90158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 18 Jul 2017 10:55:49 +0200 Subject: [PATCH] Fix get_attachment. Fix #105 --- pymisp/api.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pymisp/api.py b/pymisp/api.py index bd00517..5ddd387 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -993,15 +993,22 @@ class PyMISP(object): session = self.__prepare_session(async_implemented=(async_callback is not None)) return self.__query(session, 'restSearch/download', query, controller, async_callback) - def get_attachment(self, event_id): - """Get attachement of an event (not sample) + def get_attachment(self, attribute_id): + """Get an attachement (not a malware sample) by attribute ID. + Returns the attachment as a bytestream, or a dictionary containing the error message. - :param event_id: Event id from where the attachements will be fetched + :param attribute_id: Attribute ID to fetched """ - attach = urljoin(self.root_url, 'attributes/downloadAttachment/download/{}'.format(event_id)) + attach = urljoin(self.root_url, 'attributes/downloadAttachment/download/{}'.format(attribute_id)) session = self.__prepare_session() response = session.get(attach) - return self._check_response(response) + try: + response.json() + # The query fails, response contains a json blob + return self._check_response(response) + except ValueError: + # content contains the attachment in binary + return response.content def get_yara(self, event_id): to_post = {'request': {'eventid': event_id, 'type': 'yara'}}