ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

trivy开源安全漏洞扫描器——筑梦之路

trivy开源安全漏洞扫描器——筑梦之路 开源地址https://github.com/aquasecurity/trivy.git可扫描的对象容器镜像文件系统Git存储库远程虚拟机镜像Kubernetes在容器镜像安全方面使用广泛其他使用相对较少。能够发现的问题正在使用的操作系统包和软件依赖项 SBOM已知漏洞 CVEIaC 问题和错误配置敏感信息和机密软件许可证。漏洞扫描原理Trivy通过分析容器镜像的文件系统识别出其中的软件包及其版本号然后与漏洞数据库进行匹配找出存在安全漏洞的软件包。Trivy采用了以下技术实现漏洞扫描Dockerfile解析Trivy可以根据Dockerfile自动解析出容器镜像的构建过程获取镜像中包含的软件包及其版本号。漏洞数据库匹配Trivy将容器镜像中的软件包及其版本号与漏洞数据库进行匹配找出存在安全漏洞的软件包。漏洞详情展示Trivy提供了详细的漏洞信息包括漏洞描述、影响版本、修复建议等帮助用户了解漏洞风险。CVE全称是Common Vulnerabilities and Exposures即通用漏洞披露它是MITRE公司维护和更新的安全漏洞列表列表中的每个条目都会有一个唯一的CVE编号即CVE ID供安全研究员和受攻击的软件供应商使用以便确定和回应安全漏洞。CVE条目包含了与CVE ID相关的漏洞的描述性数据即简要描述和至少一个参考。当前CVE累计收录了19万个安全漏洞。Trivy漏洞扫描原理简单来说就是Trivy下载漏洞数据库CVE到本地Trivy本地数据库记录了常见的漏洞信息Trivy读取镜像里的程序和本地数据库进行比对确定镜像是否存在漏洞。环境搭建1. 二进制方式安装https://github.com/aquasecurity/trivy/releases/download/v0.61.1/trivy_0.61.1_Linux-64bit.tar.gz tar -zxvf trivy_0.61.1_Linux-64bit.tar.gz -C /usr/local/bin/ chmod x /usr/local/bin/trivy trivy -v2. 二进制rpm包方式安装https://github.com/aquasecurity/trivy/releases/download/v0.61.1/trivy_0.61.1_Linux-64bit.rpm rpm -ivh trivy_0.61.1_Linux-64bit.rpm trivy -v3. 离线漏洞数据库trivy-db是一个包含漏洞数据库的开源项目用于帮助开发人员和安全团队识别容器镜像中的漏洞。它可以与 trivy 工具配合使用帮助用户快速发现容器镜像中存在的安全漏洞并提供修复建议。trivy-db 中包含了各种已知的漏洞信息Trivy在内部使用trivy-db来操作漏洞数据库。此数据库包含来自NVD、Red Hat、Debian等的漏洞信息。用户可以通过更新 trivy-db 来获取最新的漏洞数据以保持镜像的安全性trivy-java-db 是 Trivy 工具的一个组件用于存储 Java 相关的漏洞数据库。Trivy 是一个开源的漏洞扫描工具用于帮助用户检测容器镜像中的安全漏洞。trivy-java-db 会包含 Java 相关的漏洞信息用户可以通过更新 trivy-java-db 来获取最新的漏洞数据以保持镜像的安全性。这个组件可以帮助用户识别容器镜像中与 Java 相关的漏洞并提供修复建议以提高容器镜像的安全性。https://github.com/oras-project/oras.git使用oras下载离线漏洞数据库 # 二进制方式安装 https://github.com/oras-project/oras/releases/download/v1.2.2/oras_1.2.2_linux_amd64.tar.gz VERSION1.2.2 curl -LO https://github.com/oras-project/oras/releases/download/v${VERSION}/oras_${VERSION}_linux_amd64.tar.gz mkdir -p oras-install/ tar -zxf oras_${VERSION}_*.tar.gz -C oras-install/ sudo mv oras-install/oras /usr/local/bin/ rm -rf oras_${VERSION}_*.tar.gz oras-install/ # 获取trivy漏洞数据库离线包 # 官网 oras pull ghcr.io/aquasecurity/trivy-db:2 oras pull ghcr.io/aquasecurity/trivy-java-db:1 tar -zxvf db.tar.gz -C ~/.cache/trivy/db/ tar -zxvf javadb.tar.gz -C ~/.cache/trivy/java-db/ # 国内加速 oras pull ghcr.nju.edu.cn/aquasecurity/trivy-db:2 oras pull ghcr.nju.edu.cn/aquasecurity/trivy-java-db:1trivy使用示例1. 容器镜像扫描# 默认扫描默认以table格式输出 trivy image nginx 可选参数 --cache-dir 指定漏洞数据库的本地缓存目录默认在是 ~/.cache/trivy 下 --skip-db-update, 扫描启动的时候会检查本地漏洞库。如果超过 12 小时为更新会自动下载更新漏洞库。由于官方漏洞库放在 github下载比较慢可以使用 --skip-db-update 跳过这一过程。 --skip-java-db-update 同样java 的漏洞库每周四凌晨自动更新也是存在github上以使用--skip-java-db-update 跳过这一过程。 --severity CRITICAL 指定扫描的严重程度分为CRITICAL[紧急],HIGH[高的],MEDIUM[中等],LOW[低的] # 以json格式输出结果 trivy image -f json -o results.json nginx:1.16 # 扫描结果以html格式保存到文件 trivy image --severity HIGH,CRITICAL,MEDIUM debian:stable-slim --format template --template /usr/local/share/trivy/templates/html.tpl -o debian_report.html # 指定漏洞数据库目录、漏洞级别、跳过更新数据库 trivy --cache-dir /home/application/trivy-db image nginx:1.25 --skip-db-update --severity CRITICAL,HIGH # 扫描镜像离线包 trivy --input [image_name.tar] # 漏洞类型过滤 trivy --vuln-type os [image_name] # 旧版 trivy image --scanners vuln --pkg-types os --severity HIGH,CRITICAL,MEDIUM debian:stable-slim # 只更新某个系统的漏洞数据库 trivy --only-update alpine,debia [image_name] # 指定退出代码主要是为后续判断提供可操作性通常在CI中集成 trivy --exit-code 1 [image_name] # 清除漏洞数据库缓存 trivy --clear-cache [image_name] # 重建漏洞数据库 trivy --reset [image_name] # 忽略未修复的安全漏洞 trivy image --ignore-unfixed [image_name]2. 文件系统扫描trivy fs --scanners vuln --pkg-types os --severity HIGH,CRITICAL,MEDIUM / trivy rootfs --exit-code 1--no-progress / # 对jar包进行扫描 trivy rootfs --skip-db-update --offline-scan --dependency-tree --timeout 999m /root/code/xxxxxx-web.jar -o /root/code/trivy_扫描结果.txt3. 代码仓库扫描trivy repo https://github.com/test/text.git trivy repo https://github.com/test/text.git --ignore-unfixed trivy repo https://github.com/test/text.git --format template --template ./trivy/contrib/html.tpl -o qapm_report.html4. 虚拟机镜像扫描# 虚拟机磁盘文件 trivy vm --scanners vuln disk.vmdk # AMI trivy vm ami:${your_ami_id} trivy vm --scanners vuln ami:ami-0123456789abcdefg5. K8S扫描trivy k8s [flags] [CONTEXT] - if the target name [CONTEXT] is not specified, the default will be used. trivy k8s --report summary trivy k8s --kubeconfig ~/.kube/config2 trivy k8s --report summary --skip-images trivy k8s --report summary --skip-images trivy k8s --report summary --exclude-namespace dev-system,staging-system trivy k8s --scanners vuln --report allharbor与trivy集成# 修改harbor.yml配置 #配置trivy相关配置设置离线配置 trivy: ignore_unfixed: false skip_update: true skip_java_db_update: true offline_scan: true security_check: vuln insecure: false # 安装时加上参数 ./install.sh --with-trivyJenkins CI/CD集成trivyJenkins上安装HTML Publisher插件此插件是用来将构建生成的 HTML 报告发布到作业和构建页面可方便我们进行查阅--skip-db-update跳过数据库的更新--exit-code指定退出状态码--severity指定漏洞严重级别--cache-dir指定缓存目录官方html模板https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/html.tplpipeline流水线示例pipeline { agent any stages { stage(镜像安全扫描) { steps{ script { def formatOption --format template --template \/opt/jenkins/html.tpl\ sh( trivy image --skip-db-update --exit-code 1 --severity CRITICAL IMAGES:TAG --cache-dir trivy_db $formatOption --timeout 10m --output trivy.html ) // reportDir 报告所在目录reportFiles 报告名称reportName 在Jenkins菜单栏显示的名称 reportTitles 点进报告显示的Title publishHTML (target : [allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: ., reportFiles: trivy.html, reportName: Trivy Scan, reportTitles: Trivy Scan]) } } } } }这里简单地学习了下trivy安全漏洞扫描工具的使用更多使用方法得在不断实践中去探索。
返回列表