-
-
Notifications
You must be signed in to change notification settings - Fork 49
Add auto extraction of FireHol lists. Closes #548 #642
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
Open
opbot-xd
wants to merge
6
commits into
intelowlproject:develop
Choose a base branch
from
opbot-xd:feat/auto-extract-firehol-lists
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72f35c7
feat: add auto extraction of FireHol lists for classification purposes
opbot-xd ad67c25
fix: add missing __init__.py files for test discovery
opbot-xd 3fcffc4
Merge remote-tracking branch 'origin/develop' into feat/auto-extract-…
opbot-xd 05231be
feat: integrate FireHol lists with API and admin
opbot-xd 27c1166
refactor: address code review feedback
opbot-xd 1410f36
Move FireHol enrichment to IOC creation and add CIDR support
opbot-xd 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import requests | ||
| from greedybear.cronjobs.base import Cronjob | ||
| from greedybear.models import IOC, FireHolList | ||
|
|
||
|
|
||
| class FireHolCron(Cronjob): | ||
| def run(self) -> None: | ||
| base_path = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master" | ||
| sources = { | ||
| "blocklist_de": f"{base_path}/blocklist_de.ipset", | ||
| "greensnow": f"{base_path}/greensnow.ipset", | ||
| "bruteforceblocker": f"{base_path}/bruteforceblocker.ipset", | ||
| "dshield": f"{base_path}/dshield.netset", | ||
| } | ||
mlodic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for source, url in sources.items(): | ||
| self.log.info(f"Processing {source} from {url}") | ||
| try: | ||
| try: | ||
| response = requests.get(url, timeout=60) | ||
| response.raise_for_status() | ||
| except requests.RequestException as e: | ||
| self.log.error(f"Network error fetching {source}: {e}") | ||
| continue | ||
|
|
||
| lines = response.text.splitlines() | ||
| for line in lines: | ||
| line = line.strip() | ||
| if not line or line.startswith("#"): | ||
| continue | ||
|
|
||
| # FireHol .ipset and .netset files contain IPs or CIDRs, one per line | ||
| # Comments (lines starting with #) are filtered out above | ||
|
|
||
| try: | ||
| FireHolList.objects.get(ip_address=line, source=source) | ||
| except FireHolList.DoesNotExist: | ||
| FireHolList(ip_address=line, source=source).save() | ||
|
|
||
| except Exception as e: | ||
| self.log.exception(f"Unexpected error processing {source}: {e}") | ||
|
|
||
| # Clean up old FireHolList entries | ||
| self._cleanup_old_entries() | ||
|
|
||
| def _cleanup_old_entries(self): | ||
| """ | ||
| Delete FireHolList entries older than 30 days to keep database clean. | ||
| """ | ||
| from datetime import datetime, timedelta | ||
|
|
||
| cutoff_date = datetime.now() - timedelta(days=30) | ||
| deleted_count, _ = FireHolList.objects.filter(added__lt=cutoff_date).delete() | ||
|
|
||
| if deleted_count > 0: | ||
| self.log.info(f"Cleaned up {deleted_count} old FireHolList entries") | ||
46 changes: 46 additions & 0 deletions
46
greedybear/migrations/0023_ioc_firehol_categories_alter_statistics_view_and_more.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Generated by Django 5.2.8 on 2025-12-22 11:24 | ||
|
|
||
| import datetime | ||
| import django.contrib.postgres.fields | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("greedybear", "0022_whatsmyip"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="ioc", | ||
| name="firehol_categories", | ||
| field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=64), blank=True, default=list, size=None), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="statistics", | ||
| name="view", | ||
| field=models.CharField( | ||
| choices=[ | ||
| ("feeds", "Feeds View"), | ||
| ("enrichment", "Enrichment View"), | ||
| ("command sequence", "Command Sequence View"), | ||
| ("cowrie session", "Cowrie Session View"), | ||
| ], | ||
| default="feeds", | ||
| max_length=32, | ||
| ), | ||
| ), | ||
| migrations.CreateModel( | ||
| name="FireHolList", | ||
| fields=[ | ||
| ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
| ("ip_address", models.CharField(max_length=256)), | ||
| ("added", models.DateTimeField(default=datetime.datetime.now)), | ||
| ("source", models.CharField(blank=True, max_length=64, null=True)), | ||
| ], | ||
| options={ | ||
| "indexes": [models.Index(fields=["ip_address"], name="greedybear__ip_addr_e01f2f_idx")], | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Generated by Django 5.2.8 on 2025-12-23 21:00 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("greedybear", "0023_ioc_firehol_categories_alter_statistics_view_and_more"), | ||
| ("greedybear", "0023_rename_massscanners_massscanner_and_more"), | ||
| ] | ||
|
|
||
| operations = [] |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from greedybear.cronjobs.firehol import FireHolCron | ||
| from greedybear.models import IOC, FireHolList | ||
| from tests import CustomTestCase | ||
|
|
||
|
|
||
| class FireHolCronTestCase(CustomTestCase): | ||
| @patch("greedybear.cronjobs.firehol.requests.get") | ||
| def test_run(self, mock_get): | ||
| # Setup mock responses | ||
| mock_response_blocklist_de = MagicMock() | ||
| mock_response_blocklist_de.status_code = 200 | ||
| mock_response_blocklist_de.text = "# blocklist_de\n1.1.1.1\n2.2.2.2" | ||
|
|
||
| mock_response_greensnow = MagicMock() | ||
| mock_response_greensnow.status_code = 200 | ||
| mock_response_greensnow.text = "# greensnow\n3.3.3.3" | ||
|
|
||
| mock_response_bruteforceblocker = MagicMock() | ||
| mock_response_bruteforceblocker.status_code = 200 | ||
| mock_response_bruteforceblocker.text = "# bruteforceblocker\n1.1.1.1" | ||
|
|
||
| mock_response_dshield = MagicMock() | ||
| mock_response_dshield.status_code = 200 | ||
| mock_response_dshield.text = "# dshield\n4.4.4.0/24" | ||
|
|
||
| # Side effect for multiple calls | ||
| def side_effect(url, timeout): | ||
| if "blocklist_de" in url: | ||
| return mock_response_blocklist_de | ||
| elif "greensnow" in url: | ||
| return mock_response_greensnow | ||
| elif "bruteforceblocker" in url: | ||
| return mock_response_bruteforceblocker | ||
| elif "dshield" in url: | ||
| return mock_response_dshield | ||
| return MagicMock(status_code=404) | ||
|
|
||
| mock_get.side_effect = side_effect | ||
|
|
||
| # Run the cronjob | ||
| cronjob = FireHolCron() | ||
| cronjob.execute() | ||
|
|
||
| # Check FireHolList entries were created | ||
| self.assertTrue(FireHolList.objects.filter(ip_address="1.1.1.1", source="blocklist_de").exists()) | ||
| self.assertTrue(FireHolList.objects.filter(ip_address="2.2.2.2", source="blocklist_de").exists()) | ||
| self.assertTrue(FireHolList.objects.filter(ip_address="3.3.3.3", source="greensnow").exists()) | ||
| self.assertTrue(FireHolList.objects.filter(ip_address="1.1.1.1", source="bruteforceblocker").exists()) | ||
| self.assertTrue(FireHolList.objects.filter(ip_address="4.4.4.0/24", source="dshield").exists()) | ||
|
|
||
| # Verify FireHolList data is available for IOC enrichment at creation time | ||
| # (Note: Enrichment now happens in iocs_from_hits during IOC creation, not here) | ||
| firehol_entries = FireHolList.objects.filter(ip_address="1.1.1.1") | ||
| self.assertEqual(firehol_entries.count(), 2) | ||
| sources = list(firehol_entries.values_list("source", flat=True)) | ||
| self.assertIn("blocklist_de", sources) | ||
| self.assertIn("bruteforceblocker", sources) |
This file contains hidden or 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
Oops, something went wrong.
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.
ah one thing about this. This is a netset so there won't be any match with the current logic. For netsets, you should use the ipaddress library to check whether and IPAddress is inside an IPNetwork
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.
Great catch! The dshield.netset contains network ranges, not individual IPs, so the current exact match logic won't work.
I'll update the enrichment logic to use the
ipaddresslibrary to check network membership. Specifically, I need to:iocs_from_hits, check if the IP address is contained within any of the stored network rangesipaddress.ip_address()andipaddress.ip_network()to perform proper CIDR matchingI'll push an update shortly that handles both:
Thanks for pointing this out!