{"id":354171,"date":"2024-05-20T22:43:01","date_gmt":"2024-05-20T22:43:01","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=354171"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=354171","title":{"rendered":"<span>\u0421\u043f\u043e\u0441\u043e\u0431 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u00ab\u043a\u0438\u0448\u043e\u0447\u043a\u0438\u00bb \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, Docker \u0438\u0437 PostgreSQL \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u0430 \u0438\u0434\u0435\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0435\u0442\u0440\u0438\u043a\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0438 \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u0438\u0437 PostgreSQL, \u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\" rel=\"noopener noreferrer nofollow\">\u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e<\/a>. \u042f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u044e \u043d\u0430 \u0435\u0433\u043e \u0437\u0440\u0435\u043b\u043e\u0441\u0442\u044c \u0438 \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u044c \u043a \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438\u0437 osquery \u0432 PostgreSQL \u0432 \u0432\u0438\u0434\u0435 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445\/JSON. \u0414\u0430\u043b\u044c\u0448\u0435 \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u044d\u0442\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<h2>osquery<\/h2>\n<p>\u041a\u0440\u043e\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439 open source \u043f\u0440\u043e\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c \u0438 \u043c\u0435\u0442\u0440\u0438\u043a\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043a\u0430\u043a \u043a \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0432 SQLite, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u044d\u0442\u043e\u0439 embedded \u0431\u0430\u0437\u044b, \u0441 \u043f\u0440\u0438\u0441\u0443\u0449\u0438\u043c\u0438 \u0435\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<ul>\n<li>\n<p><code>osqueryd<\/code>\u0434\u0435\u043c\u043e\u043d, \u043f\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u043c\u0435\u044e\u0449\u0438\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043e\u0431 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445;<\/p>\n<\/li>\n<li>\n<p><code>osqueryi<\/code> \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u043e\u043b\u044c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0438 \u043d\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441<code>osqueryd;<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/osquery.readthedocs.io\/en\/stable\/development\/osquery-sdk\/#thrift-api\" rel=\"noopener noreferrer nofollow\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Thrift API<\/a>, \u043b\u0438\u0431\u043e \u0437\u0430\u043f\u0443\u0441\u043a <code>osqueryi<\/code>\u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 json\/csv. \u0412 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f <a href=\"https:\/\/github.com\/igor-suhorukov\/osquery-java\/blob\/master\/src\/main\/java\/com\/questdb\/griffin\/engine\/functions\/osquery\/OsqueryFunctionFactory.java\" rel=\"noopener noreferrer nofollow\">\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <\/a>\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e Thrift API \u0447\u0435\u0440\u0435\u0437 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442 UNIX domain socket \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u0412 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437, \u0440\u0435\u0448\u0438\u043b \u0447\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 stdout \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0422\u0430\u043a \u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f postgres_osquery. \u0420\u0430\u043d\u044c\u0448\u0435 \u0431\u044b\u043b\u043e \u0435\u0449\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 <a href=\"https:\/\/github.com\/shish\/pgosquery\" rel=\"noopener noreferrer nofollow\">pgosquery<\/a> \u0438 \u044d\u0442\u043e \u043d\u0435 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 osquery, \u0430 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e &#171;\u0441 \u043d\u0443\u043b\u044f&#187;, \u0441 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c &#8212; \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b \u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0437\u0430\u0431\u0440\u043e\u0448\u0435\u043d.<\/p>\n<h2>postgres_osquery<\/h2>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e<code>run_osquery<\/code> \u043a\u0430\u043a <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/functions.sql\" rel=\"noopener noreferrer nofollow\">\u0445\u0440\u0430\u043d\u0438\u043c\u043a\u0443 \u0432 PostgreSQL<\/a> \u043d\u0430 Python, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0441\u0442\u0440\u043e\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a osquery, \u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u044b\u0434\u0430\u0435\u0442 JSON, \u0437\u0430\u043c\u0435\u043d\u044f\u044f \u043f\u0443\u0441\u0442\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 null.<\/p>\n<pre><code class=\"python\">CREATE OR REPLACE FUNCTION run_osquery(query text) RETURNS json AS $$  def replace_empty_strings_with_null(json_obj):     if isinstance(json_obj, dict):         for key, value in json_obj.items():             if isinstance(value, str) and value == \"\":                 json_obj[key] = None             elif isinstance(value, (dict, list)):                 replace_empty_strings_with_null(value)     elif isinstance(json_obj, list):         for i, item in enumerate(json_obj):             if isinstance(item, str) and item == \"\":                 json_obj[i] = None             elif isinstance(item, (dict, list)):                 replace_empty_strings_with_null(item)  import subprocess import plpy import json  try:   process_output = subprocess.check_output(['osqueryi', '--json', query ]).decode('utf-8')   process_json = json.loads(process_output)   replace_empty_strings_with_null(process_json)   return json.dumps(process_json, indent=4)  except subprocess.CalledProcessError as e:     plpy.error('External process failed: ' + str(e))  except Exception as e:     plpy.error('An error occurred: ' + str(e)) $$ LANGUAGE plpython3u;<\/code><\/pre>\n<p>\u0418 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b &#8212; \u0432\u043e\u0442 \u043e\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043d\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c json \u0432 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u0443\u044e \u0444\u043e\u0440\u043c\u0443, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f \u0437\u0430\u043f\u0438\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 PostgreSQL \u0444\u0443\u043d\u043a\u0446\u0438\u044f json_to_recordset. \u042d\u0442\u043e \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u043e \u0438 \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">SELECT * from json_to_recordset(run_osquery($$ SELECT * FROM routes $$)) as  (destination text, flags text, gateway text, hopcount text, interface text, metric text, mtu text, netmask text, source text, type text);<\/code><\/pre>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0435 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432\u044b\u0445 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 osquery \u043f\u043e\u0440\u0442\u044f\u043d\u043a\u0443 \u043f\u043e\u043b\u0435\u0439 \u0438 \u0438\u0445 \u0442\u0438\u043f\u043e\u0432. \u0418 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/osquery_types.sql\" rel=\"noopener noreferrer nofollow\">\u0430\u0432\u0442\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c(UDT)<\/a> \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 json_populate_recordset, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u0442\u0438\u043f \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0438\u043f \u0432 \u0431\u0430\u0437\u0435 PostgreSQL:<\/p>\n<pre><code class=\"sql\">CREATE TYPE osquery_routes AS (   \"destination\" text,    \"netmask\" integer,    \"gateway\" text,    \"source\" text,    \"flags\" integer,    \"interface\" text,    \"mtu\" integer,    \"metric\" integer,    \"type\" text,    \"hopcount\" integer);<\/code><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"sql\">SELECT * FROM json_populate_recordset(                                       null::osquery_routes,                                        run_osquery('select * from routes'));<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 <code>select * from routes<\/code> \u0432 \u0432\u0438\u0434\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3cf\/74d\/14e\/3cf74d14e4310aa10d34b5e43afc0623.png\" alt=\"\" title=\"\" width=\"1016\" height=\"493\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3cf\/74d\/14e\/3cf74d14e4310aa10d34b5e43afc0623.png\"\/><\/p>\n<div><figcaption>\u0418<\/figcaption><\/div>\n<\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u044d\u0442\u043e \u0440\u0443\u0447\u043a\u0430\u043c\u0438, \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/examples\/query_template.sql\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u0430\u0440\u0438\u0438<\/a>, \u043f\u0435\u0440\u0435\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0434\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0438\u043f\u044b <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/osquery_types.sql\" rel=\"noopener noreferrer nofollow\">\u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445<\/a> \u0437\u0430\u043f\u0438\u0441\u0435\u0439.<\/p>\n<p>\u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0441 JSON \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c:<\/p>\n<pre><code class=\"sql\">SELECT run_osquery($$    SELECT p.pid, name, p.path as process_path, pf.path as open_path                     FROM osquery_info i                       JOIN processes p ON p.pid = i.pid                          JOIN process_open_files pf ON pf.pid = p.pid                     WHERE pf.path LIKE '\/dev\/%' $$);<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bbe\/11a\/77b\/bbe11a77b539d7a12408074ba179d10f.png\" width=\"1911\" height=\"251\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bbe\/11a\/77b\/bbe11a77b539d7a12408074ba179d10f.png\"\/><\/figure>\n<p>\u041c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/1602fbbf77897e84afc9a8209ea0e4f4070e7684\/functions.sql#L34\" rel=\"noopener noreferrer nofollow\">get_common_json_record<\/a> \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c JSON \u0432 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/30d\/f95\/955\/30df95955b816c641b44f90b29a53d4f.png\" width=\"1108\" height=\"285\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/30d\/f95\/955\/30df95955b816c641b44f90b29a53d4f.png\"\/><\/figure>\n<h2>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p><a href=\"https:\/\/osquery.readthedocs.io\/en\/stable\/introduction\/sql\/\" rel=\"noopener noreferrer nofollow\">https:\/\/osquery.readthedocs.io\/en\/stable\/introduction\/sql\/<\/a><\/p>\n<p><a href=\"https:\/\/osquery.io\/schema\/5.9.1\/osqueryi\" rel=\"noopener noreferrer nofollow\">https:\/\/osquery.io\/schema\/5.9.1\/osqueryi<\/a><\/p>\n<p><a href=\"https:\/\/fleetdm.com\/guides\/osquery-evented-tables-overview\" rel=\"noopener noreferrer nofollow\">https:\/\/fleetdm.com\/guides\/osquery-evented-tables-overview<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/jmpsec\/osctrl\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/jmpsec\/osctrl<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/fleetdm\/fleet\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/fleetdm\/fleet<\/a><\/p>\n<p><a href=\"https:\/\/wazuh.com\" rel=\"noopener noreferrer nofollow\">https:\/\/wazuh.com<\/a><\/p>\n<p><a href=\"https:\/\/docs.elastic.co\/en\/integrations\/osquery_manager\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.elastic.co\/en\/integrations\/osquery_manager<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/citusdata\/pg_cron\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/citusdata\/pg_cron<\/a><\/p>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>run_osquery<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pg_cron \u043f\u043e \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0438\u043b\u0438 \u0436\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0437\u043b\u0435 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430 CitusDB \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>run_command_on_all_nodes().<\/code>\u0418\u043b\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0432\u043e\u0435 SIEM( Security information and event management) \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0445\u043e\u0441\u0442\u043e\u0432 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u043e\u043b\u044c\u043a\u043e PostgreSQL.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u043f\u043e\u043c\u043e\u0433 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043d\u043e\u0432\u043e\u0439 &#171;\u0438\u0433\u0440\u0443\u0448\u043a\u0438&#187; \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u043e\u0432 PostgreSQL. \u0412\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0448\u044c \u0441 osquery \u0438\u0437 PostgreSQL, \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0448\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u0449\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u0438 \u0437\u0430\u0431\u0430\u0432\u043d\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/759342\/\"> https:\/\/habr.com\/ru\/articles\/759342\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u0430 \u0438\u0434\u0435\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0435\u0442\u0440\u0438\u043a\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0438 \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u0438\u0437 PostgreSQL, \u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\" rel=\"noopener noreferrer nofollow\">\u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e<\/a>. \u042f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u044e \u043d\u0430 \u0435\u0433\u043e \u0437\u0440\u0435\u043b\u043e\u0441\u0442\u044c \u0438 \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u044c \u043a \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438\u0437 osquery \u0432 PostgreSQL \u0432 \u0432\u0438\u0434\u0435 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445\/JSON. \u0414\u0430\u043b\u044c\u0448\u0435 \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u044d\u0442\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<h2>osquery<\/h2>\n<p>\u041a\u0440\u043e\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439 open source \u043f\u0440\u043e\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c \u0438 \u043c\u0435\u0442\u0440\u0438\u043a\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043a\u0430\u043a \u043a \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0432 SQLite, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u044d\u0442\u043e\u0439 embedded \u0431\u0430\u0437\u044b, \u0441 \u043f\u0440\u0438\u0441\u0443\u0449\u0438\u043c\u0438 \u0435\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<ul>\n<li>\n<p><code>osqueryd<\/code>\u0434\u0435\u043c\u043e\u043d, \u043f\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u043c\u0435\u044e\u0449\u0438\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043e\u0431 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445;<\/p>\n<\/li>\n<li>\n<p><code>osqueryi<\/code> \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u043e\u043b\u044c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0438 \u043d\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441<code>osqueryd;<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/osquery.readthedocs.io\/en\/stable\/development\/osquery-sdk\/#thrift-api\" rel=\"noopener noreferrer nofollow\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Thrift API<\/a>, \u043b\u0438\u0431\u043e \u0437\u0430\u043f\u0443\u0441\u043a <code>osqueryi<\/code>\u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 json\/csv. \u0412 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f <a href=\"https:\/\/github.com\/igor-suhorukov\/osquery-java\/blob\/master\/src\/main\/java\/com\/questdb\/griffin\/engine\/functions\/osquery\/OsqueryFunctionFactory.java\" rel=\"noopener noreferrer nofollow\">\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <\/a>\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e Thrift API \u0447\u0435\u0440\u0435\u0437 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442 UNIX domain socket \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u0412 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437, \u0440\u0435\u0448\u0438\u043b \u0447\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 stdout \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0422\u0430\u043a \u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f postgres_osquery. \u0420\u0430\u043d\u044c\u0448\u0435 \u0431\u044b\u043b\u043e \u0435\u0449\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 <a href=\"https:\/\/github.com\/shish\/pgosquery\" rel=\"noopener noreferrer nofollow\">pgosquery<\/a> \u0438 \u044d\u0442\u043e \u043d\u0435 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 osquery, \u0430 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e &#171;\u0441 \u043d\u0443\u043b\u044f&#187;, \u0441 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c &#8212; \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b \u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0437\u0430\u0431\u0440\u043e\u0448\u0435\u043d.<\/p>\n<h2>postgres_osquery<\/h2>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e<code>run_osquery<\/code> \u043a\u0430\u043a <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/functions.sql\" rel=\"noopener noreferrer nofollow\">\u0445\u0440\u0430\u043d\u0438\u043c\u043a\u0443 \u0432 PostgreSQL<\/a> \u043d\u0430 Python, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0441\u0442\u0440\u043e\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a osquery, \u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u044b\u0434\u0430\u0435\u0442 JSON, \u0437\u0430\u043c\u0435\u043d\u044f\u044f \u043f\u0443\u0441\u0442\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 null.<\/p>\n<pre><code class=\"python\">CREATE OR REPLACE FUNCTION run_osquery(query text) RETURNS json AS $$  def replace_empty_strings_with_null(json_obj):     if isinstance(json_obj, dict):         for key, value in json_obj.items():             if isinstance(value, str) and value == \"\":                 json_obj[key] = None             elif isinstance(value, (dict, list)):                 replace_empty_strings_with_null(value)     elif isinstance(json_obj, list):         for i, item in enumerate(json_obj):             if isinstance(item, str) and item == \"\":                 json_obj[i] = None             elif isinstance(item, (dict, list)):                 replace_empty_strings_with_null(item)  import subprocess import plpy import json  try:   process_output = subprocess.check_output(['osqueryi', '--json', query ]).decode('utf-8')   process_json = json.loads(process_output)   replace_empty_strings_with_null(process_json)   return json.dumps(process_json, indent=4)  except subprocess.CalledProcessError as e:     plpy.error('External process failed: ' + str(e))  except Exception as e:     plpy.error('An error occurred: ' + str(e)) $$ LANGUAGE plpython3u;<\/code><\/pre>\n<p>\u0418 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b &#8212; \u0432\u043e\u0442 \u043e\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043d\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c json \u0432 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u0443\u044e \u0444\u043e\u0440\u043c\u0443, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f \u0437\u0430\u043f\u0438\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 PostgreSQL \u0444\u0443\u043d\u043a\u0446\u0438\u044f json_to_recordset. \u042d\u0442\u043e \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u043e \u0438 \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">SELECT * from json_to_recordset(run_osquery($$ SELECT * FROM routes $$)) as  (destination text, flags text, gateway text, hopcount text, interface text, metric text, mtu text, netmask text, source text, type text);<\/code><\/pre>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0435 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432\u044b\u0445 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 osquery \u043f\u043e\u0440\u0442\u044f\u043d\u043a\u0443 \u043f\u043e\u043b\u0435\u0439 \u0438 \u0438\u0445 \u0442\u0438\u043f\u043e\u0432. \u0418 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/osquery_types.sql\" rel=\"noopener noreferrer nofollow\">\u0430\u0432\u0442\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c(UDT)<\/a> \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 json_populate_recordset, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u0442\u0438\u043f \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0438\u043f \u0432 \u0431\u0430\u0437\u0435 PostgreSQL:<\/p>\n<pre><code class=\"sql\">CREATE TYPE osquery_routes AS (   \"destination\" text,    \"netmask\" integer,    \"gateway\" text,    \"source\" text,    \"flags\" integer,    \"interface\" text,    \"mtu\" integer,    \"metric\" integer,    \"type\" text,    \"hopcount\" integer);<\/code><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"sql\">SELECT * FROM json_populate_recordset(                                       null::osquery_routes,                                        run_osquery('select * from routes'));<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 <code>select * from routes<\/code> \u0432 \u0432\u0438\u0434\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0418<\/figcaption><\/div>\n<\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u044d\u0442\u043e \u0440\u0443\u0447\u043a\u0430\u043c\u0438, \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/examples\/query_template.sql\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u0430\u0440\u0438\u0438<\/a>, \u043f\u0435\u0440\u0435\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0434\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0438\u043f\u044b <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/main\/osquery_types.sql\" rel=\"noopener noreferrer nofollow\">\u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445<\/a> \u0437\u0430\u043f\u0438\u0441\u0435\u0439.<\/p>\n<p>\u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0441 JSON \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c:<\/p>\n<pre><code class=\"sql\">SELECT run_osquery($$    SELECT p.pid, name, p.path as process_path, pf.path as open_path                     FROM osquery_info i                       JOIN processes p ON p.pid = i.pid                          JOIN process_open_files pf ON pf.pid = p.pid                     WHERE pf.path LIKE '\/dev\/%' $$);<\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u041c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <a href=\"https:\/\/github.com\/igor-suhorukov\/postgres_osquery\/blob\/1602fbbf77897e84afc9a8209ea0e4f4070e7684\/functions.sql#L34\" rel=\"noopener noreferrer nofollow\">get_common_json_record<\/a> \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c JSON \u0432 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p><a href=\"https:\/\/osquery.readthedocs.io\/en\/stable\/introduction\/sql\/\" rel=\"noopener noreferrer nofollow\">https:\/\/osquery.readthedocs.io\/en\/stable\/introduction\/sql\/<\/a><\/p>\n<p><a href=\"https:\/\/osquery.io\/schema\/5.9.1\/osqueryi\" rel=\"noopener noreferrer nofollow\">https:\/\/osquery.io\/schema\/5.9.1\/osqueryi<\/a><\/p>\n<p><a href=\"https:\/\/fleetdm.com\/guides\/osquery-evented-tables-overview\" rel=\"noopener noreferrer nofollow\">https:\/\/fleetdm.com\/guides\/osquery-evented-tables-overview<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/jmpsec\/osctrl\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/jmpsec\/osctrl<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/fleetdm\/fleet\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/fleetdm\/fleet<\/a><\/p>\n<p><a href=\"https:\/\/wazuh.com\" rel=\"noopener noreferrer nofollow\">https:\/\/wazuh.com<\/a><\/p>\n<p><a href=\"https:\/\/docs.elastic.co\/en\/integrations\/osquery_manager\" rel=\"noopener noreferrer nofollow\">https:\/\/docs.elastic.co\/en\/integrations\/osquery_manager<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/citusdata\/pg_cron\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/citusdata\/pg_cron<\/a><\/p>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>run_osquery<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pg_cron \u043f\u043e \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0438\u043b\u0438 \u0436\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0437\u043b\u0435 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430 CitusDB \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>run_command_on_all_nodes().<\/code>\u0418\u043b\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0432\u043e\u0435 SIEM( Security information and event management) \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0445\u043e\u0441\u0442\u043e\u0432 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u043e\u043b\u044c\u043a\u043e PostgreSQL.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u043f\u043e\u043c\u043e\u0433 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043d\u043e\u0432\u043e\u0439 &#171;\u0438\u0433\u0440\u0443\u0448\u043a\u0438&#187; \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u043e\u0432 PostgreSQL. \u0412\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0448\u044c \u0441 osquery \u0438\u0437 PostgreSQL, \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0448\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0435\u0449\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u0438 \u0437\u0430\u0431\u0430\u0432\u043d\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/759342\/\"> https:\/\/habr.com\/ru\/articles\/759342\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-354171","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354171"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354171\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}