fkie-cad/yapscan
GitHub: fkie-cad/yapscan
基于 YARA 的进程与文件扫描器,提供精细的内存段过滤、加密规则加载和丰富的报告功能。
Stars: 61 | Forks: 14
# yapscan  [](https://codecov.io/gh/fkie-cad/yapscan) [](https://goreportcard.com/report/github.com/fkie-cad/yapscan)
Yapscan 是一个基于 **YA**ra 的**进程扫描器** (**P**rocess **SCAN**ner),旨在提供更多关于扫描内容的控制,并提供关于匹配项的详细报告。
**报告格式现已版本化,并发布了具有兼容性保证的稳定版本 1.1.0,请参阅 [报告格式文档](report/v1.1.0/README.md)。**
## 功能
你可以使用 yapscan 选择性地扫描运行进程的内存,以及本地硬盘和/或挂载共享中的文件。
与标准 yara 最显著的区别在于(参见 [用法](#usage) 章节),
- 支持从加密的 zip 文件加载 yara 规则,以防止杀毒软件将规则检测为恶意软件。
- 也可以递归地从目录中加载多个 yara 规则。
- 可以暂停要扫描的进程(请谨慎使用,可能会导致系统崩溃)。
- 允许根据大小、类型(映像、映射、私有)、状态(提交、空闲、保留)和权限过滤要扫描的内存段。
- 允许轻松扫描所有正在运行的进程、本地驱动器和/或挂载的共享。
- 附带丰富的报告功能,以便后续分析规则的有效性。
- 匹配的内存段甚至可以自动转储并存储为
其他提升生活质量的功能包括
- (实验性)报告服务器:在安全机器上运行 `yapscan receive :8000`,并在受感染的机器上使用 `--report-server` 标志运行 `yapscan scan`。不再因为勒索软件而导致报告丢失。
- 静态构建、无依赖的 Windows exe
- 列出正在运行的进程
- 列出和转储特定进程的内存段
- 编译 yara 规则并将其压缩成加密的 zip。
- 为 VDI 等锁定环境提供“可执行 DLL”(参见 [可执行 DLL](#executable-dll) 章节)
- 使用预定义或随机生成的盐对报告进行匿名化处理
Yapscan 同时支持 Windows 和 Linux,但 Windows 是主要且测试最充分的目标操作系统。
## 用法
*我很快会写一份正规的手册页。目前,请使用* `yapscan --help` 和 `yapscan --help` 获取用法信息。
```
COMMANDS:
list-processes, ps, lsproc lists all running processes
list-process-memory, lsmem lists all memory segments of a process
dump dumps memory of a process
scan scans processes or paths with yara rules
receive starts a server receiving reports from other yapscan clients (see --report-server flag of scan command)
anonymize anonymize reports
zip-rules creates an encrypted zip containing compiled yara rules
join joins dumps with padding
crash-process, crash crash a process
help, h Shows a list of commands or help for one command
```
```
> yapscan scan --help
NAME:
yapscan scan - scans processes or paths with yara rules
USAGE:
yapscan scan [command options] [pid/path...]
OPTIONS:
--rules value, -r value, -C value path to yara rules file or directory, if it's a file it can be a yara rules file or a zip containing a rules file encrypted with password "infected"
--rules-recurse, --recurse-rules, --rr if --rules specifies a directory, compile rules recursively (default: false)
--all-processes, --all-p scan all running processes (default: false)
--all-drives, --all-d scan all files in all local drives, implies --recurse (default: false)
--all-shares, --all-s scan all files in all mounted net-shares, implies --recurse (default: false)
--file-extensions value, -e value [ --file-extensions value, -e value ] list of file extensions to scan, use special extension "-" as no extension, use --file-extensions "" to allow any (default: "-", "so", "exe", "dll", "sys")
--threads value, -t value number of threads (goroutines) used for scanning files (default: 6)
--full-report create a full report (default: false)
--scan-mapped-files when encountering memory-mapped files also scan the backing file on disk (default: false)
--report-dir value the directory to which the report archive will be written (default: current working directory)
--report-server value the address of the server, the reports will be sent to
--server-ca value CA.pem to use when validating the server
--client-cert value certificate.pem to use for client authentication
--client-key value key.pem to use for client authentication
--store-dumps store dumps of memory regions that match rules, implies --full-report, the report will be encrypted with --password (default: false)
--password value, -p value setting this will encrypt the report with the given password; ignored without --full-report
--pgpkey value, -k value setting this will encrypt the report with the public key in the given file; ignored without --full-report
--anonymize anonymize any output, hashing any usernames, hostnames and IPs with a salt (default: false)
--salt value the salt (base64 string) to use for anonymization, ignored unless --anonmyize is provided (default: random salt)
--verbose, -v show more information about rule matches (default: false)
--filter-permissions value, --f-perm value only consider segments with the given permissions or more, examples: "rw" includes segments with rw, rc and rwx
--filter-permissions-exact value, --f-perm-e value [ --filter-permissions-exact value, --f-perm-e value ] comma separated list of permissions to be considered, supported permissions: r, rw, rc, rwx, rcx
--filter-type value, --f-type value [ --filter-type value, --f-type value ] comma separated list of considered types, supported types: image, mapped, private
--filter-state value, --f-state value [ --filter-state value, --f-state value ] comma separated list of considered states, supported states: free, commit, reserve (default: "commit")
--filter-size-max value, --f-size-max value maximum size of memory segments to be considered, can be absolute (e.g. "1.5GB"), percentage of total RAM (e.g. "10%T") or percentage of free RAM (e.g. "10%F") (default: "10%F")
--filter-size-min value, --f-size-min value minimum size of memory segments to be considered
--filter-rss-ratio-min value, --f-rss-min value minimum RSS/Size ratio of memory segments to eb considered
--suspend, -s suspend the process before reading its memory (default: false)
--force, -f don't ask before suspending a process (default: false)
--help, -h show help (default: false)
```
以下是一些额外的用法示例
```
# 创建 rules zip(可选)
yapscan zip-rules --output rules.zip rules.yara
# 使用默认过滤器扫描 PID 为 423 的进程
yapscan scan -r rules.zip 423
# 使用默认过滤器扫描所有进程
yapscan scan -r rules.zip --all-processes
# 使用默认过滤器扫描所有进程和所有本地驱动器
yapscan scan -r rules.zip --all-processes --all-drives
# 使用默认过滤器扫描所有内容
yapscan scan -r rules.zip --all-processes --all-drives --all-shares
# 仅扫描具有执行权限或更高权限的内存段
yapscan scan -r rules.zip --filter-permissions x --all-processes
# 仅扫描具有读取和执行权限的内存段(无写入)
yapscan scan -r rules.zip --filter-permissions-exact rx --all-processes
# 启用日志记录、报告和匹配段的自动转储
yapscan --log-level debug --log-path yapscan.log scan -r rules.zip --full-report --store-dumps --all-processes
```
## 作为服务运行
Yapscan 可以作为 Windows 服务运行,以获取 SYSTEM 权限。
这允许你使用 crash 命令甚至使其他 Windows 服务崩溃。
作为服务运行目前是一个**实验性功能**。
对于内存扫描,这应该是不必要的。
在我的实验中,以管理员身份运行 yapscan 足以读取任何进程的内存。
如果你发现某个进程在具有管理员权限的情况下 yapscan 无法扫描,但作为服务却可以扫描,请在 [问题](https://github.com/fkie-cad/yapscan/issues/new) 中告诉我。
为了将 yapscan 作为服务使用,只需将 `as-service` 命令添加到你希望执行的命令(和标志)之前。
示例:
```
# Normal mode(普通模式)
.\yapscan.exe crash 42
# Service mode(服务模式)
.\yapscan.exe as-service crash 42
```
Windows 服务的输出通过两个 TCP 连接传输到终端。
如果中断,将发出警告。
在这种情况下,服务可能仍在运行,只是你看不到任何输出。
此外,CTRL-C 会中断代理命令,使你无法看到任何输出,但不会影响正在运行的服务。
如果你想终止服务,目前必须使用 Windows 服务管理器。
## 可执行 DLL
**此项目构建的 DLL 不是用于从中导入函数的常规 DLL。**
相反,它的行为类似于 exe,具有两个导出的高级入口点:
```
// start acts same as you would expect a main function to act.
// It assumes a terminal with stdout/stderr and stdin has already
// been allocated.
extern int start(int argc, char** argv);
// run is meant for use with rundll32.
// It opens a new console window via AllocConsole(), then parses the
// lpCmdLine to extract the arguments and calls starts yapscan
// with the extracted arguments.
extern void run(HWND hWnd, HINSTANCE hInst, LPTSTR lpCmdLine, int nCmdShow);
```
某些环境(如 VDI(虚拟桌面基础架构))可能会阻止执行任意 exe 文件,但仍允许使用任意 DLL。
如果你在此类环境中获得了命令行终端的访问权限,可以像下面这样通过构建的 DLL 调用 yapscan。
```
rundll32.exe yapscan.dll,run scan -r rules.zip --all-processes
```
**注意**:此功能仍处于实验阶段!
参数解析很可能存在一些问题。
## 项目状态
**测试版 (BETA),功能冻结**
目前 yapscan 处于测试期。
我目前正在努力发布稳定的 1.0 版本。
此版本将拥有定义明确且有文档记录的稳定 API 和 CLI 接口,在所有 1.x 版本中将保持向后兼容。
在发布 1.0 之前,我还希望至少有合理的测试覆盖率。
欢迎随意使用和测试,并为发现的任何错误提交 issue。
如果你希望添加某些功能,也可以提交带有功能请求的 issue,但在 1.0 发布之前,我不会致力于开发或合并任何新功能。
## 扫描技术
实际扫描工作交由 yara 库完成。
在文件扫描的情况下,使用高级 yara 库函数 `yr_rules_scan_file`。
此函数将给定文件进行内存映射。
另一方面,扫描进程内存是在较低级别上完成的。
Yapscan 一次将一个内存段复制到其自身内存的缓冲区中,然后使用 `yr_rules_scan_mem` 扫描此缓冲区。
## 构建 Yapscan
要在 **Linux 上原生**为 Linux 构建,你需要安装 Go 和 yara 库。
安装好依赖项后,只需执行:
```
# 安装 Golang 和 libyara
git clone https://github.com/fkie-cad/yapscan
cd yapscan/cmd/yapscan
go build
```
如果你想在 Linux 上为 Windows 构建,只需安装 docker。
```
# 安装 docker
git clone https://github.com/fkie-cad/yapscan
cd yapscan/cicd/
./crossBuildForWindows.sh
```
生成的二进制文件将放置在 `cicd/build/` 中。
使用 MSYS2 在 **Windows 上原生**构建,请遵循以下说明
1. 安装 Go
2. 安装 MSYS2 并按照 [MSYS2 网站] 上的初步步骤通过 pacman 进行更新。
3. 安装构建依赖项 `pacman --needed -S base-devel git autoconf automake libtool mingw-w64-{x86_64,i686}-{gcc,make,pkgconf}`
4. 在 `cicd/` 目录中打开 PowerShell 并执行 `.\buildOnWindows.ps1 -MsysPath -BuildDeps`
其中 `` 是 MSYS2 的安装目录,默认为 `C:\msys64`。
**注意:**依赖项安装完成后,你需要在 MSYS 窗口中按 `Enter`。
5. 在 `cicd/build/` 中享受构建好的文件
如果你想在 Windows 上运行测试,只需运行一次 `.\cicd\buildOnWindows.ps1 -BuildDeps`。
然后打开 PowerShell 并执行 `.\cicd\enableMingw.ps1 -MsysPath ` 以设置相应的环境变量。
现在只需执行 `go test -tags yara_static ./...`。
如果你使用构建脚本,`-tags yara_static` 是必要的,因为它们不安装任何 Windows DLL,只安装静态库。
**注意:**在 Windows 上使用 `-race` 可能会遇到莫名其妙的失败。
根据 [golang 1.14 发布说明](https://golang.org/doc/go1.14#compiler),新的指针算术检查在 Windows 上有些过于严格。
在 Golang v1.15 中,这个问题似乎尚未修复,但检查会自动启用。
你可以像这样停用它们 `go test -tags yara_static -race -gcflags=all=-d=checkptr=0 ./...`。
你不必依赖 powershell/bash 脚本,但它们的目的是以牺牲对编译的控制为代价,使事情尽可能简单。
如果你想要更多控制,请查看脚本的使用内容并修改它们,或者单独执行命令。
脚本执行以下任务。
1. 启动 "MSYS2 MinGW 64-bit"
1. 从 github 下载 OpenSSL
2. 静态构建 OpenSSL 并安装开发文件
3. 从 github 下载 libyara
4. 静态构建 libyara 并安装它
2. 在 powershell 中设置一些环境变量,以允许使用 mingw 工具链
3. 使用适当的构建标签调用 `go build` 命令进行静态构建
## 致谢与提及
- 感谢 [@hillu]([go-yara] 的作者),为我指明了在 Windows 上原生构建的正确方向。
如果你想了解更多详情,请参阅 #7 及其中的链接。
- 感谢 Joris 实现了 Linux 上的优化扫描,避免了 OOM 问题。详情请参阅 #25。
标签:Conpot, EDR, EVTX分析, EVTX分析, Go语言, JARM, Ruby on Rails, SecList, Windows安全, YARA, 云安全监控, 云资产可视化, 内存分析, 内存取证, 内存转储, 威胁情报, 安全测试工具, 开发者工具, 文件扫描, 日志审计, 程序破解, 端点检测, 网络安全, 网页爬虫, 脆弱性评估, 规则加密, 计算机防病毒, 进程扫描, 进程挂起, 隐私保护, 静态分析