09.11 如何在Linux上使用Osquery设置文件完整性监控(FIM)

Osquery是一个SQL驱动操作系统检测和分析工具,它由Facebook创建,支持像SQL语句一样查询系统的各项指标,可以用于OSX和Linux操作系统。

osquery是一个多平台软件,可以安装在Linux,Windows,MacOS和FreeBSD上。它允许我们使用基于SQL的查询来处理操作系统的配置文件、性能、安全检查等。

在本教程中,我们将向您展示如何使用OsQueq设置文件完整性监控(FIM)。我们使用的linux操作系统是Ubuntu 18.04和CENTOS 7。

条件

Linux(Ubuntu或CentOS)Root权限完成了第一个osquery指南

步骤一:在Linux服务器上安装osquery

osquery为所有的安装平台提供了自己的存储库,我们要做的第一步是从官方的osquery存储库安装osquery包。

在Ubuntu上

将osquery密钥添加到系统。

Export OSQUERY_KEY = 1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $ OSQUERY_KEY

添加osquery存储库并安装。

sudo add-apt-repository'deb [arch = amd64] https://pkg.osquery.io/deb deb

main'sudo apt install osquery -y

在CentOS上

将osquery密钥添加到系统。

curl -L https://pkg.osquery.io/rpm/GPG | sudo tee / etc / pki / rpm-gpg / RPM-GPG-KEY-osquery

添加并启用osquery存储库,然后安装该软件包。

Sudo yum-config-manager --add-repo https://pkg.osquery.io/rpm/osquery-s3-rpm.repo

sudo yum-config-manager --enable osquery-s3-rpm

sudo yum install osquery -y

等待安装所有软件包

注意: 如果您收到关于yum-config-manager命令的错误消息。

sudo: yum-config-manager: command not found

安装'yum-utils'包。

yum -y install yum-utils

步骤二:启用Osquery的Syslog Consumption

osquery提供了一些功能,可以使用Apple system Log (ASL)读取Apple MacOS上的系统日志,对于Linux则使用syslog。

在此步骤中,我们将通过rsyslog启用osquery的syslog consumption。

在Ubuntu上

使用如下apt命令安装rsyslog

sudo apt install rsyslog –y

在CentOS上

使用如下yum命令安装rsyslog包。

sudo yum install rsyslog -y

安装完成后,转到'/etc/rsyslog.d'目录并创建一个新的配置文件osquery.conf。

cd /etc/rsyslog.d/

vim osquery.conf

然后粘贴如下配置

template(
name =“OsqueryCsvFormat”
type =“string”
string =“%timestamp ::: date-rfc3339,csv%,%hostname ::: csv%,%syslogseverity ::: csv%,%syslogfacility-text ::: csv%,%syslogtag ::: csv%,%msg ::: csv%\\ n“

*。* action(type =”ompipe“Pipe =”/ var / osquery / syslog_pipe“template =”OsqueryCsvFormat“)

保存并退出

步骤三: osquery基本配置

osquery默认配置是'osquery.conf',通常位于'/ etc / osquery'目录中。

在这一步,我们将了解osquery配置组件,创建自定义osquery配置,然后将osqueryd部署为服务。

格式化为JSON 文件的osquery 配置包含如下规范:

Options:osqueryd CLI命令的一部分,它决定应用程序的启动和初始化。Schedule:将计划的查询名称的Flow定义到查询详情。Decorators:用于向结果和快照日志添加额外的“decorations”。Packs:一组调度查询More:File Path, YARA, Prometheus, Views, EC2, Chef Configuration。

转到'/ etc / osquery'目录并创建一个新的自定义配置'osquery.conf'。

cd / etc / osquery /

vim osquery.conf

粘贴以下配置。

保存并退出

注意:

我们使用'filesystem'作为配置文件和记录器插件定义'/ var / log / osquery'目录的记录器路径将SysLog-PIP启用到'/Va/ysLogy/SySalpJixBube文件中。在调度程序中,我们定义了三个查询来检查crontab, system info, 和ssh login.启用名为“osquery-monitoring”的osquery包,并将该包放置在'/Ur/Stuts/OsQue/Pokes’目录中。

启动osqueryd daemon service(使其在每次系统引导时启动)。

systemctl start osqueryd

systemctl enable osqueryd

重新启动rsyslog服务

systemctl restart rsyslog

osquery基本配置完成。

步骤四:使用osquery配置文件完整性监控(FIM)

osquery使用Linux和FSEvents提供Linux和MacOS Darwin上的文件完整性监控。简单地的说,它使用'file_path'检测定义目录上的任何一个文件的更改,然后将所有活动存储到file_events表中。

在这个步骤中,我们将配置Osquery来使用自定义FIM包监视重要目录,如HOST、SSH目录,以及tmp和www web根目录等。

转到“/usr/share/osquery/packs”目录,创建一个新的软件包配置文件“fim.conf”。

cd / usr / share / osquery / packs

vim fim.conf

配置如下:

保存并退出

返回到 '/etc/osquery'配置目录,编辑osquery.conf文件。

cd /etc/osquery/

vim osquery.conf

在 'packs' 中添加文件完整性监控包配置。

"packs": {
"osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf",
"fim": "/usr/share/osquery/packs/fim.conf"
}

保存并退出,然后重新启动osqueryd服务。

systemctl restart osqueryd

注意: 继续使用JSON linter“ http://jsonlint.com/ ” 检查JSON配置文件,确保没有错误。

步骤五:测试

我们将通过在定义的目录“home”和“www”上创建一个新文件来测试文件完整性监控包。

转到“/var/www/”目录,创建一个名为“howtoforge.md”的新文件。

cd /var/www/

touch howtoforge.md

转到'/ home / youruser /'目录并创建一个名为'hakase-labs.md'的新文件。

cd / home / vagrant /

touch hakase-labs.md

使用实时交互模式osqueryi 和osquery 的结果日志检查所有日志监控。

osqueryi

运行下面的osqueryi命令。

osqueryi --config-path /etc/osquery/osquery.conf

检查'file_events'表中文件更改的所有日志。

对于全局更改。

select * from file_events;

对于'home'目录。

select target_path, category, action, atime, ctime, mtime from file_events WHERE category="home";

对于'www' web root目录。

select target_path, category, action, atime, ctime, mtime from file_events WHERE category="www";

osqueryd 结果日志

转到'/ var / log / osquery'目录,您将获得'osqueryd.results.log'文件。

cd / var / log / osquery /

ls -lah osqueryd.results.log

使用'grep'命令筛选osquery日志。

grep -rin howtoforge.md osqueryd.results.log

grep -rin hakase-labs.md osqueryd.results.log

你将看到所有已创建的文件信息

使用osquery在Linux Server Ubuntu和CentOS上安装和配置文件完整性监控(FIM)已成功完成。