This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Parse MISP events to update attributes #14
Draft
0snap
wants to merge
1
commit into
main
Choose a base branch
from
story/ch5686
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,33 @@ def publish_sightings(outq): | |
lock.release() | ||
|
||
|
||
def forward_raw(msg_str, inq): | ||
"""Gracefully tries to parse and forward a message string. If the message | ||
cannot be parsed, an error is logged and None is returned. | ||
@param msg_str The message (JSON string) to forward | ||
@param inq The queue to forward successfully parsed message to | ||
""" | ||
global logger | ||
try: | ||
msg = json.loads(msg_str) | ||
except Exception as e: | ||
logger.error(f"Error decoding message {msg_str}: {e}") | ||
return | ||
all_intel = [] | ||
if msg.get("Attribute", None): | ||
intel = map_to_internal(msg["Attribute"], msg.get("action", None), logger) | ||
if intel: | ||
all_intel.append(intel) | ||
elif msg.get("Event", None) and msg.get("action", None) == "delete": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that you are preparing for MISP/MISP#4450 here. While you're at it, how about logging actions other than When we add an event, I think we don't have to do anything because we get all the contained attributes as well. Is that correct? Then the only other action left is updating an event. I haven't check this, but I would assume that event updates don't matter for Threat Bus right now, because at this point we only care about attribute updates. |
||
# TODO: find all deleted attributes, add to all_intel | ||
# See https://github.com/MISP/MISP/issues/4450 | ||
pass | ||
if not all_intel: | ||
return | ||
for intel in all_intel: | ||
inq.put(intel) | ||
|
||
|
||
def receive_kafka(kafka_config, inq): | ||
"""Binds a Kafka consumer to the the given host/port. Forwards all received messages to the inq. | ||
@param kafka_config A configuration object for Kafka binding | ||
|
@@ -76,19 +103,7 @@ def receive_kafka(kafka_config, inq): | |
if message.error(): | ||
logger.error(f"Kafka error: {message.error()}") | ||
continue | ||
try: | ||
msg = json.loads(message.value()) | ||
except Exception as e: | ||
logger.error(f"Error decoding Kafka message: {e}") | ||
continue | ||
if not msg.get("Attribute", None): | ||
logger.debug("Skipping message without MISP Attribute") | ||
continue | ||
intel = map_to_internal(msg["Attribute"], msg.get("action", None), logger) | ||
if not intel: | ||
logger.debug(f"Discarding unparsable intel {msg['Attribute']}") | ||
else: | ||
inq.put(intel) | ||
forward_raw(message.value(), inq) | ||
|
||
|
||
def receive_zmq(zmq_config, inq): | ||
|
@@ -99,8 +114,8 @@ def receive_zmq(zmq_config, inq): | |
|
||
socket = zmq.Context().socket(zmq.SUB) | ||
socket.connect(f"tcp://{zmq_config['host']}:{zmq_config['port']}") | ||
# TODO: allow reception of more topics, i.e. handle events. | ||
socket.setsockopt(zmq.SUBSCRIBE, b"misp_json_attribute") | ||
socket.setsockopt(zmq.SUBSCRIBE, b"misp_json_event") | ||
poller = zmq.Poller() | ||
poller.register(socket, zmq.POLLIN) | ||
|
||
|
@@ -109,18 +124,7 @@ def receive_zmq(zmq_config, inq): | |
if socket in socks and socks[socket] == zmq.POLLIN: | ||
raw = socket.recv() | ||
_, message = raw.decode("utf-8").split(" ", 1) | ||
try: | ||
msg = json.loads(message) | ||
except Exception as e: | ||
logger.error(f"Erro decoding message {message}: {e}") | ||
continue | ||
if not msg.get("Attribute", None): | ||
continue | ||
intel = map_to_internal(msg["Attribute"], msg.get("action", None), logger) | ||
if not intel: | ||
logger.debug(f"Discarding unparsable intel {msg['Attribute']}") | ||
else: | ||
inq.put(intel) | ||
forward_raw(message, inq) | ||
|
||
|
||
@threatbus.app | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.