Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add json output #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ Based on: https://github.com/thalpius/Microsoft-Defender-for-Identity-Check-Inst

# Usage

Standard Usage
```./check_mdi.py -d <domain>```


JSON Output
```./check_mdi.py -d <domain> -j```


Query government tenants
```./check_mdi.py -d <domain> --gov```
92 changes: 65 additions & 27 deletions check_mdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import dns.resolver
import xml.etree.ElementTree as ET
from urllib.request import urlopen, Request
import json


# Get domains
def get_domains(args):
domain = args.domain
json_out = {}

# Create a valid HTTP request
# Example from: https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxwsadisc/18fe58cd-3761-49da-9e47-84e7b4db36c2
Expand All @@ -24,22 +26,22 @@ def get_domains(args):
xmlns:a="http://www.w3.org/2005/08/addressing"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<a:RequestedServerVersion>Exchange2010</a:RequestedServerVersion>
<a:MessageID>urn:uuid:6389558d-9e05-465e-ade9-aae14c4bcd10</a:MessageID>
<a:Action soap:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation</a:Action>
<a:To soap:mustUnderstand="1">https://autodiscover.byfcxu-dom.extest.microsoft.com/autodiscover/autodiscover.svc</a:To>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</soap:Header>
<soap:Body>
<GetFederationInformationRequestMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover">
<Request>
<Domain>{domain}</Domain>
</Request>
</GetFederationInformationRequestMessage>
</soap:Body>
<soap:Header>
<a:RequestedServerVersion>Exchange2010</a:RequestedServerVersion>
<a:MessageID>urn:uuid:6389558d-9e05-465e-ade9-aae14c4bcd10</a:MessageID>
<a:Action soap:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation</a:Action>
<a:To soap:mustUnderstand="1">https://autodiscover.byfcxu-dom.extest.microsoft.com/autodiscover/autodiscover.svc</a:To>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
</soap:Header>
<soap:Body>
<GetFederationInformationRequestMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover">
<Request>
<Domain>{domain}</Domain>
</Request>
</GetFederationInformationRequestMessage>
</soap:Body>
</soap:Envelope>"""

# Including HTTP headers
Expand All @@ -48,17 +50,26 @@ def get_domains(args):
"User-agent": "AutodiscoverClient"
}

url = "https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc"
if args.gov:
url = "https://autodiscover-s.office365.us/autodiscover/autodiscover.svc"
elif args.cn:
url = "https://autodiscover-s.partner.outlook.cn/autodiscover/autodiscover.svc"

# Perform HTTP request
try:
httprequest = Request(
"https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc", headers=headers, data=body.encode())
url, headers=headers, data=body.encode())

with urlopen(httprequest) as response:
response = response.read().decode()
except Exception:
print("[-] Unable to execute request. Wrong domain?")
if args.json:
print(json.dumps({"error":"Unable to execute request. Wrong domain"},indent=1))
else:
print("[-] Unable to execute request. Wrong domain?")
exit()

#print(response)
# Parse XML response
domains = []

Expand All @@ -67,20 +78,42 @@ def get_domains(args):
if elem.tag == "{http://schemas.microsoft.com/exchange/2010/Autodiscover}Domain":
domains.append(elem.text)

print("\n[+] Domains found:")
print(*domains, sep="\n")
if not args.json:
print("\n[+] Domains found:")
print(*domains, sep="\n")
else:
json_out["domains"] = domains

# Get tenant name
tenant = ""

for domain in domains:
if "onmicrosoft.com" in domain:
tenant = domain.split(".")[0]

print(f"\n[+] Tenant found: \n{tenant}")

if not args.json:
print(f"\n[+] Tenant found: \n{tenant}")
else:
json_out["tenant"] = tenant

# Call check_mdi() with tenant
check_mdi(tenant)
mdi_check = check_mdi(tenant)

if args.json:
if mdi_check:
json_out["mdi"] = True
else:
json_out["mdi"] = False
else:
if mdi_check:
print(f"\n[+] An MDI instance was found for {tenant}!\n")
else:
print(f"\n[-] No MDI instance was found for {tenant}\n")


if args.json:
print(json.dumps(json_out, indent=2))



# Identify MDI usage
Expand All @@ -91,15 +124,20 @@ def check_mdi(tenant):
# Check if MDI instance exists
try:
dns.resolver.resolve(tenant)
print(f"\n[+] An MDI instance was found for {tenant}!\n")
return True
except Exception:
print(f"\n[-] No MDI instance was found for {tenant}\n")

return False

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Enumerates valid Microsoft 365 domains, retrieves tenant name, and checks for MDI instance")
parser.add_argument(
"-d", "--domain", help="input domain name, example format: example.com", required=True)
parser.add_argument(
"-j", "--json", action="store_true", help="output in JSON format", required=False)
parser.add_argument(
"--gov", action="store_true", help="query government tenancy", required=False)
parser.add_argument(
"--cn", action="store_true", help="query chinese tenancy", required=False)
args = parser.parse_args()
get_domains(args)