1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Defender for Endpoint のデバイスタグで Azure サブスクリプション ID を設定 - Microsoft Defender for Endpoint API を Python コードで扱う

Last updated at Posted at 2024-02-21

本記事について

Defender for Cloud (Defender for Servers P1/P2) から Defender for Endpoint を Azure 仮想マシンに対してオンボードする際に、よく課題に挙げられるのが、Azure の IAM と Microsoft Defender XDR のアクセス権管理が独立になっており連動していないことです。

Azure はサブスクリプションやリソースグループで IAM を設定する一方、Microsoft Defender XDR ではデバイスグループを利用してアクセス権管理を行います。

しかし、このギャップを埋めるためのヒントとして、Defender for Cloud (Defender for Servers P1/P2) から Defender for Endpoint を Azure 仮想マシンに対してオンボードすると、Defender for Endpoint (Microsoft Defender XDR) 側では、メタデータとして、サブスクリプション ID や Azure リソース ID の値を記録して保持してくれるということがあります。

image.png

今回は、このメタデータを利用して、Defender for Endpoint で各デバイスに Azure 側で所属している Azure サブスクリプション ID をデバイスタグ情報として付与する方法を見ていきます。

デバイスタグを付与することで、簡単にデバイスグループを構成することができるようになります。たとえば Azure サブスクリプションごとにひとつずつデバイスグループを作成し、そのデバイスグループに対して Azure サブスクリプションの利用者にアクセス権を付与することができるようになるというわけです。

本記事の実行内容の概念図

実現したいことの大まかな図は下記になります。独立して動作する Azure と Microsoft Defender XDR において、メタデータを介してデバイスタグを付与し、Azure サブスクリプションの ID 情報で Defender 側のデバイスグループを分割することを目指します。

image.png

そして、実現方式は下記になります。

image.png

Microsoft Defender for Endpoint API では、各デバイスの詳細情報を取得でき、その中にはサブスクリプション ID を含むメタデータも含まれます。そのデータからマシンID とサブスクリプション ID の対応表を Python のコードで作成し、その値を使って各マシンにデバイスタグとしてそのサブスクリプション ID の値を付与します。

残念ながら、デバイスグループ自体の作成はこの API では行えないので、Microsoft Defender XDR ポータルで行います。

サンプルの実装

ここから簡単にサンプルの実装を見ていきます。

Microsoft Defender for Endpoint API を呼び出すための Entra ID アプリケーション (サービスプリンシパル) の作成

下記ドキュメントを確認し、Microsoft Defender for Endpoint API を呼び出すための Entra ID アプリケーション (サービスプリンシパル) の作成を行います。このサービスプリンシパルは Python コード内で利用します。

Python コードによるメタデータの取得とデバイスタグの追加

本題の Python コードのサンプルを見ていきます。

まず、作成した Entra ID のアプリケーションを利用して、Bearer トークンを取得します。次に、そのトークンを使って、Microsoft Defender for Endpoint API でメタデータの取得を行い、情報を整理します。そして、各マシンに対してデバイスタグ追加の処理を同 API を介して行います。

Microsoft Defender for Endpoint API を使ったメタデータの取得とデバイスタグの追加

import requests
import urllib

# Bearer Tokenを取得するための Microsoft Entra ID の URL
url = 'https://login.microsoftonline.com/<Entra ID テナント ID>/oauth2/token' 

# Bearer Tokenを取得するためのパラメータ
params = {
'client_id': '<アプリケーションのクライアント ID>',
'client_secret': '<アプリケーションのシークレット>',
'resource': 'https://api.securitycenter.microsoft.com',
'grant_type': 'client_credentials'
}

# URLをエンコード
params = urllib.parse.urlencode(params)

# header で Content-Type を application/x-www-form-urlencoded に指定
headers = {'Content-Type': 'application/x-www-form-urlencoded'}

# Bearer Tokenを取得
r = requests.post(url=url, data=params, headers=headers)
access_token=r.json().get('access_token')

# Bearer Tokenを使って、Microsoft Defender for Endpoint API でマシンの一覧を取得
url_machine = "https://api.securitycenter.microsoft.com/api/machines"
headers_machine = {'Authorization': 'Bearer {}'.format(access_token)}
r_machine = requests.get(url=url_machine, headers=headers_machine)
machine_list = r_machine.json().get('value')

# マシンのIDとサブスクリプションIDを取得
machine_id_subscription = []
for machine in machine_list:
    vm_id = machine.get('id')
    vm_name = machine.get('computerDnsName')
    vm_metadata = machine.get('vmMetadata')
    if type(vm_metadata) == dict:
        machine_id_subscription.append({'vmId': vm_id, 'subscriptionId': vm_metadata.get('subscriptionId'), 'vmName': vm_name})

# Microsoft Defender for Endpoint API で各マシンのタグを追記
for machine in machine_id_subscription:
    if str(machine.get('subscriptionId')) != "None":
        url_tag = "https://api.securitycenter.microsoft.com/api/machines/" + machine.get('vmId') + "/tags"
        headers_tag =  {'Authorization': 'Bearer {}'.format(access_token)}
        body_tag = {"Value" : str(machine.get('subscriptionId')), "Action": "Add"}
        r_tag = requests.post(url=url_tag, headers=headers_tag, data=str(body_tag))

デバイスグループの作成

上記が無事完了すると、Defender XDR Portal のデバイスページで各デバイスにタグが付与されていることが確認できます。

image.png

このタグが付いているデバイスが、同デバイスグループに属するようにデバイスグループの作成を行います。

image.png

これにより同一サブスクリプションのサーバーが同じデバイスグループに属することになり、Defender XDR 側のアクセス管理の設定がしやすくなります。

最後に

*本稿は、個人の見解に基づいた内容であり、所属する会社の公式見解ではありません。また、いかなる保証を与えるものでもありません。正式な情報は、各製品の販売元にご確認ください。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?