tradewithmeai/network-security-monitor

GitHub: tradewithmeai/network-security-monitor

一款基于 Rust 的 Windows 本地网络安全监控器,提供实时流量检测、Suricata 规则联动与 Web 仪表板的一体化解决方案。

Stars: 0 | Forks: 0

# 网络安全监控器 用于 Windows 的被动网络安全监控器。捕获本地接口上的流量,运行检测规则,并提供实时 Web 仪表板。作为 Windows 服务运行,并附带系统托盘伴侣程序。 ## 功能 - **数据包捕获** — 通过 Npcap 实时捕获(混杂模式,BPF 过滤支持,可选 pcap 导出) - **与 Suricata 兼容的规则引擎** — 加载 `.rules` 文件,支持内容匹配、阈值、流量方向和规则变量 - **端口扫描检测** — 独立的 SYN、Connect 扫描和 UDP 扫描检测器,支持可配置阈值 - **连接跟踪** — 跟踪活动的 TCP/UDP 会话及其状态与持续时间 - **设备安全** — 基于 MAC 地址的设备画像、DNS 分析、 rogue DNS 服务器检测、IP 变更告警 - **GeoIP** — 对外部 IP 进行国家查询(需要 MaxMind GeoLite2-City 数据库) - **Web 仪表板** — 单页应用,支持实时数据包流、告警、连接、DNS、设备和规则页面 - **WebSocket 推送** — 实时统计、告警和数据包事件推送到仪表板 - **Webhook 通知** — 将告警推送到 Slack、Discord 或任意 HTTP 端点 - **AI 告警分析** — 通过 Claude API 为告警提供英文描述,包含用户活动上下文(若配置了 [Little Brother](https://github.com/your-org/little-brother-v4)) - **Betty Sentinel 集成** — 通过 HTTP 向 Sentinel 代理报告健康状态与统计信息 - **Windows 服务** — 安装到系统管理控制台,支持失败自动重启和每日滚动日志 - **系统托盘伴侣** — 被动状态显示,打开仪表板,不具备服务控制权限 ## 要求 - Windows 10/11 (x64) - [Npcap](https://npcap.com/) 安装并启用 **WinPcap API 兼容模式** - [Rust 工具链](https://rustup.rs/)(用于从源码构建) ## 安装 在项目目录中以 **管理员身份** 运行 PowerShell 或 cmd: ``` powershell -ExecutionPolicy Bypass -File install.ps1 ``` 此操作将: 1. 构建发布二进制文件(`cargo build --release`) 2. 将 `nsm.exe`、`config.toml` 和 `rules\` 复制到 `%LOCALAPPDATA%\nsm\` 3. 将该目录添加到用户 PATH 4. 安装并启动 Windows 服务(SCM,故障时自动重启) 5. 在启动文件夹中创建托盘伴侣快捷方式 安装完成后打开新终端 — 此时 `nsm` 可在任何位置使用。 ### 选项 ``` .\install.ps1 -NoService # Skip service install (run manually with 'nsm run') .\install.ps1 -NoTray # Skip tray autostart .\install.ps1 -Uninstall # Stop service, remove files, clean PATH ``` ### 更新 重新运行项目目录中的 `install.ps1`。二进制文件将被替换,服务会自动重启。`config.toml` 不会被覆盖。 ## 手动运行 ``` nsm run # Start the monitor (console mode, Ctrl+C to stop) nsm run --device "Ethernet" # Override capture device nsm devices # List available capture devices nsm tray # Start the tray companion only nsm install # Install Windows service (requires Administrator) nsm uninstall # Remove Windows service (requires Administrator) nsm --install-autostart # Add tray to Windows Startup folder nsm --remove-autostart # Remove tray from Windows Startup folder ``` ## 仪表板 在浏览器中打开 **http://127.0.0.1:8080**。 | 页面 | 描述 | |---|---| | 仪表板 | 实时统计、最近告警、流量汇总 | | 告警 | 完整告警历史,支持确认与 AI 分析 | | 流量 | 实时数据包流 | | 连接 | 活动的 TCP/UDP 会话 | | 设备 | 每台设备的安全画像与 DNS 活动 | | DNS | DNS 查询日志与 rogue 服务器告警 | | 安全 | 设备安全事件与异常 | | 规则 | 已加载的检测规则 | ## 配置 配置文件位于 `%LOCALAPPDATA%\nsm\config.toml`(安装后)或与二进制文件同目录(手动运行)。 ### 主要配置节 ``` [capture] device = "Wi-Fi" # Interface name — use 'nsm devices' to list promiscuous = true bpf_filter = "" # Optional BPF filter expression pcap_export = false # Write raw packets to pcap files pcap_dir = "captures" [detection] port_scan_enabled = true port_scan_syn_threshold = 15 # SYN packets to distinct ports in time window port_scan_time_window_secs = 60 rule_engine_enabled = true rule_files = ["rules/*.rules"] geoip_database = "" # Path to GeoLite2-City.mmdb [detection.rule_vars] HOME_NET = "192.168.0.0/16,10.0.0.0/8,172.16.0.0/12" EXTERNAL_NET = "!$HOME_NET" [web] listen_addr = "127.0.0.1" listen_port = 8080 api_keys = [] # Empty = no auth (fine for local use) [alerting] dedup_window_secs = 300 # Suppress duplicate alerts within this window [ai] enabled = false api_key = "" # Anthropic API key model = "claude-haiku-4-5-20251001" little_brother_url = "" # Optional: http://127.0.0.1:5000 for activity context [betty] enabled = false url = "http://127.0.0.1:8400" agent_id = "nsm-desktop" secret_hex = "" ``` ### Webhook ``` [[webhooks]] name = "slack-alerts" url = "https://hooks.slack.com/services/XXX/YYY/ZZZ" min_severity = "high" format = "slack" # slack | discord | json [[webhooks]] name = "discord-alerts" url = "https://discord.com/api/webhooks/XXX/YYY" format = "discord" ``` ## 检测规则 规则使用 Suricata 语法,位于 `rules/*.rules`。内置六条默认规则: | SID | 描述 | |---|---| | 1000001 | SSH 暴力破解(60 秒内对 22 端口发起 5+ 次 SYN) | | 1000002 | DNS 区域传输尝试(AXFR) | | 1000003 | ICMP 扫描(30 秒内发起 10+ 次 ping) | | 1000004 | 非标准端口的 HTTP 流量 | | 1000005 | Telnet 连接尝试 | | 1000006 | TCP 端口扫描(60 秒内发起 20+ 次 SYN) | 将自定义 `.rules` 文件添加到 `rules\` 目录并重启服务。规则支持:`alert`、`content`、`offset`、`depth`、`nocase`、`flow`、`flags`、`threshold`、`classtype`、`priority`、`sid`、`rev`。 ## AI 告警分析 当配置了 Anthropic API 密钥后,仪表板的每一行告警会显示一个 **AI** 按钮。点击后将告警详情发送至 Claude,并返回英文描述、可能原因与建议操作。结果会被缓存 — 后续点击即时显示。 若本地运行了 [Little Brother](https://github.com/your-org/little-brother-v4) 实例,告警触发时的活动应用与浏览器标签页会包含在提示中,以提升开发者工作站的上下文信息。 ``` [ai] enabled = true api_key = "sk-ant-..." model = "claude-haiku-4-5-20251001" little_brother_url = "http://127.0.0.1:5000" # optional context_window_minutes = 3 ``` ## GeoIP 设置 1. 在 [maxmind.com](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data) 创建免费账户 2. 下载 **GeoLite2-City.mmdb** 3. 在配置中设置路径: ```toml [detection] geoip_database = "C:/path/to/GeoLite2-City.mmdb" ``` ## 服务管理 ``` sc start nsm # Start sc stop nsm # Stop sc query nsm # Status ``` 日志写入至 `%LOCALAPPDATA%\nsm\logs\`,按天滚动存储为 JSON 文件。 服务在 SCM 中配置了故障恢复动作:失败后 10 秒、30 秒、60 秒依次重启。 ## 系统托盘伴侣 `nsm tray` 显示系统托盘图标,实时从服务拉取状态。它仅为被动显示界面 — 无法启动、停止或重启服务(由 Windows 服务管理器负责)。 **菜单项:** - 显示捕获设备与当前告警数量 - **打开仪表板** — 在默认浏览器中打开 http://127.0.0.1:8080 - **随 Windows 启动** — 在启动文件夹中切换托盘快捷方式 - **退出** — 关闭托盘伴侣;服务将继续运行 托盘伴侣启动时不带控制台窗口,可通过 `nsm --install-autostart` 在登录时自动启动。 ## 项目结构 ``` src/ ai/ Claude API integration and alert enrichment alert/ Alert types and management betty/ Betty Sentinel agent (HTTP heartbeat and health reporting) capture/ Packet capture (pcap + etherparse), pcap export config.rs Configuration structs and TOML loading detection/ Port scan detector, rule engine, device profiler, GeoIP service.rs Windows SCM integration storage/ SQLite schema, queries, batched writer, pruning tray.rs System tray companion autostart.rs Windows Startup folder shortcut management web/ Axum REST API, WebSocket, webhook delivery, static assets rules/ default.rules Built-in detection rules web/ index.html Dashboard shell js/ Frontend SPA (vanilla JS) css/ Styles install.ps1 Windows installer script config.toml Default configuration ```
标签:AI告警分析, AMSI绕过, Betty Sentinel, Discord告警, DNS分析, DNS劫持检测, Elastic, GeoIP, HTTP Webhook, IP变更告警, MAC设备识别, Metaprompt, Npcap, Packet Capture, pcap导出, Port Scan Detection, REST API, Rust, Slack告警, Suricata, Webhook, WebSocket, Web仪表盘, Wildcard支持, Windows服务, 云计算, 企业安全, 依赖分析, 力导向图, 协议安全, 可视化界面, 地理定位, 威胁检测, 安全态势感知, 异常DNS服务器, 日志滚动, 流量捕获, 现代安全运营, 端口扫描检测, 端点安全, 系统托盘, 网络安全, 网络拓扑, 网络流量分析, 网络流量审计, 网络资产管理, 自动重启, 补丁管理, 被动监控, 规则引擎, 设备画像, 连接跟踪, 通知系统, 隐私保护