怎么在Linux上使用Osquery设置文件完整性监控

发布时间:2021-09-14 18:13 来源:亿速云 阅读:0 作者:chen 栏目: 服务器 欢迎投稿:712375056

这篇文章主要讲解了“怎么在Linux上使用Osquery设置文件完整性监控”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么在Linux上使用Osquery设置文件完整性监控”吧!

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

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

在本教程中,我们将向您展示如何使用Osquery设置文件完整性监控(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

粘贴以下配置。

{
    “options”:{
        “config_plugin”:“filesystem”,
        “logger_plugin”:“filesystem”,
        “logger_path”:“/ var / log / osquery”,
        “disable_logging”:“false”,
        “log_result_events”:“true” ,
        “schedule_splay_percent”:“10”,
        “pidfile”:“/ var / osquery / osquery.pidfile”,
        “events_expiry”:“3600”,
        “database_path”:“/ var / osquery / osquery.db”,
        “verbose” :“false”,
        “worker_threads”:“2”,
        “enable_monitor“:”true“,
        ”disable_events“:”false“,
        ”disable_audit“:”false“,
        ”audit_allow_config“:”true“,
        “host_identifier”:“hakase-labs”,
        “enable_syslog”:“true”,
        “syslog_pipe_path”:“/ var / osquery / syslog_pipe”,
        “force”:“true”,
        “audit_allow_sockets”:“true”,
        “schedule_default_interval” :“3600”
    },
 
 
    “schedule”:{
        “crontab”:{
            “query”:“SELECT * FROM crontab;”,
            “interval”:300
        },
        “system_info”:{
            “query”:“SELECT hostname,cpu_brand, physical_memory FROM system_info;“,
            “interval”:3600
        },
        “ssh_login”:{
            “query”:“SELECT username,time,host FROM last WHERE type = 7”,
            “interval”:360
        }
    },
 
    “decorators”:{
        “load”:[
            “SELECT uuid AS host_uuid FROM system_info;”,
            “SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;”
        ]
    },
 
    “packs”:{
        “osquery-monitoring”:“/ usr / share / osquery / packages / osquery -monitoring.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

配置如下:

{
  "queries": {
    "file_events": {
      "query": "SELECT * FROM file_events;",
      "removed": false,
      "interval": 300
    }
  },
  "file_paths": {
    "homes": [
      "/root/.ssh/%%",
      "/home/%/.ssh/%%"
    ],
      "etc": [
      "/etc/%%"
    ],
      "home": [
      "/home/%%"
    ],
      "tmp": [
      "/tmp/%%"
    ],
      "www": [
      "/var/www/%%"
      ]
  }
}

保存并退出

返回到 '/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)已成功完成。

免责声明:本站发布的内容(图片、视频和文字)以原创、来自本网站内容采集于网络互联网转载等其它媒体和分享为主,内容观点不代表本网站立场,如侵犯了原作者的版权,请告知一经查实,将立刻删除涉嫌侵权内容,联系我们QQ:712375056,同时欢迎投稿传递力量。