Live Response Automation via API in Microsoft Defender for Endpoint

Listen to this Post

Microsoft Defender for Endpoint (MDE) provides powerful Live Response capabilities, allowing security teams to investigate and remediate threats in real time. This article explores how to automate Live Response using the Defender API, including uploading and executing files remotely—even on unmanaged devices.

🔗 Reference: Live Response Automation via API in Microsoft Defender

You Should Know:

1. Prerequisites for API Automation

  • Microsoft Defender for Endpoint API Access: Ensure you have the necessary permissions (Threat.ReadWrite, Machine.LiveResponse).
  • Authentication: Use OAuth 2.0 with Microsoft Graph API.
    PowerShell: Get OAuth Token 
    $tenantId = "YOUR_TENANT_ID" 
    $clientId = "YOUR_CLIENT_ID" 
    $clientSecret = "YOUR_CLIENT_SECRET" 
    $resource = "https://api.securitycenter.microsoft.com" </li>
    </ul>
    
    $tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" 
    $body = @{ 
    client_id = $clientId 
    client_secret = $clientSecret 
    scope = "$resource/.default" 
    grant_type = "client_credentials" 
    } 
    $response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $body 
    $accessToken = $response.access_token 
    

    2. Initiate Live Response Session

     Bash: Start Live Response Session 
    curl -X POST "https://api.securitycenter.microsoft.com/api/machines/{deviceId}/liveResponse" \ 
    -H "Authorization: Bearer $accessToken" \ 
    -H "Content-Type: application/json" 
    

    3. Upload and Execute a Script Remotely

     PowerShell: Upload a File 
    $sessionId = "YOUR_SESSION_ID" 
    $filePath = "C:\Scripts\investigate.ps1" 
    $uploadUrl = "https://api.securitycenter.microsoft.com/api/machines/{deviceId}/liveResponse/sessions/$sessionId/files"
    
    $headers = @{ 
    "Authorization" = "Bearer $accessToken" 
    "Content-Type" = "application/octet-stream" 
    } 
    Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers -InFile $filePath
    
    Execute the Script 
    $commandUrl = "https://api.securitycenter.microsoft.com/api/machines/{deviceId}/liveResponse/sessions/$sessionId/commands" 
    $body = @{ 
    "Command" = "RunScript" 
    "Input" = "investigate.ps1" 
    } | ConvertTo-Json
    
    Invoke-RestMethod -Uri $commandUrl -Method Post -Headers $headers -Body $body 
    

    4. Error Handling & Logging