如何在 Ubuntu 16.04 上使用 osquery 监控系统安全

介绍

osquery是一个开源的安全工具,将操作系统转化为一个巨大的数据库,您可以使用SQL类型的陈述进行查询的表。

它是一个跨平台的应用程序,支持最新版本的macOS,Windows 10,CentOS和Ubuntu,它被正式描述为SQL驱动的操作系统仪表,监控和分析框架,并源自Facebook。

使用 osquery,您可以对您的服务器执行类似从logged_in_users选择*的命令,并获得这样的结果:

 1[secondary_label Output]
 2+-----------+----------+-------+------------------+------------+------+
 3| type      | user     | tty   | host             | time       | pid  |
 4+-----------+----------+-------+------------------+------------+------+
 5| login     | LOGIN    | ttyS0 |                  | 1483580429 | 1546 |
 6| login     | LOGIN    | tty1  |                  | 1483580429 | 1549 |
 7| user      | root     | pts/0 | 24.27.68.82      | 1483580584 | 1752 |
 8| user      | sammy    | pts/1 | 11.11.11.11      | 1483580770 | 4057 |
 9| boot_time | reboot   | ~     | 4.4.0-57-generic | 1483580419 | 0    |
10| runlevel  | runlevel | ~     | 4.4.0-57-generic | 1483580426 | 53   |
11+-----------+----------+-------+------------------+------------+------+

如果这对你有吸引力,你会喜欢使用osquery作为你的服务器的系统安全监控和入侵检测工具。

安装 osquery 可以让你访问以下组件:

  • osqueryi: 交互式 osquery shell,用于执行临时查询。
  • osqueryd: 用于在后台安排和运行查询的daemon。
  • osqueryctl: 用于测试部署或配置osquery的辅助脚本。

osqueryiosqueryd是独立的工具,它们不进行通信,您可以使用一个没有另一个。运行每个工具所需的旗帜和选项大多是相同的,您可以使用osqueryd的配置文件启动osqueryi,以便您可以自定义环境,而无需使用许多命令行开关。

在本教程中,你将:

  • 安装 osquery.
  • 配置操作系统的各个方面,如 Rsyslog,以便 osquery 能够正常运作。
  • 设置可用于osqueryiosqueryd的配置文件。
  • 使用 osquery packs 工作,这些是您可以添加到日程表中的预定义查询组。
  • 使用osqueryi执行临时查询以寻找安全问题。

osqueryd创建的日志,是指向外部日志终端发送,需要额外的专业知识来正确设置和使用。

前提条件

要完成本教程,你需要有以下的地方:

