-
Notifications
You must be signed in to change notification settings - Fork 9
[IoC] Add MISP support #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
46 changes: 46 additions & 0 deletions
46
examples/enterprise/export_indicators_of_compromise_to_misp.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 @@ | ||
| import logging | ||
| from datetime import datetime, timedelta | ||
| from warnings import filterwarnings | ||
|
|
||
| from feedly.api_client.enterprise.indicators_of_compromise import IoCDownloaderBuilder, IoCFormat | ||
| from feedly.api_client.enterprise.misp_exporter import MispExporter | ||
| from feedly.api_client.session import FeedlySession | ||
| from feedly.api_client.utils import run_example | ||
|
|
||
| # Paste your MISP key and URL below | ||
| MISP_KEY = "" | ||
| MISP_URL = "" | ||
|
|
||
| assert MISP_KEY, "Please paste your MISP key" | ||
| assert MISP_URL, "Please paste MISP url" | ||
|
|
||
|
|
||
| def export_indicators_of_compromise_to_misp(): | ||
| """ | ||
| This example will export to your MISP instance the contextualized IoCs that Leo extracted during the past 6 hours | ||
| in all your enterprise feeds. | ||
| """ | ||
| # Authenticate using the default auth directory | ||
| session = FeedlySession() | ||
|
|
||
| # Create the MISP IoC downloader builder object, and limit it to 6 hours | ||
| # Usually newer_than will be the datetime of the last fetch | ||
| downloader_builder = IoCDownloaderBuilder( | ||
| session=session, newer_than=datetime.now() - timedelta(hours=6), format=IoCFormat.MISP | ||
| ) | ||
|
|
||
| # Fetch the IoC from all the enterprise categories, and feed them to the exporter | ||
| # You can use a different method to get the iocs from you personal categories, personal or enterprise boards, | ||
| # or from specific categories/boards using their names or ids | ||
| downloader = downloader_builder.from_all_enterprise_categories() | ||
| exporter = MispExporter(MISP_URL, MISP_KEY, ignore_errors=True, verify_certificate=False) | ||
| exporter.send_bundles(downloader.stream_bundles()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| logging.basicConfig(level="INFO") | ||
| filterwarnings("ignore") | ||
|
|
||
| # Will prompt for the token if missing, and launch the example above | ||
| # If a token expired error is raised, will prompt for a new token and restart the example | ||
| run_example(export_indicators_of_compromise_to_misp) |
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,36 @@ | ||
| import json | ||
| import logging | ||
| from typing import Iterable | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| class MispExporter: | ||
| def __init__(self, url: str, key: str, ignore_errors: bool = False, verify_certificate: bool = True): | ||
| self.url = url.rstrip("/") | ||
| self.key = key | ||
| self.ignore_errors = ignore_errors | ||
| self.verify_certificate = verify_certificate | ||
|
|
||
| def send_bundles(self, bundles: Iterable[dict]) -> None: | ||
| self.send_events(event["Event"] for bundle in bundles for event in bundle["response"]) | ||
|
|
||
| def send_events(self, events: Iterable[dict]) -> None: | ||
| for event in events: | ||
| self.send_event(event) | ||
|
|
||
| def send_event(self, event: dict) -> None: | ||
| try: | ||
| resp = requests.post( | ||
| f"{self.url}/events/add", | ||
| headers={"Authorization": self.key, "Accept": f"application/json", "content-type": f"application/json"}, | ||
| data=json.dumps(event), | ||
| verify=self.verify_certificate, | ||
| ) | ||
| resp.raise_for_status() | ||
| logging.info(f"{self.url}/events/view/{resp.json()['Event']['id']}") | ||
| except: | ||
| if self.ignore_errors: | ||
| logging.exception(f"Failed to send event {event}") | ||
| return | ||
| raise |
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
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.
why not inlining
_parse_response? Do you anticipate future inheritance to customize stuff?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.
Yes for CSV exports we will probably want to parse it to a dataframe :)