From 537d4e6a380e3d214dbc4af6d48d1ebaa0893833 Mon Sep 17 00:00:00 2001 From: Uman Shahzad Date: Mon, 16 Nov 2026 10:23:14 +0000 Subject: [PATCH] Add VPNDetection threat intelligence provider --- .../api/msticpy.context.tiproviders.rst | 1 + ...ticpy.context.tiproviders.vpndetection.rst | 7 ++ docs/source/data_acquisition/TIProviders.rst | 7 +- docs/source/getting_started/msticpyconfig.rst | 4 + msticpy/context/tiproviders/__init__.py | 1 + msticpy/context/tiproviders/vpndetection.py | 113 ++++++++++++++++++ msticpy/resources/mpconfig_defaults.yaml | 6 + tests/context/test_tiproviders.py | 53 ++++++++ tests/msticpyconfig-test.yaml | 3 + tests/testdata/msticpyconfig.yaml | 3 + 10 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 docs/source/api/msticpy.context.tiproviders.vpndetection.rst create mode 100644 msticpy/context/tiproviders/vpndetection.py diff --git a/docs/source/api/msticpy.context.tiproviders.rst b/docs/source/api/msticpy.context.tiproviders.rst index 34d285bd..f72e2de4 100644 --- a/docs/source/api/msticpy.context.tiproviders.rst +++ b/docs/source/api/msticpy.context.tiproviders.rst @@ -33,3 +33,4 @@ Submodules msticpy.context.tiproviders.ti_provider_base msticpy.context.tiproviders.tor_exit_nodes msticpy.context.tiproviders.virustotal + msticpy.context.tiproviders.vpndetection diff --git a/docs/source/api/msticpy.context.tiproviders.vpndetection.rst b/docs/source/api/msticpy.context.tiproviders.vpndetection.rst new file mode 100644 index 00000000..5c05e94e --- /dev/null +++ b/docs/source/api/msticpy.context.tiproviders.vpndetection.rst @@ -0,0 +1,7 @@ +msticpy.context.tiproviders.vpndetection module +=============================================== + +.. automodule:: msticpy.context.tiproviders.vpndetection + :members: + :show-inheritance: + :undoc-members: diff --git a/docs/source/data_acquisition/TIProviders.rst b/docs/source/data_acquisition/TIProviders.rst index 1a6184c3..56d2dbd1 100644 --- a/docs/source/data_acquisition/TIProviders.rst +++ b/docs/source/data_acquisition/TIProviders.rst @@ -33,6 +33,7 @@ Features - **CrowdSec** - **AbuseIPDB** - **ScanMalware** + - **VPNDetection** - Other pseudo-TI providers are also included: @@ -286,6 +287,9 @@ fictitious - the format of the keys may differ from what is shown ScanMalware: Primary: True Provider: "ScanMalware" + VPNDetection: + Primary: True + Provider: "VPNDetection" You need to tell `TILookup` to refresh its configuration. @@ -307,7 +311,8 @@ of providers loaded. 'AzSTI - Microsoft Sentinel TI provider class. (primary)', 'CrowdSec - CrowdSec CTI Smoke Lookup. (primary)', 'AbuseIPDB - AbuseIPDB Lookup. (primary)', - 'ScanMalware - ScanMalware Lookup. (primary)'] + 'ScanMalware - ScanMalware Lookup. (primary)', + 'VPNDetection - VPNDetection Lookup. (primary)'] .. warning:: Depending on the type of account that you have with a provider, they will typically impose a limit diff --git a/docs/source/getting_started/msticpyconfig.rst b/docs/source/getting_started/msticpyconfig.rst index 45340bc9..ba13278d 100644 --- a/docs/source/getting_started/msticpyconfig.rst +++ b/docs/source/getting_started/msticpyconfig.rst @@ -118,6 +118,7 @@ Currently supported provider names are: - CrowdSec - AbuseIPDB - ScanMalware +- VPNDetection .. code:: yaml @@ -152,6 +153,9 @@ Currently supported provider names are: ScanMalware: Primary: True Provider: "ScanMalware" + VPNDetection: + Primary: True + Provider: "VPNDetection" .. note:: You store values in the ``Args`` section as simple strings, as names of environment variables containing the value, or diff --git a/msticpy/context/tiproviders/__init__.py b/msticpy/context/tiproviders/__init__.py index ea5370f2..db08b514 100644 --- a/msticpy/context/tiproviders/__init__.py +++ b/msticpy/context/tiproviders/__init__.py @@ -31,4 +31,5 @@ "BinaryEdge": ("binaryedge", "BinaryEdge"), "Cyberint": ("cyberint", "Cyberint"), "ScanMalware": ("scanmalware", "ScanMalware"), + "VPNDetection": ("vpndetection", "VPNDetection"), } diff --git a/msticpy/context/tiproviders/vpndetection.py b/msticpy/context/tiproviders/vpndetection.py new file mode 100644 index 00000000..7626fb48 --- /dev/null +++ b/msticpy/context/tiproviders/vpndetection.py @@ -0,0 +1,113 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +VPNDetection Provider. + +Flags whether an IP address is a VPN, Tor exit or relay (iCloud Private Relay and +similar), with its country and ASN. The API answers without a key; a key adds the +proxy and hosting flags and the VPN's provider on paid plans. +https://vpndetection.io/ +""" + +from __future__ import annotations + +from typing import Any, ClassVar + +from typing_extensions import Self + +from ..._version import VERSION +from ..http_provider import APILookupParams +from .ti_http_provider import HttpTIProvider +from .ti_provider_base import ResultSeverity + +__version__ = VERSION +__author__ = "Uman Shahzad" + +_ANONYMIZER_FLAGS = ( + "is_vpn", + "is_tor", + "is_relay", + "is_resproxy", + "is_dcproxy", + "is_mobproxy", + "is_pubproxy", +) + + +class VPNDetection(HttpTIProvider): + """VPNDetection Lookup.""" + + _BASE_URL: ClassVar[str] = "https://api.vpndetection.io" + + _QUERIES: ClassVar[dict[str, APILookupParams]] = { + "ipv4": APILookupParams( + path="/{observable}", + headers={"Authorization": "Bearer {AuthKey}"}, + ), + "ipv6": APILookupParams( + path="/{observable}", + headers={"Authorization": "Bearer {AuthKey}"}, + ), + } + + def _substitute_parms( + self: Self, + value: str, + value_type: str, + query_type: str | None = None, + ) -> tuple[str, dict[str, Any]]: + """Drop the Authorization header when no AuthKey is configured.""" + verb, req_dict = super()._substitute_parms(value, value_type, query_type) + if not self._request_params.get("AuthKey"): + req_dict["headers"].pop("Authorization", None) + return verb, req_dict + + def parse_results(self: Self, response: dict) -> tuple[bool, ResultSeverity, Any]: + """ + Return the details of the response. + + Parameters + ---------- + response : Dict + The returned data response + + Returns + ------- + Tuple[bool, ResultSeverity, Any] + bool = positive or negative hit + ResultSeverity = enumeration of severity + Object with match details + + """ + if self._failed_response(response) or not isinstance( + response["RawResult"], + dict, + ): + return False, ResultSeverity.information, "Not found." + raw: dict[str, Any] = response["RawResult"] + vpn: dict[str, Any] = raw.get("vpn") or {} + location: dict[str, Any] = raw.get("location") or {} + asn: dict[str, Any] = raw.get("asn") or {} + result_dict: dict[str, Any] = { + "IsVPN": raw.get("is_vpn"), + "VPNProvider": vpn.get("provider_name") or vpn.get("provider"), + "IsTor": raw.get("is_tor"), + "IsRelay": raw.get("is_relay"), + "IsHosting": raw.get("is_hosting"), + "IsResidentialProxy": raw.get("is_resproxy"), + "IsDatacenterProxy": raw.get("is_dcproxy"), + "IsMobileProxy": raw.get("is_mobproxy"), + "IsPublicProxy": raw.get("is_pubproxy"), + "IsBogon": raw.get("is_bogon"), + "CountryOrRegion": location.get("country"), + "ASN": asn.get("asn"), + "ASNName": asn.get("name"), + "ASNType": asn.get("type"), + } + severity = ResultSeverity.information + if any(raw.get(flag) for flag in _ANONYMIZER_FLAGS): + severity = ResultSeverity.warning + return True, severity, result_dict diff --git a/msticpy/resources/mpconfig_defaults.yaml b/msticpy/resources/mpconfig_defaults.yaml index 75f0bfbb..5dfd813d 100644 --- a/msticpy/resources/mpconfig_defaults.yaml +++ b/msticpy/resources/mpconfig_defaults.yaml @@ -104,6 +104,12 @@ TIProviders: ScanMalware: Primary: bool(default=True) Provider: "ScanMalware" + VPNDetection: + Args: + # Leave AuthKey empty to use the API without a key, or set one for the proxy and hosting flags. + AuthKey: *cred_key + Primary: bool(default=False) + Provider: "VPNDetection" RiskIQ: Primary: bool(default=True) Provider: "RiskIQ" diff --git a/tests/context/test_tiproviders.py b/tests/context/test_tiproviders.py index 9173dc9b..9a851da8 100644 --- a/tests/context/test_tiproviders.py +++ b/tests/context/test_tiproviders.py @@ -183,6 +183,7 @@ class TiTestCase: TiTestCase("IPQualityScore", exp_responses=20), TiTestCase("BinaryEdge"), TiTestCase("ScanMalware"), + TiTestCase("VPNDetection"), ] @@ -257,6 +258,7 @@ def verify_result(result, ti_lookup): "IPQualityScore", "BinaryEdge", "ScanMalware", + "VPNDetection", ], ) check.is_not_none(lu_result["Ioc"]) @@ -279,6 +281,37 @@ def verify_result(result, ti_lookup): check.is_instance(result_df, pd.DataFrame) +def test_vpndetection_auth_header(ti_lookup): + """Test VPNDetection sends a key only when one is configured.""" + ti_provider = ti_lookup.loaded_providers["VPNDetection"] + _, req = ti_provider._substitute_parms("146.70.116.130", "ipv4") + check.equal(req["url"], "https://api.vpndetection.io/146.70.116.130") + check.is_not_in("Authorization", req["headers"]) + + ti_provider._request_params["AuthKey"] = "test-key" + try: + _, req = ti_provider._substitute_parms("2001:db8::1", "ipv6") + finally: + ti_provider._request_params["AuthKey"] = None + check.equal(req["headers"]["Authorization"], "Bearer test-key") + + +def test_vpndetection_parse_results(ti_lookup): + """Test VPNDetection flags an anonymizer as a warning.""" + ti_provider = ti_lookup.loaded_providers["VPNDetection"] + raw = _PROVIDER_RESPONSES["https://api.vpndetection.io"]["response"] + result, severity, details = ti_provider.parse_results({"Status": 200, "RawResult": raw}) + check.is_true(result) + check.equal(severity, ResultSeverity.warning) + check.is_true(details["IsVPN"]) + check.equal(details["ASN"], 9009) + check.equal(details["CountryOrRegion"], "AT") + + clean = {**raw, "is_vpn": False} + _, severity, _ = ti_provider.parse_results({"Status": 200, "RawResult": clean}) + check.equal(severity, ResultSeverity.information) + + @pytest.mark.filterwarnings("ignore::UserWarning") def test_ti_config_and_load(ti_lookup): """Test loading TI providers.""" @@ -776,6 +809,26 @@ def _scanmalware_scans(**kwargs): "message": "Success", }, }, + "https://api.vpndetection.io": { + "ioc_param": "url", + "response": { + "ip": "146.70.116.130", + "is_bogon": False, + "is_vpn": True, + "is_relay": False, + "is_tor": False, + "relay": {}, + "tor": {}, + "location": {"country": "AT", "country_name": "Austria"}, + "asn": { + "asn": 9009, + "name": "M247 Europe SRL", + "domain": "m247global.com", + "type": "hosting", + "prefix": "146.70.116.0/24", + }, + }, + }, "https://www.ipqualityscore.com": { "ioc_param": "ipv4", "response": { diff --git a/tests/msticpyconfig-test.yaml b/tests/msticpyconfig-test.yaml index e62fb909..839c5c23 100644 --- a/tests/msticpyconfig-test.yaml +++ b/tests/msticpyconfig-test.yaml @@ -71,6 +71,9 @@ TIProviders: ScanMalware: Primary: True Provider: "ScanMalware" + VPNDetection: + Primary: True + Provider: "VPNDetection" OpenPageRank: Args: AuthKey: diff --git a/tests/testdata/msticpyconfig.yaml b/tests/testdata/msticpyconfig.yaml index bf7e53a4..4439d377 100644 --- a/tests/testdata/msticpyconfig.yaml +++ b/tests/testdata/msticpyconfig.yaml @@ -43,6 +43,9 @@ TIProviders: ScanMalware: Primary: True Provider: "ScanMalware" + VPNDetection: + Primary: True + Provider: "VPNDetection" AzureSentinel: Args: WorkspaceID: "a927809c-8142-43e1-96b3-4ad87cfe95a3"