PowerShell を使用したエンドポイント API 用 Microsoft DefenderMicrosoft Defender for Endpoint APIs using PowerShell
適用対象: Microsoft Defender for EndpointApplies to: Microsoft Defender for Endpoint
Microsoft Defender for Endpoint を体験してみませんか?Want to experience Microsoft Defender for Endpoint? 無料試用版にサインアップします。Sign up for a free trial.
注意
米国政府機関のお客様の場合は、Microsoft Defender for Endpoint for US Government のお客様に記載されている URI を使用してください。If you are a US Government customer, please use the URIs listed in Microsoft Defender for Endpoint for US Government customers.
ヒント
パフォーマンスを向上するために、地理的な場所に近いサーバーを使用できます。For better performance, you can use server closer to your geo location:
- api-us.securitycenter.microsoft.comapi-us.securitycenter.microsoft.com
- api-eu.securitycenter.microsoft.comapi-eu.securitycenter.microsoft.com
- api-uk.securitycenter.microsoft.comapi-uk.securitycenter.microsoft.com
Microsoft Defender for Endpoint を体験してみませんか?Want to experience Microsoft Defender for Endpoint? 無料試用版にサインアップします。Sign up for a free trial.
Microsoft Defender for Endpoint の複数の API を使用した完全なシナリオ。Full scenario using multiple APIs from Microsoft Defender for Endpoint.
このセクションでは、PowerShell サンプルをIn this section, we share PowerShell samples to
- トークンの取得Retrieve a token
- トークンを使用して Microsoft Defender for Endpoint の最新のアラートを取得するUse token to retrieve the latest alerts in Microsoft Defender for Endpoint
- アラートごとに、アラートの優先度が中または高で、まだ進行中の場合は、デバイスが不審な URL に接続した回数を確認します。For each alert, if the alert has medium or high priority and is still in progress, check how many times the device has connected to suspicious URL.
前提条件: 最初にアプリを 作成する必要があります。Prerequisite: You first need to create an app.
準備手順Preparation instructions
- PowerShell のウィンドウを開きます。Open a PowerShell window.
- ポリシーで PowerShell コマンドを実行できない場合は、次のコマンドを実行できます。If your policy does not allow you to run the PowerShell commands, you can run the below command:
Set-ExecutionPolicy -ExecutionPolicy Bypass
詳細については 、「PowerShell のドキュメント」を参照してください。For more information, see PowerShell documentation
トークンの取得Get token
以下を実行します。Run the below:
$tenantId: クエリを実行する代わりにテナントの ID (つまり、このテナントのデータに対してクエリが実行されます)$tenantId: ID of the tenant on behalf of which you want to run the query (i.e., the query will be run on the data of this tenant)
$appId: AAD アプリの ID (アプリに Defender for Endpoint への 「高度なクエリの実行」 アクセス許可が必要)$appId: ID of your AAD app (the app must have 'Run advanced queries' permission to Defender for Endpoint)
$appSecret: Azure AD アプリの秘密$appSecret: Secret of your Azure AD app
$suspiciousUrl: URL$suspiciousUrl: The 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