osquery 是一个强大的系统监控和操作系统检测工具,它为安全、DevOps 和合规团队收集系统指标和数据点。 信息存储在高性能关系数据库中,可以使用类似 SQL 的语句在本地或远程访问。
osquery 是一个开源程序,可以在 Windows、macOS 或 Linux 上运行。 有关该主题的文档非常广泛,并且它还拥有一个非常活跃的在线用户社区。
本指南将介绍在 CentOS 7 上安装 osquery 以在本地收集数据。提供的步骤也适用于 CentOS 8。
osquery 安装过程
步骤 1. 安装 yum-utils 包
在开始之前,我们将安装 yum-utils 包来帮助安装 osquery。
# yum install yum-utils
步骤 2. osquery 存储库安装
现在,我们必须首先将 osquery 存储库和包签名密钥添加到我们的系统中。 我们可以使用以下命令获取并存储签名密钥。
# curl -L https://pkg.osquery.io/rpm/GPG | tee /etc/pki/rpm-gpg/RPM-GPG-KEY-osquery
现在我们有了密钥,我们可以将包存储库添加到我们的系统中。
# yum-config-manager --add-repo https://pkg.osquery.io/rpm/osquery-s3-rpm.repo
接下来,启用包存储库。
# yum-config-manager --enable osquery-s3-rpm-repo
步骤 3. 安装 osquery
现在我们已经安装并启用了包存储库,我们可以安装 osquery。
# yum install osquery
将弹出一个提示,要求您批准软件包安装并接受软件包的签名密钥。 对这两个提示说是。
现在我们已经安装了 osquery,我们可以看到我们可以用它做的一些事情。
步骤 4. 访问 osquery
使用提供的交互式 shell 在本地访问 osquery。
# osqueryi
下面的输出是从上面的命令生成的。
您现在可以使用类似 SQL 的语句来查看正在运行的系统、软件和进程。 如果您需要帮助,请使用 .help 命令通过 osquery 交互式 shell 获取更多信息。
osquery 中的可用数据
表
要获取所有不同类型的可用数据的列表,请使用交互式 shell 中的 .tables 命令。 正如您在下面看到的,表格列表很广泛。

中央处理器信息
通过查询 cpuid 表可以获得 CPU 的服务器信息。

您可以使用以下命令轻松获取系统上所有用户的列表。
osquery> SELECT * FROM users;

IP 地址信息
如果您想在不显示环回的情况下获取有关服务器在特定网络接口上的 IP 地址的信息,您可以尝试以下操作。
osquery> SELECT interface,address FROM interface_addresses WHERE interface NOT LIKE '%lo%';

步骤 5. 配置 osquery
到目前为止,我们一直在使用 osqueryi。 这是快速查看操作系统当前状态的好方法,但是如果您想记录输出或跟踪系统上各种项目的更改,那么我们将需要配置和启用 osqueryd。 它作为服务持续运行。 不幸的是,我们选择在我们的系统上获取 osquery 的安装方法默认情况下没有设置它,所以我们需要设置一个配置文件并启用守护程序。
使用您选择的编辑器创建配置文件。
[root@morty ~]# vim /etc/osquery/osquery.conf
粘贴到以下配置文件中。
{
// Configure the daemon below:
"options": {
// Select the osquery config plugin.
"config_plugin": "filesystem",
// Select the osquery logging plugin.
"logger_plugin": "filesystem",
// The log directory stores info, warning, and errors.
// If the daemon uses the 'filesystem' logging retriever then the log_dir
// will also contain the query results.
"logger_path": "/var/log/osquery",
// Set 'disable_logging' to true to prevent writing any info, warning, error
// logs. If a logging plugin is selected it will still write query results.
"disable_logging": "false",
// Splay the scheduled interval for queries.
// This is very helpful to prevent system performance impact when scheduling
// large numbers of queries that run a smaller or similar intervals.
"schedule_splay_percent": "10",
// A filesystem path for disk-based backing storage used for events and
// query results differentials. See also 'use_in_memory_database'.
"database_path": "/var/osquery/osquery.db",
// Comma-delimited list of table names to be disabled.
// This allows osquery to be launched without certain tables.
//"disable_tables": "foo_bar,time",
// Comma-delimited list of table names to be enabled.
// This allows osquery to be launched with certain tables only.
//"enable_tables": "foo_bar,time",
"utc": "true"
},
// Define a schedule of queries:
"schedule": {
// This is a simple example query that outputs basic system information.
"system_info": {
// The exact query to run.
"query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
// The interval in seconds to run this query, not an exact interval.
"interval": 3600
}
},
// Decorators are normal queries that append data to every query.
"decorators": {
"load": [
"SELECT uuid AS host_uuid FROM system_info;",
"SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
]
}
}
现在我们的配置文件已经到位,我们必须启动 osqueryd 守护进程。
[root@morty ~]# systemctl start osqueryd
现在我们应该启用 osqueryd 守护进程,以便它在未来自动运行。
[root@morty ~]# systemctl enable osqueryd
我们可以检查 osqueryd 守护进程的状态以确保它正常启动。
[root@morty ~]# systemctl status osqueryd

结论
在本指南中,我们逐步完成了 osquery 的安装、基本使用和配置。 它可以安装在 macOS、Linux 和 Windows 上,并具有大量潜在用例,例如监控、合规性、安全性、事件响应和漏洞管理。 此外,Osquery 是一个令人难以置信的可配置工具,可以帮助您更快地了解您的系统。
您需要监控、安全或合规帮助吗? Liquid Web 提供了多种产品来增强您的服务器,所以今天就联系我们吧!