eCrimeLabs/cratos-fastapi
GitHub: eCrimeLabs/cratos-fastapi
CRATOS 是一个 FastAPI 代理服务,从 MISP 威胁情报平台提取 IoC 指示器并安全分发给 SIEM、防火墙、EDR 等下游安全组件,同时隔离 MISP 上下文以防止敏感信息泄露。
Stars: 13 | Forks: 3
[
](logo)
[MISP 威胁共享平台](https://misp-project.org/) 是一个出色的平台,用于收集和维护您的 CTI/事件响应发现与上下文,但它也可用于日常追踪任务、事件响应案例、标准 SecOps 及其他场景;而无需让您的基础设施或外包合作伙伴直接访问 MISP 的上下文。
CRATOS 代理 API 可与一个或多个 MISP 实例集成,并允许提取可被安全组件(如 SIEM、DNS、Proxies、Firewalls、EDR、NDR 以及其他能使用包含指示器的文件的组件)使用的指示器。
使用 CRATOS API 还能确保指示器是可共享的,您既不会泄露上下文,也无需为您的 MISP 实例授予访问权限,从而能够在您希望进一步保护数据的环境中共享这些指示器。
# 常见用例
以下内容仅供参考,您可以在适用的地方摄取数据。
- 将数据摄取到您的防护 pipeline 中
- Firewall(s) - 主动阻断
- 邮件网关 - 主动阻断
- AV 产品 - 主动阻断
- EDR 产品 - 主动阻断或被动检测
- Proxy 产品 - 主动阻断
- SIEM 解决方案 - 被动检测
- 为您的漏洞团队提供 feed
# 我该如何进行设置?
以下指南已在 Debian 和 Ubuntu 上经过测试和验证,建议创建一个虚拟环境。
## Debian / Ubuntu
我们建议将 Cratos FastAPI 通过 git clone 到系统中,以便在更新发布时能更轻松地进行更新。
```
$ sudo apt install git
$ cd /opt
$ git clone https://github.com/eCrimeLabs/cratos-fastapi.git
```
## Cratos FastAPI 配置文件
我们将从这里开始,因为运行代码的依赖项稍后会用到。
| 文件名/文件夹 | 描述 |
| -------------------- | ------------------------------------------------------------------- |
| log_conf.yaml | 这是 uvicorn 的日志配置文件 |
| config/config.yaml | 包含核心配置 |
| sites/\.yaml | 包含与每个 MISP 实例相关的配置文件 |
### config/config.yaml
这里有一个可用于创建加密密钥的单行命令
```
openssl rand -base64 4096 | sha256sum | tr '[:lower:]' '[:upper:]'
```
将 SHA256 字符串复制到 "encryption_key" 字段中。
对于 "salt" 也是类似的:
```
< /dev/urandom tr -dc 'A-Za-z0-9!#?' | head -c 32; echo
```
现在更新 "config/config.yaml 并保存。
```
---
debug: False
encryption_key: ""
salt: ""
memcached_user: ""
memcached_pass: ""
memcached_host: "127.0.0.1"
memcached_port: "11211"
allways_allowed_ips:
- "100.64.3.0/24"
- "10.0.0.0/8"
- "172.16.0.0/12"
- "192.168.0.0/16"
```
***注意:"allways_allowed_ips" 是针对所有站点全局设置的,通常是您的监控设置,以确保服务正在运行,特定于站点的设置将在下文定义。***
## 配置您的第一个 MISP 连接配置
在 sites 文件夹中,生成一个名为 "\.yaml" 的文件,也可以是 "\.yaml",但它必须映射到 MISP 实例,因为它被用作验证 Cratos FastAPI 是否被允许以及如何连接到该实例的依据之一。
配置文件位于 "sites" 文件夹中。
### misp.example.net
因此,在这种情况下,我们的 MISP 实例是 "misp.example.net",所以我们在 sites 文件夹中创建该文件:
```
$ touch sites/misp.example.net.yaml
```
现在编辑该文件:
```
---
enabled: true
debug: false
company: Example ApS
tag: example
mispVerifyCert: true
mispTimeoutSeconds: 100
mispDebug: true
memcached_all_timeout: 300
falsepositive_timeout: "1w"
list_stats: "1w"
allowed_ips:
- "10.0.0.0/8"
- "127.0.0.1/32"
- "192.168.1.0/24"
custom_feeds:
cust1: ":incident-classification=cust1"
cust2: ":incident-classification=cust2"
cust3: ":incident-classification=cust3"
cust4: ":incident-classification=cust4"
cust5: ":incident-classification=cust5"
```
来自 "tag" 的字段结合一些内置的 feed 和 5 个自定义 feed 将映射到标记系统,因此通过此设置,您将拥有映射到这些总体 feed 组的以下标签。
| feed 名称/组 | 您必须创建的 MISP 标签(请记住将标签锁定到您的组织) |
| --------------- | ------------------------------------------------------------------- |
| incident | example:incident-classification=incident |
| alert | example:incident-classification=alert |
| block | example:incident-classification=block |
| hunt | example:incident-classification=hunt |
| cust1 | example:incident-classification=sinkhole |
| cust2 | example:incident-classification=permanent-block |
| cust3 | example:incident-classification=tor-exitnode |
| cust4 | example:incident-classification=cust4 |
| cust5 | example:incident-classification=cust5 |
## 安装系统依赖
```
$ sudo apt install nginx libnginx-mod-http-headers-more-filter supervisor gcc openssl libssl-dev python3-dev python3-venv
```
## 配置反向代理设置(如果使用)
在 'config.yaml' 文件中有以下两个选项
- reverse_proxy (Boolean)
- reverse_proxy_header (String)
如果 API 前面没有使用反向代理,请设置 "reverse_proxy: False",否则如果设置为 "True",请确保使用包含真实源 IP 的正确反向代理 header key。
已知的反向代理通常使用的 header:
- X-Forwarded-For
- X-Real-IP
## Memcached(可选)
如果您正在使用现有的 memcached 服务器或集群,这是“可选的”。
确保 memcached 正在运行并在重启后启用。
```
sudo apt install memcached libmemcached-tools
sudo systemctl status memcached
sudo systemctl enable memcached
```
确保在 memcached 配置中添加足够的内存来存储数据。
文件:“/etc/memcached.conf” 并查找以下选项 "-m"
```
# memory (以 MB 为单位分配)
-m 4096
```
如果修改了此文件,请记住重启服务。
```
sudo systemctl restart memcached
```
## 创建并安装 Cratos python 依赖项
```
$ python3 -m venv .venv
$ .venv/bin/pip install -r requirements.txt
```
## 添加 fastapi 用户
```
sudo adduser fastapiuser --system --no-create-home --shell /usr/sbin/nologin
```
## Nginx 的配置与设置(如果需要)
在 [nginx.conf](/INSTALLATION/nginx.conf) 中,确保修改设置以匹配您的环境,并安装 SSL 证书,可以通过您自己的证书或 Let's Encrypt 等服务。
替换位于 "/etc/nginx/nginx.conf" 的当前 "nginx.conf"。
## supervisord 的配置与设置(启动方式 1)
[Supervisor](http://supervisord.org/introduction.html) 是一个客户端/服务器系统,允许其用户在类 UNIX 操作系统上监视和控制多个进程。
修改 [uvicorn_start](/INSTALLATION/uvicorn_start) 以满足您的需求,并将其复制到项目的根目录。
```
$ cp INSTALLATION/uvicorn_start uvicorn_start.sh
# 我们想测试它是否正常工作
$ chmod +x uvicorn_start.sh
$ ./uvicorn_start.sh
```
它应该使用 Uvicorn 和预定义的设置启动 Cratos FastAPI 应用程序。
```
$ cp INSTALLATION/cratos.conf_example /etc/supervisor/conf.d/cratos.conf
```
请记住修改 "cratos.conf" 中的配置,以适应您的环境。
测试并安装配置(如果您对 cratos.conf 进行了更改,也请使用此命令)
```
$ sudo supervisorctl reread
$ sudo supervisorctl update
```
最后,测试它是否启动了由 Supervisor 管理的 Cratos 应用程序。
```
sudo supervisorctl start cratos
```
了解 supervisor 的实用命令
```
sudo supervisorctl start cratos
sudo supervisorctl stop cratos
sudo supervisorctl restart cratos
sudo supervisorctl status cratos
sudo supervisorctl
```
## Systemd 的配置与设置(启动方式 2) - 推荐
systemd 可以在系统启动时自动启动 Gunicorn,并在其崩溃时重新启动它,从而确保高可用性。此外,systemd 还提供高级资源管理功能,例如 CPU 和内存限制,这有助于防止 Gunicorn 消耗过多资源。
利用 systemd 还提供沙盒和进程隔离等安全功能,这有助于提高 Gunicorn 服务的安全性。
**确保更新 INSTALLATION/gunicorn.service_example 以适应您的安装路径**
```
cp INSTALLATION/gunicorn.service_example /etc/systemd/system/gunicorn.service
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
```
# 一切正常
如果 Cratos FastAPI 正在运行,您应该能够连接到它,我们建议从帮助页面 "https://cratos.yourdomain.com/v1/help" 开始。
"https://cratos.yourdomain.com/v1/generate_token_form" 以生成您的 auth token。
# 单元测试
如果向此项目提交代码,请确保事先运行单元测试,以识别潜在错误。
前置条件。
将文件 "test.token.example" 复制为 "test.token" 并插入有效的 API token。
*标准测试*
```
pytest tests/unit/test_api.py
```
*带 HTML 报告的标准测试*
```
pytest tests/unit/test_api.py --html=report.html
```
# 许可证
本软件采用 [MIT](https://github.com/eCrimeLabs/cratos-fastapi/blob/main/LICENSE) 授权许可。
# 待办事项
- 支持允许 API key 同时获取存在特定指示器的 MISP 实例的 URL,这应该在生成 api token 时通过一个布尔值来实现。
# Hack.lu 2023 的视频演示
"Cratos - Use your bloody indicators"
https://www.youtube.com/watch?v=yFvvFIq7TKk

标签:API代理, AV绕过, FastAPI, 威胁情报, 安全运营, 开发者工具, 扫描框架, 漏洞靶场, 逆向工具, 防御检测