Spaces:
Running
Running
File size: 4,807 Bytes
90531db 7fddf3d 90531db 7fddf3d 90531db 7fddf3d 90531db 7fddf3d 90531db 7fddf3d 90531db 7fddf3d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | """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()]
|