通过 PowerShell 高级搜寻

适用于:

希望体验 Microsoft Defender for Endpoint? 注册免费试用版

注意

如果你是美国政府客户,请使用美国政府客户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 运行高级查询。 有关详细信息,请参阅 高级搜寻 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

若要以 json 格式输出文件file1.json查询的结果,请运行以下命令:

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

相关文章

提示

想要了解更多信息? Engage技术社区中的 Microsoft 安全社区:Microsoft Defender for Endpoint技术社区