Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions greedybear/cronjobs/extraction/ioc_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def _merge_iocs(self, existing: IOC, new: IOC) -> IOC:
existing.destination_ports = sorted(set(existing.destination_ports + new.destination_ports))
existing.protocols = sorted(set(existing.protocols + new.protocols))
existing.cves = sorted(set(existing.cves + new.cves))
existing.days_seen = sorted(set(existing.days_seen) | set(new.days_seen))
existing.http_attack_types = sorted(set(existing.http_attack_types + new.http_attack_types))
existing.high_credential_reuse = existing.high_credential_reuse or new.high_credential_reuse
existing.ip_reputation = existing.ip_reputation or new.ip_reputation
Expand Down
2 changes: 2 additions & 0 deletions greedybear/cronjobs/extraction/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def iocs_from_hits(hits: Sequence[Hit], allow_non_global: bool = False) -> list[
ip_reputation=correct_ip_reputation(ip, next((h.get("ip_rep", "") for h in ip_hits if h.get("ip_rep")), ""), mass_scanner_ips),
autonomous_system=autonomous_system,
destination_ports=sorted(dest_ports),
# every day with a hit, not just the latest: event batches can span several days
days_seen=sorted({timestamp.date() for timestamp in timestamps}),
login_attempts=login_attempts,
attacker_country=attacker_country,
attacker_country_code=attacker_country_code,
Expand Down
46 changes: 46 additions & 0 deletions tests/greedybear/test_process_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,52 @@ def test_credential_protocol_matches_the_event_field_widths(self):
self.assertEqual(credential_width, EventSerializer().fields["protocol"].max_length)


class TestMultiDayBatchDaysSeen(CustomTestCase):
"""A batch can span several days, so days_seen must list every day with an event."""

ATTACKER = "193.32.162.44"

def setUp(self):
super().setUp()
self.user = make_user(username="multi_day_user")
self.api_source = make_api_source(self.user, name="MultiDaySource")
self.sensor = make_sensor(api_source=self.api_source, address="203.0.113.60", honeypot_software="Cowrie")

def _process(self, task_id, *days_ago):
batch = make_batch(self.api_source, task_id=task_id)
now = timezone.now().replace(microsecond=0)
for days in days_ago:
make_raw_event(batch, self.sensor, src_ip=self.ATTACKER, timestamp=now - timedelta(days=days))
process_incoming_event(self.api_source.pk, task_id)

@patch(PATCH_UPDATE_SCORES)
def test_new_ioc_gets_every_day_of_the_batch(self, mock_scores):
self._process("task-days-new", 5, 3, 3, 1)

ioc = IOC.objects.get(name=self.ATTACKER)
today = timezone.now().date()
self.assertEqual(ioc.days_seen, [today - timedelta(days=d) for d in (5, 3, 1)])
self.assertEqual(ioc.number_of_days_seen, 3)

@patch(PATCH_UPDATE_SCORES)
def test_later_batch_adds_its_days_to_the_stored_ones(self, mock_scores):
self._process("task-days-first", 2)
self._process("task-days-second", 6, 4)

ioc = IOC.objects.get(name=self.ATTACKER)
today = timezone.now().date()
self.assertEqual(ioc.days_seen, [today - timedelta(days=d) for d in (6, 4, 2)])
self.assertEqual(ioc.number_of_days_seen, 3)

@patch(PATCH_UPDATE_SCORES)
def test_single_day_batch_still_records_one_day(self, mock_scores):
self._process("task-days-single", 1, 1)

ioc = IOC.objects.get(name=self.ATTACKER)
self.assertEqual(ioc.days_seen, [timezone.now().date() - timedelta(days=1)])
self.assertEqual(ioc.number_of_days_seen, 1)


INVALIDATION_CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
Expand Down
28 changes: 27 additions & 1 deletion tests/test_extraction_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import date, datetime
from unittest.mock import Mock, patch

from greedybear.consts import IP
Expand Down Expand Up @@ -134,6 +134,32 @@ def test_malformed_src_ip_skipped_without_dropping_chunk(self):
iocs = iocs_from_hits(hits)
self.assertEqual({ioc.name for ioc in iocs}, {"1.2.3.4", "5.6.7.8"})

def test_collects_every_day_with_a_hit(self):
hits = [
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-03T23:59:00.000Z"),
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-01T08:00:00.000Z"),
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-01T09:00:00.000Z"),
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-05T00:01:00.000Z"),
]
ioc = iocs_from_hits(hits)[0]
self.assertEqual(ioc.days_seen, [date(2025, 1, 1), date(2025, 1, 3), date(2025, 1, 5)])

def test_single_day_gives_single_day_seen(self):
hits = [
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-01T12:00:00.000Z"),
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-01T12:05:00.000Z"),
]
ioc = iocs_from_hits(hits)[0]
self.assertEqual(ioc.days_seen, [date(2025, 1, 1)])

def test_days_seen_are_kept_per_ip(self):
hits = [
self._create_hit(src_ip="8.8.8.8", timestamp="2025-01-01T12:00:00.000Z"),
self._create_hit(src_ip="1.1.1.1", timestamp="2025-01-02T12:00:00.000Z"),
]
days_by_ip = {ioc.name: ioc.days_seen for ioc in iocs_from_hits(hits)}
self.assertEqual(days_by_ip, {"8.8.8.8": [date(2025, 1, 1)], "1.1.1.1": [date(2025, 1, 2)]})

def test_aggregates_destination_ports(self):
hits = [
self._create_hit(src_ip="8.8.8.8", dest_port=22),
Expand Down
40 changes: 40 additions & 0 deletions tests/test_ioc_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,46 @@ def test_full_update_flow(self):
self.assertEqual(len(result.days_seen), 2)
self.assertTrue(result.payload_request)

def test_merge_adds_all_days_of_the_new_ioc(self):
self.mock_sensor_repo.cache = {}
existing = self._create_mock_ioc(days_seen=[date(2025, 1, 1)], last_seen=datetime(2025, 1, 1, 12, 0, 0))
self.mock_ioc_repo.get_ioc_by_name.return_value = existing
new = self._create_mock_ioc(
days_seen=[date(2025, 1, 3), date(2025, 1, 4), date(2025, 1, 6)],
last_seen=datetime(2025, 1, 6, 12, 0, 0),
)
self.mock_ioc_repo.save.return_value = existing

result = self.processor.add_ioc(new, attack_type=SCANNER)

self.assertEqual(result.days_seen, [date(2025, 1, 1), date(2025, 1, 3), date(2025, 1, 4), date(2025, 1, 6)])
self.assertEqual(result.number_of_days_seen, 4)

def test_merge_records_days_older_than_last_seen(self):
# a sensor uploads yesterday's events after today's
self.mock_sensor_repo.cache = {}
existing = self._create_mock_ioc(days_seen=[date(2025, 1, 2)], last_seen=datetime(2025, 1, 2, 12, 0, 0))
self.mock_ioc_repo.get_ioc_by_name.return_value = existing
new = self._create_mock_ioc(days_seen=[date(2025, 1, 1)], last_seen=datetime(2025, 1, 1, 12, 0, 0))
self.mock_ioc_repo.save.return_value = existing

result = self.processor.add_ioc(new, attack_type=SCANNER)

self.assertEqual(result.days_seen, [date(2025, 1, 1), date(2025, 1, 2)])
self.assertEqual(result.number_of_days_seen, 2)

def test_merge_does_not_duplicate_known_days(self):
self.mock_sensor_repo.cache = {}
existing = self._create_mock_ioc(days_seen=[date(2025, 1, 1), date(2025, 1, 2)], last_seen=datetime(2025, 1, 2, 12, 0, 0))
self.mock_ioc_repo.get_ioc_by_name.return_value = existing
new = self._create_mock_ioc(days_seen=[date(2025, 1, 2)], last_seen=datetime(2025, 1, 2, 13, 0, 0))
self.mock_ioc_repo.save.return_value = existing

result = self.processor.add_ioc(new, attack_type=SCANNER)

self.assertEqual(result.days_seen, [date(2025, 1, 1), date(2025, 1, 2)])
self.assertEqual(result.number_of_days_seen, 2)

def test_only_checks_whatsmyip_for_domains(self):
self.processor._whatsmyip_domains = {"1.2.3.4"}
self.mock_sensor_repo.cache = {}
Expand Down
Loading