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.listsudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1484120AC4E9F8A1A577AEEE97A80C63C9D8B80Bsudo 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 'in-memory' 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 valuescolumn   Left-aligned columns see .widthline     One value per linelist     Values delimited by .separator stringpretty   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 itsbFROM cpu_time
) AS cpu_time
ORDER BY user_time+system_time DESC
LIMIT 5;

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

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

SELECT ext.name, ext.description, ext.versionFROM usersJOIN 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 系统,我们可以创建如下的文件:

/etc/osquery/osquery.conf

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

Security:osquery 介绍相关推荐

  1. Spring Security 基本介绍,初窥路径

    Spring Security 基本介绍和环境搭建 实验介绍 Spring Security 是一个非常强大的身份验证和授权控制框架.为了满足企业项目的不同需求,它提供了很多定制化开发的解决方案,通过 ...

  2. 教程篇(6.0) 01. FortiGate及其Security Fabric介绍 ❀ FortiGate 安全 ❀ Fortinet 网络安全专家 NSE 4

    在本课中,你将了解 FortiGate 管理基础知识和 FortiGate 中可以扩展功能的组件.这一课还包括如何以及在哪里将 FortiGate 安装到你现有的网络体系结构和 Security Fa ...

  3. spring security——基本介绍(一)

    一.spring security 简介 spring security 的核心功能主要包括: 认证 (你是谁) 授权 (你能干什么) 攻击防护 (防止伪造身份) 其核心就是一组过滤器链,项目启动后将 ...

  4. 翼支付门户架构之Spring Security框架介绍

    Spring Security3,其前身是"spring的acegi安全系统". 先来谈一谈Acegi的基础知识,Acegi的架构比较复杂.如果对Web资源进行保护,最好的办法莫过 ...

  5. 【Spring Security】基本功能介绍

    文章目录 1.spring security 简介 spring security 基本原理 2 入门项目 2.1 web工程配置 2.1 加入Spring Security 3. 参数详解 3.1. ...

  6. Spring Security:身份验证入口AuthenticationEntryPoint介绍与Debug分析

    ExceptionTranslationFilter ExceptionTranslationFilter(Security Filter)允许将AccessDeniedException和Authe ...

  7. iOS Application Security

    文章分A,B,C,D 4个部分. A) iOS Application Security 下面介绍iOS应用安全,如何分析和动态修改app.  1)iOS Application security P ...

  8. SpringBoot + Spring Security 简单入门

    这篇文章主要介绍了SpringBoot + Spring Security 简单入门 Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考 ...

  9. 蓝牙协议分析(11)_BLE安全机制之Security Manager

    1. 前言 书接上文,我们在"蓝牙协议分析(10)_BLE安全机制之LE Encryption"中介绍了BLE安全机制中的终极武器----数据加密.不过使用这把武器有个前提,那就是 ...

最新文章

  1. 转: 理解AngularJS中的依赖注入
  2. python语言百分号的含义_python【百分号】
  3. 知识图谱 图数据库 推理_图数据库的知识表示与推理
  4. 电开大计算机应用基础作业,2016年电大-电大计算机应用基础作业 答案.doc
  5. oracle ebs 安装教程,Oracle_EBS_系统安装以及维护.doc
  6. Windows Server 2008 R2托管服务账户(MSA)的功能
  7. Spring Boot从入门到精通-项目搭建
  8. 常用工具类(初级中的初级)
  9. 帝云CMS内容管理系统
  10. gbk、gb2312、big5、unicode、utf-8
  11. css样式,鼠标移动上去变成禁用、小手等样式。
  12. CCNA系列十一之Frame-Relay
  13. TI FMCW毫米波雷达基础(2)——测速原理
  14. unity步步生花(触发类互动)
  15. 数据结构——邻接表Adjacency List
  16. 晴天的魔法乐园——谢尔宾斯基地毯(递归打印图形)
  17. 4K显示器和防蓝光眼镜膜
  18. openGauss社区理事会正式成立!云和恩墨与3大运营商、7大头部银行等18家理事单位加入,共建、共享、共治优质社区...
  19. 自从被国家认定为新时代的农民工过后,国家又不让我 996 了.
  20. SftpGo:一款高性能的sftp server服务

热门文章

  1. JavaWeb学习笔记(JavaScript)
  2. 神州控股、神州信息、神州数码集团与腾讯云签署战略合作
  3. 英文视频字幕生成和翻译工具、AI拟声工具
  4. 使用BG/NBD模型与Gamma-Gamma模型预测客户的生命周期价值CLV/LTV
  5. 使用mysqladmin检测MySQL运行状态的教程
  6. Ubuntu安装Elasticsearch集群及Kibana
  7. Vue3的reactive函数
  8. 2019第十一周作业
  9. 单源最短路径算法java_数据结构 - 单源最短路径之迪杰斯特拉(Dijkstra)算法详解(Java)...
  10. 【面试题】概率题总结(随机数、抛硬币)