Spaces:
Running

ioc-core / src /utils /ioc_detector.py
hellium1999's picture
feat: Add unified API, URLScan integration, IOC limit, updated README
7fddf3d
Raw History Blame
1.67 kB
"""IOC type detection utilities."""
from enum import Enum
from src.utils.validators import (
HashType,
validate_domain,
validate_hash,
validate_ip,
validate_url,
)
class IOCType(str, Enum):
"""Supported IOC types."""
IP = "ip"
DOMAIN = "domain"
URL = "url"
HASH = "hash"
UNKNOWN = "unknown"
def detect_ioc_type(value: str) -> IOCType:
"""
Automatically detect the type of IOC based on its format.
Detection order (most specific to least specific):
1. URL (has http:// or https://)
2. Hash (hexadecimal string of 32, 40, or 64 chars)
3. IP (valid IPv4 or IPv6)
4. Domain (valid domain format)
5. Unknown
Args:
value: The IOC value to detect
Returns:
IOCType enum indicating the detected type
"""
value = value.strip()
if not value:
return IOCType.UNKNOWN
# Check URL first (most specific - has scheme)
if validate_url(value):
return IOCType.URL
# Check hash (hexadecimal strings of specific lengths)
hash_type = validate_hash(value)
if hash_type != HashType.INVALID:
return IOCType.HASH
# Check IP address
if validate_ip(value):
return IOCType.IP
# Check domain
if validate_domain(value):
return IOCType.DOMAIN
return IOCType.UNKNOWN
def detect_ioc_types_batch(values: list[str]) -> list[tuple[str, IOCType]]:
"""
Detect IOC types for a batch of values.
Args:
values: List of IOC values to detect
Returns:
List of tuples containing (value, detected_type)
"""
return [(value, detect_ioc_type(value)) for value in values]