您还应该对 SQL 具有基本的理解和 Linux 系统安全的基本知识(https://andsky.com/tech/tutorials/an-introduction-to-securing-your-linux-vps)。

步骤 1 – 在服务器上安装 osquery

您可以通过从源编译或使用包管理器来安装 osquery. 由于官方 Ubuntu 存储库中没有可安装的包,您将不得不将项目的官方 Ubuntu 存储库添加到系统中。

首先,添加存储库的公共密钥:

1sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B

然后添加库存:

1sudo add-apt-repository "deb [arch=amd64] https://osquery-packages.s3.amazonaws.com/xenial xenial main"

更新包数据库:

1sudo apt-get update

最后,安装 Osquery:

1sudo apt-get install osquery

除了盒子之外,osquery并不令人难以置信的有用;它不是一个插入式应用程序. 无论您打算使用交互式壳还是戴蒙,您必须通过一些旗帜和选项,无论是从命令行还是通过配置文件。

1osqueryd --help

输出将包括数十个命令行标志和配置选项,下面显示的是本文使用的测试服务器的一部分输出。

 1[secondary_label Output]
 2osquery 2.1.2, your OS as a high-performance relational database
 3Usage: osqueryd [OPTION]... 
 4
 5osquery command line flags:
 6
 7    --flagfile PATH Line-delimited file of additional flags
 8    --config_check Check the format of an osquery config and exit
 9    --config_dump Dump the contents of the configuration
10    --config_path VALUE Path to JSON config file
11    --config_plugin VALUE Config plugin name
12    --config_tls_endpoint VALUE TLS/HTTPS endpoint for config retrieval
13    --config_tls_max_attempts VALUE Number of attempts to retry a TLS config/enroll request
14    --config_tls_refresh VALUE Optional interval in seconds to re-read configuration
15    --daemonize Run as daemon (osqueryd only)
16
17...
18
19...
20
21osquery configuration options (set by config or CLI flags):
22
23    --audit_allow_config Allow the audit publisher to change auditing configuration
24    --audit_allow_sockets Allow the audit publisher to install socket-related rules
25    --audit_persist Attempt to retain control of audit
26    --aws_access_key_id VALUE AWS access key ID
27    --aws_firehose_period VALUE Seconds between flushing logs to Firehose (default 10)
28    --aws_firehose_stream VALUE Name of Firehose stream for logging
29    --aws_kinesis_period VALUE Seconds between flushing logs to Kinesis (default 10)
30    --aws_kinesis_random_partition_key Enable random kinesis partition keys
31    --aws_kinesis_stream VALUE Name of Kinesis stream for logging
32    --aws_profile_name VALUE AWS profile for authentication and region configuration
33    --aws_region VALUE AWS region

若要查看仅用于交互式壳的其他命令行旗,请键入:

1osqueryi --help

运行osqueryi是列出和查询可用的osquery表的最简单的方法,例如,使用以下命令启动它:

1osqueryi --verbose

这将使您处于交互式壳中,您将看到类似于此的输出:

1[secondary_label Output]
2I0105 01:52:54.987584 4761 init.cpp:364] osquery initialized [version=2.1.2]
3I0105 01:52:54.987808 4761 extensions.cpp:351] Could not autoload extensions: Failed reading: /etc/osquery/extensions.load
4I0105 01:52:54.987944 4761 extensions.cpp:364] Could not autoload modules: Failed reading: /etc/osquery/modules.load
5I0105 01:52:54.988209 4761 init.cpp:606] Error reading config: config file does not exist: /etc/osquery/osquery.conf
6I0105 01:52:54.988334 4761 events.cpp:886] Error registering subscriber: socket_events: Subscriber disabled via configuration
7I0105 01:52:54.993973 4763 interface.cpp:307] Extension manager service starting: /home/sammy/.osquery/shell.em
8Using a virtual database. Need help, type '.help'
9osquery>

由于输出中的错误和信息信息消息,显而易见,黑暗的所有部分都无法正常运作. 某些查询,如从 yara 选择 *;不返回任何信息,显示表中没有数据。

其他查詢,如「選擇時間、嚴重性、訊息從 syslog ;」會返回如下這樣的訊息,表示我們需要做更多工作:

1[secondary_label Output]
2W1202 15:44:48.600539 1720 virtual_table.cpp:492] Table syslog is event-based but events are disabled
3W1202 15:44:48.600587 1720 virtual_table.cpp:499] Please see the table documentation: https://osquery.io/docs/#syslog

我们将对我们的服务器配置进行一些修改,以解决此问题。

通过键入退出控制台:

1.exit

在下一节中,我们将修改 OS 所需的 osquery 功能。

步骤 2 – 允许 osquery 访问系统日志

在此步骤中,我们将修改操作系统的 syslog 应用程序,以允许 osquery 消耗和查询系统日志. 在 Ubuntu 16.04,这意味着修改 Rsyslog 配置文件。

首先,打开 /etc/rsyslog.conf 文件:

1sudo nano /etc/rsyslog.conf

我们需要添加一些配置行,告诉Rsyslog该写到哪个管道,以及该管道要写到哪个syslog参数。

将以下行附加到文件中:

