承接 rafalmasiarek/threat-detector 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rafalmasiarek/threat-detector

Composer 安装命令:

composer require rafalmasiarek/threat-detector

包简介

Heuristic, modular threat detection (signal-only) with weighted float scoring, predefined thresholds, and PSR-15 middleware for PSR-7 apps.

README 文档

README

Latest Version License PHP

Heuristic, modular threat detection (signal-only) with weighted float scoring, predefined thresholds, and PSR-15 middleware for PSR-7 applications.

⚠️ This library is a signal generator. It does not replace proper validation/sanitization/escaping, CSP, prepared statements, etc.

Features

  • 🧩 Modular scanners — each category (XSS, SQLi, SSRF, …) in a separate class.
  • ⚖️ Weighted float scoring — per-category weights; combine multiple signals.
  • 🎚️ Predefined thresholdsLOW, MEDIUM, HIGH (or custom floats).
  • 🧵 PSR-15 middleware — scan query, body, headers, cookies; annotate request; optional header X-Threat-Suspect.
  • 📝 phpDocs & comments — production-friendly code with clear docs.
  • Unit tests — a couple of quick checks to get you started.
  • 📂 Examples — basic HTML form + PSR-15 middleware demo.

Requirements

  • PHP 8.1+
  • ext-mbstring

Installation

Using Composer:

composer require rafalmasiarek/threat-detector

If you are using this repository locally (path repo):

composer config repositories.threat-detector path ./threat-detector
composer require rafalmasiarek/threat-detector:dev-main

Quick Start (Core)

use rafalmasiarek\Threat\Core\ThreatDetector;
use rafalmasiarek\Threat\Core\ScoringPolicy;
use rafalmasiarek\Threat\Core\Thresholds;

// Create a policy with default weights and MEDIUM threshold
$policy = ScoringPolicy::withDefaults()
    ->withThreshold(Thresholds::MEDIUM)  // or 'LOW' | 'HIGH' | 3.5 (float)
    ->withWeight('SQLI', 2.25);          // optional: override category weights

$detector = ThreatDetector::default($policy);

// Scan a string
$input = "<script>alert(1)</script>";
$result = $detector->scanString($input);

var_dump($result->suspect); // bool
var_dump($result->score);   // float
var_dump($result->hits);    // array{category => list<string>}
var_dump($result->norm);    // normalized input

Example output:

bool(true)
float(3)
array(1) {
  ["XSS"]=>
  array(2) {
    [0]=> string(10) "TAG_SCRIPT"
    [1]=> string(9)  "HTML_TAGS"
  }
}
string(23) "<script>alert(1)</script>"

Quick Start (PSR-15 Middleware)

use rafalmasiarek\Threat\Middleware\ThreatDetectMiddleware;

$middleware = new ThreatDetectMiddleware([
    'threshold'    => 'MEDIUM',          // 'LOW' | 'MEDIUM' | 'HIGH' | float
    'weights'      => ['SQLI' => 2.1],   // optional overrides
    'scan_query'   => true,
    'scan_body'    => true,
    'scan_headers' => false,             // true or array of headers to scan
    'scan_cookies' => false,
    'attribute'    => 'threat.result',   // request attribute name
    'set_header'   => true,              // add X-Threat-Suspect: 1 when suspect
]);

// Add to your PSR-15 stack (Slim/Mezzio/etc.)
$result = $request->getAttribute('threat.result'); 

Example result:

[
  'suspect' => true,
  'score'   => 3.5,
  'hits'    => ['XSS' => ['TAG_SCRIPT','HTML_TAGS']],
]

Scoring

  • Weights: per category (e.g., SQLI=2.0, CMD_INJECTION=2.5, CRLF=1.0).
  • Score formula:
    score = Σ ( weight(category) × unique_hits(category) )
    
  • Threshold: request is suspect when score ≥ threshold.

Predefined thresholds

Name Value Sensitivity
LOW 1.0 Very sensitive
MEDIUM 2.5 Balanced (default)
HIGH 5.0 Strict

Examples

  • <script>alert(1)</script>
    Hits: XSS=[TAG_SCRIPT, HTML_TAGS]
    Score: 1.5 × 2 = 3.0 → suspect at MEDIUM

  • UNION SELECT password FROM users
    Hits: SQLI=[UNION_SELECT]
    Score: 2.0 × 1 = 2.0 → not suspect at MEDIUM, suspect at LOW

Categories & Scanners

  • XSS — inline event handlers, <script>, javascript: URIs.
  • SQLIUNION SELECT, SLEEP(), INFORMATION_SCHEMA, etc.
  • CMD_INJECTION — subshells, ;, &&, wget/curl, redirects.
  • PATH_TRAVERSAL../, URL-encoded traversal, file://, wrappers.
  • CRLF — header injection sequences.
  • SSRF — URLs to localhost, 127.0.0.1, RFC1918 ranges.
  • XXE<!DOCTYPE>, <!ENTITY>, external SYSTEM.
  • NOSQL — Mongo-like operators $where, $regex.
  • LDAP — wildcards, null bytes.
  • SERIALIZATION — PHP serialized payload patterns.

Integration Ideas

  • Add to Slim/Mezzio pipeline as PSR-15 middleware.
  • Run against form input before sending mail (contact forms).
  • Log suspect inputs into a security audit trail.
  • Flag suspicious requests in rate-limiting / WAF logic.

Tests & Examples

  • PHPUnit tests included (tests/):
    • TruePositiveDetectionsTest.php
    • FalsePositiveHeuristicsTest.php
  • Example apps in examples/:
    • basic/ (HTML form demo)
    • psr15/ (middleware demo)

Run tests:

./vendor/bin/phpunit --colors=always

Security Notice

This library generates signals only.
Always combine with:

  • Prepared statements for SQL queries
  • Proper HTML escaping and CSP
  • Strong input validation

Folder Structure

src/
  Contracts/ScannerInterface.php
  Core/{ThreatDetector.php, ThreatResult.php, ScoringPolicy.php, Thresholds.php}
  Scanner/{XssScanner.php, SqliScanner.php, CmdInjectionScanner.php, PathTraversalScanner.php, CrlfScanner.php, SsrfScanner.php, XxeScanner.php, NoSqlScanner.php, LdapScanner.php, SerializationScanner.php}
  Middleware/ThreatDetectMiddleware.php
tests/
  ModularThreatDetectorTest.php
examples/
  basic/
  psr15/

License

MIT

rafalmasiarek/threat-detector 适用场景与选型建议

rafalmasiarek/threat-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 rafalmasiarek/threat-detector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rafalmasiarek/threat-detector 我们能提供哪些服务?
定制开发 / 二次开发

基于 rafalmasiarek/threat-detector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 49
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-07