使用osqueryd監控系統
osquery初識主要是藉由osqueryi
的方式對osquery
進行了一個基本的介紹。可以看到osqueryi
是一個互動式的shell
,我們可以很方便使用它進行測試,但是如果我們要將osquery
投入實際使用,明顯是osqueryd
更加合適。本篇文章將詳細地介紹osqueryd
的使用。
osqueryd配置
如果使用osqueryi
,我們可以通過osqueryi -audit_allow_config=true --audit_allow_sockets=true --audit_persist=true
這樣的方式傳入設定。如果是osqueryd
呢?其實我們安裝好osquery
之後,會以service
的方式存在於系統中,同時可以利用systemctl
的方式進行控制,其檔案位於/usr/lib/systemd/system/osqueryd.service
。
[Unit] Description=The osquery Daemon After=network.service syslog.service [Service] TimeoutStartSec=0 EnvironmentFile=/etc/sysconfig/osqueryd ExecStartPre=/bin/sh -c "if [ ! -f $FLAG_FILE ]; then touch $FLAG_FILE; fi" ExecStartPre=/bin/sh -c "if [ -f $LOCAL_PIDFILE ]; then mv $LOCAL_PIDFILE $PIDFILE; fi" ExecStart=/usr/bin/osqueryd \ --flagfile $FLAG_FILE \ --config_path $CONFIG_FILE Restart=on-failure KillMode=process KillSignal=SIGTERM [Install] WantedBy=multi-user.target
啟動方式就是ExecStart=/usr/bin/osqueryd --flagfile $FLAG_FILE --config_path $CONFIG_FILE
,通過--flagfile
和--config_path
的方式指定配置檔案的路徑。$FLAG_FILE
和$CONFIG_FILE
是在/etc/sysconfig/osqueryd
中定義。
FLAG_FILE="/etc/osquery/osquery.flags" CONFIG_FILE="/etc/osquery/osquery.conf" LOCAL_PIDFILE="/var/osquery/osqueryd.pidfile" PIDFILE="/var/run/osqueryd.pidfile"
預設的配置檔案就是位於/etc/osquery/osquery.flags
和/etc/osquery/osquery.conf
。當啟動osqueryd
時,如果不存在osquery.flags
和osquery.conf
會建立兩個空檔案,否則直接讀取此檔案的內容。其實osquery.conf
可以認為是osquery.flags
的超集,因為osquery.flags
僅僅只是設定一些配置,而這些配置也同樣可以在osquery.conf
中實現,同時在osquery.conf
中還可以配置osqueryd
需要執行的SQL。所以接下來本文將僅僅只介紹osquery.conf
的使用。
osquery.conf
osquery
本身提供了一個osquery.conf
的例子,其寫法是一個JSON
格式的檔案,在這裡我們將其簡化一下。
{ // Configure the daemon below: "options": { // Select the osquery config plugin. "config_plugin": "filesystem", // Select the osquery logging plugin. "logger_plugin": "filesystem", // The log directory stores info, warning, and errors. // If the daemon uses the 'filesystem' logging retriever then the log_dir // will also contain the query results. //"logger_path": "/var/log/osquery", // Set 'disable_logging' to true to prevent writing any info, warning, error // logs. If a logging plugin is selected it will still write query results. //"disable_logging": "false", // Splay the scheduled interval for queries. // This is very helpful to prevent system performance impact when scheduling // large numbers of queries that run a smaller or similar intervals. //"schedule_splay_percent": "10", // A filesystem path for disk-based backing storage used for events and // query results differentials. See also 'use_in_memory_database'. //"database_path": "/var/osquery/osquery.db", // Comma-delimited list of table names to be disabled. // This allows osquery to be launched without certain tables. //"disable_tables": "foo_bar,time", "utc": "true" }, // 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", .... }, }
osquery.conf
檔案大致可以分為4個部分。
-
options
,配置選項,ofollow,noindex">Command Line Flags 基本上對所有的配置選項都進行了說明。其實osquery.flags
所配置也是這個部分。這也是之前說的osquery.conf可以認為是osquery.flags的超集 的原因; -
schedule
,配置SQL語句。因為osqueryd
是以daemon
的方式執行,所以需要通過在schedule
中定義SQL語句使其定期執行返回結果; -
decorators
,中文意思是“裝飾”。在decorators
中也是定義了一系列的SQL語句,執行得到的結果會附加在是在執行schedule
中的結果的後面;所以我們看到在decorators
我們取的是uuid
和登入的username
; -
packs
,就是一系列SQL語句的合集;
配置說明
上一節中對osquery.conf
中的配置進了一個簡單的說明,在本節中將詳細說明。
options
options
就是配置。Command Line Flags
基本上對所有的配置選項都進行了說明。我們可以進行多種配置,有興趣的可以自行研究。本節僅僅說明幾個常用的配置;
-
config_plugin
,配置選項是filesystem
。如果是通過osquery.conf
管理osquery
就是採用filesystem
,還有一種選項是tls
(這一種主要是通過API的方式來配置osquery
)。 -
logger_plugin
,配置選項是filesystem
,這也是osquery
的預設值。根據Logger plugins ,還可以配置tls
,syslog (for POSIX
,windows_event_log (for Windows)
,kinesis
,firehose
,kafka_producer
。 -
database_path
,預設值是/var/osquery/osquery.db
。因為osquery
內部會使用到資料,所以配置此目錄是osquery
的資料庫檔案位置。 -
disable_logging
,是配置設定osquery
的結果是否需要儲存到本地,這個配置其實和logger_plugin:filesystem
有點重複。 -
hostIdentifier
,相當於表示每個主機的標識,比如可以採用hostname
作為標識。
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
,可選選項,可以配置為snapshot:true
。osquery
預設執行的是增量模式,使用了snapshot
則是快照模式。比如執行select * from processes;
,osqeury
每次產生的結果是相比上一次變化的結果;如果採用的是snapshot
,則會顯示所有的程序的,不會與之前的結果進行對比; -
removed
,可選選項,預設值是true
,用來設定是否記錄action
為remove
的日誌。
當然還有一些其他的不常用選項,如platform
、version
、shard
、description
等等。
更多關於schedule
的介紹可以參考schedule
decorators
正如其註釋Decorators are normal queries that append data to every query
所說,Decorators
會把他的執行結果新增到schedule
中的sql
語句執行結果中。所以根據其作用Decorators
也不是必須存在的。。在本例中Decorators
存在兩條記錄:
SELECT uuid AS host_uuid FROM system_info; SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;
-
SELECT uuid AS host_uuid FROM system_info
,從system_info
獲取uuid
作為識別符號1; -
SELECT user AS username FROM logged_in_users ORDER BY time DESC LIMIT 1;
,從logged_in_users
選擇user
(其實查詢的是使用者名稱)的第一項作為識別符號2;
當然可以在Decorators
寫多條語句作為識別符號,但是感覺沒有必要;
packs
packs
就是打包的SQL
語句的合集,本示例中使用的/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
的配置方法並沒有什麼區別。我們在packs
中查詢到的資訊包括:
-
從
osquery_schedule
拿到osqueryd
設定的schedule
的配置資訊; -
從
osquery_events
中拿到osqueryd
所支援的所有的event
; -
從
processes
和osquery_info
中拿到程序相關的資訊;
使用packs
的好處是可以將一系列相同功能的SQL語句放置在同一個檔案中;
執行osqueryd
當以上配置完畢之後,我們就可以通過sudo osqueryd
的方式啟動;如果我們設定logger_plugin:filesystem
,那麼日誌就會落在本地/var/log/osquery
下。此目錄下包含了多個檔案,每個檔案分別記錄不同的資訊。
-
osqueryd.results.log
,osqueryd
的增量日誌的資訊都會寫入到此檔案中;儲存結果的形式是JSON
形式。示例如下:{"name":"auditd_process_info","hostIdentifier":"localhost.localdomain","calendarTime":"Wed Oct 24 13:07:12 2018 UTC","unixTime":1540386432,"epoch":0,"counter":0,"decorations":{"host_uuid":"99264D56-9A4E-E593-0B4E-872FBF3CD064","username":"username"},"columns":{"atime":"1540380461","auid":"4294967295","btime":"0","cmdline":"awk { sum += $1 }; END { print 0+sum }","ctime":"1538239175","cwd":"\"/\"","egid":"0","euid":"0","gid":"0","mode":"0100755","mtime":"1498686768","owner_gid":"0","owner_uid":"0","parent":"4086","path":"/usr/bin/gawk","pid":"4090","time":"1540386418","uid":"0","uptime":"1630"},"action":"added"} {"name":"auditd_process_info","hostIdentifier":"localhost.localdomain","calendarTime":"Wed Oct 24 13:07:12 2018 UTC","unixTime":1540386432,"epoch":0,"counter":0,"decorations":{"host_uuid":"99264D56-9A4E-E593-0B4E-872FBF3CD064","username":"username"},"columns":{"atime":"1540380461","auid":"4294967295","btime":"0","cmdline":"sleep 60","ctime":"1538240835","cwd":"\"/\"","egid":"0","euid":"0","gid":"0","mode":"0100755","mtime":"1523421302","owner_gid":"0","owner_uid":"0","parent":"741","path":"/usr/bin/sleep","pid":"4091","time":"1540386418","uid":"0","uptime":"1630"},"action":"added"}
其中的
added
表示的就是相當於上一次增加的程序資訊;每一次執行的結果都是一條JSON記錄; -
squeryd.snapshots.log
,記錄的是osqueryd
中使用snapshot:true
標記的SQL語句執行結果;{"snapshot":[{"header":"Defaults","rule_details":"!visiblepw"},{"header":"Defaults","rule_details":"always_set_home"},{"header":"Defaults","rule_details":"match_group_by_gid"},{"header":"Defaults","rule_details":"env_reset"},{"header":"Defaults","rule_details":"env_keep = \"COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS\""},{"header":"Defaults","rule_details":"env_keep += \"MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE\""},{"header":"Defaults","rule_details":"env_keep += \"LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES\""},{"header":"Defaults","rule_details":"env_keep += \"LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE\""},{"header":"Defaults","rule_details":"env_keep += \"LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY\""},{"header":"Defaults","rule_details":"secure_path = /sbin:/bin:/usr/sbin:/usr/bin"},{"header":"root","rule_details":"ALL=(ALL) ALL"},{"header":"%wheel","rule_details":"ALL=(ALL) ALL"}],"action":"snapshot","name":"sudoers","hostIdentifier":"localhost.localdomain","calendarTime":"Tue Oct9 11:54:00 2018 UTC","unixTime":1539086040,"epoch":0,"counter":0,"decorations":{"host_uuid":"99264D56-9A4E-E593-0B4E-872FBF3CD064","username":"username"}} {"snapshot":[{"header":"Defaults","rule_details":"!visiblepw"},{"header":"Defaults","rule_details":"always_set_home"},{"header":"Defaults","rule_details":"match_group_by_gid"},{"header":"Defaults","rule_details":"env_reset"},{"header":"Defaults","rule_details":"env_keep = \"COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS\""},{"header":"Defaults","rule_details":"env_keep += \"MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE\""},{"header":"Defaults","rule_details":"env_keep += \"LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES\""},{"header":"Defaults","rule_details":"env_keep += \"LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE\""},{"header":"Defaults","rule_details":"env_keep += \"LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY\""},{"header":"Defaults","rule_details":"secure_path = /sbin:/bin:/usr/sbin:/usr/bin"},{"header":"root","rule_details":"ALL=(ALL) ALL"},{"header":"%wheel","rule_details":"ALL=(ALL) ALL"}],"action":"snapshot","name":"sudoers","hostIdentifier":"localhost.localdomain","calendarTime":"Tue Oct9 11:54:30 2018 UTC","unixTime":1539086070,"epoch":0,"counter":0,"decorations":{"host_uuid":"99264D56-9A4E-E593-0B4E-872FBF3CD064","username":"username"}}
由於
snapshot
是快照模式,所以即使兩次結果相同也會全部顯示出來; -
osqueryd.INFO
,記錄osqueryd
中正在執行的情況。示例如下:Log file created at: 2018/11/22 17:06:06 Running on machine: osquery.origin Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg I1122 17:06:06.729902 22686 events.cpp:862] Event publisher not enabled: auditeventpublisher: Publisher disabled via configuration I1122 17:06:06.730651 22686 events.cpp:862] Event publisher not enabled: syslog: Publisher disabled via configuration
-
osqueryd.WARNING
,記錄osquery
的警告。示例如下:Log file created at: 2018/10/09 19:53:45 Running on machine: localhost.localdomain Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg E1009 19:53:45.471046 104258 events.cpp:987] Requested unknown/failed event publisher: auditeventpublisher E1009 19:53:45.471606 104259 events.cpp:987] Requested unknown/failed event publisher: inotify E1009 19:53:45.471634 104260 events.cpp:987] Requested unknown/failed event publisher: syslog E1009 19:53:45.471658 104261 events.cpp:987] Requested unknown/failed event publisher: udev
-
osqueryd.ERROR
,記錄的是osquery
的錯誤資訊。示例如下:Log file created at: 2018/10/09 19:53:45 Running on machine: localhost.localdomain Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg E1009 19:53:45.471046 104258 events.cpp:987] Requested unknown/failed event publisher: auditeventpublisher E1009 19:53:45.471606 104259 events.cpp:987] Requested unknown/failed event publisher: inotify E1009 19:53:45.471634 104260 events.cpp:987] Requested unknown/failed event publisher: syslog E1009 19:53:45.471658 104261 events.cpp:987] Requested unknown/failed event publisher: udev
在本例中錯誤資訊和警告資訊完全相同。在實際情況下,可能很多時候均不相同;
總結
本文主要是對osqueryd
的常用配置進行了簡要的說法。通過本文能夠快速地利用上手osquery
,由於篇幅的原因,有關osquery的很多東西沒有介紹或者說明得很詳細。官方的文件[]對osqueryd的配置已經說明得很是詳盡了,如果對本文有任何的不解,可以去查閱相關的文件,也歡迎大家就相關問題與我討論。
以上