资讯详情

Security:osquery 介绍

osquery 是适用于 Windows、OS X (macOS) 和 Linux 操作系统检测框架。这些工具使低级操作系统的分析和监控既高效又直观。 osquery将操作系统公开为高性能关系数据库。 这允许你基于写作 SQL 对操作系统数据进行查询。 使用 osquery,SQL table 表示抽象概念,如运行过程、加载核模块、打开网络连接、浏览器插件、硬件事件或文件哈希。

  • 对 OSs 查询就像操作数据库一样
  • 使用最基本的 SQL 命令提问设备
  • 对 OSs 展示系统中的资源和设备

SQL 通过简单的插件和扩展表 API 实现的。 有各种各样的手表,并且正在编写更多的手表:https://osquery.io/schema。

安装

下载最新的稳定版本、存储信息和安装说明,请访问 https://osquery.io/downloads。可根据自己的操作系统单独安装 Windows, macOS 及 Linux。 简单地说:

Red Hat, CentOS 及 Fedora

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 yum install osquery

基于 Debian 及Ubuntu Linux 发行版

echo "deb [arch=amd64] https://pkg.osquery.io/deb deb main" | sudo tee /etc/apt/sources.list.d/osquery.list  sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B  sudo apt-get update sudo apt-get install osquery

Windows

https://pkg.osquery.io/windows/osquery-4.8.0.msi

我将在今天的演示中使用它 Ubuntu 20.04 来进行展示。具体安装步骤可以参阅How to Install and Use Osquery in Ubuntu

使用 osqueryi

osqueryi 是 osquery 交互式查询控制台/shell。 在这种模式下,它是完全独立的,不与守护过程沟通,也不需要作为管理员运行(尽管有些表在作为非管理员运行时可能会返回更少的结果)。 使用 osquery shell 设计查询原型,探索操作系统的现状。

执行 SQL queries

osqueryi 允许您操作命令和查询 osquery 表。 请参考表格、类型和列描述的完整列表 schema API。 有关 SQL 请参考语法帮助 SQLite 所理解的 SQL。

我们可以在自己的 terminal 中运行 osqueryi

osqueryi
liuxg@liuxgu:~$ osqueryi Using a virtual database. Need help, type '.help' osquery> 

接下来,我们试着做一些事情 help。首先,我们想知道什么 table 我们可以查询:

osquery> .table   => acpi_tables   => apparmor_events   => apparmor_profiles   => apt_sources   => arp_cache   => atom_packages   => augeas   => authorized_keys   => azure_instance_metadata   => azure_instance_tags   => block_devices   => bpf_process_events   => bpf_socket_events   => carbon_black_info   => carves   => chrome_extension_content_scripts   => chrome_extensions   => cpu_time   [...]

我们可以通过 .table 命令查询一切 table。我们可以查询这些表格。

接下来,我们可以使用以下命令来查看我们感兴趣的 table 的 schema:

osquery> .schema routes CREATE TABLE routes(`destination` TEXT, `netmask` INTEGER, `gateway` TEXT, `source` TEXT, `flags` INTEGER, `interface` TEXT, `mtu` INTEGER, `metric` INTEGER, `type` TEXT, `hopcount` INTEGER, PRIMARY KEY (`destination`, `netmask`, `gateway`, `source`, `flags`, `interface`, `mtu`, `metric`, `type`, `hopcount`)) WITHOUT ROWID;

或使用以下命令:

osquery> pragma table_info(routes);  ----- ------------- --------- --------- ------------ ----  | cid | name        | type    | notnull | dflt_value | pk |  ----- ------------- --------- --------- ------------ ----  | 0   | destination | TEXT    | 1       |            | 1  | | 1   | netmask     | INTEGER | 1       |            | 2  | | 2   | gateway     | TEXT    | 1       |            | 3  | | 3   | source      | TEXT    | 1       |            | 4  | | 4   | flags       | INTEGER | 1       |            | 5  | | 5   | interface   | TEXT    | 1       |            | 6  | | 6   | mtu         | INTEGER | 1       |            | 7  | | 7   | metric      | INTEGER | 1       |            | 8  | | 8   | type        | TEXT    | 1       |            | 9  | | 9   | hopcount    | INTEGER | 1       |            | 10 |  ----- ------------- --------- --------- ------------ ---- 

