LoginSignup
0
1

More than 3 years have passed since last update.

Osqueryでエンドポイントセキュリティソリューションを構築する方法

Last updated at Posted at 2020-12-08

この記事では、Osqueryを使用してAlibaba Cloud Elastic Compute Service上にエンドポイントセキュリティソリューションを構築する方法を紹介します。

本ブログは英語版からの翻訳です。オリジナルはこちらからご確認いただけます。一部機械翻訳を使用しております。翻訳の間違いがありましたら、ご指摘いただけると幸いです。

Alibaba Cloud Tech Share 執筆者、Raushan Raj。Tech Shareは、技術的な知識やベストプラクティスをクラウドコミュニティ内で共有することを奨励するAlibaba Cloudのインセンティブプログラムです。

Alibaba Cloud上に展開しているマシンのエンドポイントセキュリティソリューションを構築するにはどうすればよいでしょうか?HIDS(ホスト侵入検知システム)を組織に設定したいですか?答えを探しているのであれば、ぜひこの記事を読んでみてください。

この記事を読みながら、最初にOsqueryの基本とAlibaba Cloud Elastic Compute Service (ECS)での使用について学びます。記事の最後には、商用ソリューションに一銭も投資することなく、オープンソースソリューションを使用して、コンプライアンス、脆弱性管理、インシデント対応、マシンへの侵入を検出することがいかに簡単に管理できるかを知ることができます。

Osqueryとは?

Osqueryは、オペレーティングシステムを高性能リレーショナルデータベースとして公開するオペレーティングシステムのインストルメンテーションフレームワークです。これにより、オペレーティングシステムのデータを探索するためのSQLクエリを記述することができます。

Osqueryは、数千台のマシンを同時に監視する能力を持っているため、適切に構成されていればHIDSに最適なツールです。osqueryのログの上に分析とアラートを追加すると、社内EDRソリューションの簡単なセットアップに役立ちます。

また、osqueryは、お客様のマシン(Linux、Windows、Mac)に常駐し、セキュリティ分析と監視のためにログを中央サーバに転送するエージェントです。osqueryはマシンをSQLデータベースとして扱い、SQLベースのクエリ構文を提供して、簡単に情報を収集することができます。osqueryは以下のように使用することができます。

  1. osqueryd:Daemon、バックグラウンドでクエリを実行し、結果をログソースに保存します。
  2. osqueryi : インタラクティブシェル、シェル上でクエリを実行して結果を得ることができる。

クエリがどのようなものなのかについては、これから説明していきますので、ご安心ください。

Alibaba Cloud ECSにOsqueryをインストール

  • Ubuntuサーバーを用意する必要があります。こちらの記事を参照して、Alibaba Cloud ECS上でUbuntuサーバーを設定する方法を説明します。
  • 以下のコマンドを実行して、デフォルトの設定でosqueryをインストールして実行します。
    $ export OSQUERY_KEY=1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B
    $ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $OSQUERY_KEY
    $ sudo add-apt-repository 'deb [arch=amd64] https://pkg.osquery.io/deb deb main'
    $ sudo apt-get update
    $ sudo apt-get install osquery
    $ sudo service osqueryd restart

Osqueryiによるシステムの問い合わせ

Osqueryはlogin_in_users、cpu_time、process_events、file_eventsなどのテーブルを内蔵しています。

OsqueryはSQLクエリの構文を理解しています。以下は、システムで実行されている5つのプロセスを取得するクエリです。

root@home:~$ osqueryi
Using a virtual database. Need help, type '.help'
osquery> select pid, name, path, cmdline, system_time from processes limit 5;
+------+-----------------+------+----------------------------------------+-------------+
| pid  | name            | path | cmdline                                | system_time |
+------+-----------------+------+----------------------------------------+-------------+
| 1    | systemd         |      | /sbin/init splash                      | 3810        |
| 10   | rcu_sched       |      |                                        | 8470        |
| 1025 | rabbitmq-server |      | /bin/sh /usr/lib/rabbitmq/bin/rabbitmq | 0           |
| 1051 | redis-server    |      | /usr/bin/redis-server 127.0.0.1:6379   | 9020        |
| 106  | kthrotld        |      |                                        | 0           |
+------+-----------------+------+----------------------------------------+-------------+

