InSpec:审计和测试框架
作者:Sec-Labs | 发布时间:
项目地址
https://github.com/inspec/inspec
Chef InSpec 项目简介
相关技术点
- 基于 Ruby 编写的开源测试框架。
- 支持人机可读的语言规范基础设施的合规性、安全性和策略要求。
- 支持命令行接口
CLI以方便测试。
项目用途
Chef InSpec 项目是一个基于 Ruby 编写的测试框架,用于测试基础设施的合规性、安全性和策略要求,支持人机可读的语言规范。使用该项目,可以轻松地在需要的地方运行测试,并支持多种测试方式,如 local 、SSH 、WinRM 和 Docker 等。
该项目的主要特点:
- 内置合规性:合规性不再发生在发布周期的末尾。
- 有目的的测试:Chef InSpec 编写的测试针对特定的合规问题。
- 元数据:包括安全和合规专业人员所需的元数据。
- 易于测试:包括一个命令行接口以快速运行测试。
Chef InSpec 项目的安装方式有多种,包括包安装和 RubyGems 安装,且支持在 Docker 中使用。
Chef InSpec: 检查您的基础架构
- 项目状态:活跃
- 问题响应SLA:14个工作日
- 拉请求响应SLA:14个工作日
有关项目状态和SLA的更多信息,请参见此文档。
Chef InSpec是一个开源的基础设施测试框架,具有人和机器可读的语言,用于指定合规性、安全性和策略要求。
# 通过测试来禁用不安全的协议
describe package('telnetd') do
it { should_not be_installed }
end
describe inetd_conf do
its("telnet") { should eq nil }
end
Chef InSpec使您可以在需要的任何地方运行测试。更多选项可以在我们的CLI文档中找到。
# 本地运行测试
inspec exec test.rb
# 通过SSH在远程主机上运行测试
inspec exec test.rb -t ssh://user@hostname -i /path/to/key
# 使用SSH代理私钥身份验证在远程主机上运行测试。需要Chef InSpec 1.7.1
inspec exec test.rb -t ssh://user@hostname
# 通过WinRM在远程Windows主机上运行测试
inspec exec test.rb -t winrm://Administrator@windowshost --password 'your-password'
# 作为域用户通过WinRM在远程Windows主机上运行测试
inspec exec test.rb -t winrm://windowshost --user 'UserName@domain' --password 'your-password'
# 在docker容器上运行测试
inspec exec test.rb -t docker://container_id
特征
- 内置合规性:合规性不再在发布周期结束时发生
- 定向测试:Chef InSpec编写的测试专门针对合规问题
- 元数据:包括安全和合规专业人员所需的元数据
- 简单测试:包括一个命令行界面,可以快速运行测试
安装
Chef InSpec需要Ruby(> = 2.7)。
目前支持的所有Chef InSpec版本(4.0及更高版本)都需要接受EULA才能使用。请访问Chef文档站点上的许可证接受页面获取更多信息。
安装软件包
Chef InSpec软件包可用于MacOS、RedHat、Ubuntu和Windows。在Chef InSpec Downloads下载最新软件包,或通过脚本安装Chef InSpec:
# RedHat、Ubuntu和macOS
curl https://omnitruck.chef.io/install.sh | sudo bash -s -- -P inspec
# Windows
. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install -project inspec
通过rubygems.org安装
从源代码安装Chef InSpec可能需要安装Ruby构建工具来管理gem依赖关系。(还提供了一种功能有限的无编译器变体;使用inspec-core-bin和inspec-core。)
要安装构建工具,请使用您的软件包管理器。
对于CentOS/RedHat/Fedora:
yum -y install ruby ruby-devel make gcc gcc-c++
对于Ubuntu:
apt-get -y install ruby ruby-dev gcc g++ make
要安装inspec可执行文件,需要接受Chef许可证,请运行:
gem install inspec-bin
您也可以使用inspec作为库,而无需可执行文件。这不需要接受许可证。要将库作为gem安装,请运行:
gem install inspec
通过Docker使用
下载镜像并定义便捷函数:
对于Linux:
docker pull chef/inspec
function inspec { docker run -it --rm -v $(pwd):/share chef/inspec "$@"; }
对于Windows (PowerShell):
docker pull chef/inspec
function inspec { docker run -it --rm -v "$(pwd):/share" chef/inspec $args; }
如果从shell调用inspec,它会自动将当前目录挂载到Docker容器中。因此,您可以轻松使用本地测试和密钥文件。注意:只有当前目录和子目录中的文件才能在容器中使用。
$ ls -1
vagrant
test.rb
$ inspec exec test.rb -t ssh://root@192.168.64.2:11022 -i vagrant
..
Finished in 0.04321 seconds (files took 0.54917 seconds to load)
2 examples, 0 failures
要使用容器化的InSpec扫描运行在主机上的docker容器,我们需要将Unix套接字/var/run/docker.sock从主机机器绑定到InSpec容器中。
docker pull chef/inspec
function inspec { docker run -it --rm -v $(pwd):/share -v /var/run/docker.sock:/var/run/docker.sock chef/inspec "$@"; }
/var/run/docker.sock是Docker守护程序默认侦听的Unix套接字。### 从源码安装
请注意,从下载页面安装操作系统包是首选方法。
这需要bundler:
bundle install
bundle exec inspec help
要本地安装它作为一个gem,请运行:
gem build inspec.gemspec
gem install inspec-*.gem
在Windows上,你需要安装Ruby和Ruby Development Kit来构建并使用其本地扩展的依赖项。
通过Chef Habitat安装
目前,这种安装方法仅支持Linux。有关更多信息,请参见Chef Habitat网站。
从Chef Habitat网站下载hab二进制文件。
hab pkg install chef/inspec --binlink
inspec
运行Chef InSpec
现在你应该可以运行:
$ inspec --help
Commands:
inspec archive PATH # archive a profile to tar.gz (default) ...
inspec check PATH # verify all tests at the specified PATH
inspec automate SUBCOMMAND ... # Chef Automate commands
inspec compliance SUBCOMMAND ... # Chef Automate commands (backwards compatible alias)
inspec detect # detect the target OS
inspec exec PATH(S) # run all test files at the specified PATH.
inspec help [COMMAND] # Describe available commands or one spe...
inspec init TEMPLATE ... # Scaffolds a new project
inspec json PATH # read all tests in PATH and generate a ...
inspec shell # open an interactive debugging shell
inspec supermarket SUBCOMMAND ... # Supermarket commands
inspec version # prints the version of this tool
Options:
[--diagnose], [--no-diagnose] # Show diagnostics (versions, configurations)
示例
- 仅接受在安全端口上的请求 - 此测试确保Web服务器仅在安全端口上侦听。
describe port(80) do
it { should_not be_listening }
end
describe port(443) do
it { should be_listening }
its('protocols') {should include 'tcp'}
end
- 测试
kitchen.yml文件以验证是否仅将Vagrant配置为驱动程序。 %w()格式化将通过rubocop linting并允许您访问嵌套映射。
describe yaml('.kitchen.yml') do
its(%w(driver name)) { should eq('vagrant') }
end
此外,请查看我们的示例:
- 使用Test Kitchen和Chef Infra来使用Chef InSpec
- 使用Test Kitchen和Puppet使用Chef InSpec
- 使用Test Kitchen和Ansible使用Chef InSpec
- 实施Chef InSpec配置文件
或测试:测试a或b
- 使用describe.one,您可以测试a或b。如果满足任一条件,则该控件将被标记为通过。
control 'or-test' do
impact 1.0
title 'This is a OR test'
describe.one do
describe ssh_config do
its('Protocol') { should eq('3') }
end
describe ssh_config do
its('Protocol') { should eq('2') }
end
end
end
命令行使用
exec
针对不同目标运行测试:
# run test locally
inspec exec test.rb
# run test on remote host on SSH
inspec exec test.rb -t ssh://user@hostname
# run test on remote windows host on WinRM
inspec exec test.rb -t winrm://Administrator@windowshost --password 'your-password'
# run test on docker container
inspec exec test.rb -t docker://container_id
# run test on podman container
inspec exec test.rb -t podman://container_id --podman-url "unix:///run/user/1000/podman/podman.sock"
# run with sudo
inspec exec test.rb --sudo [--sudo-password ...] [--sudo-options ...] [--sudo_command ...]
# run in a subshell
inspec exec test.rb --shell [--shell-options ...] [--shell-command ...]
# run a profile targeting AWS using env vars
inspec exec test.rb -t aws://
# or store your AWS credentials in your ~/.aws/credentials profiles file
inspec exec test.rb -t aws://us-east-2/my-profile
# run a profile targeting Azure using env vars
inspec exec test.rb -t azure://
# or store your Azure credentials in your ~/.azure/credentials profiles file
inspec exec test.rb -t azure://subscription_id
```### 检测
验证你的配置并检测
```bash
id=$( docker run -dti ubuntu:14.04 /bin/bash )
inspec detect -t docker://$id
这将提供给你:
{"family":"ubuntu","release":"14.04","arch":null}
支持的操作系统
远程目标
| 平台 | 版本 | 架构 |
|---|---|---|
| AIX | 6.1, 7.1, 7.2 | ppc64 |
| CentOS | 6, 7, 8 | i386, x86_64 |
| Debian | 9, 10 | i386, x86_64 |
| FreeBSD | 9, 10, 11 | i386, amd64 |
| macOS | 10.14, 10.15, 11.0 | x86_64 |
| Oracle Enterprise Linux | 6, 7, 8 | i386, x86_64 |
| Red Hat Enterprise Linux | 6, 7, 8 | i386, x86_64 |
| Solaris | 10, 11 | sparc, x86 |
| Windows* | 8, 8.1, 10, 2012, 2012R2, 2016, 2019 | x86, x86_64 |
| Ubuntu Linux | x86, x86_64 | |
| SUSE Linux Enterprise Server | 12, 15 | x86_64 |
| Scientific Linux | 6, 7 | i386, x86_64 |
| Fedora | x86_64 | |
| OpenSUSE | 15 | x86_64 |
| OmniOS | x86_64 | |
| Gentoo Linux | x86_64 | |
| Arch Linux | x86_64 | |
| HP-UX | 11.31 | ia64 |
| Alpine Linux | x86_64 |
*对于Windows,需要PowerShell 5.0或更高版本。
此外,还提供了运行时支持:
| 平台 | 版本 | 架构 |
|---|---|---|
| macOS | 10.14+ | x86_64 |
| Debian | 9, 10 | x86_64 |
| RHEL | 6, 7, 8 | x86_64 |
| Fedora | 29+ | x86_64 |
| Ubuntu | 16.04+ | x86_64 |
| Windows | 8+ | x86_64 |
| Windows | 2012+ | x86_64 |
文档
文档
- https://docs.chef.io/inspec/
- https://docs.chef.io/inspec/resources/
- https://github.com/inspec/inspec/tree/main/docs-chef-io
学习Chef:
与其他工具的关系(RSpec,Serverspec):
共享你的配置文件
你可以在 Chef Supermarket 的 Tools & Plugins section 中共享你的Chef InSpec配置文件。登录 并添加你的配置文件详情。
致谢
Chef InSpec最初由Christoph Hartmann(@chris-rock)和Dominik Richter(@arlimus)创建。
Chef InSpec受到了精妙的Serverspec项目的启发。向mizzy和所有贡献者致敬!
AWS资源受arothian的inspec-aws启发。## 贡献
- Fork本项目
- 创建功能分支(git checkout -b my-new-feature)
- 提交更改(git commit -am '添加一些功能')
- 推送到分支(git push origin my-new-feature)
- 创建新的Pull请求
Chef InSpec社区和维护者非常活跃和乐于助人。该项目从这种活动中极大地受益。
如果您想直接与社区和维护者交流,请加入我们的#inspec频道,访问Chef Community Slack。
提醒一下,所有参与者都应遵守行为准则。
测试Chef InSpec
我们提供“单元测试”和“集成测试”。
- “单元测试”确保实现的预期行为
- “集成测试”通过基于Docker的虚拟机运行测试厨房和kitchen-inspec
单元测试
bundle exec rake test
如果您只想运行一个测试文件:
bundle exec m test/unit/resources/user_test.rb
您还可以按行号在文件中运行单个测试:
bundle exec m test/unit/resources/user_test.rb -l 123
集成测试
这些测试下载各种虚拟机,以确保Chef InSpec在不同操作系统上按预期工作。
这些测试需要以下gem:
- 测试厨房
- kitchen-dokken
- kitchen-inspec
这些宝石通过项目的Gemfile中的“integration”组提供。
此外,这些测试要求在您的计算机上或通过标准的Docker环境变量配置的远程Docker机器上可用的Docker。
运行集成测试
列出各种可用的测试实例:
KITCHEN_YAML=kitchen.dokken.yml bundle exec kitchen list
平台和测试套件在kitchen.dokken.yml文件中进行配置。一旦您知道要测试哪个实例,请测试该实例:
KITCHEN_YAML=kitchen.dokken.yml bundle exec kitchen test <INSTANCE_NAME>
您可以并行测试所有实例:
KITCHEN_YAML=kitchen.dokken.yml bundle exec kitchen test -c 3
许可证
| 作者: | Dominik Richter (drichter@chef.io) |
| 作者: | Christoph Hartmann (chartmann@chef.io) |
| 版权所有: | 版权所有(c)2015 Vulcano Security GmbH. |
| 版权所有: | 版权所有(c)2017-2020 Chef Software Inc. |
| 版权所有: | 版权所有(c)2020-2023 Progress Software Corp. |
| 许可证: | Apache许可证,版本2.0 |
| 许可证: | Chef最终用户许可协议 |
Chef InSpec在Apache许可证版本2.0下分发。 使用该软件的许可由Chef EULA管理。
根据Apache许可证第2.0版(“许可证”)获得许可; 除非符合许可证,否则您不得使用此文件。 您可以在以下位置获取许可证的副本
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律规定或书面同意,否则在许可证下分发的软件 分发是基于“原样”提供的,没有任何形式的明示或暗示的担保, 包括但不限于对适销性和特定用途的适用性的担保。 请参阅许可证以获取特定语言的许可证的管理权限和 限制。