MS Intune Integration
#Log Integration procedure Introduction:
This document outlines the process for integrating MS Intune with Logstash to collect and process Mobile Device Management (MDM) logs. Prerequisites
• Client credentials for MS Intune API access:
• Client ID
• Client Secret
• Tenant ID
#Configuration:
• We are fetching the device logs through below Graph URLs.
https://graph.microsoft.com/v1.0/deviceManagement/managedDevices https://graph.microsoft.com/v1.0/deviceManagement/managedDeviceOverview https://graph.microsoft.com/v1.0/deviceManagement/detectedApps
Update the script with your MS intune Client details, Logstash details (host and port).
Code Snippet (Generate access token, Log Fetching and Processing Script)
Python import requests import socket import json import os
Replace these values with your own
tenant_id = '98e37d3b-c3b5-4dad-916d-e0c5e9a8e9c4' client_id = '910f5b05-59a3-4261-a568-b8c497cd1f53' client_secret = 'YxC8Q~JPWv0fFqA_e1ih18cPC6DzeJFPFWtzbdm4' local_host = '127.0.0.1' local_port = 12225 # You can change this to the desired port processed_device_names_file = 'processed_ids.txt' processed_device_overview_file = 'processed_overview.txt' processed_detected_apps_file = 'processed_apps.txt'
URLs to fetch logs from
graph_urls = [
'https://graph.microsoft.com/v1.0/deviceManagement/managedDeviceOverview'
]
graph_urls = [ 'https://graph.microsoft.com/v1.0/deviceManagement/managedDevices', 'https://graph.microsoft.com/v1.0/deviceManagement/managedDeviceOverview', 'https://graph.microsoft.com/v1.0/deviceManagement/detectedApps' ]
Get access token
def get_access_token(tenant_id, client_id, client_secret): token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" token_data = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret, 'scope': 'https://graph.microsoft.com/.default' }
Fetch logs
def fetch_intune_logs(access_token, url): headers = { 'Authorization': f'Bearer {access_token}' }
Send logs to localhost port
def send_logs_to_localhost(logs, host, port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) logs_str = json.dumps(logs) s.sendall(logs_str.encode('utf-8')) s.close()
Read all processed data from a file
def read_processed_data(file): if os.path.exists(file): with open(file, 'r') as f: try: return json.load(f) except json.JSONDecodeError: return [] return []
Write new processed data to a file
def write_processed_data(file, data): with open(file, 'w') as f: json.dump(data, f)
def main(): try: access_token = get_access_token(tenant_id, client_id, client_secret) all_logs = [] processed_data = []
if name == "main":
main()
Setup a Logstash pipeline to fetch the logs at input port 12225/tcp and follow further steps.
Last updated