PowerShell を使用した API のMicrosoft Defender for Endpoint
適用対象:
重要
Defender for Business には高度なハンティング機能は含まれていません。 「Microsoft Defender for Businessとプラン 1 とプラン 2 Microsoft Defender for Endpoint比較する」を参照してください。
Microsoft Defender ATP を試してみたいですか? 無料試用版にサインアップしてください。
注:
米国政府機関のお客様の場合は、米国政府機関のお客様のMicrosoft Defender for Endpointに記載されている URI を使用してください。
ヒント
パフォーマンスを向上させるために、地理的な場所に近いサーバーを使用できます。
- api-us.securitycenter.microsoft.com
- api-eu.securitycenter.microsoft.com
- api-uk.securitycenter.microsoft.com
Microsoft Defender ATP を試してみたいですか? 無料試用版にサインアップしてください。
Microsoft Defender for Endpointから複数の API を使用する完全なシナリオ。
このセクションでは、PowerShell サンプルを
- トークンを取得する
- トークンを使用して、Microsoft Defender for Endpointの最新のアラートを取得する
- アラートごとに、アラートの優先度が中程度または高く、まだ進行中の場合は、デバイスが不審な URL に接続した回数を確認します。
前提条件: 最初に アプリを作成する必要があります。
準備手順
- PowerShell のウィンドウを開きます。
- ポリシーで PowerShell コマンドを実行できない場合は、次のコマンドを実行できます。
Set-ExecutionPolicy -ExecutionPolicy Bypass
詳細については、PowerShell のドキュメントを参照してください。
トークンを取得する
以下を実行します。
$tenantId: クエリを実行するテナントの ID (つまり、このテナントのデータに対してクエリが実行されます)
$appId: AAD アプリの ID (アプリには Defender for Endpoint に対する "高度なクエリの実行" アクセス許可が必要です)
$appSecret: Azure AD アプリのシークレット
$suspiciousUrl: URL
$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
$suspiciousUrl = 'www.suspiciousUrl.com' # Paste your own URL here
$resourceAppIdUri = 'https://securitycenter.onmicrosoft.com/windowsatpservice'
$oAuthUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
$authBody = [Ordered] @{
resource = "$resourceAppIdUri"
client_id = "$appId"
client_secret = "$appSecret"
grant_type = 'client_credentials'
}
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop
$aadToken = $authResponse.access_token
#Get latest alert
$alertUrl = "https://api.securitycenter.microsoft.com/api/alerts?`$top=10"
$headers = @{
'Content-Type' = 'application/json'
Accept = 'application/json'
Authorization = "Bearer $aadToken"
}
$alertResponse = Invoke-WebRequest -Method Get -Uri $alertUrl -Headers $headers -ErrorAction Stop
$alerts = ($alertResponse | ConvertFrom-Json).value
$machinesToInvestigate = New-Object System.Collections.ArrayList
Foreach($alert in $alerts)
{
#echo $alert.id $alert.machineId $alert.severity $alert.status
$isSevereAlert = $alert.severity -in 'Medium', 'High'
$isOpenAlert = $alert.status -in 'InProgress', 'New'
if($isOpenAlert -and $isSevereAlert)
{
if (-not $machinesToInvestigate.Contains($alert.machineId))
{
$machinesToInvestigate.Add($alert.machineId) > $null
}
}
}
$commaSeparatedMachines = '"{0}"' -f ($machinesToInvestigate -join '","')
$query = "NetworkCommunicationEvents
| where MachineId in ($commaSeparatedMachines)
| where RemoteUrl == `"$suspiciousUrl`"
| summarize ConnectionsCount = count() by MachineId"
$queryUrl = "https://api.securitycenter.microsoft.com/api/advancedqueries/run"
$queryBody = ConvertTo-Json -InputObject @{ 'Query' = $query }
$queryResponse = Invoke-WebRequest -Method Post -Uri $queryUrl -Headers $headers -Body $queryBody -ErrorAction Stop
$response = ($queryResponse | ConvertFrom-Json).Results
$response