-
-
Notifications
You must be signed in to change notification settings - Fork 543
Phunter Analyzer #2841
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
Phunter Analyzer #2841
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
300cb3b
phunter
AnshSinghal d42f950
fixed DeepSource errors
AnshSinghal 5d260d1
Merge branch 'develop' into phunter
AnshSinghal 4f9a601
fixed errors related to docker run method
AnshSinghal 373e875
fixed errors with sensitive data leaks
AnshSinghal 4aa4a06
fixed wrong number issue
AnshSinghal fb9084f
fixed some minor bugs in phunter
AnshSinghal 4fb7ba3
used shlex and removed repeated phonenumber check
AnshSinghal 6caac86
fixed migration issue
AnshSinghal b32e62f
cleaned
AnshSinghal 0c91e86
fixed migration file name
AnshSinghal a669283
chore: trigger CI
AnshSinghal 7e9c01f
Merge branch 'develop' into phunter
AnshSinghal 6afa95e
updated requirements file
AnshSinghal 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
125 changes: 125 additions & 0 deletions
125
api_app/analyzers_manager/migrations/0157_analyzer_config_phunter.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,125 @@ | ||
| 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": "phunter.PhunterAnalyzer", | ||
| "base_path": "api_app.analyzers_manager.observable_analyzers", | ||
| }, | ||
| "name": "Phunter", | ||
| "description": "[Phunter Analyzer](https://github.com/N0rz3/Phunter) is an OSINT tool for finding information about a phone number.", | ||
| "disabled": False, | ||
| "soft_time_limit": 60, | ||
| "routing_key": "default", | ||
| "health_check_status": True, | ||
| "type": "observable", | ||
| "docker_based": True, | ||
| "maximum_tlp": "RED", | ||
| "observable_supported": ["generic"], | ||
| "supported_filetypes": [], | ||
| "run_hash": False, | ||
| "run_hash_type": "", | ||
| "not_supported_filetypes": [], | ||
| "mapping_data_model": {}, | ||
| "model": "analyzers_manager.AnalyzerConfig", | ||
| } | ||
|
|
||
| params = [] | ||
|
|
||
| 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", "0071_delete_last_elastic_report"), | ||
| ("analyzers_manager", "0156_alter_analyzer_config_required_api_key_abuse_ch"), | ||
| ] | ||
|
|
||
| operations = [migrations.RunPython(migrate, reverse_migrate)] |
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,75 @@ | ||
| import logging | ||
|
|
||
| import phonenumbers | ||
| import requests | ||
|
|
||
| from api_app.analyzers_manager.classes import DockerBasedAnalyzer, ObservableAnalyzer | ||
| from api_app.analyzers_manager.exceptions import AnalyzerRunException | ||
| from tests.mock_utils import MockUpResponse | ||
|
|
||
| logging.basicConfig(level=logging.DEBUG) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PhunterAnalyzer(ObservableAnalyzer, DockerBasedAnalyzer): | ||
| name: str = "Phunter" | ||
| url: str = "http://phunter:5612/analyze" | ||
| max_tries: int = 1 | ||
| poll_distance: int = 0 | ||
|
|
||
| def run(self): | ||
| try: | ||
| parsed_number = phonenumbers.parse(self.observable_name) | ||
|
|
||
| formatted_number = phonenumbers.format_number( | ||
| parsed_number, phonenumbers.PhoneNumberFormat.E164 | ||
| ) | ||
| except phonenumbers.phonenumberutil.NumberParseException: | ||
| logger.error(f"Phone number parsing failed for: {self.observable_name}") | ||
| return {"success": False, "error": "Invalid phone number"} | ||
|
|
||
| req_data = {"phone_number": formatted_number} | ||
| logger.info(f"Sending {self.name} scan request: {req_data} to {self.url}") | ||
|
|
||
| try: | ||
| response = self._docker_run( | ||
| req_data, analyzer_name=self.name, avoid_polling=True | ||
| ) | ||
| logger.info(f"[{self.name}] Scan successful by Phunter. Result: {response}") | ||
| return response | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| raise AnalyzerRunException( | ||
| f"[{self.name}] Request failed due to network issue: {e}" | ||
| ) | ||
|
|
||
| except ValueError as e: | ||
| raise AnalyzerRunException(f"[{self.name}] Invalid response format: {e}") | ||
|
|
||
| except Exception as e: | ||
| raise AnalyzerRunException(f"{self.name} An unexpected error occurred: {e}") | ||
|
|
||
| @classmethod | ||
| def update(self): | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def mocked_docker_analyzer_post(*args, **kwargs): | ||
| mock_response = { | ||
| "success": True, | ||
| "report": { | ||
| "valid": "yes", | ||
| "views": "9", | ||
| "carrier": "Vodafone", | ||
| "location": "India", | ||
| "operator": "Vodafone", | ||
| "possible": "yes", | ||
| "line_type": "FIXED LINE OR MOBILE", | ||
| "local_time": "21:34:45", | ||
| "spam_status": "Not spammer", | ||
| "phone_number": "+911234567890", | ||
| "national_format": "01234567890", | ||
| "international_format": "+91 1234567890", | ||
| }, | ||
| } | ||
| return MockUpResponse(mock_response, 200) | ||
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,24 @@ | ||
| FROM python:3.12-slim | ||
|
|
||
| # Install dependencies | ||
| RUN apt-get update && apt-get install -y --no-install-recommends git | ||
|
|
||
| # Clone Phunter | ||
| RUN git clone https://github.com/N0rz3/Phunter.git /app/Phunter | ||
|
|
||
| # Set working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy requirements file and app.py to the working directory | ||
| COPY requirements.txt app.py ./ | ||
|
|
||
| # Upgrade pip and install Python packages | ||
| RUN pip install --no-cache-dir --upgrade pip && \ | ||
| pip install --no-cache-dir -r requirements.txt && \ | ||
| pip install --no-cache-dir -r /app/Phunter/requirements.txt | ||
|
|
||
| # Expose port | ||
| EXPOSE 5612 | ||
|
|
||
| # Run the app | ||
| CMD ["python", "app.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,104 @@ | ||
| import logging | ||
| import re | ||
| import shlex | ||
| import subprocess | ||
|
|
||
| from flask import Flask, jsonify, request | ||
|
|
||
| # Logging Configuration | ||
| logging.basicConfig(level=logging.DEBUG) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
|
|
||
| def strip_ansi_codes(text): | ||
| """Remove ANSI escape codes from terminal output""" | ||
| return re.sub(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", "", text) | ||
|
|
||
|
|
||
| def parse_phunter_output(output): | ||
| """Parse output from Phunter CLI and convert to structured JSON""" | ||
| result = {} | ||
| key_mapping = { | ||
| "phone number:": "phone_number", | ||
| "possible:": "possible", | ||
| "valid:": "valid", | ||
| "operator:": "operator", | ||
| "possible location:": "location", | ||
| "location:": "location", | ||
| "carrier:": "carrier", | ||
| "line type:": "line_type", | ||
| "international:": "international_format", | ||
| "national:": "national_format", | ||
| "local time:": "local_time", | ||
| "views count:": "views", | ||
| } | ||
|
|
||
| lines = output.splitlines() | ||
|
|
||
| for line in lines: | ||
| line = line.strip().lower() | ||
|
|
||
| if "not spammer" in line: | ||
| result["spam_status"] = "Not spammer" | ||
| continue | ||
|
|
||
| for keyword, key in key_mapping.items(): | ||
| if keyword in line: | ||
| value = line.partition(":")[2].strip() | ||
| if key in ("possible", "valid"): | ||
| result[key] = "yes" if "✔" in value else "no" | ||
| else: | ||
| result[key] = value | ||
| break | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| @app.route("/analyze", methods=["POST"]) | ||
| def analyze(): | ||
| data = request.get_json() | ||
| phone_number = data.get("phone_number") | ||
|
|
||
| logger.info("Received analysis request") | ||
|
|
||
| if not phone_number: | ||
| logger.warning("No phone number provided in request") | ||
| return jsonify({"error": "No phone number provided"}), 400 | ||
|
|
||
| try: | ||
|
||
| logger.info("Executing Phunter CLI tool") | ||
| command_str = f"python3 phunter.py -t {phone_number}" | ||
| command = shlex.split(command_str) | ||
| result = subprocess.run( | ||
| command, | ||
|
||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| cwd="/app/Phunter", | ||
| ) | ||
|
|
||
| raw_output = result.stdout | ||
| clean_output = strip_ansi_codes(raw_output) | ||
| parsed_output = parse_phunter_output(clean_output) | ||
|
|
||
| logger.info("Phunter analysis completed") | ||
|
|
||
| return ( | ||
| jsonify( | ||
| { | ||
| "success": True, | ||
| "report": parsed_output, | ||
| } | ||
| ), | ||
| 200, | ||
| ) | ||
|
|
||
| except subprocess.CalledProcessError as e: | ||
| return jsonify({"error": f"Phunter execution failed with error {e}"}), 500 | ||
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| logger.info("Starting Phunter Flask API...") | ||
| app.run(host="0.0.0.0", port=5612) | ||
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,6 @@ | ||
| services: | ||
| phunter: | ||
| build: | ||
| context: ../integrations/phunter | ||
| dockerfile: Dockerfile | ||
| image: intelowlproject/intelowl_phunter:test |
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,11 @@ | ||
| services: | ||
| phunter: | ||
| image: intelowlproject/intelowl_phunter:${REACT_APP_INTELOWL_VERSION} | ||
| container_name: intelowl_phunter | ||
| restart: unless-stopped | ||
| expose: | ||
| - "5612" | ||
| volumes: | ||
| - generic_logs:/var/log/intel_owl | ||
| depends_on: | ||
| - uwsgi |
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 @@ | ||
| flask==3.1.1 |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.