说明

osquery是一个由FaceBook开源用于对系统进行查询、监控以及分析的一款软件。osquery对其的说明如下:

osquery exposes an operating system as a high-performance relational database. This allows you to write SQL-based queries to explore operating system data. With osquery, SQL tables represent abstract concepts such as running processes, loaded kernel modules, open network connections, browser plugins, hardware events or file hashes.

我们知道当你们在Linux中使用诸如pstopls -l等等命令的时候,可以发下其实他们的输出结果的格式都是很固定的很像一张表。或许是基于这样的想法,facebook开发了osqueryosquery将操作系统当作是一个高性能的关系型数据库。使用osquery运行我们能够使用类似于SQL语句的方式去查询数据库中的信息,比如正在运行的进程信息,加载的内核模块,网络连接,浏览器插件等等信息(一切查询的信息的粒度取决于osquery的实现粒度了)。

osquery也广泛地支持多个平台,包括MacOSCentOSUbuntuWindows 10以及FreeBSD,具体所支持的版本的信息也可以在osquery主页查看。除此之外,osquery的配套文档/网站也是一应俱全,包括主页、Github、readthedocs、slack。

本篇文章以CentOS为例说明Osquery的安装以及使用。

安装

在主页上面提供了不同操作系统的安装包,我们下载CentOS对应的rpm文件即可。在本例中文件名是osquery-3.3.0-1.linux.x86_64.rpm,使用命令sudo yum install osquery-3.3.0-1.linux.x86_64.rpm安装。安装成功之后会出现:

Installed:osquery.x86_64 0:3.3.0-1.linux
Complete!

运行

osquery存在两种运行模式,分别是osqueryi(交互式模式)、osqueryd(后台进程模式)。

  • osqueryi,与osqueryd安全独立,不需要以管理员的身份运行,能够及时地查看当前操作系统的状态信息。
  • osqueryd,我们能够利用osqueryd执行定时查询记录操作系统的变化,例如在第一次执行和第二次执行之间的进程变化(增加/减少),osqueryd会将进程执行的结果保存(文件或者是直接打到kafka中)。osqueryd还会利用操作系统的API来记录文件目录的变化、硬件事件、网络行为的变化等等。osqueryd在Linux中是以系统服务的方式来运行。

为了便于演示,我们使用osqueyi来展示osquery强大的功能。我们直接在terminal中输入osqueryi即可进入到osqueryi的交互模式中(osqueryi采用的是sqlite的shell的语法,所以我们也可以使用在sqlite中的所有的内置函数)。

[user@localhost Desktop]$ osqueryi
Using a virtual database. Need help, type '.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
.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 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
.width [NUM1]+   Set column widths for "column" mode
.timer ON|OFF      Turn the CPU timer measurement on or off

通过.help,我们能够查看在osqueryi模式下的一些基本操作。比如.exit表示退出osqueryi,.mode切换osqueryi的输出结果,.show展示目前osqueryi的配置信息,.tables展示在当前的操作系统中能够支持的所有的表名。.schema [TABLE]显示具体的表的结构信息。

osquery> .show
osquery - being built, with love, at Facebook
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
osquery 3.3.0
using SQLite 3.19.3General settings:Flagfile: Config: filesystem (/etc/osquery/osquery.conf)Logger: filesystem (/var/log/osquery/)Distributed: tlsDatabase: ephemeralExtensions: coreSocket: /home/xingjun/.osquery/shell.emShell settings:echo: offheaders: onmode: prettynullvalue: ""output: stdoutseparator: "|"width: Non-default flags/options:database_path: /home/xingjun/.osquery/shell.dbdisable_database: truedisable_events: truedisable_logging: truedisable_watchdog: trueextensions_socket: /home/xingjun/.osquery/shell.emhash_delay: 0logtostderr: truestderrthreshold: 3

可以看到设置包括常规设置(General settings)、shell设置(Shell settings)、非默认选项(Non-default flags/options)。在常规设置中主要是显示了各种配置文件的位置(配置文件/存储日志文件的路径)。 在shell设置中包括了是否需要表头信息(headers),显示方式(mode: pretty),分隔符(separator: "|")。

.table可以查看在当前操作系统中所支持的所有的表,虽然在schema中列出了所有的表(包括了win平台,MacOS平台,Linux平台)。但是具体到某一个平台上面是不会包含其他平台上的表。下方显示的就是我在CentOS7下显示的表。

osquery> .table=> acpi_tables=> apt_sources=> arp_cache=> augeas=> authorized_keys=> block_devices=> carbon_black_info=> carves=> chrome_extensions=> cpu_time=> cpuid=> crontab
...

.schema [TABLE]可以用于查看具体的表的结构信息。如下所示:

