{"id":50134405,"url":"https://github.com/dylan-sutton-chavez/http-threat-classifier","last_synced_at":"2026-05-23T21:00:54.992Z","repository":{"id":342987529,"uuid":"1082233854","full_name":"dylan-sutton-chavez/http-threat-classifier","owner":"dylan-sutton-chavez","description":"A linear classifier extended with a bounded uncertainty region ±ε around the decision boundary.","archived":false,"fork":false,"pushed_at":"2026-03-08T09:54:49.000Z","size":4139,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-08T14:00:47.452Z","etag":null,"topics":["active-learning","cybersecurity","epsilon","hashing","low-latency","model-destilation","n-grams","owasp","perceptron","python","threat-detection","waf"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dylan-sutton-chavez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-23T23:55:35.000Z","updated_at":"2026-03-08T09:54:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"5ebd03ee-bb4f-447e-a5f2-2626694ff5b5","html_url":"https://github.com/dylan-sutton-chavez/http-threat-classifier","commit_stats":null,"previous_names":["dylan-sutton-chavez/http-threat-classifier"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/dylan-sutton-chavez/http-threat-classifier","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylan-sutton-chavez%2Fhttp-threat-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylan-sutton-chavez%2Fhttp-threat-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylan-sutton-chavez%2Fhttp-threat-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylan-sutton-chavez%2Fhttp-threat-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dylan-sutton-chavez","download_url":"https://codeload.github.com/dylan-sutton-chavez/http-threat-classifier/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylan-sutton-chavez%2Fhttp-threat-classifier/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33412082,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-23T18:09:33.147Z","status":"ssl_error","status_checked_at":"2026-05-23T18:09:31.380Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["active-learning","cybersecurity","epsilon","hashing","low-latency","model-destilation","n-grams","owasp","perceptron","python","threat-detection","waf"],"created_at":"2026-05-23T21:00:31.676Z","updated_at":"2026-05-23T21:00:54.983Z","avatar_url":"https://github.com/dylan-sutton-chavez.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HTTP Threat Classifier with a Grey Area\r\n\r\nA linear classifier extended with a bounded uncertainty region ±ε around the decision boundary. Inputs whose net activation satisfies −ε ≤ z ≤ ε yield a third output state instead of a forced classification, and are escalated to an LLM oracle — a two-tier architecture that trades latency for precision on the inputs where a linear model is least reliable.\r\n\r\n---\r\n\r\n## The Epsilon Uncertainty Activation Function\r\n\r\nStandard step function:\r\n\r\n$$\r\nh(x) = \\begin{cases} 1 \u0026 x \\geq 0 \\\\ 0 \u0026 x \u003c 0 \\end{cases}\r\n$$\r\n\r\nEpsilon activation (EUAF):\r\n\r\n$$\r\nh(x) = \\begin{cases} 1 \u0026 x \u003e \\varepsilon \\\\ 0.5 \u0026 -\\varepsilon \\leq x \\leq \\varepsilon \\\\ 0 \u0026 x \u003c -\\varepsilon \\end{cases}\r\n$$\r\n\r\nThe ε parameter controls the width of the uncertainty band. A prediction of `0.5` means the model's net input landed too close to the decision boundary to be trusted. What you do with that signal is up to the caller — escalate to a slower model, log it, or route it differently.\r\n\r\n---\r\n\r\n## The Salted Polynomial Rolling Hash\r\n\r\nConverting variable-length text into a fixed-size numeric vector is a solved problem — but a predictable vectorization is an attack surface. If an adversary can predict where their input lands in vector space, they can craft inputs that evade the classifier.\r\n\r\n**epsilon** salts the hash with a 384-bit secret at initialization:\r\n\r\n$$\r\nH(S) = \\left( \\sum_{i=0}^{n-1} U_i \\cdot (p + S)^i \\right) \\bmod m \\;\\Big/\\; S\r\n$$\r\n\r\nWhere `S` is the salt, `p = 53` is the polynomial base, and `m = 1{,}000{,}000{,}007`. The salt shifts both the base and the output scale, making the resulting vector space private to each deployment.\r\n\r\n```python\r\nfrom secrets import randbits\r\nfrom text_vectorizer.ngram_hasher import NGramHashVectorizer\r\n\r\nsalt = randbits(384)\r\nvectorizer = NGramHashVectorizer(salt, chunk_size=150, ngram_size=3)\r\n\r\nvectors = vectorizer.vectorized_slices(\"SELECT * FROM users WHERE id=1 OR 1=1--\")\r\n# → list[list[float]]  (one sublist per 150-char chunk)\r\n```\r\n\r\n---\r\n\r\n## The Learning Rule\r\n\r\nWeights and bias update only on misclassified examples:\r\n\r\n$$\r\nw_i \\leftarrow w_i + \\eta \\,(y - \\hat{y})\\, x_i\r\n\\qquad\r\nb \\leftarrow b + \\eta \\,(y - \\hat{y})\r\n$$\r\n\r\nFeatures are normalized with z-score before training and inference so the learning rate is scale-independent:\r\n\r\n$$\r\nz_i = \\frac{x_i - \\mu_i}{\\sigma_i}\r\n$$\r\n\r\nThe normalization parameters (`μ`, `σ`) are computed from the training set and stored alongside the model weights, so inference uses the same scale as training without requiring the original dataset.\r\n\r\n---\r\n\r\n## Thread-safe Model Cache\r\n\r\nModels live in a shared LRU cache — a `threading.Lock`-guarded `OrderedDict`. Multiple threads can run inference concurrently while a new model is being loaded into a separate slot. When the cache is full, the least-recently-used model is evicted.\r\n\r\n```python\r\nfrom core.perceptron_cache import ModelCache\r\nfrom core.uncertainty_perceptron import SimplePerceptron\r\n\r\ncache = ModelCache(cache_length=10)\r\nperceptron = SimplePerceptron(cache)\r\n\r\ncache_id = perceptron.train(\r\n    epochs=30,\r\n    patience=3,\r\n    labeled_dataset_path=\"data/labeled.json\",\r\n    learning_rate=0.65,\r\n    model_metadata={\"model_name\": \"v1\", \"description\": \"...\", \"author\": \"...\"}\r\n)\r\n\r\nprediction = perceptron.inference(features=[0.82, 0.44, 0.91], cache_id=cache_id, epsilon=0.12)\r\n# → 0 | 0.5 | 1\r\n```\r\n\r\nDataset format — `list[dict]`:\r\n```json\r\n[\r\n    {\"features\": [0.82, 0.44, 0.91], \"label\": 1},\r\n    {\"features\": [0.11, 0.20, 0.30], \"label\": 0}\r\n]\r\n```\r\n\r\n---\r\n\r\n## HTTP Feature Extractors\r\n\r\nFour extractors that produce numeric features from the components of an HTTP request:\r\n\r\n| Module | Extracts |\r\n|--------|----------|\r\n| `features/uri_syntax.py` | URL length, path depth, query string |\r\n| `features/http_header.py` | Browser type, OS, referer depth, cookie count |\r\n| `features/payload_statistical.py` | Shannon entropy, digit count, special chars, max word length |\r\n| `features/client_profiler.py` | HTTP method encoding |\r\n\r\n**Shannon entropy** (normalized):\r\n\r\n$$\r\nH_{norm}(X) = \\frac{-\\sum_{i} p(x_i) \\cdot \\log_2 p(x_i)}{\\log_2 \\lvert \\Sigma \\rvert}\r\n$$\r\n\r\nHigh entropy in a short payload is a strong signal for encoding or obfuscation. Low entropy in a long payload often indicates pattern repetition typical of scanners.\r\n\r\n---\r\n\r\n## LLM Oracle for the ε-zone\r\n\r\nWhen the perceptron outputs `0.5`, the input can be passed to a language model for a second opinion. The oracle uses structured output (Pydantic) and isolates the untrusted payload inside `\u003cpayload\u003e...\u003c/payload\u003e` delimiters so prompt injection attempts are classified, not executed.\r\n\r\n```python\r\nfrom feedback.knowledge_client import KnowledgeDistillerLLM\r\n\r\ndistiller = KnowledgeDistillerLLM(\"grok-4-fast-reasoning\", api_key=\"xai-...\")\r\n\r\nresult = distiller.inference_query(\r\n    payload=\"rate: 120 req/min; \u003cpayload\u003eSELECT * FROM users WHERE 1=1--\u003c/payload\u003e\"\r\n)\r\n# → {\"label\": 1.0, \"explanation\": \"SQL tautology → SQLi. High rate compounds risk.\"}\r\n```\r\n\r\n---\r\n\r\n## Running\r\n\r\n```bash\r\n# Train and infer on the included OR gate dataset\r\npython -B -m core.uncertainty_perceptron\r\n\r\n# LRU cache isolated test\r\npython -B -m core.perceptron_cache\r\n\r\n# N-gram vectorizer\r\npython -B -m text_vectorizer.ngram_hasher\r\n```\r\n\r\n---\r\n\r\n## Dependencies\r\n\r\n```\r\nnumpy\r\npydantic\r\nxai-sdk\r\n```\r\n\r\n---\r\n\r\n## Reference\r\n\r\n\u003e Dylan Sutton Chávez (2025).\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylan-sutton-chavez%2Fhttp-threat-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdylan-sutton-chavez%2Fhttp-threat-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylan-sutton-chavez%2Fhttp-threat-classifier/lists"}