PyMISP/examples/misp2clamav.py

53 lines
1.4 KiB
Python
Raw Normal View History

2017-03-27 16:50:56 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
2017-03-27 17:43:11 +02:00
#
2017-03-27 16:50:56 +02:00
# Export file hashes from MISP to ClamAV hdb file
import sys
2017-03-27 17:43:11 +02:00
from pymisp import PyMISP, MISPAttribute
2017-03-27 16:50:56 +02:00
from keys import misp_url, misp_key
2017-03-27 17:43:11 +02:00
2017-03-27 16:50:56 +02:00
def init_misp():
global mymisp
mymisp = PyMISP(misp_url, misp_key)
2017-03-27 17:43:11 +02:00
2017-03-27 16:50:56 +02:00
def echeck(r):
if r.get('errors'):
if r.get('message') == 'No matches.':
return
else:
print(r['errors'])
sys.exit(1)
2017-03-27 17:43:11 +02:00
2017-03-27 16:50:56 +02:00
def find_hashes(htype):
2017-03-27 17:43:11 +02:00
r = mymisp.search(controller='attributes', type_attribute=htype)
2017-03-27 16:50:56 +02:00
echeck(r)
2017-03-27 17:43:11 +02:00
if not r.get('response'):
return
for a in r['response']['Attribute']:
attribute = MISPAttribute(mymisp.describe_types)
2018-02-26 11:25:14 +01:00
attribute.from_dict(**a)
2017-03-27 17:43:11 +02:00
if '|' in attribute.type and '|' in attribute.value:
c, value = attribute.value.split('|')
comment = '{} - {}'.format(attribute.comment, c)
else:
comment = attribute.comment
value = attribute.value
mhash = value.replace(':', ';')
mfile = 'MISP event {} {}'.format(a['event_id'], comment.replace(':', ';').replace('\r', '').replace('\n', ''))
print('{}:*:{}:73'.format(mhash, mfile))
2017-03-27 16:50:56 +02:00
if __name__ == '__main__':
init_misp()
find_hashes('md5')
find_hashes('sha1')
find_hashes('sha256')
find_hashes('filename|md5')
find_hashes('filename|sha1')
find_hashes('filename|sha256')