osquery> .schema users
CREATE TABLE users(`uid` BIGINT, `gid` BIGINT, `uid_signed` BIGINT, `gid_signed` BIGINT, `username` TEXT, `description` TEXT, `directory` TEXT, `shell` TEXT, `uuid` TEXT, `type` TEXT HIDDEN, PRIMARY KEY (`uid`, `username`)) WITHOUT ROWID;
osquery> .schema processes
CREATE TABLE processes(`pid` BIGINT, `name` TEXT, `path` TEXT, `cmdline` TEXT, `state` TEXT, `cwd` TEXT, `root` TEXT, `uid` BIGINT, `gid` BIGINT, `euid` BIGINT, `egid` BIGINT, `suid` BIGINT, `sgid` BIGINT, `on_disk` INTEGER, `wired_size` BIGINT, `resident_size` BIGINT, `total_size` BIGINT, `user_time` BIGINT, `system_time` BIGINT, `disk_bytes_read` BIGINT, `disk_bytes_written` BIGINT, `start_time` BIGINT, `parent` BIGINT, `pgroup` BIGINT, `threads` INTEGER, `nice` INTEGER, `is_elevated_token` INTEGER HIDDEN, `upid` BIGINT HIDDEN, `uppid` BIGINT HIDDEN, `cpu_type` INTEGER HIDDEN, `cpu_subtype` INTEGER HIDDEN, `phys_footprint` BIGINT HIDDEN, PRIMARY KEY (`pid`)) WITHOUT ROWID;

上面通过.schema查看usersprocesses表的信息,结果输出的是他们对应的DDL。

基本使用

在本章节中,将会演示使用osqueryi来实时查询操作系统中的信息(为了方便展示查询结果使用的是.mode line模式)。

查看系统信息

osquery> select * from system_info;hostname = localhostuuid = 4ee0ad05-c2b2-47ce-aea1-c307e421fa88cpu_type = x86_64cpu_subtype = 158cpu_brand = Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
cpu_physical_cores = 1cpu_logical_cores = 1cpu_microcode = 0x84physical_memory = 2924228608hardware_vendor = hardware_model = hardware_version = hardware_serial = computer_name = localhost.localdomainlocal_hostname = localhost

查询的结果包括了CPU的型号,核数,内存大小,计算机名称等等;

查看OS版本

osquery> select * from os_version;name = CentOS Linuxversion = CentOS Linux release 7.4.1708 (Core)major = 7minor = 4patch = 1708build = platform = rhel
platform_like = rhelcodename =

可以看到我的本机的操作系统的版本是CentOS Linux release 7.4.1708 (Core)

查看内核信息版本

osquery> SELECT * FROM kernel_info;version = 3.10.0-693.el7.x86_64
arguments = ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8path = /vmlinuz-3.10.0-693.el7.x86_64device = /dev/mapper/centos-rootosquery> SELECT * FROM kernel_modules LIMIT 3;name = tcp_lpsize = 12663
used_by = -status = Live
address = 0xffffffffc06cf000name = fusesize = 91874
used_by = -status = Live
address = 0xffffffffc06ae000name = xt_CHECKSUMsize = 12549
used_by = -status = Live
address = 0xffffffffc06a9000

查询repo和pkg信息

osquery提供查询系统中的repo和okg相关信息的表。在Ubuntu中对应的是apt相关的包信息,在Centos中对应的是yum相关的包信息。本例均以yum包为例进行说明。

osquery> SELECT * FROM yum_sources  limit 2;name = CentOS-$releasever - Basebaseurl = enabled =
gpgcheck = 1gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7name = CentOS-$releasever - Updatesbaseurl = enabled =
gpgcheck = 1gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

我们可以直接利用yum_sources来查看操作系统的yum源相关的信息。

osquery> SELECT name, version FROM rpm_packages order by name limit 3;name = GConf2
version = 3.2.6name = GeoIP
version = 1.5.0name = ModemManager
version = 1.6.0

利用rpm_packages查看系统中已经安装的rpm包信息。我们也可以通过name对我们需要查询的包进行过滤,如下:

osquery> SELECT name, version FROM rpm_packages where name="osquery";name = osquery
version = 3.3.0

挂载信息

我们可以使用mounts表来查询系统中的具体的驱动信息。例如我们可以如下的SQL语句进行查询:

SELECT * FROM mounts;
SELECT device, path, type, inodes_free, flags FROM mounts;

我们也可以使用where语句查询摸一个具体的驱动信息,例如ext4或者是tmpfs信息。如下:

