Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge multiple intelligence attributes if present #3113

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion timesketch/lib/analyzers/yetiindicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,31 @@ def add_intelligence_entry(
self._intelligence_attribute["data"].append(intel)
self._intelligence_refs.add((match_in_sketch, uri))

def _merge_intelligence_attributes(self, attribute_values):
"""Merges multiple intelligence values that might have been stored."""
data = []
existing_refs = set()
for value in attribute_values:
for ioc in value['data']:
if ioc['externalURI'] in existing_refs:
continue
data.append(ioc)
existing_refs.add(ioc['externalURI'])
return {"data": data}

def get_intelligence_attribute(self) -> Tuple[Dict, Set[Tuple[str, str]]]:
"""Fetches the intelligence attribute from the database."""
try:
intelligence_attribute = self.sketch.get_sketch_attributes("intelligence")

# In some cases, the intelligence attribute may be split into
# multiple "values" due tu race conditions. Merge them if that's
# the case. The API will return only the first value if the list
# has 1 element, so this check is necessary.
if isinstance(intelligence_attribute, list):
intelligence_attribute = self._merge_intelligence_attributes(
intelligence_attribute)

refs = {
(ioc["ioc"], ioc["externalURI"])
for ioc in intelligence_attribute["data"]
Expand All @@ -327,9 +348,10 @@ def save_intelligence(self) -> None:
if (ioc["ioc"], ioc["externalURI"]) not in self._intelligence_refs:
self._intelligence_attribute["data"].append(ioc)

attribute_string = json.dumps(self._intelligence_attribute)
self.sketch.add_sketch_attribute(
"intelligence",
[json.dumps(self._intelligence_attribute)],
[attribute_string],
ontology="intelligence",
overwrite=True,
)
Expand Down
Loading