Download ml-engine/models/disentangled_dom.py from Amal340/phishing-framework-code: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/datasets/Amal340/phishing-framework-code/resolve/main/ml-engine/models/disentangled_dom.py
- Command line
-
hf download hf://datasets/Amal340/phishing-framework-code/ml-engine/models/disentangled_dom.py
-
curl -L -o disentangled_dom.py https://huggingface.co/datasets/Amal340/phishing-framework-code/resolve/main/ml-engine/models/disentangled_dom.py
2.16 kB
| """ | |
| Reinforced Disentangled HTML/DOM Representation Learning with Hard-Sample Mining | |
| (Yoon, Buu, & Kim, 2025) | |
| Separates structural DOM tree topology from surface text semantics to expose obfuscated phishing templates. | |
| """ | |
| import numpy as np | |
| class DisentangledDOMEncoder: | |
| """ | |
| Projects raw HTML/DOM tree metrics into two orthogonal subspaces: | |
| 1. Structural Subspace (z_struct): DOM depth, tag transitions, hidden form/iframe manipulations. | |
| 2. Semantic Subspace (z_sem): Textual token distribution and form field labels. | |
| Includes Hard-Sample Mining loss weighting to focus gradient updates on deceptive clones. | |
| """ | |
| def __init__(self, input_dim: int = 24, latent_dim: int = 32, seed: int = 101): | |
| rng = np.random.default_rng(seed) | |
| self.W_struct = rng.normal(0, np.sqrt(2.0 / input_dim), size=(input_dim, latent_dim)) | |
| self.b_struct = np.zeros(latent_dim) | |
| self.W_sem = rng.normal(0, np.sqrt(2.0 / input_dim), size=(input_dim, latent_dim)) | |
| self.b_sem = np.zeros(latent_dim) | |
| def forward(self, x_dom: np.ndarray) -> tuple[np.ndarray, np.ndarray, float]: | |
| """ | |
| Computes disentangled structural and semantic representations and their orthogonality penalty. | |
| """ | |
| z_struct = np.tanh(x_dom @ self.W_struct + self.b_struct) | |
| z_sem = np.tanh(x_dom @ self.W_sem + self.b_sem) | |
| # Orthogonality regularization loss: || Z_struct^T @ Z_sem ||_F^2 | |
| corr_matrix = (z_struct.T @ z_struct) if z_struct.shape[0] == 0 else (z_struct.T @ z_sem) / max(1, x_dom.shape[0]) | |
| ortho_loss = float(np.mean(corr_matrix ** 2)) | |
| z_combined = np.concatenate([z_struct, z_sem], axis=-1) | |
| return z_combined, z_struct, ortho_loss | |
| def mine_hard_samples(probs: np.ndarray, labels: np.ndarray, margin: float = 0.25) -> np.ndarray: | |
| """ | |
| Hard-Sample Mining (Yoon et al., 2025): Assigns amplified sample weights (2.5x) | |
| to borderline or misclassified samples where |y - p| > margin. | |
| """ | |
| errors = np.abs(labels - probs) | |
| weights = np.where(errors >= margin, 2.5, 1.0) | |
| return weights | |