osquery> SELECT device, path, type, inodes_free, flags FROM mounts WHERE type="ext4";
osquery> SELECT device, path, type, inodes_free, flags FROM mounts WHERE type="tmpfs";device = tmpfspath = /dev/shmtype = tmpfs
inodes_free = 356960flags = rw,seclabel,nosuid,nodevdevice = tmpfspath = /runtype = tmpfs
inodes_free = 356386flags = rw,seclabel,nosuid,nodev,mode=755device = tmpfspath = /sys/fs/cgrouptype = tmpfs
inodes_free = 356945flags = ro,seclabel,nosuid,nodev,noexec,mode=755device = tmpfspath = /run/user/42type = tmpfs
inodes_free = 356955flags = rw,seclabel,nosuid,nodev,relatime,size=285572k,mode=700,uid=42,gid=42device = tmpfspath = /run/user/1000type = tmpfs
inodes_free = 356939flags = rw,seclabel,nosuid,nodev,relatime,size=285572k,mode=700,uid=1000,gid=1000

内存信息

使用memory_info查看内存信息,如下:

osquery> select * from memory_info;
memory_total = 2924228608memory_free = 996024320buffers = 4280320cached = 899137536swap_cached = 0active = 985657344inactive = 629919744swap_total = 2684350464swap_free = 2684350464

网卡信息

使用interface_addresses查看网卡信息,如下:

osquery> SELECT * FROM interface_addresses;interface = loaddress = 127.0.0.1mask = 255.0.0.0broadcast =
point_to_point = 127.0.0.1type = interface = virbr0address = 192.168.122.1mask = 255.255.255.0broadcast = 192.168.122.255
point_to_point = type = interface = loaddress = ::1mask = ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffffbroadcast =
point_to_point = type =

还可以使用interface_details查看更加具体的网卡信息。

SELECT * FROM interface_details;
SELECT interface, mac, ipackets, opackets, ibytes, obytes FROM interface_details;

查询结果如下

osquery> SELECT * FROM interface_details;interface = lomac = 00:00:00:00:00:00type = 4mtu = 65536metric = 0flags = 65609ipackets = 688opackets = 688ibytes = 59792obytes = 59792ierrors = 0oerrors = 0idrops = 0odrops = 0collisions = 0
last_change = -1link_speed = pci_slot = ....

系统启动时间

osquery> select * from uptime;days = 0hours = 2minutes = 23seconds = 51
total_seconds = 8631

查询用户信息

osquery提供了多个表用于查询用户的信息,包括使用users表检索系统中所有的用户,使用last表查看用户上次登录的信息,使用logged_in_user查询具有活动shell的用户信息。
使用select * from users查看所有用户信息,使用类似于uid>1000的方式过滤用户。

osquery> select * from users where uid>1000;uid = 65534gid = 65534uid_signed = 65534gid_signed = 65534username = nfsnobody
description = Anonymous NFS Userdirectory = /var/lib/nfsshell = /sbin/nologinuuid =

我们可以使用last表查询最终的登录信息,如SELECT * FROM last;。对于普通用户来说,其type值为7。那么我们的查询条件如下:

osquery> SELECT * FROM last where type=7;
username = usertty = :0pid = 12776type = 7time = 1539882439host = :0username = usertty = pts/0pid = 13754type = 7time = 1539882466host = :0

其中的time是时间戳类型,转换为具体的日期之后就可以看到具体的登录时间了。

使用SELECT * FROM logged_in_users;查看当前已经登录的用户信息。

防火墙信息

我们可以使用iptables来查看具体的防火墙信息,如select * from iptables;,也可以进行过滤查询具体的防火墙信息。如SELECT chain, policy, src_ip, dst_ip FROM iptables WHERE chain="POSTROUTING" order by src_ip;

进程信息

我们可以使用processes来查询系统上进程的信息,包括pid,name,path,command等等。
可以使用select * from processes;或者查看具体的某几项信息,select pid,name,path,cmdline from processes;

osquery> select pid,name,path,cmdline from processes limit 2;pid = 1name = systemdpath =
cmdline = /usr/lib/systemd/systemd --switched-root --system --deserialize 21pid = 10name = watchdog/0path =
cmdline =

检查计划任务

我们可以使用crontab来检查系统中的计划任务。

osquery> select * from crontab;event = minute = 01hour = *
day_of_month = *month = *day_of_week = *command = root run-parts /etc/cron.hourlypath = /etc/cron.d/0hourlyevent = minute = 0hour = 1
day_of_month = *month = *day_of_week = Suncommand = root /usr/sbin/raid-checkpath = /etc/cron.d/raid-check

其他

在Linux中还存在其他很多的表能够帮助我们更好地进行入侵检测相关的工作,包括process_eventssocket_eventsprocess_open_sockets等等,这些表可供我们进行入侵检测的确认工作。至于这些表的工作原理,有待阅读osquery的源代码进行进一步分析。

总结

