Greemty/NASCAN
GitHub: Greemty/NASCAN
面向自托管基础设施的轻量级 Linux 安全守护进程,融合 YARA 恶意文件检测、自动隔离与 eBPF 网络层 C2 关联分析,实现从文件落地到外联通信的闭环防御。
Stars: 1 | Forks: 0
# nascan
一个轻量级的 Linux 安全守护进程,可实时监控文件系统,使用 [YARA-Forge](https://github.com/YARAHQ/yara-forge) 规则扫描文件,通过 eBPF 将检测结果与网络活动进行关联,并自动隔离恶意文件。
使用 Go 语言构建,专为家庭实验室和自托管基础设施设计。
## 为什么需要它
大多数 NAS 安全工具都是闭源的、依赖云端的,或者需要专有硬件。nascan 是一个简单、可审计的守护进程,可以在你自己的 Linux 主机上运行——无需代理、无需云端、无需订阅。
## 工作原理
nascan 实现了三层防御:
```
Layer 1 — Detection (YARA)
New or modified files are scanned against 5000+ YARA-Forge rules.
Threat intel from Feodo Tracker, CINS Army, and Emerging Threats
is loaded at startup and refreshed every 24 hours (~15 000 C2 IPs).
│
▼
Layer 2 — Containment (Quarantine)
Matched files are immediately moved to an isolated directory
with chmod 000 — unreadable by any user, including root.
│
▼
Layer 3 — Monitoring (eBPF)
Kernel probes watch outbound TCP connections and process executions.
If a matched file is followed by a connection to a known C2 IP
within 60 seconds, a correlated alert is raised.
```
## 架构
```
Filesystem (NFS mount, local path, ...)
│
▼
nascan daemon (Go)
├── inotify watcher recursive, real-time file events
├── YARA scanner YARA-Forge rules (auto-downloaded)
├── Quarantine atomic move + chmod 000
├── Threat intel ~15 000 C2 IPs, refreshed every 24h
├── eBPF probes tcp_connect kprobe + execve tracepoint
├── Correlator YARA ↔ network, 60s sliding window
└── Prometheus /metrics scraped by your existing stack
```
## 快速开始
### 1. 依赖项
```
# Debian / Ubuntu
sudo apt install libyara-dev clang llvm libbpf-dev linux-headers-$(uname -r)
```
### 2. 构建
```
# 生成 eBPF objects (仅限 Linux, 需要 clang)
go install github.com/cilium/ebpf/cmd/bpf2go@latest
go generate ./internal/ebpf/
# 构建
go build -o nascan ./cmd/nascan
```
### 3. 下载 YARA-Forge 规则
```
./nascan update-rules
```
这将从 YARA-Forge 获取最新的 `yara-rules-core.yar` 规则集(约 5000 条精选规则)。
使用 `-bundle extended` 或 `-bundle full` 可获取更广泛的覆盖范围。
### 4. 运行
```
sudo ./nascan \
-watch /mnt/nas/downloads \
-quarantine /var/lib/nascan/quarantine \
-scan-existing
```
`-scan-existing` 会在切换到监控模式之前,触发对现有文件的全面扫描。
## 选项
```
-watch Path to watch (default: /mnt/nas)
-quarantine Quarantine directory — disabled if empty
-scan-existing Scan files already present at startup
-bundle YARA-Forge bundle: core | extended | full (default: core)
-rules-dir Where YARA rules are stored (default: ./rules-data)
-metrics Prometheus endpoint (default: :9100, empty to disable)
-force-update Re-download rules even if already present
```
## 监控
nascan 会在 `:9100` 端口暴露 `/metrics`。将你现有的 Prometheus 指向它:
```
scrape_configs:
- job_name: "nascan"
static_configs:
- targets: [":9100"]
```
### 指标
| 指标 | 标签 | 描述 |
|--------|--------|-------------|
| `nascan_files_scanned_total` | — | 已扫描的总文件数 |
| `nascan_scan_duration_seconds` | — | 扫描耗时直方图 |
| `nascan_yara_hits_total` | `rule`、`namespace` | 每个规则和规则集的 YARA 匹配数 |
| `nascan_quarantine_total` | `rule` | 每条 YARA 规则隔离的文件数 |
| `nascan_c2_connections_total` | — | 连接到已知 C2 IP 的出站连接数 |
| `nascan_correlations_total` | `type` | 关联事件:`yara_only`、`c2_only`、`yara_c2` |
`nascan_correlations_total` 上的 `type` 标签用于区分:
- `yara_only` — YARA 匹配,但无 C2 连接
- `c2_only` — 连接到已知 C2 IP,但无 YARA 匹配(可能存在未被检测到的恶意软件)
- `yara_c2` — YARA 匹配后发生 C2 连接(最高严重级别)
## 威胁情报源
nascan 在启动时会从三个来源聚合 IP 黑名单,并每 24 小时刷新一次:
| 源 | 覆盖范围 |
|------|----------|
| [Feodo Tracker](https://feodotracker.abuse.ch) | 僵尸网络 C2 服务器 |
| [CINS Army](http://cinsscore.com) | 约 15000 个恶意 IP |
| [Emerging Threats](https://rules.emergingthreats.net) | 被攻陷的主机 |
所有源均在内存中合并——无需数据库,无需持久化存储。重启时会自动重新获取源数据。
## eBPF 探针
nascan 在运行时会附加两个内核探针:
- **`kprobe/tcp_connect`** — 捕获所有出站 TCP 连接(PID、comm、src/dst IP、端口)
- **`tracepoint/syscalls/sys_enter_execve`** — 捕获进程执行操作
这两个探针使用环形缓冲区将事件传递到用户空间,开销极低。关联器维护一个 60 秒的活动 YARA 告警滑动窗口,并将每个网络事件与该窗口及威胁情报源进行交叉比对。
需要支持 BTF 的 Linux 内核 5.8+。已在 Debian 13(内核 6.12)上测试通过。
## 使用场景:Docker 上的 torrent 客户端 + NFS NAS
nascan 正是为这种架构设计的:
```
Torrent client (Docker)
└── downloads to NAS share (NFS mounted on Proxmox host)
└── nascan watches the mount point
├── detects malicious files immediately after download
├── quarantines them before they can be executed
└── alerts if a C2 connection follows
```
为了增加一层安全性,可以在挂载 NFS 共享时使用 `noexec` 参数——无论权限如何,内核都将拒绝执行该挂载中的任何二进制文件:
```
/mnt/nas/downloads 10.0.0.0/24(rw,noexec,nosuid)
```
## 许可证
MIT
标签:C2通信检测, Docker镜像, Go语言, inotify, IP 地址批量处理, NAS安全, YARA, 云资产可视化, 威胁情报, 子域名枚举, 实时安全, 客户端加密, 家庭实验室, 开发者工具, 搜索语句(dork), 文件系统监控, 日志审计, 沙箱, 程序破解, 系统安全, 网络安全, 自动隔离, 自定义请求头, 自托管基础设施, 轻量级守护进程, 隐私保护