日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

osquery的认识

發(fā)布時(shí)間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osquery的认识 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

說(shuō)明

osquery是一個(gè)由FaceBook開(kāi)源用于對(duì)系統(tǒng)進(jìn)行查詢(xún)、監(jiān)控以及分析的一款軟件。osquery對(duì)其的說(shuō)明如下:

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.

我們知道當(dāng)你們?cè)贚inux中使用諸如ps、top、ls -l等等命令的時(shí)候,可以發(fā)下其實(shí)他們的輸出結(jié)果的格式都是很固定的很像一張表。或許是基于這樣的想法,facebook開(kāi)發(fā)了osquery。osquery將操作系統(tǒng)當(dāng)作是一個(gè)高性能的關(guān)系型數(shù)據(jù)庫(kù)。使用osquery運(yùn)行我們能夠使用類(lèi)似于SQL語(yǔ)句的方式去查詢(xún)數(shù)據(jù)庫(kù)中的信息,比如正在運(yùn)行的進(jìn)程信息,加載的內(nèi)核模塊,網(wǎng)絡(luò)連接,瀏覽器插件等等信息(一切查詢(xún)的信息的粒度取決于osquery的實(shí)現(xiàn)粒度了)。

osquery也廣泛地支持多個(gè)平臺(tái),包括MacOS、CentOS、Ubuntu、Windows 10以及FreeBSD,具體所支持的版本的信息也可以在osquery主頁(yè)查看。除此之外,osquery的配套文檔/網(wǎng)站也是一應(yīng)俱全,包括主頁(yè)、Github、readthedocs、slack。

本篇文章以CentOS為例說(shuō)明Osquery的安裝以及使用。

安裝

在主頁(yè)上面提供了不同操作系統(tǒng)的安裝包,我們下載CentOS對(duì)應(yīng)的rpm文件即可。在本例中文件名是osquery-3.3.0-1.linux.x86_64.rpm,使用命令sudo yum install osquery-3.3.0-1.linux.x86_64.rpm安裝。安裝成功之后會(huì)出現(xiàn):

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

運(yùn)行

osquery存在兩種運(yùn)行模式,分別是osqueryi(交互式模式)、osqueryd(后臺(tái)進(jìn)程模式)。

  • osqueryi,與osqueryd安全獨(dú)立,不需要以管理員的身份運(yùn)行,能夠及時(shí)地查看當(dāng)前操作系統(tǒng)的狀態(tài)信息。
  • osqueryd,我們能夠利用osqueryd執(zhí)行定時(shí)查詢(xún)記錄操作系統(tǒng)的變化,例如在第一次執(zhí)行和第二次執(zhí)行之間的進(jìn)程變化(增加/減少),osqueryd會(huì)將進(jìn)程執(zhí)行的結(jié)果保存(文件或者是直接打到kafka中)。osqueryd還會(huì)利用操作系統(tǒng)的API來(lái)記錄文件目錄的變化、硬件事件、網(wǎng)絡(luò)行為的變化等等。osqueryd在Linux中是以系統(tǒng)服務(wù)的方式來(lái)運(yùn)行。

