Spaces:
Running
Running
Download src/utils/validators.py from reputeradar/ioc-core: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/spaces/reputeradar/ioc-core/resolve/main/src/utils/validators.py
- Command line
-
hf download hf://spaces/reputeradar/ioc-core/src/utils/validators.py
-
curl -L -o validators.py https://huggingface.co/spaces/reputeradar/ioc-core/resolve/main/src/utils/validators.py
4.81 kB
| """Validation utilities for IOCs.""" | |
| import ipaddress | |
| import re | |
| from enum import Enum | |
| class HashType(str, Enum): | |
| """Supported hash types.""" | |
| SHA256 = "SHA256" | |
| SHA1 = "SHA1" | |
| MD5 = "MD5" | |
| INVALID = "INVALID" | |
| def validate_hash(hash_value: str) -> HashType: | |
| """ | |
| Validate and identify hash type based on length. | |
| Args: | |
| hash_value: The hash string to validate | |
| Returns: | |
| HashType enum indicating the type of hash | |
| """ | |
| hash_value = hash_value.strip() | |
| # Check if it's a valid hexadecimal string | |
| if not re.match(r"^[a-fA-F0-9]+$", hash_value): | |
| return HashType.INVALID | |
| hash_length = len(hash_value) | |
| if hash_length == 64: | |
| return HashType.SHA256 | |
| elif hash_length == 40: | |
| return HashType.SHA1 | |
| elif hash_length == 32: | |
| return HashType.MD5 | |
| else: | |
| return HashType.INVALID | |
| def validate_txt_file(filename: str | None) -> bool: | |
| """ | |
| Validate that a file is a TXT file. | |
| Args: | |
| filename: Name of the file to validate | |
| Returns: | |
| True if the file is a TXT, False otherwise | |
| """ | |
| if not filename: | |
| return False | |
| return filename.lower().endswith(".txt") | |
| def validate_ip(ip_address: str) -> bool: | |
| """ | |
| Validate an IP address (IPv4 or IPv6) using ipaddress module. | |
| Args: | |
| ip_address: IP address string to validate | |
| Returns: | |
| True if valid IP address, False otherwise | |
| """ | |
| ip_address = ip_address.strip() | |
| try: | |
| ipaddress.ip_address(ip_address) | |
| return True | |
| except ValueError: | |
| return False | |
| def is_private_ip(ip_address: str) -> bool: | |
| """ | |
| Check if an IP address is private, reserved, or loopback. | |
| Private ranges include: | |
| - 10.0.0.0/8 | |
| - 172.16.0.0/12 | |
| - 192.168.0.0/16 | |
| - 127.0.0.0/8 (loopback) | |
| - ::1 (IPv6 loopback) | |
| - fc00::/7 (IPv6 unique local) | |
| - fe80::/10 (IPv6 link-local) | |
| Args: | |
| ip_address: IP address string to check | |
| Returns: | |
| True if IP is private/reserved/loopback, False otherwise | |
| """ | |
| ip_address = ip_address.strip() | |
| try: | |
| ip_obj = ipaddress.ip_address(ip_address) | |
| return ( | |
| ip_obj.is_private | |
| or ip_obj.is_loopback | |
| or ip_obj.is_reserved | |
| or ip_obj.is_link_local | |
| or ip_obj.is_multicast | |
| ) | |
| except ValueError: | |
| return False | |
| def validate_domain(domain: str) -> bool: | |
| """ | |
| Validate a domain name. | |
| Args: | |
| domain: Domain name to validate | |
| Returns: | |
| True if valid domain, False otherwise | |
| """ | |
| domain = domain.strip().lower() | |
| # Remove trailing dot if present | |
| if domain.endswith("."): | |
| domain = domain[:-1] | |
| if not domain or len(domain) > 253: | |
| return False | |
| # Check for valid domain pattern | |
| # Must have at least one dot, valid characters, and valid TLD | |
| domain_pattern = ( | |
| r"^(?!-)" # Cannot start with hyphen | |
| r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+" # Subdomains | |
| r"[a-zA-Z]{2,}$" # TLD (at least 2 chars) | |
| ) | |
| return bool(re.match(domain_pattern, domain)) | |
| def validate_url(url: str) -> bool: | |
| """ | |
| Validate a URL format. | |
| Args: | |
| url: URL string to validate | |
| Returns: | |
| True if valid URL, False otherwise | |
| """ | |
| url = url.strip() | |
| # URL pattern - supports http and https | |
| url_pattern = ( | |
| r"^https?://" # http:// or https:// | |
| r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,}|" # domain | |
| r"localhost|" # localhost | |
| r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # or IP | |
| r"(?::\d+)?" # optional port | |
| r"(?:/?|[/?]\S+)$" # path | |
| ) | |
| return bool(re.match(url_pattern, url, re.IGNORECASE)) | |
| def parse_iocs_from_text(text: str) -> list[str]: | |
| """ | |
| Parse IOCs from text input. | |
| Supports both comma-separated and newline-separated formats. | |
| - Text input: "ioc1, ioc2, ioc3" or "ioc1\\nioc2\\nioc3" | |
| - Mixed: "ioc1, ioc2\\nioc3, ioc4" | |
| Args: | |
| text: Raw text containing IOCs | |
| Returns: | |
| List of stripped, non-empty IOC strings | |
| """ | |
| # Replace commas with newlines to normalize the input | |
| normalized = text.replace(",", "\n") | |
| # Split by newlines and clean up | |
| lines = normalized.strip().split("\n") | |
| # Strip whitespace and filter empty strings | |
| return [line.strip() for line in lines if line.strip()] | |
| def parse_iocs_from_file(content: str) -> list[str]: | |
| """ | |
| Parse IOCs from file content (one per line). | |
| Args: | |
| content: File content with IOCs, one per line | |
| Returns: | |
| List of stripped, non-empty IOC strings | |
| """ | |
| lines = content.strip().split("\n") | |
| return [line.strip() for line in lines if line.strip()] | |