1[secondary_label /etc/rsyslog.conf]
2template(
3  name="OsqueryCsvFormat"
4  type="string"
5  string="%timestamp:::date-rfc3339,csv%,%hostname:::csv%,%syslogseverity:::csv%,%syslogfacility-text:::csv%,%syslogtag:::csv%,%msg:::csv%\n"
6)
7*.* action(type="ompipe" Pipe="/var/osquery/syslog_pipe" template="OsqueryCsvFormat")

保存并关闭文件. 若要应用更改,请重新启动 syslog daemon:

1sudo systemctl restart rsyslog

现在,让我们创建一个配置文件,设置一些默认选项,并安排一些查询。

步骤 3 – 创建 osquery 配置文件

创建配置文件使运行osqueryi更容易,而不是必须通过许多命令行选项,osqueryi可以从位于/etc/osquery/osquery.conf中的配置文件中读取这些选项。

配置文件还包含需要按日程执行的查询,但是,您可以按日程执行的大多数查询都被发送为被称为 packs

osquery 没有配置文件,但有一个样本配置文件,你可以复制到 `/etc/osquery’ 并修改它,但是,这个文件没有你需要在 Ubuntu 这样的 Linux 发行版上运行的所有选项,所以我们会创建自己的。

配置文件有三个部分:

  • 戴蒙选项和功能设置列表. 这些选项也可以通过osqueryi读取。
  • 运行日程式查询列表以及它们应该运行时。

以下是我们将使用的配置文件选项列表,它们的含义以及我们将设置它们的值. 此选项列表足以在 Ubuntu 16.04 和其他 Linux 发行版上运行 osqueryiosqueryd

  • ** 配置_插件**: Osquery想从哪里读取它的配置 。 默认情况下,它们是从磁盘上的文件读取的,因此其值是"文件系统".
  • ** logger_plugin **: 指定Osquery应当写出计划查询的结果。 我们将再次使用`文件系统'。
  • ** logger_ path**: 这是通往日志目录的路径,您将在那里找到包含信息,警告,错误和预定查询结果的文件. 默认是`/var/log/osquery'。
  • ** 残疾_博客**: 通过确定这一工具的 " 虚假 " 值,我们就可以进行伐木。
  • log_结果_活动: 如果将这一点设定为`真实',结果记录中的每行将代表状态变化。
  • ** 排程_ splay_% ** : 这告诉Osquery,当大量查询被安排在同一时间段时,会跑出它们以限制对服务器的任何性能影响. 默认值为`10',即一个百分比。
  • pidfile:在何地写作Osquery守护进程ID. 默认为/var/osquery/osquery. (原始内容存档于2017-09-26). pidfile. *** 活动_结束** : 数秒后, 订户的持有时间就会在Osquery后置店里。 框外,此为3600'。
  • 数据库_路径 : Osquery数据库的路径. 我们将使用默认值,即`/var/osquery/osquery.db'。
  • ** 动词**:在启用了日志后,用于启用或禁用动词信息消息. 我们把这个设定为"虚假".
  • ** 工人_线程** : 用于处理查询的工作调度线索数量 。 这被设定为 " 2" 默认,所以我们将离开它。
  • ** 允许_监视器** : 用于启用或禁用调度显示器 。 我们将启用它,所以它的价值将是`真实'的。
  • ** 残疾_活动**: 用于规范Osquery的出版/订阅系统. 我们需要这个功能,因此这里的价值将是 " 虚假的 " 。
  • ** 残疾_审计** : 用于禁用操作系统审计子系统的接收事件. 我们需要启用,因此这里使用的价值将是 " 虚假的 " 。
  • ** 审计_allow_配置** : 允许审计出版单位改变审计配置. 默认是`真实'。
  • ** 审计_ allow_sockets**: 这使得审计出版商可以安装与套接字相关的规则. 其价值将是`真实'的。
  • ** 主机_识别器** : 这个用于识别运行中的宿主Osquery. 当从多个服务器取出外观时,它有助于容易地确定某个特定的日志条目来自哪个服务器. 其价值是主名'或uid'。 盒子外,它被设定为"主机名",所以我们将使用这个值.
  • ** 允许_syslog** : 为了使Osquery消耗syslog信息,必须将其设定为`真实'。
  • ** 计划_默认_中间** : 当计划查询的间隔没有设置时,请使用此值. 数秒,我们定出"3600" (英语)

