Fontailll/NetWatch
GitHub: Fontailll/NetSentinel
NetWatch 是一款跨平台网络事件响应工具,扫描活动 TCP/UDP 连接并映射到进程,通过风险评分引擎帮助分析师调查和拦截可疑网络活动。
Stars: 1 | Forks: 0
# NetWatch
**网络事件响应工具** — 扫描活动的 TCP/UDP 连接,计算风险评分,导出为 JSON,并通过防火墙进行拦截。
跨平台 (Linux + Windows),C++17。
[](https://github.com/Fontailll/NetWatch/actions/workflows/build.yml)
[](https://github.com/Fontailll/NetWatch/releases/latest)
[](LICENSE)
[]()
[]()
[](https://github.com/Fontailll/NetWatch/releases/latest)
[]()
## 快速开始 (单行命令)
### Linux
```
# 自动检测发行版,下载预编译二进制文件或从源码构建
curl -sL https://raw.githubusercontent.com/Fontailll/NetWatch/main/install.sh | sudo bash
# 使用所有功能进行扫描
sudo netwatch --all
```
### Windows (以管理员身份运行 PowerShell)
```
# 1. 从 https://www.msys2.org 下载并安装 MSYS2
# 2. 从开始菜单打开“MSYS2 UCRT64”
# 3. 更新包:
pacman -Syu
# 4. 安装 GCC、CMake 和 Ninja(pacman 是 MSYS2 的包管理器):
pacman -S mingw-w64-ucrt-x86_64-gcc cmake ninja
# 5. 构建:
cd /c/Users/ereno/Desktop/netwatch
mkdir build && cd build
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release
ninja
# 6. 运行:
./netwatch.exe --all
```
## Linux 安装说明
### 方式 1 — 自动安装 (推荐)
```
curl -sL https://raw.githubusercontent.com/Fontailll/NetWatch/main/install.sh | sudo bash
sudo netwatch --all
```
该脚本将:
1. 尝试从 GitHub Releases 下载**完全静态预编译的二进制文件**
2. 如果失败,则检测您的发行版,安装依赖项并从源码构建
3. 安装至 `/usr/local/bin/netwatch`
### 方式 2 — 预编译二进制文件
```
curl -sL https://github.com/Fontailll/NetWatch/releases/latest/download/netwatch-linux-x86_64 -o netwatch
chmod +x netwatch
sudo ./netwatch --all
```
该二进制文件是**静态链接**的 —— 无需任何依赖即可在任何 Linux 发行版上运行。
### 方式 3 — 从源码构建
```
# 依赖
# Debian/Ubuntu: sudo apt install build-essential cmake
# Fedora: sudo dnf install gcc-c++ cmake make
# Arch: sudo pacman -S gcc cmake make
# Alpine: sudo apk add g++ cmake make
# openSUSE: sudo zypper install gcc-c++ cmake make
git clone https://github.com/Fontailll/NetWatch.git
cd NetWatch
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
sudo ./netwatch --all
```
### 作为服务安装
#### systemd (Fedora, Debian 8+, Ubuntu 16+, Arch)
```
sudo ./netwatch --install-service systemd > /etc/systemd/system/netwatch.service
sudo systemctl daemon-reload
sudo systemctl enable --now netwatch
sudo systemctl status netwatch
```
#### runit (Void Linux, Artix)
```
sudo mkdir -p /etc/sv/netwatch
sudo ./netwatch --install-service runit > /etc/sv/netwatch/run
sudo chmod +x /etc/sv/netwatch/run
sudo ln -s /etc/sv/netwatch /var/service/
```
#### OpenRC (Gentoo, Alpine)
```
sudo ./netwatch --install-service openrc > /etc/init.d/netwatch
sudo chmod +x /etc/init.d/netwatch
sudo rc-update add netwatch default
sudo rc-service netwatch start
```
#### 自动检测
```
sudo ./netwatch --install-service auto
```
### Daemon 模式
```
sudo ./netwatch --watch --daemon --pidfile /run/netwatch.pid --logfile /var/log/netwatch.log
```
## Windows 安装说明
### 前置条件
安装 [MSYS2](https://www.msys2.org) (推荐使用 UCRT64 变体)。
### 构建
打开 **MSYS2 UCRT64** 终端并运行:
```
# 首次设置(如果尚未完成)
pacman -Syu # Update package database
pacman -S mingw-w64-ucrt-x86_64-gcc cmake ninja # Install toolchain
# 构建
cd /c/Users/ereno/Desktop/netwatch # Use /c/ for C:\
mkdir build && cd build
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release
ninja
```
### 运行
```
netwatch.exe # Colored table output
netwatch.exe --all # All features (signature + detail)
netwatch.exe --json # JSON output (pipe-friendly)
netwatch.exe --export report.json
netwatch.exe --watch # Real-time monitoring
```
**注意事项:**
- 需以**管理员身份**运行才能获取完整的连接可见性
- 签名验证使用 WinVerifyTrust (Authenticode)
- 二进制文件为静态链接 —— 无需 MSYS2 DLL
- 扫描完成后按 Enter 键退出 (双击运行时会保持窗口打开状态)
## 用法
```
Usage: netwatch [OPTIONS]
Options:
--export Export connections to JSON file
--json Print connections as JSON to stdout
--detail Show detailed info for high-risk connections
--verify-sig Enable digital signature verification
--geoip Enable GeoIP country lookup
--geoip-db Path to GeoIP CSV database (implies --geoip)
--watch Real-time monitoring mode
--watch-interval Watch polling interval (default: 5)
--all Enable all features (--verify-sig --geoip --detail)
--daemon Run as daemon (Linux, requires --watch)
--pidfile PID file path (for --daemon)
--logfile Log file path (for --daemon)
--install-service Print service template (systemd/runit/openrc/auto)
--version Show version and platform info
--help, -h Show this help
```
## 风险评分引擎
| 规则 | 评分 | 描述 |
|------|-------|-------------|
| 未签名的可执行文件 | +30 | 进程未签名 (需要 `--verify-sig`) |
| Temp 文件夹 | +25 | 从 Temp 或 AppData\Local\Temp 运行 |
| 恶意软件端口 | +20 | 连接到已知的 C2/恶意软件端口 |
| 未知发布者 | +15 | 已签名的二进制文件但没有发布者名称 |
| 可疑进程名 | +15 | powershell, cmd, curl, wget 等 |
| 隐藏进程 | +10 | PID 存在但名称为空 |
| 异常监听端口 | +10 | 正在非标准的低端口号上监听 |
| 外部连接 | +5 | 远程 IP 不在私有地址范围内 (需要 `--geoip`) |
**风险等级:** 低 (0-29) · 中 (30-59) · 高 (60-79) · 严重 (80+)
## 项目结构
```
netwatch/
├── include/ # Common headers (interfaces)
│ ├── scanner.h IScanner interface (scan_tcp, scan_udp)
│ ├── connection.h Connection struct + enums
│ ├── risk.h RiskEngine (modular rules)
│ ├── output.h Table, detail, help, version output
│ ├── json_export.h Minimal JSON writer
│ ├── firewall.h IFirewall interface
│ ├── process.h ProcessInfo struct
│ ├── geoip.h GeoIP lookup module
│ ├── daemon.h Daemonize + service templates
│ └── utils.h String helpers
├── src/ # Cross-platform implementation
│ ├── main.cpp CLI parser, scan pipeline
│ ├── scanner.cpp IScanner::scan() + factory
│ ├── risk.cpp 8 modular risk rules
│ ├── output.cpp Colored table, detail cards, help
│ ├── json_export.cpp
│ ├── geoip.cpp
│ ├── daemon.cpp fork/setsid + systemd/runit/openrc
│ ├── firewall.cpp IFirewall factory
│ └── utils.cpp
├── platform/
│ ├── windows/ Win32 API (GetExtendedTcpTable, WinVerifyTrust, COM)
│ └── linux/ /proc/net, iptables/nftables
├── tests/ Catch2 unit tests (21 tests)
└── install.sh One-liner Linux installer
```
## 许可证
MIT
标签:Bash脚本, C++, Linux, Mr. Robot, WSL, 库, 应急响应, 数据擦除, 网络安全, 防火墙, 隐私保护