2016-11-18 10:28:00 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2016-11-18 12:54:58 +01:00
|
|
|
######
|
|
|
|
# TODO: DETECT DUPLICATE DATA
|
|
|
|
#####
|
|
|
|
|
2016-11-18 10:28:00 +01:00
|
|
|
import pymisp
|
2016-11-18 12:54:58 +01:00
|
|
|
import tempfile
|
|
|
|
import os
|
2016-11-18 10:28:00 +01:00
|
|
|
|
|
|
|
from opentaxii.signals import (
|
|
|
|
CONTENT_BLOCK_CREATED, INBOX_MESSAGE_CREATED
|
|
|
|
)
|
|
|
|
|
|
|
|
## CONFIG
|
|
|
|
|
2016-11-18 12:54:58 +01:00
|
|
|
CONFIG = {
|
|
|
|
"MISP_URL" : "[URL]",
|
|
|
|
"MISP_API" : "[APIKEY]",
|
|
|
|
}
|
|
|
|
|
|
|
|
MISP = pymisp.PyMISP(
|
|
|
|
CONFIG["MISP_URL"],
|
|
|
|
CONFIG["MISP_API"],
|
|
|
|
)
|
|
|
|
|
2016-11-18 12:45:30 +01:00
|
|
|
def post_stix(manager, content_block, collection_ids, service_id):
|
2016-11-18 12:54:58 +01:00
|
|
|
'''
|
|
|
|
Callback function for when our taxii server gets new data
|
|
|
|
Will convert it to a MISPEvent and push to the server
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Create a temporary file to load STIX data from
|
|
|
|
f = tempfile.NamedTemporaryFile(delete=False, mode="w")
|
|
|
|
f.write(content_block.content)
|
|
|
|
f.close()
|
2016-11-18 10:28:00 +01:00
|
|
|
|
2016-11-18 12:54:58 +01:00
|
|
|
# Load the package
|
|
|
|
package = pymisp.tools.stix.load_stix(f.name)
|
2016-11-18 15:42:17 +01:00
|
|
|
|
|
|
|
# Check for duplicates
|
|
|
|
for attrib in package.attributes:
|
|
|
|
try:
|
|
|
|
if (0 != len(MISP.search_index(attrib.value)["response"])):
|
|
|
|
# It's a dupe!
|
|
|
|
package.attributes.remove(attrib)
|
|
|
|
except:
|
|
|
|
# idk, this is just in case pymisp does a weird
|
|
|
|
pass
|
|
|
|
|
2016-11-18 12:54:58 +01:00
|
|
|
# Delete that old temporary file
|
|
|
|
os.unlink(f.name)
|
2016-11-18 10:28:00 +01:00
|
|
|
|
2016-11-18 12:54:58 +01:00
|
|
|
# Push the event to MISP
|
|
|
|
# TODO: There's probably a proper method to do this rather than json_full
|
|
|
|
# But I don't wanna read docs
|
2016-11-18 15:42:53 +01:00
|
|
|
if (len(package.attributes) > 0):
|
|
|
|
MISP.add_event(package._json_full())
|
2016-11-18 10:28:00 +01:00
|
|
|
|
2016-11-18 12:54:58 +01:00
|
|
|
# Make TAXII call our push function whenever it gets new data
|
2016-11-18 10:28:00 +01:00
|
|
|
CONTENT_BLOCK_CREATED.connect(post_stix)
|