osquery-初步了解

前记

目前网络安全发展速度越来越快,传统的终端防护杀毒软件,已经无法达到企业对网络终端安全状况的感知期望。在数据驱动安全的背景下,EDR产品近两年较为火热,但国内并没有成熟的EDR产品问世。一些有实力的甲方企业会根据开源的EDR产品进行二次开发来满足企业需求,我们接下来说的osquery就是其中之一。
我是一个安全分析工程师,平时的工作就是对海量安全数据做分析工作,有的时候在网络流量、安全设备上发现蛛丝马迹后由于缺少终端信息,无法准确定位安全风险,所以我认为收集终端的信息来进行关联分析(XDR),异常算法是非常有价值的。

运行模式

osquery有两种运行模式分别为shell、daemon。shell模式为临时查询的CLI形式,daemon为服务模式长期运行在系统中。

  • shell
    1.数据存储在内存表中
    2.临时的CLI交互模式来查询系统当前状态

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    [root@localhost ~]# 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 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 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
    osquery>
  • daemon
    1.在守护进程模式下运行数据存储在RocksDB数据库中
    2.以守护进程形式运行,可以记录操作系统的状态变化并生成日志(生产环境中主要的模式)
           
    举例:

       在配置文件的schedule中加入进程监控

1
2
3
4
"processes": {
"query": "SELECT pid,uid,name,path,cmdline from processes;",
"interval": 5
}

       生成记录日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"name": "processes",
"hostIdentifier": "localhost.localdomain",
"calendarTime": "Tue Apr 9 14:28:40 2019 UTC",
"unixTime": 1554820120,
"epoch": 0,
"counter": 101,
"decorations": {
"host_uuid": "4EC54D56-FFDC-98EC-C263-9424C3FE03CE",
"username": "root"
},
"columns": {
"cmdline": "curl baidu.com",
"name": "curl",
"path": "/usr/bin/curl",
"pid": "1106",
"uid": "0"
},
"action": "added"
}

配置说明

  • options
    options记录osqueryd初始化启动参数,可以通过osqueryd –help来查看
  • schedule
    schedule是osquery的核心功能,通过配置定时任务监控系统信息
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    {
    "schedule": {
    "users_browser_plugins": {
    "query": "SELECT * FROM users JOIN browser_plugins USING (uid);",
    "interval": 60
    },
    "hashes_of_bin": {
    "query": "SELECT path, hash.sha256 FROM file JOIN hash USING (path) WHERE file.directory = '/bin/';",
    "interval": 3600,
    "removed": false,
    "platform": "darwin",
    "version": "1.4.5",
    "shard": 1
    }
    }
    }

       users_browser_plugins、hashes_of_bin为查询项的键名可自定义
       query:需要查询目标的SQL语句
       interval:查询动作的时间间隔
       removed:是否记录移除动作(进程结束等),默认true
       platform:查询动作仅在对应平台下运行
       version:查询动作仅在满足条件的osquery版本运行

  • packs
    packs相当于将一组相关的查询SQL打包为一个conf文件,这样的好处可以通过适用场景来发布配置文件
  • discovery queries
    发现查询是packs查询里的一个功能,主要的目的是当满足一定条件的情况下才启用查询,避免做无意义的动作
    发现查询默认1小时更新一下状态,通过修改pack_refresh_interval的值定义更新间隔
    同一个discovery组多个条件的关系为OR

注意问题

1.osquery存在瞬时动作漏报的情况,比如interval为3600s,若一个动作在3600s的间隔内开始并结束,则不会被记录
2.osquery在机器处于休眠状态时interval将停止计时

日志收集

osqueryd可以通过filesystem (default), tls, syslog (for POSIX), windows_event_log (for Windows), kinesis, firehose, and kafka_producer等方式来记录日志,在实施阶段可能使用较多的模式为filesystem、syslog、kafka_producer三种方式。

osqueryd有两种记录日志的模式Differential(差异记录)、Snapshot(快照记录)两种模式区别如下:
Differentia:在osqueryd第一次查询后,下一次查询只记录与上次查询结果不同的项
Snapshot:每次都保存全部查询结果

后记

这篇就是根据官方的Doc文档做一个初步的了解,后面会模拟一个osqueryd的实施架构,在思考一下根据osqueryd数据能做出哪些场景

参考

https://osquery.readthedocs.io/en/stable/
https://osquery.io/schema/3.3.2