msAD
msad is a library and command line tool for working with an Active Directory / LDAP server from Unix, Linux and macOS systems.
It supports authentication with user/password and Kerberos, and pagination (it can retrieve more than 2000 objects, a limit of AD).
Features:
- search objects (users, groups, computers, ...) with a raw LDAP filter
- search users by field (name, surname, mail, sAMAccountName, department)
- search groups by name / cn / sAMAccountName / displayName
- search computers by name / dNSHostName / operatingSystem
- search organizational units (OUs) and list their contents
- find inactive users / stale computers (last logon older than N days)
- audit reads: domain info, default password policy, privileged groups
- audit reads: FSMO role owners, domain trusts, AD sites, subnets, site links and servers
- get a single AD site or subnet; find computers by OS or disabled computer accounts
- audit reads: AD-integrated DNS zones and records (decoded A/AAAA/NS/CNAME/PTR/MX/SRV/SOA/TXT)
- audit reads: authorized DHCP servers and domain-based DFS namespaces
- audit reads: GPOs, fine-grained password policies (PSOs)
- list domain controllers, empty groups, and computers grouped by OS
- find contacts, security groups (optionally by scope), and expired/never-expiring accounts
- find risky accounts: password not required, Kerberos pre-auth not required, "sensitive and not delegated"
- find users whose password violates the domain policy
- test AD connectivity (health check)
- get a single user or group
- fetch any entry directly by its DN (resolve
manager/managedBy) - list group members (direct or recursive/nested)
- list a user's groups (direct or recursive/nested)
- check whether a user is a (nested) member of a group
- add/remove members to/from AD groups using DN or sAMAccountName
- change AD passwords
- unlock a locked-out user account (reset
lockoutTimeto 0) - check if a user is disabled, locked, or has an expired/never-expiring password
- decode raw AD attributes (userAccountControl, groupType, FILETIME, SID, GUID)
- LDAP filter escaping to prevent injection
By default, user lookups return a useful attribute set — identity fields plus
manager (the DN of the user's manager), memberOf (direct group DNs),
userAccountControl, accountExpires, lastLogonTimestamp, whenCreated and
all extensionAttribute1-extensionAttribute15 (commonly used for custom/HR
data such as company code, employee type or division).
Group lookups include managedBy (owner DN), member, groupType and
whenCreated. Pass --attributes to override the defaults.
Prerequisites
Python >= 3.11
For Kerberos authentication, install the optional kerberos extra
(msad[kerberos]). It installs a different backend per platform:
- macOS/Linux: the
gssapipackage, a native extension that needs system Kerberos headers to build:- krb5 library and tools (like
kinit, ...) - krb5 dev headers:
libkrb5-devon Debian/Ubuntu,krb5-develon RHEL/Fedora, orkrb5via Homebrew on macOS - a keytab file or
krb5.confconfigured
- krb5 library and tools (like
- Windows: the
winkerberospackage instead — a prebuilt wheel, no system headers or separate Kerberos install needed. It wraps native SSPI directly, so a domain-joined machine logged in as a domain user already has everything it needs.
If you only authenticate with user/password, skip the kerberos extra
entirely — no native build, no system headers needed.
Installation
With uv (recommended)
Install as an isolated tool on your PATH:
uv tool install msad
# with Kerberos support:
uv tool install 'msad[kerberos]'
Run it directly without installing (ephemeral, great for one-off use):
uvx msad --help
# uvx is shorthand for: uv tool run msad
Upgrade or uninstall the installed tool:
uv tool upgrade msad
uv tool uninstall msad
With pipx
pipx install msad
# with Kerberos support:
pipx install 'msad[kerberos]'
Configuration
msad reads a TOML file, $HOME/.msad.toml by default (override with
--config-file <path>). The init command creates that file from a
ready-to-edit template (it will not overwrite an existing file unless you pass
--force):
msad init
# creates ~/.msad.toml (warns if it already exists)
# then edit ~/.msad.toml
The template looks like this (abridged):
[defaults]
# Domain used when --domain is not passed on the command line.
domain = "mydomain"
[domains.mydomain]
host = "dc.example.com"
base = "dc=example,dc=com"
port = 636 # 389 for plain LDAP
use_ssl = true # false for plain LDAP
# Optional: abort an LDAP operation that gets no response within N seconds
# (ldap3 receive_timeout). Omit to keep ldap3's default (no timeout).
# query_timeout = 30
# Omit user/password to authenticate with Kerberos (SASL); get a ticket
# first with `kinit`. Set BOTH to authenticate with user/password:
# user = "svc_account"
# password = "..."
You can declare multiple domains under [domains.<name>] and select one at
runtime with --domain <name>.
Multiple domains
Declare as many domains as you need; each can use its own authentication
(Kerberos or user/password). [defaults].domain is used when --domain is
not given:
[defaults]
domain = "corp"
# Kerberos (SASL): omit user/password, run `kinit` first.
[domains.corp]
host = "dc.corp.example.com"
base = "dc=corp,dc=example,dc=com"
port = 636
use_ssl = true
# A second domain using user/password authentication.
[domains.lab]
host = "dc.lab.example.com"
base = "dc=lab,dc=example,dc=com"
port = 636
use_ssl = true
user = "svc_reader"
password = "s3cr3t"
Then target a domain per command (defaults to [defaults].domain when omitted):
msad user-get matteo # uses the default domain (corp)
msad user-get jdoe --domain lab # runs against the lab domain
msad test-connection --domain lab # check connectivity to a specific domain
Connecting to the Global Catalog
The Global Catalog (GC) listens on port 3268 (plain) / 3269 (SSL). In a
multi-domain forest it replicates a partial attribute set of every
domain, so one query can span the whole forest without chasing referrals —
search with an empty/root base:
[domains.corp-gc]
host = "dc.corp.example.com"
base = "" # empty/root base: search the whole forest via GC
port = 3269
use_ssl = true
- Single-domain forest: no difference from a normal query — there's only one domain to replicate.
- Trusted forests (
msad trusts) are never visible via the GC: trusts enable cross-forest auth, not data federation. Query that forest's own DC/GC directly instead. - Only indexed, commonly-searched attributes are part of the GC's partial set; some less common ones won't come back.
Usage
msad --help
python -m msad --help
For Kerberos authentication, first obtain a ticket:
kinit # or: kinit myaduser
Kerberos ticket per platform. msad's
kerberosextra installs a different backend (see Prerequisites):gssapion macOS/Linux,winkerberoson Windows.
- macOS/Linux:
kinitis required first — the credential cache is empty otherwise (or point at a keytab viaKRB5_CLIENT_KTNAME).- Windows:
winkerberoswraps native SSPI, so a domain-joined machine logged in as a domain user gets SSO automatically — nokinitneeded. Not verified end-to-end against a real AD on Windows by this project — validate it yourself withmsad test-connection/whoami.- Installing
gssapimanually on Windows anyway disables this:ldap3only useswinkerberoswhengssapiis not importable.
Verify connectivity and authentication before running other commands:
# Prints an ok/target/auth/error status; exits non-zero on failure
msad test-connection
Searching
# Raw LDAP filter (all attributes as JSON)
msad search "(samaccountname=matteo)" --out-format json
# Restrict returned attributes
msad search "(cn=redaelli*)" --attributes mail --attributes sAMAccountName --out-format json
# Find users by field (criteria are ANDed; values may contain *)
msad user-search --surname "Rossi" --department "IT"
# Get a single user (exact match on sAMAccountName / UPN / mail / cn)
msad user-get matteo
# Find groups (supports * wildcards)
msad group-search "qlik_*"
# Get a single group
msad group-get qlik_analyzer_users
# Find computers (name / dNSHostName / operatingSystem, values may contain *)
msad computer-search --name "PC0*" --os "Windows Server*"
# Exclude domain controllers from a computer search
msad computer-search --exclude-domain-controllers
# Get a single computer (sAMAccountName with or without trailing $, cn, or FQDN)
msad computer-get PC001
# Find computers by operating system (values may contain *)
msad computers-by-os "Windows Server 2019*"
# Find disabled computer accounts
msad disabled-computers
# Restrict any search to a specific OU subtree with --base
msad user-search --surname "Rossi" --base "OU=Sales,DC=group,DC=example,DC=com"
# Find organizational units (name matches the ou attribute; may contain *)
msad ou-search --name "Sales*"
# Get a single OU by its ou name or full DN
msad ou-get "OU=Sales,DC=group,DC=example,DC=com"
# List everything under an OU (optionally filter by objectClass)
msad ou-contents "OU=Sales,DC=group,DC=example,DC=com"
msad ou-contents "OU=Sales,DC=group,DC=example,DC=com" --object-class user
# Find users who haven't logged in for 90 days (default)
msad inactive-users
# Inactive users in a specific OU, last 30 days
msad inactive-users --days 30 --base "OU=Staff,DC=group,DC=example,DC=com"
# Find computers that haven't logged on in 180 days
msad stale-computers --days 180
# Include accounts that have never logged on (no lastLogonTimestamp)
msad inactive-users --days 90 --include-never
msad stale-computers --days 180 --include-never
# Fetch any entry by its DN (e.g. resolve a user's manager)
msad get-by-dn "CN=Anna Bianchi,OU=Staff,DC=group,DC=example,DC=com"
# Find contacts (cn / mail / displayName, values may contain *)
msad contact-search "anna*"
# Find security-enabled groups, optionally by scope
msad security-groups
msad security-groups --scope global
# Risk: accounts with no password required / no Kerberos pre-auth / not delegated
msad password-not-required
msad kerberos-preauth-not-required
msad not-delegated
# Accounts whose expiration is set to "never", or whose expiration date has passed
msad never-expiring-accounts
msad expired-accounts
Groups and membership
# Direct members of a group
msad group-members qlik_analyzer_users
# All (nested) members
msad group-members qlik_analyzer_users --nested
# A user's groups (direct, or nested)
msad user-groups matteo
msad user-groups matteo --nested
# Is a user a (nested) member of a group? Prints true/false
msad is-member qlik_analyzer_users matteo
# Add / remove a member (by DN or sAMAccountName)
msad group-add-member qlik_analyzer_users matteo
msad group-remove-member qlik_analyzer_users matteo
Auditing (read-only)
# Domain object: security-relevant settings and metadata
msad domain-info
# Default domain password policy (maxPwdAge, minPwdLength, lockout, ...)
msad password-policy
# Users whose password violates the policy (expired, or must change at logon)
msad password-policy-violations
# Well-known privileged groups with member counts
msad privileged-groups
# Include the actual members (optionally expand nested membership)
msad privileged-groups --with-members
msad privileged-groups --with-members --nested
# Network topology (Configuration NC): subnets, site links and servers (DCs)
msad subnets
msad site-links
msad servers
# Get a single site or subnet by name/CIDR
msad get-site Default-First-Site-Name
msad get-subnet 10.20.0.0/16
# AD-integrated DNS: zones, and the (decoded) records of a zone
msad dns-zones
msad dns-records example.com
# Authorized DHCP servers and domain-based DFS namespaces
msad authorized-dhcp-servers
msad dfs-namespaces
Account checks
# Individual checks (print true / false / not found)
msad is-disabled matteo
msad is-locked matteo
msad has-expired-password matteo --max-age 90
msad has-never-expires-password matteo
# Run all checks at once, optionally checking group memberships
msad check-user matteo --group qlik_analyzer_users --group domain_admins
Passwords
# Change a user's password (prompts interactively)
msad change-password matteo
Output formats
Most commands accept --out-format:
jsonl(default): one JSON object per line (JSON Lines / NDJSON)json: a single JSON arraycsv: tab-separated, list values joined with|
Shell completion
msad is built with Typer, which provides
bash/zsh/fish/PowerShell completion out of the box — no extra setup needed
beyond running:
msad --install-completion # auto-detects your shell and installs the script
# then restart your terminal
To inspect or install the script manually instead:
msad --show-completion
Completion covers commands and options (--domain, --base,
--attributes, --out-format, ...) and stays in sync automatically as
commands are added or renamed, since it's generated dynamically from the
CLI definition rather than a static script.
Projects using msad
msad ships type hints and a stable public API (msad.__all__), so it can be
embedded in other tools. Three front ends build on it:
-
mcpAD — a FastMCP server that exposes
msadas read-only MCP tools (search users / groups / computers / OUs, memberships, account status, audit reads) for use by AI agents and LLM clients. It reuses themsadTOML configuration and opens a short-lived connection per tool call. Install withpip install mcpador run withuvx mcpad. -
restAD — a read-only REST interface over
msad, built with FastAPI + Pydantic. It exposes the same search and audit operations as HTTPGETendpoints (with OpenAPI docs at/docs), resolving the connection from themsadTOML file plusMSAD_*environment overrides. Install withpip install restador run withuvx restad. -
graphqlAD — a read-only GraphQL interface over
msad, built with Strawberry + FastAPI. It exposes the same search and audit operations as a single GraphQLQuerytype (no mutations), with an in-browser GraphiQL IDE at/graphql, resolving the connection from themsadTOML file plusMSAD_*environment overrides. Install withpip install graphqlador run withuvx graphqlad.
All three are read-only: no write operation of msad (add/remove member,
change password, unlock account) is reachable through them.
Development
This project uses uv. A Makefile wraps the common
tasks:
make sync # install/refresh the dev environment
make test # run the test suite with coverage
make lint # ruff check + format check + pyrefly (strict)
make format # apply ruff autofixes and formatting
make build # build wheel and sdist into dist/
make publish # upload to the package index (needs credentials)
make clean # remove build artifacts and caches
Equivalent raw commands:
uv sync --group dev
uv run pytest
uv run ruff check src/ tests/
uv run pyrefly check
uv build
The library ships type hints (py.typed) and its public API is declared in
msad.__all__, so it can be imported and type-checked by downstream projects.
License
Copyright © 2021 - 2025 Matteo Redaelli
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Metadata
Release files for msad 0.17.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| msad-0.17.0.tar.gz | 134.9 kB | Without provenance | Details for msad-0.17.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| msad-0.17.0-py3-none-any.whl | Python 3 | none | any | Details for msad-0.17.0-py3-none-any.whl |
Total release size: 201.5 kB
Release files / msad-0.17.0.tar.gz
| Download URL | msad-0.17.0.tar.gz |
|---|---|
| Size | 134.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
98cf9963ece4d9f773b6a22c2a5b6c7ce37bee2c99edd42c56b7abdfb3d52b2f
|
|
BLAKE2b-256 checksum How to use checksums |
8287d9349300ff1f08d0d51664252518c5b38a1f5998ef2b103d05664ed83be2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / msad-0.17.0-py3-none-any.whl
| Download URL | msad-0.17.0-py3-none-any.whl |
|---|---|
| Size | 66.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d4cd369143587e57442359310ebb0e5187b95598cf7fa469c7d9d7b4305a39a7
|
|
BLAKE2b-256 checksum How to use checksums |
9968066d670298051b1310e74aa2884b23e854531c6f040e8c123a085723a175
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for msad 0.17.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.