From 5034d382bd0550bf6fcc3c3ee7b2fcd20e7da7a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Gro=C3=9F?= Date: Wed, 12 Feb 2025 11:03:32 +0100 Subject: [PATCH] Added option to set the tenant id for the request --- main.py | 3 ++- src/auth.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 0cc5e5f..c536222 100644 --- a/main.py +++ b/main.py @@ -15,13 +15,14 @@ def main(): help="Output file to save JSON data. Writes all JSON collected from calling the Graph API for more info.") parser.add_argument("--auth-mode", type=str, choices=["device_code", "azure_sdk"], default="device_code", help="Authentication method: 'device_code' (default) or 'azure_sdk'.") + parser.add_argument("--tenant-id", type=str, help="Isert a Tenant Id for the Tenant that you wish to access", default="common") args = parser.parse_args() print_logo() print("[*] Authenticating to Azure...") - access_token = authenticate_to_azure(auth_mode=args.auth_mode) + access_token = authenticate_to_azure(auth_mode=args.auth_mode, tenant_id=args.tenant_id) if not access_token: print( diff --git a/src/auth.py b/src/auth.py index e6a0eb5..74fee64 100644 --- a/src/auth.py +++ b/src/auth.py @@ -5,11 +5,11 @@ GRAPH_RESOURCE = "https://graph.microsoft.com" GRAPH_SCOPE = GRAPH_RESOURCE + "/.default" -DEVICE_CODE_URL = "https://login.microsoftonline.com/common/oauth2/devicecode?api-version=1.0" + TOKEN_URL = "https://login.microsoftonline.com/common/oauth2/token?api-version=1.0" -def authenticate_with_device_code(): +def authenticate_with_device_code(tenant_id="common"): """ Perform the device code flow to authenticate to Azure and obtain an access token. """ @@ -19,7 +19,7 @@ def authenticate_with_device_code(): "resource": GRAPH_RESOURCE } - device_code_response = requests.post(DEVICE_CODE_URL, data=payload) + device_code_response = requests.post("https://login.microsoftonline.com/"+tenant_id+"/oauth2/devicecode?api-version=1.0", data=payload) device_code_data = device_code_response.json() print(Fore.YELLOW + "[!] To authenticate, visit " + Fore.CYAN + "https://microsoft.com/devicelogin" + @@ -73,7 +73,7 @@ def authenticate_with_azure_sdk(): return None -def authenticate_to_azure(auth_mode="device_code"): +def authenticate_to_azure(auth_mode="device_code", tenant_id="common"): """ Authenticate to Azure using the selected method. @@ -89,4 +89,4 @@ def authenticate_to_azure(auth_mode="device_code"): "[-] Azure SDK authentication failed. Falling back to device code authentication...") print("[*] Attempting authentication using device code flow...") - return authenticate_with_device_code() + return authenticate_with_device_code(tenant_id)