-
-
Notifications
You must be signed in to change notification settings - Fork 635
Feature ipqs #3431
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
Closed
Closed
Feature ipqs #3431
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4be648e
Add files via upload
RamboV 3bc5e1d
Add files via upload
RamboV c43b164
Add files via upload
RamboV 174232f
IPQS mixin
RamboV 00b3b5a
Add files via upload
RamboV 5be04c1
Add files via upload
RamboV 24c0e90
fix linting
RamboV d28b642
Update ipqs.py
RamboV 94d486a
Update mixins.py
RamboV a709d36
Update ipqs.py
RamboV ecfbfda
Update mixins.py
RamboV 2a3a33f
Update ipqsurl.py
RamboV a6d1cbf
Update ipqs.py
RamboV bb82c9e
Update test_ipqsfile.py
RamboV fd02b47
update 0178_analyzer_config_ipqs_file_url_scanner.py
RamboV 7e3fc58
update 0179_analyzer_config_ipqs_malware_file_scanner.py
RamboV 4f041a2
Refactor IPQualityScoreMixin for polling and timeout
RamboV 13a1606
update linters
RamboV c20eef8
Add polling interval and max retries to config
RamboV e6b0c79
Add polling_interval and max_retries to config
RamboV e3b2306
update linting
RamboV 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # This file is a part of IntelOwl https://github.com/intelowlproject/IntelOwl | ||
| # See the file 'LICENSE' for copying permission. | ||
|
|
||
| """IPQualityScore file analyzer. | ||
|
|
||
| Provides `IPQSFileScan`, a file-scanning analyzer that uploads files to | ||
| IPQualityScore for malware detection and polls for results. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from api_app.analyzers_manager.classes import FileAnalyzer | ||
| from api_app.mixins import IPQualityScoreMixin | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class IPQSFileScan(FileAnalyzer, IPQualityScoreMixin): | ||
| """ | ||
| Scan a binary file using IPQualityScore malware detection service. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def update(cls): | ||
| pass | ||
|
|
||
| def run(self): | ||
| binary = self.read_file_bytes() | ||
| files = {"files": (self.filename, binary)} | ||
| # lookup endpoint check for cached result | ||
| lookup_result = self._make_request( | ||
| self.lookup_endpoint, | ||
| method="POST", | ||
| _api_key=self._ipqs_api_key, | ||
| files=files, | ||
| ) | ||
| if lookup_result.get("status", False) == "cached": | ||
| lookup_result.pop("update_url", None) | ||
| return lookup_result | ||
| # sending file to ipqs for scan | ||
| scan_result = self._make_request( | ||
| self.scan_endpoint, | ||
| method="POST", | ||
| _api_key=self._ipqs_api_key, | ||
| files=files, | ||
| ) | ||
| # waiting for scan result with help of request id | ||
| result = self._poll_for_report( | ||
| endpoint=self.postback_endpoint, | ||
| _api_key=self._ipqs_api_key, | ||
| request_id=scan_result.get("request_id"), | ||
| ) | ||
| result.pop("update_url", None) | ||
| return result |
163 changes: 163 additions & 0 deletions
163
api_app/analyzers_manager/migrations/0178_analyzer_config_ipqs_file_url_scanner.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,163 @@ | ||
| from django.db import migrations | ||
| from django.db.models.fields.related_descriptors import ( | ||
| ForwardManyToOneDescriptor, | ||
| ForwardOneToOneDescriptor, | ||
| ManyToManyDescriptor, | ||
| ReverseManyToOneDescriptor, | ||
| ReverseOneToOneDescriptor, | ||
| ) | ||
|
|
||
| plugin = { | ||
| "python_module": { | ||
| "health_check_schedule": None, | ||
| "update_schedule": None, | ||
| "module": "ipqsurl.IPQSUrlScan", | ||
| "base_path": "api_app.analyzers_manager.observable_analyzers", | ||
| }, | ||
| "name": "IPQS_File_URL_Scanner", | ||
| "description": ( | ||
| "Scans files hosted or accessible via a URL using " | ||
| "[IPQualityScore](https://www.ipqualityscore.com/) malware " | ||
| "detection API." | ||
| ), | ||
| "disabled": False, | ||
| "soft_time_limit": 60, | ||
| "routing_key": "default", | ||
| "health_check_status": True, | ||
| "type": "observable", | ||
| "docker_based": False, | ||
| "maximum_tlp": "AMBER", | ||
| "observable_supported": ["url"], | ||
| "supported_filetypes": [], | ||
| "run_hash": False, | ||
| "run_hash_type": "", | ||
| "not_supported_filetypes": [], | ||
| "mapping_data_model": {}, | ||
| "model": "analyzers_manager.AnalyzerConfig", | ||
| } | ||
|
|
||
| params = [ | ||
| { | ||
| "python_module": { | ||
| "module": "ipqsurl.IPQSUrlScan", | ||
| "base_path": "api_app.analyzers_manager.observable_analyzers", | ||
| }, | ||
| "name": "ipqs_api_key", | ||
| "type": "str", | ||
| "description": "Please provide the IPQS API key.", | ||
| "is_secret": True, | ||
| "required": True, | ||
| }, | ||
| { | ||
| "python_module": { | ||
| "module": "ipqsurl.IPQSUrlScan", | ||
| "base_path": "api_app.analyzers_manager.observable_analyzers", | ||
| }, | ||
| "name": "polling_interval", | ||
| "type": "int", | ||
| "description": "Please provide the polling interval.", | ||
| "is_secret": False, | ||
| "required": True, | ||
| }, | ||
| { | ||
| "python_module": { | ||
| "module": "ipqsurl.IPQSUrlScan", | ||
| "base_path": "api_app.analyzers_manager.observable_analyzers", | ||
| }, | ||
| "name": "max_retries", | ||
| "type": "int", | ||
| "description": "Please provide the maximum number of retries.", | ||
| "is_secret": False, | ||
| "required": True, | ||
| }, | ||
| ] | ||
|
|
||
| values = [] | ||
|
|
||
|
|
||
| def _get_real_obj(Model, field, value): | ||
| def _get_obj(Model, other_model, value): | ||
| if isinstance(value, dict): | ||
| real_vals = {} | ||
| for key, real_val in value.items(): | ||
| real_vals[key] = _get_real_obj(other_model, key, real_val) | ||
| value = other_model.objects.get_or_create(**real_vals)[0] | ||
| # it is just the primary key serialized | ||
| else: | ||
| if isinstance(value, int): | ||
| if Model.__name__ == "PluginConfig": | ||
| value = other_model.objects.get(name=plugin["name"]) | ||
| else: | ||
| value = other_model.objects.get(pk=value) | ||
| else: | ||
| value = other_model.objects.get(name=value) | ||
| return value | ||
|
|
||
| if ( | ||
| type(getattr(Model, field)) | ||
| in [ | ||
| ForwardManyToOneDescriptor, | ||
| ReverseManyToOneDescriptor, | ||
| ReverseOneToOneDescriptor, | ||
| ForwardOneToOneDescriptor, | ||
| ] | ||
| and value | ||
| ): | ||
| other_model = getattr(Model, field).get_queryset().model | ||
| value = _get_obj(Model, other_model, value) | ||
| elif type(getattr(Model, field)) in [ManyToManyDescriptor] and value: | ||
| other_model = getattr(Model, field).rel.model | ||
| value = [_get_obj(Model, other_model, val) for val in value] | ||
| return value | ||
|
|
||
|
|
||
| def _create_object(Model, data): | ||
| mtm, no_mtm = {}, {} | ||
| for field, value in data.items(): | ||
| value = _get_real_obj(Model, field, value) | ||
| if type(getattr(Model, field)) is ManyToManyDescriptor: | ||
| mtm[field] = value | ||
| else: | ||
| no_mtm[field] = value | ||
| try: | ||
| o = Model.objects.get(**no_mtm) | ||
| except Model.DoesNotExist: | ||
| o = Model(**no_mtm) | ||
| o.full_clean() | ||
| o.save() | ||
| for field, value in mtm.items(): | ||
| attribute = getattr(o, field) | ||
| if value is not None: | ||
| attribute.set(value) | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def migrate(apps, schema_editor): | ||
| Parameter = apps.get_model("api_app", "Parameter") | ||
| PluginConfig = apps.get_model("api_app", "PluginConfig") | ||
| python_path = plugin.pop("model") | ||
| Model = apps.get_model(*python_path.split(".")) | ||
| if not Model.objects.filter(name=plugin["name"]).exists(): | ||
| exists = _create_object(Model, plugin) | ||
| if not exists: | ||
| for param in params: | ||
| _create_object(Parameter, param) | ||
| for value in values: | ||
| _create_object(PluginConfig, value) | ||
|
|
||
|
|
||
| def reverse_migrate(apps, schema_editor): | ||
| python_path = plugin.pop("model") | ||
| Model = apps.get_model(*python_path.split(".")) | ||
| Model.objects.get(name=plugin["name"]).delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| atomic = False | ||
| dependencies = [ | ||
| ("api_app", "0072_update_check_system"), | ||
| ("analyzers_manager", "0177_update_urlscan_observable_supported"), | ||
| ] | ||
|
|
||
| operations = [migrations.RunPython(migrate, reverse_migrate)] | ||
Oops, something went wrong.
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.
max TLP must be AMBER for external services