我们还可以列出所有可用的命令: .help

osquery> .help Welcome to the osquery shell. Please explore your OS! You are connected to a transient 'inmemory' virtual database.

.all [TABLE]     Select all from a table
.bail ON|OFF     Stop after hitting an error
.connect PATH    Connect to an osquery extension socket
.disconnect      Disconnect from a connected extension socket
.echo ON|OFF     Turn command echo on or off
.exit            Exit this program
.features        List osquery's features and their statuses
.headers ON|OFF  Turn display of headers on or off
.help            Show this message
.mode MODE       Set output mode where MODE is one of:
                   csv      Comma-separated values
                   column   Left-aligned columns see .width
                   line     One value per line
                   list     Values delimited by .separator string
                   pretty   Pretty printed SQL results (default)
.nullvalue STR   Use STRING in place of NULL values
.print STR...    Print literal STRING
.quit            Exit this program
.schema [TABLE]  Show the CREATE statements
.separator STR   Change separator used by output mode
.socket          Show the local osquery extensions socket path
.show            Show the current values for various settings
.summary         Alias for the show meta command
.tables [TABLE]  List names of tables
.types [SQL]     Show result of getQueryColumns for the given query
.width [NUM1]+   Set column widths for "column" mode
.timer ON|OFF      Turn the CPU timer measurement on or off

如果你想退出,那么使用 .exit 命令或者 ^D。

接下来我们来做一些简单的 query。

列出来所有的用户

我们打入如下的命令:

SELECT * from users;

上面的显示有些凌乱,我们可以只显示部分的信息:

SELECT username, shell FROM users;

我们甚至可以使用 WHERE 来限定特定的条件:

SELECT * FROM users WHERE username="root";

 我们可以显示我们感兴趣的字段:

SELECT username, gid FROM users WHERE username="root";

列出所有的 processes

我们可以通过在交互式 shell 中运行以下命令来列出在 ubuntu 中运行的前五个进程

SELECT * FROM processes LIMIT 5;

由于系统中有很多进程在运行,所以我们使用 LIMIT 关键字只显示了五个进程。

我们可以找到特定进程的进程ID,例如我们想找到 Java 的进程 ID,所以我们将在交互式 shell 中运行以下命令

SELECT pid FROM processes WHERE name="java";

查找 Ubuntu 版本信息

我们可以通过在交互式 shell 中运行以下命令来找到我们的 Ubuntu 系统的版本:

SELECT * FROM os_version;

检查网络接口和 IP 地址

我们可以通过在交互式 shell 中运行以下查询来检查 IP 地址、网络接口的子网掩码。

SELECT interface,address,mask FROM interface_addresses
WHERE interface NOT LIKE '%lo%';

检查登录用户

我们还可以通过从 “logged_in_users” 表中查询数据来检查您系统上的登录用户。 运行以下命令以查找已登录的用户。

SELECT user,host,time FROM logged_in_users WHERE tty NOT LIKE '-';

检查系统内存

我们还可以通过在交互式 shell 中运行一些基于 SQL 的命令来检查总内存、空闲内存缓存内存等。 要检查总内存,请运行以下命令。 这将为我们提供系统的总内存(以字节为单位)。

SELECT memory_total FROM memory_info;

我们还可以通过运行以下查询使用 memory_info 表检查系统的缓存内存。

select cached from memory_info;

列出 groups

我们可以通过在交互式 shell 中运行以下查询来找到系统中的所有组

SELECT * FROM groups;

显示监听端口

我们可以通过在交互式 shell 中运行以下命令来显示我们系统的所有监听端口:

SELECT * FROM listening_ports;

我们还可以通过在交互式 shell 中运行以下命令来检查端口是否正在侦听:

SELECT port, address, path,family,protocol FROM listening_ports WHERE port=9200;

检测一个正在运行的进程但是没有 binary 在磁盘上

SELECT name, path, pid FROM processes WHERE on_disk = 0;

这通常是一个病毒。

检测一个用户的登录时间

SELECT * FROM last
WHERE username = "liuxg"
AND time > (( SELECT unix_time FROM time ) - 3600 );