バックグラウンドでクエリを実行するようにosquerydを設定する

  1. /etc/osquery/osquery.conf:設定ファイルは以下の構文に従います。
    {
        "options" {...},
        "schedule"{
            "deb_packages":{
                "query" : "select * from deb_packages;"
                "interval" : "86400",
                "snapshot" : true,
                "platform": "linux",
                "description" : "Fetches all deb packages installed in system"
            },
            "query_name_2":{
                ...
            }
        },
        "packs":{
            "pack1" : "/path/to/pack1.conf",
            "pack2" : "/path/to/pack2/conf"
            ...
        },
        "file_paths":{
            "configurations":[
            "/etc/%%",
            "/bin/%%",
            ]
        }
    }

  • options: osqueryd --help はすべての可能なオプションを与えてくれます。空のままでも構いません。
         "options": {
            "events_max": 100000,
            "enable_monitor": true,
            "host_identifier": "uuid"
          }
* `schedule`: schedule block contains a list of all the queries you want to execute. Query block  has the following keys:
    1. _query_: SQL query you need to execute on a system level. In the first query, we are fetching deb_packages installed on the system.
    2. _interval_: Time in seconds to execute the query next time.
    3. _snapshot_: If the snapshot is False then only added or removed deb packages will be returned over time, else whole deb packages will be returned over an interval.
    4. _platform_: Queries needs to be defined platform specific i.e; Linux, POSIX, Darwin, Windows, FreeBSD
* `packs`:  Packs are a collection of queries intended for the specific purpose. Example: A "vulnerability management" pack may perform general asset management queries that build event logs around package and software install changes. _pack.conf_  looks like below
{
    "queries":{
        "query_name_1":{
        ...
    },
    ...
    }
}
* `file_paths`: This block contains path of files/directory which you want to monitor for addtion, removal, access changes, etc. The changes are received in `file_events` table, hence _select * from file_events;_ query is required in schedule block. Generally, any table with `_events` suffix like _user_events_,_process_events_,_socket_events_ are populated using pubsub framework only when events got triggered. 
  1. /etc/osquery/osquery.flag:osquerydを起動する際の--flagfile argsのデフォルトパスです。osquerydを起動するために必要な設定を提供します。
--config_plugin = filesystem
--logger_plugin = filesystem
--logger_path = /var/log/osquery
--host_identifier = uuid
--utc=true
* `config_plugin <tls|filesystem>`: Configuration can be fetched via tls also.
* `logger_plugin <tls|filesystem>` : Output of the queries can be written to tls also.
* `logger_path`: In case of filesystem, path of directory to write logs.

セキュリティに使用する設定ファイルとフラグファイル

1、 Osquery GitHub repoには、あらかじめ定義されたパックが含まれています。confファイルをコピーし、ファイル内の必要なパックをアンコメントして、osquerydを再起動します。

$ cp /usr/share/osquery/osquery.example.conf /etc/osquery/osquery.conf
  "packs": {
         "incident-response": "/usr/share/osquery/packs/incident-response.conf",
         "it-compliance": "/usr/share/osquery/packs/it-compliance.conf",
         "osx-attacks": "/usr/share/osquery/packs/osx-attacks.conf",
         "vuln-management": "/usr/share/osquery/packs/vuln-management.conf",
         "hardware-monitoring": "/usr/share/osquery/packs/hardware-monitoring.conf",
         "ossec-rootkit": "/usr/share/osquery/packs/ossec-rootkit.conf",
    },

2、ここから推奨される設定ファイルとフラグファイルをダウンロードして、/etc/osquery/ディレクトリ内の/etc/osquery/ディレクトリ内で、osquerydを再起動します。

セキュリティパックの説明

上記の推奨構成の文脈で、以下、セキュリティパックについて、いくつかの例を挙げて説明します。

セキュリティ対応について

すべての組織は、特定のセキュリティ基準を満たす必要があります。このパックには、ロックダウンされたオペレーティングシステムの機能やユーザー設定の変更をチェックするクエリが含まれています。また、GDPR、PCI DSSの要件のいくつかで組織を支援します。

"disk_encryption": {
      "query" : "select * from disk_encryption;",
      "interval" : "86400",
      "version" : "1.4.5",
      "platform" : "posix",
      "description" : "Retrieves the current disk encryption status for the target system.",
      "value" : "Identifies a system potentially vulnerable to disk cloning."
    },
"chrome_extensions": {
      "query" : "select * from users join chrome_extensions using (uid);",
      "interval" : "86400",
      "version" : "1.4.5",
      "description" : "Retrieves the list of extensions for Chrome in the target system.",
      "value" : "General security posture."
    },

脆弱性管理

脆弱性管理とは、ソフトウェアのセキュリティホールを特定し、パッチを当てて機密システムやデータへの不正アクセスを防ぐプロセスです。脆弱性管理パックは、パッケージやソフトウェアのインストール変更に関するイベントログを構築する一般的なアセット管理クエリを実行することができます。

