04_osquery_osqueryd的使用
1. osqueryd
在装好osquery之后,osqueryd会以service的方式存在于系统中,所以可以利用systemctl的方式进行控制,其文件位于(Linux系统)/usr/lib/systemd/system/osqueryd.service。
1.1. osquery.conf文件
osquery.conf文件大致分为四个部分
- options,配置选项,基本上对所有的配置选项都进行了说明。其实osquery.flags所配置也是这个部分。这也是之前说的osquery.conf可以认为是osquery.flags的超集的原因;
- schedule,配置SQL语句;
- decorators,中文意思是“装饰”。在decorators中也是定义了一系列的SQL语句,执行得到的结果会附加在是在执行schedule中的结果的后面;
- packs,就是一系列SQL语句的合集;
如下是一个osquery.conf的模板
{
// Configure the daemon below:
"options": {
// Select the osquery config plugin.
"config_plugin": "filesystem",
},
// Define a schedule of queries:
"schedule": {
// This is a simple example query that outputs basic system information.
"system_info": {
// The exact query to run.
"query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
// The interval in seconds to run this query, not an exact interval.
"interval": 3600
}
},
// Decorators are normal queries that append data to every query.
"decorators": {
"load": [
"SELECT uuid AS host_uuid FROM system_info;",
"SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
]
},
"packs": {
// "osquery-monitoring": "/usr/share/osquery/packs/osquery-monitoring.conf",
....
},
}
1.2. options
options部分是osquery.conf的第一部分,即配置部分。其官方文档点击这里
config_plugin:希望osquery从哪里读取其配置。默认情况下,它们是从磁盘上的文件中读取的,因此它的值为filesystem。
logger_plugin:指定osquery应写入预定查询结果的位置。我们将再次使用filesystem。
logger_path:这是日志目录的路径,您可以在其中找到包含信息,警告,错误和计划查询结果的文件。默认情况下为/var/log/osquery。
disable_logging:通过将此值设置为false,我们可以启用日志记录。
log_result_events:通过将此值设置为true,日志中的每一行都将表示一个状态更改。
schedule_splay_percent:这告诉osquery,当以相同的间隔调度大量查询时,为了限制对服务器的性能影响,将它们分开运行。默认值为10百分比。
pidfile:在哪里编写osquery守护进程的进程id。默认为/var/osquery/osquery.pidfile。
events_expiry:以秒为单位,将订阅者结果保留在osquery后备存储中的时间。开箱后设置为3600。
database_path:osquery数据库的路径。我们将使用默认值,即/var/osquery/osquery.db。
verbose:启用日志记录后,将用于启用或禁用详细信息性消息。我们将此设置为false。
worker_threads:用于处理查询的工作分派线程数。默认设置为2,我们将保留它。
enable_monitor:用于启用或禁用计划监视器。我们将启用它,因此值将设置为true。
disable_events:用于管理osquery的发布/订阅系统。我们需要启用此功能,因此此处的值将设置为false。
disable_audit:用于禁用从操作系统的审计子系统接收事件。我们需要启用它,因此这里使用的值将是false。
audit_allow_config:允许审计发布者更改审计配置。默认是true。
audit_allow_sockets:允许审计发布者安装与套接字相关的规则。此值设置为true。
host_identifier:用于标识运行osquery的主机。当聚合来自多个服务器的结果时,有助于轻松确定特定日志条目来自哪个服务器。此值为hostname或uuid。开箱后设置为hostname,所以我们将使用该值。
enable_syslog:为了使osquery使用syslog信息,必须将其设置为true。
schedule_default_interval:未设置预定查询的时间间隔时,请使用此值。它大约几秒钟,我们将把它设置为3600。
1.3. schedule
schedule是osqeuryd用于写SQL语句的标签。
"system_info": {
// The exact query to run.
"query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
// The interval in seconds to run this query, not an exact interval.
"interval": 3600
}
其中system_info是定义的一个SQL任务的名字,也是一个JSON格式。在其中可以进行多项设置
- query,定义需要执行的SQL语句;
- interval,运行查询的时间间隔(以秒为单位),示例中是3600,表示每隔3600秒执行一次;
- snapshot,可选选项,默认为false;可以配置为snapshot:true。osquery默认执行的是增量模式,使用了snapshot则是快照模式。比如执行select * from processes; osqeury每次产生的结果是相比上一次变化的结果;如果采用的是snapshot,则会显示所有的进程的,不会与之前的结果进行对比;
- removed,可选选项,默认值是true,确定是否应记录“removed”操作的值。
- platform,将此查询限制为给定平台,默认为“所有”平台;您可以使用逗号设置多个平台
1.4. decorators
装饰器(decorators)的查询结果是将数据添加到其他计划查询的查询。
"decorators": {
"load": [
"SELECT uuid AS host_uuid FROM system_info;",
"SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;"
]
},
1.5. packs
packs
本示例中使用的/usr/share/osquery/packs/osquery-monitoring.conf,这是官方提供的一个监控系统信息的SQL语句的集合;
{
"queries": {
"schedule": {
"query": "select name, interval, executions, output_size, wall_time, (user_time/executions) as avg_user_time, (system_time/executions) as avg_system_time, average_memory, last_executed from osquery_schedule;",
"interval": 7200,
"removed": false,
"blacklist": false,
"version": "1.6.0",
"description": "Report performance for every query within packs and the general schedule."
},
"events": {
"query": "select name, publisher, type, subscriptions, events, active from osquery_events;",
"interval": 86400,
"removed": false,
"blacklist": false,
"version": "1.5.3",
"description": "Report event publisher health and track event counters."
},
"osquery_info": {
"query": "select i.*, p.resident_size, p.user_time, p.system_time, time.minutes as counter from osquery_info i, processes p, time where p.pid = i.pid;",
"interval": 600,
"removed": false,
"blacklist": false,
"version": "1.2.2",
"description": "A heartbeat counter that reports general performance (CPU, memory) and version."
}
}
}
packs中的配置和schedule的配置方法并没有什么区别。
- 从osquery_schedule拿到osqueryd设置的schedule的配置信息;
- 从osquery_events中拿到osqueryd所支持的所有的event;
- 从processes和osquery_info中拿到进程相关的信息;
使用packs的好处是可以将一系列相同功能的SQL语句放置在同一个文件中;
2. 运行osqueryd
当完成以上配置后,就可以通过sudo osqueryd或者systemctl start osqueryd.service启动,如果设置了options中的logger_plugin:filesystem,那么日志位置就在/var/log/osquery/osqueryd.results.log
{"name":"crontab","hostIdentifier":"tianyiyi","calendarTime":"Thu Dec 3 15:25:21 2020 UTC","unixTime":1607009121,"epoch":0,"counter":0,"numerics":false,"decorations":{"host_uuid":"714F4D56-61A6-EC1B-92E1-309D711B81B9","username":"root"},"columns":{"command":"root /usr/lib64/sa/sa2 -A","day_of_month":"*","day_of_week":"*","event":"","hour":"23","minute":"53","month":"*","path":"/etc/cron.d/sysstat"},"action":"added"}