本文主要是对Osquery的基础功能进行了介绍。Oquery的强大功能需要进一步地挖掘和发现。总体来说,Osquery将操作系统中的信息抽象成为一张张表,对于进行基线检查,系统监控是一个非常优雅的方式。当然由于Osquery在这方面的优势,也可以考虑将其作为HIDS的客户端,但是如果HIDS仅仅只有Osquery也显然是不够的。

osquery的认识相关推荐

  1. CentOS 7 安装osquery监控系统

    osquery 简介 osquery是一个SQL驱动操作系统检测和分析工具.osquery支持像SQL语句一样查询系统的各项指标,可以用于OSX和Linux操作系统.它使得底层操作系统分析和监控性能更 ...

  2. 使用 OSquery 和 YARA 进行审计

    Part 1: osquery 简介 osquery是适用于 Windows.OSX.Linux 和 FreeBSD 的操作系统工具框架.这些工具使低级操作系统分析和监控既高效又直观.     osq ...

  3. Security:osquery 介绍

    osquery 是适用于 Windows.OS X (macOS) 和 Linux 的操作系统检测框架.这些工具使低级操作系统分析和监控既高效又直观.   osquery 将操作系统公开为高性能关系数 ...

  4. Fabio 安装和简单使用

    Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的微服务. Fabio 由 eBay Classifieds Group 开发,用于处理 ...

  5. 入门系列之使用Sysdig监视您的Ubuntu 16.04系统

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由乌鸦 发表于云+社区专栏 介绍 Sysdig是一个全面的开源系统活动监控,捕获和分析应用程序.它具有强大的过滤语言和可自定义的输出,以 ...

  6. 大数据、机器学习与深度学习类命令行工具汇总

    <命令行上的数据科学(Data Science at the Command Line)>一书与GitHub皆为我们带来大量高水平的预处理与后处理类工具选项,大家亦可根据需要对其进行针对性 ...

  7. linux 进程创建 进程启动 监控

    0x00 简介 在入侵检测的过程中,进程创建监控是必不可少的一点,因为攻击者的绝大多数攻击行为都是以进程的方式呈现,所以及时获取到新进程创建的信息能帮助我们快速地定位攻击行为. 本文将介绍一些常见的监 ...

  8. 入门系列之使用Sysdig监视您的Ubuntu 16.04系统 1

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由乌鸦 发表于云+社区专栏 介绍 Sysdig是一个全面的开源系统活动监控,捕获和分析应用程序.它具有强大的过滤语言和可自定义的输出,以 ...

  9. 盘点互联网巨头奉献的十大开源安全工具

    Facebook等大型互联网公司推动的服务器与数据中心.大数据工具的开源化项目类似,当大型互联网公司们在超大规模基础设施运营方面面临的挑战 超出技术厂商的能力时,这些巨头就选择反客为主,成为创新技术的 ...

最新文章

  1. 分享Kali Linux 2017年第11周镜像文件
  2. 项目管理生命周期各个阶段的文档
  3. setTimeout() setInterval()
  4. 2021年程序员1月薪资大幅度上涨,你的2021有奔头了吗?
  5. oc 画一个圆弧_UG建模一个蜗杆的方法,纯手工建模无插件
  6. neo4j 查 match
  7. mysql语句1=1_mysql - “where 1 = 1”语句
  8. 小白系列:LNMP搭建
  9. 十六进制的形式在屏幕中间显示二进制byte类型数据
  10. linux c 宏 文件名,C语言标准宏获取文件名、行号、函数名的方法以及#和##的用法...
  11. HyperLedger Composer 查看所有容器 | 进入指定容器
  12. 企业中有关Server Sharing的一点想法
  13. c语言运算优先级口诀简单,C语言运算符优先级口诀
  14. 电子元器件简介——电容与电感篇
  15. mysql_front安装_MySql5.5安装步骤及MySql_Front视图配置
  16. System.out.println()标准输出方法性能影响一窥
  17. 各种工具配置忽略证书
  18. 一种采集USB热敏小票打印机的硬件,用于商超购物中心营业小票采集的硬件方案
  19. 数据结构:按成绩输出名次排序
  20. 基于ssm的奥博羽毛球俱乐部管理系统-计算机毕业设计

热门文章

  1. python-opencv图像处理之哈里斯角检测
  2. 经典工具 | 使用SIFT预测错义突变的有害性
  3. 蜜蜂性别调控又有新机制?
  4. PATH和path,傻傻分不清
  5. macOS 新功能:【控制中心】让你的 Mac 系统更方便!
  6. P5707 【深基2.例12】上学迟到(python3实现)
  7. python抓取页面数据实例
  8. java精通时间_你真的精通 Java 吗?
  9. OMNeT学习之OMNeT安装与运行
  10. php 定时缓存,php定时清理缓存文件的简单示例