"backdoored_python_packages": {
      "query" : "select name as package_name, version as package_version, path as package_path from python_packages where package_name = 'acqusition' or package_name = 'apidev-coop' or package_name = 'bzip' or package_name = 'crypt' or package_name = 'django-server' or package_name = 'pwd' or package_name = 'setup-tools' or package_name = 'telnet' or package_name = 'urlib3' or package_name = 'urllib';",
      "interval" : "86400",
      "platform" : "posix",
      "version" : "1.4.5",
      "description" : "Watches for the backdoored Python packages installed on system. See (http://www.nbu.gov.sk/skcsirt-sa-20170909-pypi/index.html)",
      "value" : "Gives some assurances that no bad Python packages are installed on the system."
    }

インシデント対応と侵入検知

システムがマルウェアに感染したり、攻撃者がバックドアを設置したりしているかもしれません。インシデント対応の主な目的は、侵入者を特定し、将来のインシデントのリスクを軽減するために時間内に対策を講じることです。

"logged_in_users": {
      "query" : "select liu.*, p.name, p.cmdline, p.cwd, p.root from logged_in_users liu, processes p where liu.pid = p.pid;",
      "interval" : "3600",
      "platform": "posix",
      "version" : "1.4.5",
      "description" : "Retrieves the list of all the currently logged in users in the target system.",
      "value" : "Useful for intrusion detection and incident response. Verify assumptions of what accounts should be accessing what systems and identify machines accessed during a compromise."
    },

"open_sockets": {
      "query" : "select distinct pid, family, protocol, local_address, local_port, remote_address, remote_port, path from process_open_sockets where path <> '' or remote_address <> '';",
      "interval" : "86400",
      "platform": "posix",
      "version" : "1.4.5",
      "description" : "Retrieves all the open sockets per process in the target system.",
      "value" : "Identify malware via connections to known bad IP addresses as well as odd local or remote port bindings"
    },

Osquerydの出力ログ

生成されたログはすべてJSON形式です。

Differential Logs /var/log/osquery/osqueryd.results.log

これらは、最後の(直近の)クエリ実行と現在の実行との間の差分変更です。各ログ行は、どのようなデータがどのクエリによって追加/削除されたかを示すJSON文字列です。

{
  "action": "added",
  "columns": {
    "name": "osqueryd",
    "path": "/usr/local/bin/osqueryd",
    "pid": "97830"
  },
  "name": "processes",
  "hostname": "hostname.local",
  "calendarTime": "Tue Sep 30 17:37:30 2014",
  "unixTime": "1412123850",
  "epoch": "314159265",
  "counter": "1"
}
{
  "action": "removed",
  "columns": {
    "name": "osqueryd",
    "path": "/usr/local/bin/osqueryd",
    "pid": "97650"
  },
  "name": "processes",
  "hostname": "hostname.local",
  "calendarTime": "Tue Sep 30 17:37:30 2014",
  "unixTime": "1412123850",
  "epoch": "314159265",
  "counter": "1"
}

Snapshot Logs /var/log/osquery/osqueryd.snapshots.log

スナップショットは、「正確な時点での」結果のセットであり、差異はありません。マウントの追加や削除ではなく、常にマウントのリストが必要な場合は、スナップショットを使用します。

{
  "action": "snapshot",
  "snapshot": [
    {
      "parent": "0",
      "path": "/sbin/launchd",
      "pid": "1"
    },
    {
      "parent": "1",
      "path": "/usr/sbin/syslogd",
      "pid": "51"
    },
    {
      "parent": "1",
      "path": "/usr/libexec/UserEventAgent",
      "pid": "52"
    },
    {
      "parent": "1",
      "path": "/usr/libexec/kextd",
      "pid": "54"
    }
  ],
  "name": "process_snapshot",
  "hostIdentifier": "hostname.local",
  "calendarTime": "Mon May  2 22:27:32 2016 UTC",
  "unixTime": "1462228052",
  "epoch": "314159265",
  "counter": "1"
}

次回

これでosqueryの設定方法とログファイルにクエリの出力を取得する方法がわかりました。今後の記事では、Alibabaのマシンフリートでのosqueryの管理、tlsプラグインの使い方、logstashでログを解析してElasticSearchにインジェストする方法、セキュリティインシデントのアラートにElastAlertを使用する方法、脅威のハンティングを有効にする方法などを載せる予定です。

アリババクラウドは日本に2つのデータセンターを有し、世界で60を超えるアベラビリティーゾーンを有するアジア太平洋地域No.1(2019ガートナー)のクラウドインフラ事業者です。
アリババクラウドの詳細は、こちらからご覧ください。
アリババクラウドジャパン公式ページ

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1