다음을 통해 공유


PowerShell을 사용하는 지능형 헌팅

적용 대상:

엔드포인트용 Microsoft Defender를 경험하고 싶으신가요? 무료 평가판을 신청하세요.

참고

미국 정부 고객인 경우 미국 정부 고객을 위해 엔드포인트용 Microsoft Defender 나열된 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에 대한 '고급 쿼리 실행' 권한이 있어야 합니다.)
  • $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

관련 문서

더 자세히 알아보고 싶으신가요? 기술 커뮤니티: 엔드포인트용 Microsoft Defender Tech Community의 Microsoft 보안 커뮤니티와 Engage.