From afcaa6ca783fd2a1b42666e59715a71b01786ba5 Mon Sep 17 00:00:00 2001 From: dev-aditya-hub Date: Thu, 29 Oct 2026 21:10:41 +0000 Subject: [PATCH] Document that include_similar is not bounded by the date window. Closes #1729 The cluster expansion in the Cowrie session view deliberately ignores start_date and end_date, but nothing said so. The help text for include_similar now states it, and both date fields point back at the exception, so it is visible from either side of the API docs. Because commands, sources and credentials are aggregated after the expansion, out-of-window data reaches them too, even without include_session_data. The help text says that as well. Three tests pin the behaviour, which the suite did not cover before: the expansion crossing the window, the aggregated fields crossing it, and an empty window staying empty because the expansion has no in-window session to take its clusters from. No behaviour change. --- api/serializers/cowrie_session.py | 15 ++++- tests/api/views/test_cowrie_session_view.py | 70 ++++++++++++++++++++- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/api/serializers/cowrie_session.py b/api/serializers/cowrie_session.py index 3280a155..4d67ab7a 100644 --- a/api/serializers/cowrie_session.py +++ b/api/serializers/cowrie_session.py @@ -29,7 +29,10 @@ class CowrieSessionRequestSerializer(serializers.Serializer): "When `true`, expands the result to include all sessions that executed command sequences " "belonging to the same cluster(s) as command sequences found in the initial query result. " "Requires CLUSTER_COWRIE_COMMAND_SEQUENCES enabled in configuration. " - "Only allowed together with `query` or `id`." + "Only allowed together with `query` or `id`. " + "Sessions added this way are not bounded by the date window, so the response, " + "including the aggregated `commands`, `sources` and `credentials`, " + "can contain data from outside `start_date` and `end_date`." ), ) include_credentials = serializers.BooleanField( @@ -39,10 +42,16 @@ class CowrieSessionRequestSerializer(serializers.Serializer): required=False, default=False, help_text="When `true`, includes detailed information about matching Cowrie sessions." ) start_date = serializers.DateField( - format="%Y-%m-%d", required=False, allow_null=True, help_text="Only sessions occurring on or after this date (YYYY-MM-DD)." + format="%Y-%m-%d", + required=False, + allow_null=True, + help_text="Only sessions occurring on or after this date (YYYY-MM-DD). Does not apply to sessions added by `include_similar`.", ) end_date = serializers.DateField( - format="%Y-%m-%d", required=False, allow_null=True, help_text="Only sessions occurring on or before this date (YYYY-MM-DD)." + format="%Y-%m-%d", + required=False, + allow_null=True, + help_text="Only sessions occurring on or before this date (YYYY-MM-DD). Does not apply to sessions added by `include_similar`.", ) def validate_id(self, value: str) -> str: diff --git a/tests/api/views/test_cowrie_session_view.py b/tests/api/views/test_cowrie_session_view.py index cf846993..e112f291 100644 --- a/tests/api/views/test_cowrie_session_view.py +++ b/tests/api/views/test_cowrie_session_view.py @@ -1,7 +1,10 @@ +from datetime import datetime +from hashlib import sha256 + from django.test import override_settings from rest_framework.test import APIClient -from greedybear.models import IOC, CowrieSession, Credential, IocType +from greedybear.models import IOC, CommandSequence, CowrieSession, Credential, IocType from tests import CustomTestCase @@ -518,3 +521,68 @@ def test_include_similar_with_id_is_allowed(self): response = self.client.get("/api/cowrie_session?id=ffffffffffff&include_similar=true") self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data["sources"]), 2) + + def test_include_similar_is_not_bounded_by_the_date_window(self): + """Sessions added by include_similar are not bounded by the date window. + + The expansion is seeded from the sessions inside the window, then adds + every session in their cluster(s), whenever it was seen. This is the + behaviour chosen in #1515, so it is pinned here on purpose. + """ + response = self.client.get( + f"/api/cowrie_session?query={self.ioc_3.name}&include_session_data=true&include_similar=true&start_date=2026-01-15&end_date=2026-01-25" + ) + + self.assertEqual(response.status_code, 200) + self.assertEqual( + sorted(session["time"][:10] for session in response.data["sessions"]), + ["2026-01-10", "2026-01-20", "2026-01-30"], + ) + + def test_include_similar_adds_nothing_when_the_window_is_empty(self): + """A window that matches nothing stays empty. + + The expansion needs an in-window session to take its cluster(s) from, + so an empty window cannot pull in the rest of a cluster. + """ + response = self.client.get( + f"/api/cowrie_session?query={self.ioc_3.name}&include_session_data=true&include_similar=true&start_date=2026-03-01&end_date=2026-03-05" + ) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data["sessions"], []) + self.assertEqual(response.data["commands"], []) + self.assertEqual(response.data["sources"], []) + + def test_include_similar_adds_out_of_window_data_to_the_aggregated_fields(self): + """commands, sources and credentials are aggregated after the expansion. + + They are built from the expanded set, so they carry data from outside + the window too, even without include_session_data. + """ + out_of_window_commands = CommandSequence.objects.create( + first_seen=self.current_time, + last_seen=self.current_time, + commands=["uname -a"], + commands_hash=sha256(b"uname -a").hexdigest(), + cluster=self.command_sequence_3.cluster, + ) + out_of_window_session = CowrieSession.objects.create( + session_id=int("cccccccccccc", 16), + source=self.ioc_2, + start_time=datetime(2026, 3, 15, 12, 0, 0), + duration=30.0, + login_attempt=True, + commands=out_of_window_commands, + ) + credential, _ = Credential.objects.get_or_create(username="admin", password="admin1729", protocol="") + out_of_window_session.credentials.add(credential) + + response = self.client.get( + f"/api/cowrie_session?query={self.ioc_3.name}&include_credentials=true&include_similar=true&start_date=2026-01-15&end_date=2026-01-25" + ) + + self.assertEqual(response.status_code, 200) + self.assertIn("uname -a", response.data["commands"]) + self.assertIn(self.ioc_2.name, response.data["sources"]) + self.assertIn("admin | admin1729", response.data["credentials"])