寻找侦听网络端口的新进程

SELECT DISTINCT process.name, listening.port, listening.address, process.pid FROM processes AS process JOIN listening_ports AS listening ON process.pid = listening.pid;

查找 Python 安装的包

select name, version from python_packages; 

查找已经装载的 kernel 模块

select name from kernel_modules;

查找消耗 CPU 最高的5个进程

SELECT pid, uid, name, ROUND((
  (user_time + system_time) / (cpu_time.tsb - cpu_time.itsb)
) * 100, 2) AS percentage
FROM processes, (
SELECT (
  SUM(user) + SUM(nice) + SUM(system) + SUM(idle) * 1.0) AS tsb,
  SUM(COALESCE(idle, 0)) + SUM(COALESCE(iowait, 0)) AS itsb
  FROM cpu_time
) AS cpu_time
ORDER BY user_time+system_time DESC
LIMIT 5;

 如上所示,那个 java 进程就是 Elasticsearch 运行的进程。它占用的 CPU 时间比较多。

对 Chrome 浏览器中的插件进行查询

SELECT ext.name, ext.description, ext.version
   FROM users
   JOIN chrome_extensions ext using (uid);

运动 osqueryd

在有些时候,我们希望运行 osquery 并让它作为一个服务一直运行,并定期收集信息。这些信息可以写到一个日志文件中。为了能到这个目的,我们必须创建一个 osquery 的配置文件。这个配置文件依赖于不同的操作系统,它的位置有所改变:

Windows: C:\Program Files\osquery\osquery.conf
Linux: /etc/osquery/osquery.conf and /etc/osquery/osquery.conf.d/
macOS: /var/osquery/osquery.conf and /var/osquery/osquery.conf.d/

你可以使用 --config_path=/path/to/osquery.conf 覆盖文件系统插件的路径。针对 Linux 系统,我们可以创建如下的文件:

liuxg@liuxgu:~$ ls /etc/osquery/osquery.conf
/etc/osquery/osquery.conf
liuxg@liuxgu:~$ cat /etc/osquery/osquery.conf
{
    "options": {
        "config_plugin": "filesystem",
        "logger_plugin": "filesystem",
        "utc": "true"
    },

    "schedule": {
        "system_info": {
        "query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
        "interval": 20
        },
        "high_load_average": {
        "query": "SELECT period, average, '70%' AS 'threshold' FROM load_average WHERE period = '15m' AND average > '0.7';",
        "interval": 10,
        "description": "Report if load charge is over 70 percent."
        },
        "low_free_memory": {
        "query": "SELECT memory_total, memory_free, CAST(memory_free AS real) / memory_total AS memory_free_perc, '10%' AS threshold FROM memory_info WHERE memory_free_perc < 0.1;",
        "interval": 60,
        "description": "Free RAM is under 10%."
        }
    }
}

在上面的 schedule 里,定义的时间间隔是以秒来计算的。它们定期收集信息并生成日志。

我们来启动 osqueryd:

systemctl enable osqueryd
systemctl start osqueryd

我们接下来进入到如下的路径:

liuxg@liuxgu:~$ cd /var/log/osquery/
liuxg@liuxgu:/var/log/osquery$ ls
osqueryd.INFO  osqueryd.INFO.20220428-150819.82777  osqueryd.results.log
liuxg@liuxgu:/var/log/osquery$ 

我们查看其中的 osqueryd.results.log 内容:

我们可以看到有些已经被收集起来的信息。这些信息可以被传送到一些系统中做更进一步的分析。 

结论

Osquery 是一个非常有用的软件实用程序,可用于查找有关你的系统的任何类型的信息。 如果你已经了解基于 SQL 的查询,那么它对你来说非常容易使用,或者如果你不了解基于 SQL 的查询,那么我已尽力向你展示一些对查找数据有用的主要查询。 你可以通过运行类似的查询从任何表中找到任何类型的数据。

在将来的文章中,我将详细讲述如何使用 Elastic Agent来获取 osquery 信息。敬请期待!

参考:

【1】 Demos/Osquery at main · OpenSecureCo/Demos · GitHub

标签: 二极管dflt48a

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台