您已经看到如何查看所有命令行旗帜和配置选项可用于osqueryiosqueryd,但上述选项将足以在这个服务器上运行osquery。

使用以下命令创建并打开配置文件:

1sudo nano /etc/osquery/osquery.conf

配置文件使用 JSON格式. 将以下内容复制到文件中:

 1[label /etc/osquery/osquery.conf]
 2{
 3  "options": {
 4    "config_plugin": "filesystem",
 5    "logger_plugin": "filesystem",
 6    "logger_path": "/var/log/osquery",
 7    "disable_logging": "false",
 8    "log_result_events": "true",
 9    "schedule_splay_percent": "10",
10    "pidfile": "/var/osquery/osquery.pidfile",
11    "events_expiry": "3600",
12    "database_path": "/var/osquery/osquery.db",
13    "verbose": "false",
14    "worker_threads": "2",
15    "enable_monitor": "true",
16    "disable_events": "false",
17    "disable_audit": "false",
18    "audit_allow_config": "true",
19    "host_identifier": "hostname",
20    "enable_syslog": "true",
21    "audit_allow_sockets": "true",
22    "schedule_default_interval": "3600" 
23  },

配置文件的下一个部分是时间安排部分. 每个查询都被标记为一个密钥或名称,该密钥必须在文件中是唯一的,然后是要运行的查询,并在几秒钟内运行查询的间隔。

将这些行添加到配置文件中:

1[label /etc/osquery/osquery.conf]
2  "schedule": {
3    "crontab": {
4      "query": "SELECT * FROM crontab;",
5      "interval": 300
6    }
7  },

您可以写任何您想要的查询数目。只需保持正确的格式。如果没有,文件将失败验证. 例如,若要添加几个查询,请添加这些行:

 1[label /etc/osquery/osquery.conf]
 2  "schedule": {
 3    "crontab": {
 4      "query": "SELECT * FROM crontab;",
 5      "interval": 300
 6    },
 7    "system_profile": {
 8      "query": "SELECT * FROM osquery_schedule;"
 9    }, 
10    "system_info": {
11      "query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
12      "interval": 3600
13    }
14  },

计划查询后,您可以添加名为 decorators的特殊查询,这些查询将数据提前给其他计划查询。

将这些行附加到文件中:

1[label /etc/osquery/osquery.conf]
2  "decorators": {
3    "load": [
4      "SELECT uuid AS host_uuid FROM system_info;",
5      "SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
6    ]
7  },

最后,我们可以将 osquery 指向包含更具体的查询的包列表。每个安装的 osquery 都配有位于 `/usr/share/osquery/packs’ 目录中的默认包组合。其中一个包适用于 macOS,其余的则适用于 Linux 系统。

将这些行添加到文件中以完成文件。

1[label /etc/osquery/osquery.conf]
2  "packs": {
3     "osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf",
4     "incident-response": "/usr/share/osquery/packs/incident-response.conf",
5     "it-compliance": "/usr/share/osquery/packs/it-compliance.conf",
6     "vuln-management": "/usr/share/osquery/packs/vuln-management.conf"
7  }
8}

请注意在末尾的关闭曲线,这匹配了文件的第一行上的打开曲线。

 1[label /etc/osquery/osquery.conf]
 2{
 3  "options": {
 4    "config_plugin": "filesystem",
 5    "logger_plugin": "filesystem",
 6    "logger_path": "/var/log/osquery",
 7    "disable_logging": "false",
 8    "log_result_events": "true",
 9    "schedule_splay_percent": "10",
10    "pidfile": "/var/osquery/osquery.pidfile",
11    "events_expiry": "3600",
12    "database_path": "/var/osquery/osquery.db",
13    "verbose": "false",
14    "worker_threads": "2",
15    "enable_monitor": "true",
16    "disable_events": "false",
17    "disable_audit": "false",
18    "audit_allow_config": "true",
19    "host_identifier": "hostname",
20    "enable_syslog": "true",
21    "audit_allow_sockets": "true",
22    "schedule_default_interval": "3600" 
23  },
24  "schedule": {
25    "crontab": {
26      "query": "SELECT * FROM crontab;",
27      "interval": 300
28    },
29    "system_profile": {
30      "query": "SELECT * FROM osquery_schedule;"
31    }, 
32    "system_info": {
33      "query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
34      "interval": 3600
35    }
36  },
37  "decorators": {
38    "load": [
39      "SELECT uuid AS host_uuid FROM system_info;",
40      "SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
41    ]
42  },
43  "packs": {
44     "osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf",
45     "incident-response": "/usr/share/osquery/packs/incident-response.conf",
46     "it-compliance": "/usr/share/osquery/packs/it-compliance.conf",
47     "vuln-management": "/usr/share/osquery/packs/vuln-management.conf"
48  }
49}

保存并关闭文件,然后使用以下命令验证它:

1sudo osqueryctl config-check

输出应该是这样的:

1[secondary_label Output]
2I0104 11:11:46.022858 24501 rocksdb.cpp:187] Opening RocksDB handle: /var/osquery/osquery.db

如果出现错误,输出将显示错误的位置,以便您可以修复它。

一旦您有有效的配置文件,您可以继续配置文件完整性监控所需的 osquery 包。

步骤 4 – 设置 osquery 文件完整性监控包

密切关注服务器上的文件的完整性是监控其系统安全的关键方面。

在本节中,我们将添加另一个包到列表中,该包将包含用于文件完整性监控的查询和指令。

创建此文件并在您的编辑器中打开它:

1sudo nano /usr/share/osquery/packs/fim.conf

我们将创建一个包,它将监控在/home/etc/tmp目录中的文件事件每300秒。

 1[secondary_label /usr/share/osquery/packs/fim.conf]
 2{
 3  "queries": {
 4    "file_events": {
 5      "query": "select * from file_events;",
 6      "removed": false,
 7      "interval": 300
 8    }
 9  },
10  "file_paths": {
11    "homes": [
12      "/root/.ssh/%%",
13      "/home/%/.ssh/%%"
14    ],
15      "etc": [
16      "/etc/%%"
17    ],
18      "home": [
19      "/home/%%"
20    ],
21      "tmp": [
22      "/tmp/%%"
23    ]
24  }
25}

保存并关闭文件。

要使新文件及其规则可供 osquery 使用,请在 /etc/osquery/osquery.conf 末尾的包列表中引用它。

1sudo nano /etc/osquery/osquery.conf

然后修改包部分以包含新文件:

 1[secondary_label /etc/osquery/osquery.conf]
 2
 3...
 4
 5"packs": {
 6     "fim": "/usr/share/osquery/packs/fim.conf",
 7     "osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf",
 8     "incident-response": "/usr/share/osquery/packs/incident-response.conf",
 9     "it-compliance": "/usr/share/osquery/packs/it-compliance.conf",
10     "vuln-management": "/usr/share/osquery/packs/vuln-management.conf"
11  }

保存和关闭文件. 只是为了确保您没有在文件中犯错误,请再次验证它:

1sudo osqueryctl config-check

现在让我们开始使用osqueryi来查询系统。

步骤 5 – 使用 osqueryi 执行临时安全检查

在本节中,您将使用osqueryi,交互式壳,在您的系统上执行各种安全检查. 请记住,在这一点上,我们还没有启动osquery daemon,这就是osquery的美丽 - 您可以使用osqueryi运行查询,即使osqueryi没有活跃,同时仍然使用我们构建的配置文件来配置环境。

要在配置文件中启动osquery,键入:

1sudo osqueryi --config_path /etc/osquery/osquery.conf --verbose

<$>[注意] 注意:通过osqueryiosquerydverbose选项是一个很好的做法,因为它允许你看到任何错误或警告,可能表明与osquery有关的问题。

让我们从基本的安全检查开始,从那里开始工作,例如,除了你之外,现在还有谁登录到系统?用这个查询来找出:

1select * from logged_in_users ;

输出应该是这样的:

 1[secondary_label Output]
 2+-----------+----------+-------+------------------+------------+------+
 3| type      | user     | tty   | host             | time       | pid  |
 4+-----------+----------+-------+------------------+------------+------+
 5| boot_time | reboot   | ~     | 4.4.0-57-generic | 1483580419 | 0    |
 6| runlevel  | runlevel | ~     | 4.4.0-57-generic | 1483580426 | 53   |
 7| login     | LOGIN    | ttyS0 |                  | 1483580429 | 1546 |
 8| login     | LOGIN    | tty1  |                  | 1483580429 | 1549 |
 9| user      | root     | pts/0 | 11.11.11.11      | 1483580584 | 1752 |
10| user      | sammy    | pts/1 | 11.11.11.11      | 1483580770 | 4057 |
11+-----------+----------+-------+------------------+------------+------+

在此输出中,有两个真实的用户帐户登录到机器,它们都来自同一个IP地址. 该IP地址应该是一个已知的IP地址. 如果不是,你应该调查该登录来自哪里。

上一个查询告诉你现在谁已登录,但以前的登录怎么样?你可以通过查询 最后的表来了解:

1select * from last ;

输出表明没有什么不正常的,所以最近没有其他人登录到机器:

 1[secondary_label Output]
 2+----------+-------+------+------+------------+------------------+
 3| username | tty   | pid  | type | time       | host             |
 4+----------+-------+------+------+------------+------------------+
 5| reboot   | ~     | 0    | 2    | 1483580419 | 4.4.0-57-generic |
 6| runlevel | ~     | 53   | 1    | 1483580426 | 4.4.0-57-generic |
 7|          | ttyS0 | 1546 | 5    | 1483580429 |                  |
 8| LOGIN    | ttyS0 | 1546 | 6    | 1483580429 |                  |
 9|          | tty1  | 1549 | 5    | 1483580429 |                  |
10| LOGIN    | tty1  | 1549 | 6    | 1483580429 |                  |
11| root     | pts/0 | 1752 | 7    | 1483580584 | 11.11.11.11      |
12| sammy    | pts/1 | 4057 | 7    | 1483580770 | 11.11.11.11      |
13+----------+-------+------+------+------------+------------------+

防火墙配置和激活了吗?防火墙仍在运行吗?如果您有疑问,请运行此查询以查找:

1select * from iptables ;

如果没有输出,那么这意味着IPTables防火墙尚未配置,对于面向互联网的服务器来说,这不是一件好事,所以最好配置防火墙。

您可以运行以前的命令,修改为过滤特定列,如下:

1select chain, policy, src_ip, dst_ip from iptables ;

该查询应该产生如下结果. 寻找您未配置的任何不寻常的源和目的地 IP 地址:

 1[secondary_label Output]
 2+---------+--------+---------+-----------+
 3| chain   | policy | src_ip  | dst_ip    |
 4+---------+--------+---------+-----------+
 5| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
 6| INPUT   | ACCEPT | 0.0.0.0 | 127.0.0.0 |
 7| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
 8| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
 9| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
10| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
11| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
12| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
13| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
14| INPUT   | ACCEPT | 0.0.0.0 | 0.0.0.0   |
15| FORWARD | ACCEPT | 0.0.0.0 | 0.0.0.0   |
16| FORWARD | ACCEPT | 0.0.0.0 | 0.0.0.0   |
17| OUTPUT  | ACCEPT | 0.0.0.0 | 0.0.0.0   |
18| OUTPUT  | ACCEPT | 0.0.0.0 | 0.0.0.0   |
19+---------+--------+---------+-----------+

在 crontab 安排了什么类型的工作?你是否安排了这些工作? 这个查询将帮助你找到计划在特定的间隔运行的恶意软件:

1select command, path from crontab ;

任何看起来可疑的命令都需要进一步调查:

 1[secondary_label Output]
 2+----------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+
 3| command                                                                                                                                | path                           |
 4+----------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+
 5| root cd / && run-parts --report /etc/cron.hourly                                                                                       | /etc/crontab                   |
 6| root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )                                                       | /etc/crontab                   |
 7| root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )                                                      | /etc/crontab                   |
 8| root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )                                                     | /etc/crontab                   |
 9| root if [ -x /usr/share/mdadm/checkarray ] && [ $(date +\%d) -le 7 ]; then /usr/share/mdadm/checkarray --cron --all --idle --quiet; fi | /etc/cron.d/mdadm              |
10| root test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond                                          | /etc/cron.d/popularity-contest |
11+----------------------------------------------------------------------------------------------------------------------------------------+--------------------------------+

系统上是否有设置允许的文件?在任何Ubuntu 16.04服务器上都有很少的文件,但它们是什么,并且是否有任何不应该在系统上存在的文件? 这些问题的答案将帮助您检测后门二进制。 定期运行此查询并将其结果与较旧的结果进行比较,以便您可以关注任何补充。

1select * from suid_bin ;

此查询的部分输出可能看起来像这样:

 1[secondary_label Output]
 2+-------------------------------+----------+-----------+-------------+
 3| path                          | username | groupname | permissions |
 4+-------------------------------+----------+-----------+-------------+
 5| /bin/ping6                    | root     | root      | S           |
 6| /bin/su                       | root     | root      | S           |
 7| /bin/mount                    | root     | root      | S           |
 8| /bin/umount                   | root     | root      | S           |
 9| /bin/fusermount               | root     | root      | S           |
10| /bin/ntfs-3g                  | root     | root      | S           |
11| /bin/ping                     | root     | root      | S           |
12| /sbin/mount.ntfs-3g           | root     | root      | S           |
13| /sbin/mount.ntfs              | root     | root      | S           |
14| /sbin/unix_chkpwd             | root     | shadow    | G           |
15| /sbin/pam_extrausers_chkpwd   | root     | shadow    | G           |
16| /usr/bin/chage                | root     | shadow    | G           |
17| /usr/bin/locate               | root     | mlocate   | G           |
18| /usr/bin/chfn                 | root     | root      | S           |
19| /usr/bin/chsh                 | root     | root      | S           |
20| /usr/bin/newuidmap            | root     | root      | S           |
21| /usr/bin/write                | root     | tty       | G           |
22| /usr/bin/mlocate              | root     | mlocate   | G           |
23| /usr/bin/at                   | daemon   | daemon    | SG          |
24| /usr/bin/sg                   | root     | root      | S           |

若要查看已加载的内核模块列表,请运行此查询:

1select name, used_by, status from kernel_modules where status="Live" ;

这是另一个您需要定期运行的查询,并将其输出与较旧的结果进行比较,以查看是否有任何变化。

还有一种方法,可以帮助您在服务器上找到后门,就是运行一个列出所有倾听端口的查询。

1select * from listening_ports ;

在新鲜的服务器上,只有SSH在端口22上运行,输出将看起来像这样:

1[secondary_label Output]
2+-------+------+----------+--------+---------+
3| pid   | port | protocol | family | address |
4+-------+------+----------+--------+---------+
5| 1686  | 22   | 6        | 2      | 0.0.0.0 |
6| 1686  | 22   | 6        | 10     | ::      |
7| 25356 | 0    | 0        | 0      |         |
8+-------+------+----------+--------+---------+

在您的服务器上,如果输出仅包含您知道服务器应该倾听的端口,则无需担心,但如果还有其他端口打开,则需要调查这些端口是什么。

要查看服务器上的文件活动,运行此查询:

1select target_path, action, uid from file_events ;

输出显示了服务器上的所有最近的文件活动,以及负责该活动的用户ID。

 1[secondary_label Output]
 2+---------------------------+---------+------+
 3| target_path               | action  | uid  |
 4+---------------------------+---------+------+
 5| /home/sammy/..bashrc.swp  | CREATED | 1000 |
 6| /home/sammy/..bashrc.swp  | UPDATED | 1000 |
 7| /home/sammy/..bashrc.swp  | UPDATED | 1000 |
 8| /home/sammy/.bashrc       | UPDATED | 1000 |
 9| /home/sammy/..bashrc.swp  | DELETED | 1000 |
10| /home/sammy/..bashrc.swp  | CREATED | 1000 |
11| /home/sammy/..bashrc.swp  | UPDATED | 1000 |
12| /home/sammy/..bashrc.swp  | UPDATED | 1000 |
13| /home/sammy/.bashrc       | UPDATED | 1000 |
14| /home/sammy/.bashrc       | UPDATED | 1000 |
15| /home/sammy/.bashrc       | UPDATED | 1000 |
16| /home/sammy/..bashrc.swp  | DELETED |      |
17| /etc/test_file.txt        | DELETED |      |
18| /home/sammy/.bash_history | UPDATED | 1000 |
19| /home/sammy/.bash_history | UPDATED | 1000 |
20| /etc/secret_file.md       | CREATED | 0    |
21| /etc/secret_file.md       | UPDATED | 0    |
22| /etc/secret_file.md       | UPDATED | 0    |
23+---------------------------+---------+------+

您可以在您的服务器上运行许多查询,以了解可能存在的安全问题。

如果您对表的方案不确定,请使用以下命令来查找:

1.schema name-of-table

您可以列出可用的表格:

1.tables

在包中有许多其他例子,这些包配有 osquery,许多例子旨在由osqueryd定期运行,在下一节中,您将学习如何启动 DAEMON 来运行这些查询。

步骤6 - 运行 Osqueryd

这些查询包括您在步骤 4 中配置的查询,我们在步骤 4 中指定的查询包中的查询,以及我们在步骤 5 中配置的 FIM 查询包中的查询,如果您还没有研究这些查询,现在是时候看看 `/usr/share/osquery/packs 的内容。

osqueryd生成的结果被写入名为osqueryd.results.log的文件中,在/var/log/osquery目录中。

您可以使用systemctlosqueryctl来启动osqueryd。这两者都完成了相同的事情,所以无论您使用哪个文件都无关紧要。

但是,既然您已经设置了配置文件,您只需要在这里启动 DAEMON:

1sudo systemctl start osqueryd

或者你可以打字:

1sudo osqueryctl start

在启动 DAEMON 后几分钟内,\var/log/osquery/osqueryd.results.log 的尺寸应该增加,您可以通过键入并重复下一个命令来看到这一点:

1ls -lh /var/log/osquery/osqueryd.results.log

不幸的是,osquery没有像 OSSEC这样的警告功能,所以除非你查看结果文件,否则你无法看到日程查询的结果。

1sudo tail -f /var/log/osquery/osqueryd.results.log

CTRL+C,停止对日志进行收尾。

长期而言,您希望将查询结果日志发送到您可以使用的外部分析平台,可行的开源选项包括 Doorman, ZentralElasticSearch

结论

osquery 是使用熟悉的 SQL 语法执行一次性和日程式查询的有效工具。 osqueryi' 是写一次性查询的 osquery 组件,而 osqueryd' 是用于日程式查询。 要了解日程式查询的结果,您需要将其发送到外部日志分析平台。 您可以在 https://osquery.io/找到有关 osquery 的更多信息。

Published At
Categories with 技术
comments powered by Disqus