為了便于演示,我們使用osqueyi來(lái)展示osquery強(qiáng)大的功能。我們直接在terminal中輸入osqueryi即可進(jìn)入到osqueryi的交互模式中(osqueryi采用的是sqlite的shell的語(yǔ)法,所以我們也可以使用在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

通過(guò).help,我們能夠查看在osqueryi模式下的一些基本操作。比如.exit表示退出osqueryi,.mode切換osqueryi的輸出結(jié)果,.show展示目前osqueryi的配置信息,.tables展示在當(dāng)前的操作系統(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

可以看到設(shè)置包括常規(guī)設(shè)置(General settings)、shell設(shè)置(Shell settings)、非默認(rèn)選項(xiàng)(Non-default flags/options)。在常規(guī)設(shè)置中主要是顯示了各種配置文件的位置(配置文件/存儲(chǔ)日志文件的路徑)。 在shell設(shè)置中包括了是否需要表頭信息(headers),顯示方式(mode: pretty),分隔符(separator: "|")。

.table可以查看在當(dāng)前操作系統(tǒng)中所支持的所有的表,雖然在schema中列出了所有的表(包括了win平臺(tái),MacOS平臺(tái),Linux平臺(tái))。但是具體到某一個(gè)平臺(tái)上面是不會(huì)包含其他平臺(tái)上的表。下方顯示的就是我在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;

上面通過(guò).schema查看users和processes表的信息,結(jié)果輸出的是他們對(duì)應(yīng)的DDL。

基本使用

在本章節(jié)中,將會(huì)演示使用osqueryi來(lái)實(shí)時(shí)查詢(xún)操作系統(tǒng)中的信息(為了方便展示查詢(xún)結(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

查詢(xún)的結(jié)果包括了CPU的型號(hào),核數(shù),內(nèi)存大小,計(jì)算機(jī)名稱(chēng)等等;

查看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 =

可以看到我的本機(jī)的操作系統(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

查詢(xún)r(jià)epo和pkg信息

osquery提供查詢(xún)系統(tǒng)中的repo和okg相關(guān)信息的表。在Ubuntu中對(duì)應(yīng)的是apt相關(guān)的包信息,在Centos中對(duì)應(yīng)的是yum相關(guān)的包信息。本例均以yum包為例進(jìn)行說(shuō)明。

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來(lái)查看操作系統(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包信息。我們也可以通過(guò)name對(duì)我們需要查詢(xún)的包進(jìn)行過(guò)濾,如下:

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

掛載信息

我們可以使用mounts表來(lái)查詢(xún)系統(tǒng)中的具體的驅(qū)動(dòng)信息。例如我們可以如下的SQL語(yǔ)句進(jìn)行查詢(xún):

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

我們也可以使用where語(yǔ)句查詢(xún)摸一個(gè)具體的驅(qū)動(dòng)信息,例如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;

查詢(xún)結(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)啟動(dòng)時(shí)間

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

查詢(xún)用戶信息

osquery提供了多個(gè)表用于查詢(xún)用戶的信息,包括使用users表檢索系統(tǒng)中所有的用戶,使用last表查看用戶上次登錄的信息,使用logged_in_user查詢(xún)具有活動(dòng)shell的用戶信息。
使用select * from users查看所有用戶信息,使用類(lèi)似于uid>1000的方式過(guò)濾用戶。

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表查詢(xún)最終的登錄信息,如SELECT * FROM last;。對(duì)于普通用戶來(lái)說(shuō),其type值為7。那么我們的查詢(xún)條件如下:

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

其中的time是時(shí)間戳類(lèi)型,轉(zhuǎn)換為具體的日期之后就可以看到具體的登錄時(shí)間了。

使用SELECT * FROM logged_in_users;查看當(dāng)前已經(jīng)登錄的用戶信息。

防火墻信息

我們可以使用iptables來(lái)查看具體的防火墻信息,如select * from iptables;,也可以進(jìn)行過(guò)濾查詢(xún)具體的防火墻信息。如SELECT chain, policy, src_ip, dst_ip FROM iptables WHERE chain="POSTROUTING" order by src_ip;

進(jìn)程信息

我們可以使用processes來(lái)查詢(xún)系統(tǒng)上進(jìn)程的信息,包括pid,name,path,command等等。
可以使用select * from processes;或者查看具體的某幾項(xiàng)信息,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 =

檢查計(jì)劃任務(wù)

我們可以使用crontab來(lái)檢查系統(tǒng)中的計(jì)劃任務(wù)。

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中還存在其他很多的表能夠幫助我們更好地進(jìn)行入侵檢測(cè)相關(guān)的工作,包括process_events、socket_events、process_open_sockets等等,這些表可供我們進(jìn)行入侵檢測(cè)的確認(rèn)工作。至于這些表的工作原理,有待閱讀osquery的源代碼進(jìn)行進(jìn)一步分析。

總結(jié)

本文主要是對(duì)Osquery的基礎(chǔ)功能進(jìn)行了介紹。Oquery的強(qiáng)大功能需要進(jìn)一步地挖掘和發(fā)現(xiàn)。總體來(lái)說(shuō),Osquery將操作系統(tǒng)中的信息抽象成為一張張表,對(duì)于進(jìn)行基線檢查,系統(tǒng)監(jiān)控是一個(gè)非常優(yōu)雅的方式。當(dāng)然由于Osquery在這方面的優(yōu)勢(shì),也可以考慮將其作為HIDS的客戶端,但是如果HIDS僅僅只有Osquery也顯然是不夠的。

總結(jié)

以上是生活随笔為你收集整理的osquery的认识的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。