osquery的认识
說明
osquery是一個由FaceBook開源用于對系統(tǒng)進行查詢、監(jiān)控以及分析的一款軟件。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中使用諸如ps、top、ls -l等等命令的時候,可以發(fā)下其實他們的輸出結(jié)果的格式都是很固定的很像一張表。或許是基于這樣的想法,facebook開發(fā)了osquery。osquery將操作系統(tǒng)當作是一個高性能的關(guān)系型數(shù)據(jù)庫。使用osquery運行我們能夠使用類似于SQL語句的方式去查詢數(shù)據(jù)庫中的信息,比如正在運行的進程信息,加載的內(nèi)核模塊,網(wǎng)絡連接,瀏覽器插件等等信息(一切查詢的信息的粒度取決于osquery的實現(xiàn)粒度了)。
osquery也廣泛地支持多個平臺,包括MacOS、CentOS、Ubuntu、Windows 10以及FreeBSD,具體所支持的版本的信息也可以在osquery主頁查看。除此之外,osquery的配套文檔/網(wǎng)站也是一應俱全,包括主頁、Github、readthedocs、slack。
本篇文章以CentOS為例說明Osquery的安裝以及使用。
安裝
在主頁上面提供了不同操作系統(tǒng)的安裝包,我們下載CentOS對應的rpm文件即可。在本例中文件名是osquery-3.3.0-1.linux.x86_64.rpm,使用命令sudo yum install osquery-3.3.0-1.linux.x86_64.rpm安裝。安裝成功之后會出現(xiàn):
Installed:osquery.x86_64 0:3.3.0-1.linux Complete!運行
osquery存在兩種運行模式,分別是osqueryi(交互式模式)、osqueryd(后臺進程模式)。
- osqueryi,與osqueryd安全獨立,不需要以管理員的身份運行,能夠及時地查看當前操作系統(tǒng)的狀態(tài)信息。
- osqueryd,我們能夠利用osqueryd執(zhí)行定時查詢記錄操作系統(tǒng)的變化,例如在第一次執(zhí)行和第二次執(zhí)行之間的進程變化(增加/減少),osqueryd會將進程執(zhí)行的結(jié)果保存(文件或者是直接打到kafka中)。osqueryd還會利用操作系統(tǒng)的API來記錄文件目錄的變化、硬件事件、網(wǎng)絡行為的變化等等。osqueryd在Linux中是以系統(tǒng)服務的方式來運行。
為了便于演示,我們使用osqueyi來展示osquery強大的功能。我們直接在terminal中輸入osqueryi即可進入到osqueryi的交互模式中(osqueryi采用的是sqlite的shell的語法,所以我們也可以使用在sqlite中的所有的內(nèi)置函數(shù))。
[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的輸出結(jié)果,.show展示目前osqueryi的配置信息,.tables展示在當前的操作系統(tǒng)中能夠支持的所有的表名。.schema [TABLE]顯示具體的表的結(jié)構(gòu)信息。
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可以看到設置包括常規(guī)設置(General settings)、shell設置(Shell settings)、非默認選項(Non-default flags/options)。在常規(guī)設置中主要是顯示了各種配置文件的位置(配置文件/存儲日志文件的路徑)。 在shell設置中包括了是否需要表頭信息(headers),顯示方式(mode: pretty),分隔符(separator: "|")。
.table可以查看在當前操作系統(tǒng)中所支持的所有的表,雖然在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]可以用于查看具體的表的結(jié)構(gòu)信息。如下所示:
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查看users和processes表的信息,結(jié)果輸出的是他們對應的DDL。
基本使用
在本章節(jié)中,將會演示使用osqueryi來實時查詢操作系統(tǒng)中的信息(為了方便展示查詢結(jié)果使用的是.mode line模式)。
查看系統(tǒng)信息
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查詢的結(jié)果包括了CPU的型號,核數(shù),內(nèi)存大小,計算機名稱等等;
查看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 =可以看到我的本機的操作系統(tǒng)的版本是CentOS Linux release 7.4.1708 (Core)
查看內(nèi)核信息版本
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提供查詢系統(tǒng)中的repo和okg相關(guān)信息的表。在Ubuntu中對應的是apt相關(guān)的包信息,在Centos中對應的是yum相關(guān)的包信息。本例均以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來查看操作系統(tǒng)的yum源相關(guān)的信息。
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查看系統(tǒng)中已經(jīng)安裝的rpm包信息。我們也可以通過name對我們需要查詢的包進行過濾,如下:
osquery> SELECT name, version FROM rpm_packages where name="osquery";name = osquery version = 3.3.0掛載信息
我們可以使用mounts表來查詢系統(tǒng)中的具體的驅(qū)動信息。例如我們可以如下的SQL語句進行查詢:
SELECT * FROM mounts; SELECT device, path, type, inodes_free, flags FROM mounts;我們也可以使用where語句查詢摸一個具體的驅(qū)動信息,例如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內(nèi)存信息
使用memory_info查看內(nèi)存信息,如下:
osquery> select * from memory_info; memory_total = 2924228608memory_free = 996024320buffers = 4280320cached = 899137536swap_cached = 0active = 985657344inactive = 629919744swap_total = 2684350464swap_free = 2684350464網(wǎng)卡信息
使用interface_addresses查看網(wǎng)卡信息,如下:
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查看更加具體的網(wǎng)卡信息。
SELECT * FROM interface_details; SELECT interface, mac, ipackets, opackets, ibytes, obytes FROM interface_details;查詢結(jié)果如下
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 = ....系統(tǒng)啟動時間
osquery> select * from uptime;days = 0hours = 2minutes = 23seconds = 51 total_seconds = 8631查詢用戶信息
osquery提供了多個表用于查詢用戶的信息,包括使用users表檢索系統(tǒng)中所有的用戶,使用last表查看用戶上次登錄的信息,使用logged_in_user查詢具有活動shell的用戶信息。
使用select * from users查看所有用戶信息,使用類似于uid>1000的方式過濾用戶。
我們可以使用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是時間戳類型,轉(zhuǎn)換為具體的日期之后就可以看到具體的登錄時間了。
使用SELECT * FROM logged_in_users;查看當前已經(jīng)登錄的用戶信息。
防火墻信息
我們可以使用iptables來查看具體的防火墻信息,如select * from iptables;,也可以進行過濾查詢具體的防火墻信息。如SELECT chain, policy, src_ip, dst_ip FROM iptables WHERE chain="POSTROUTING" order by src_ip;
進程信息
我們可以使用processes來查詢系統(tǒng)上進程的信息,包括pid,name,path,command等等。
可以使用select * from processes;或者查看具體的某幾項信息,select pid,name,path,cmdline from processes;
檢查計劃任務
我們可以使用crontab來檢查系統(tǒng)中的計劃任務。
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中還存在其他很多的表能夠幫助我們更好地進行入侵檢測相關(guān)的工作,包括process_events、socket_events、process_open_sockets等等,這些表可供我們進行入侵檢測的確認工作。至于這些表的工作原理,有待閱讀osquery的源代碼進行進一步分析。
總結(jié)
本文主要是對Osquery的基礎功能進行了介紹。Oquery的強大功能需要進一步地挖掘和發(fā)現(xiàn)。總體來說,Osquery將操作系統(tǒng)中的信息抽象成為一張張表,對于進行基線檢查,系統(tǒng)監(jiān)控是一個非常優(yōu)雅的方式。當然由于Osquery在這方面的優(yōu)勢,也可以考慮將其作為HIDS的客戶端,但是如果HIDS僅僅只有Osquery也顯然是不夠的。
總結(jié)
以上是生活随笔為你收集整理的osquery的认识的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSLScan工具 获取SSL信息
- 下一篇: frps搭建内网穿透服务器(frp隧道)