2019-12-04 15:18:27 +01:00
|
|
|
#!/usr/bin/env python
|
2024-01-17 13:13:14 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
2019-12-04 15:18:27 +01:00
|
|
|
|
|
|
|
import logging
|
2024-02-01 14:40:12 +01:00
|
|
|
|
2019-12-04 15:18:27 +01:00
|
|
|
from urllib.parse import unquote_plus
|
|
|
|
|
2024-02-01 14:40:12 +01:00
|
|
|
from .abstractgenerator import AbstractMISPObjectGenerator
|
|
|
|
|
2022-06-16 09:38:39 +02:00
|
|
|
try:
|
|
|
|
from pyfaup.faup import Faup # type: ignore
|
|
|
|
except (OSError, ImportError):
|
|
|
|
from ._psl_faup import PSLFaup as Faup
|
|
|
|
|
2019-12-04 15:18:27 +01:00
|
|
|
logger = logging.getLogger('pymisp')
|
|
|
|
|
|
|
|
faup = Faup()
|
|
|
|
|
|
|
|
|
|
|
|
class URLObject(AbstractMISPObjectGenerator):
|
|
|
|
|
2024-02-01 14:40:12 +01:00
|
|
|
def __init__(self, url: str, generate_all=False, **kwargs) -> None: # type: ignore[no-untyped-def]
|
2021-01-11 11:49:12 +01:00
|
|
|
super().__init__('url', **kwargs)
|
2022-06-16 09:38:39 +02:00
|
|
|
self._generate_all = True if generate_all is True else False
|
2019-12-04 15:18:27 +01:00
|
|
|
faup.decode(unquote_plus(url))
|
|
|
|
self.generate_attributes()
|
|
|
|
|
2024-02-01 14:40:12 +01:00
|
|
|
def generate_attributes(self) -> None:
|
2019-12-04 15:18:27 +01:00
|
|
|
self.add_attribute('url', value=faup.url.decode())
|
|
|
|
if faup.get_host():
|
|
|
|
self.add_attribute('host', value=faup.get_host())
|
|
|
|
if faup.get_domain():
|
|
|
|
self.add_attribute('domain', value=faup.get_domain())
|
2022-06-16 09:38:39 +02:00
|
|
|
if self._generate_all:
|
|
|
|
if hasattr(faup, 'ip_as_host') and faup.ip_as_host:
|
|
|
|
self.attributes = [attr for attr in self.attributes
|
|
|
|
if attr.object_relation not in ('host', 'domain')]
|
|
|
|
self.add_attribute('ip', value=faup.ip_as_host)
|
|
|
|
if faup.get_credential():
|
|
|
|
self.add_attribute('credential', value=faup.get_credential())
|
|
|
|
if faup.get_fragment():
|
|
|
|
self.add_attribute('fragment', value=faup.get_fragment())
|
|
|
|
if faup.get_port():
|
|
|
|
self.add_attribute('port', value=faup.get_port())
|
|
|
|
if faup.get_query_string():
|
|
|
|
self.add_attribute('query_string', value=faup.get_query_string())
|
|
|
|
if faup.get_resource_path():
|
|
|
|
self.add_attribute('resource_path', value=faup.get_resource_path())
|
|
|
|
if faup.get_scheme():
|
|
|
|
self.add_attribute('scheme', value=faup.get_scheme())
|
|
|
|
if faup.get_tld():
|
|
|
|
self.add_attribute('tld', value=faup.get_tld())
|
|
|
|
if faup.get_domain_without_tld():
|
|
|
|
self.add_attribute('domain_without_tld', value=faup.get_domain_without_tld())
|
|
|
|
if faup.get_subdomain():
|
|
|
|
self.add_attribute('subdomain', value=faup.get_subdomain())
|
|
|
|
if hasattr(faup, 'get_unicode_host') and faup.get_unicode_host() != faup.get_host():
|
|
|
|
self.add_attribute('text', value=faup.get_unicode_host())
|