PowerShell を使用した高度な追求

適用対象:

Microsoft Defender ATP を試してみたいですか? 無料試用版にサインアップしてください。

注:

米国政府機関のお客様の場合は、米国政府機関のお客様のMicrosoft Defender for Endpointに記載されている URI を使用してください。

ヒント

パフォーマンスを向上させるために、地理的な場所に近いサーバーを使用できます。

  • us.api.security.microsoft.com
  • eu.api.security.microsoft.com
  • uk.api.security.microsoft.com
  • au.api.security.microsoft.com
  • swa.api.security.microsoft.com

PowerShell を使用して高度なクエリを実行します。 詳細については、「 Advanced Hunting API」を参照してください。

このセクションでは、PowerShell サンプルを共有してトークンを取得し、それを使用してクエリを実行します。

はじめに

最初にアプリを 作成する必要があります

準備手順

  • PowerShell のウィンドウを開きます。

  • ポリシーで PowerShell コマンドを実行できない場合は、次のコマンドを実行できます。

    Set-ExecutionPolicy -ExecutionPolicy Bypass
    

詳細については、 PowerShell のドキュメントを参照してください

トークンを取得する

  • 次のコマンドを実行します。
$tenantId = '00000000-0000-0000-0000-000000000000' # Paste your own tenant ID here
$appId = '11111111-1111-1111-1111-111111111111' # Paste your own app ID here
$appSecret = '22222222-2222-2222-2222-222222222222' # Paste your own app secret here

$resourceAppIdUri = 'https://api.securitycenter.microsoft.com'
$oAuthUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
$body = [Ordered] @{
    resource = "$resourceAppIdUri"
    client_id = "$appId"
    client_secret = "$appSecret"
    grant_type = 'client_credentials'
}
$response = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $body -ErrorAction Stop
$aadToken = $response.access_token

場所

  • $tenantId: クエリを実行するテナントの ID (つまり、クエリはこのテナントのデータに対して実行されます)
  • $appId: Microsoft Entra アプリの ID (アプリには Defender for Endpoint に対する "高度なクエリの実行" アクセス許可が必要です)
  • $appSecret: Microsoft Entra アプリのシークレット

クエリの実行

次のクエリを実行します。

$token = $aadToken
$query = 'DeviceRegistryEvents | limit 10' # Paste your own query here

$url = "https://api.securitycenter.microsoft.com/api/advancedhunting/run"
$headers = @{ 
    'Content-Type' = 'application/json'
    Accept = 'application/json'
    Authorization = "Bearer $aadToken" 
}
$body = ConvertTo-Json -InputObject @{ 'Query' = $query }
$webResponse = Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body -ErrorAction Stop
$response =  $webResponse | ConvertFrom-Json
$results = $response.Results
$schema = $response.Schema
  • クエリの結果を含む$results
  • $schemaクエリの結果のスキーマが含まれています

複雑なクエリ

複雑なクエリ (または複数行クエリ) を実行する場合は、クエリをファイルに保存し、上記のサンプルの最初の行ではなく、次のコマンドを実行します。

$query = [IO.File]::ReadAllText("C:\myQuery.txt"); # Replace with the path to your file

クエリ結果を操作する

クエリ結果を使用できるようになりました。

ファイル file1.csv で CSV 形式でクエリの結果を出力するには、次のコマンドを実行します。

$results | ConvertTo-Csv -NoTypeInformation | Set-Content C:\file1.csv

ファイル file1.jsonで JSON 形式でクエリの結果を出力するには、次のコマンドを実行します。

$results | ConvertTo-Json | Set-Content file1.json

関連記事

ヒント

さらに多くの情報を得るには、 Tech Community 内の Microsoft Security コミュニティ (Microsoft Defender for Endpoint Tech Community) にご参加ください。