Registry / auth-security / ntlm-auth

ntlm-auth

JSON →
library1.5.0pypypi✓ verified 23d ago

ntlm-auth is a Python library designed to create NTLM authentication structures, supporting NTLMv1 and NTLMv2, MIC for message integrity, channel binding tokens, and message signing/sealing. The current version is 1.5.0, with releases occurring periodically to add features and improve compatibility, though not on a strict schedule.

pip install ntlm-auth
INSTALL
IMPORT
SIG · NTLM-AUTH
N
ntlm-auth
auth-securitypythonv1.5.0
Install
1.6s avg
Import
19ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.021s · 18MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.018s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

NtlmContext
from ntlm_auth.ntlm import NtlmContext
from ntlm_auth.ntlm import Ntlm
The 'Ntlm' class was deprecated in v1.2.0 in favor of 'NtlmContext' for a more generic and flexible interface beyond HTTP authentication.

This quickstart demonstrates the three-step NTLM authentication handshake using the `NtlmContext` class: creating a Type 1 Negotiate message, processing a (simulated) Type 2 Challenge message from the server, and generating a Type 3 Authenticate message. In a real application, the Type 2 message bytes would be received from the server.

from ntlm_auth.ntlm import NtlmContext import base64 # Replace with your actual username and password username = "DOMAIN\\username" # or just "username" for local accounts password = "your_password" # 1. Client creates a Type 1 Negotiate message context = NtlmContext(username=username, password=password) type1_message = context.create_negotiate_message() print(f"Type 1 (Negotiate) message: {base64.b64encode(type1_message).decode()}") # 2. Server (simulated) responds with a Type 2 Challenge message # In a real scenario, this would come from the server # Example Type 2 challenge bytes (replace with actual server response) # This is a dummy example, actual challenge bytes would be different. # You can often capture this from network traffic or server logs. dummy_type2_challenge_bytes = ( b'\x4e\x54\x4c\x4d\x53\x53\x50\x00\x02\x00\x00\x00\x08\x00\x08\x00\x38\x00\x00\x00\x01\x82\x88\xe2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x01\x0c\x00\x00\x00\x00\x0f') # Client processes the Type 2 message # The context state is updated with server details. type2_processed = context.create_challenge_message(dummy_type2_challenge_bytes) print(f"Type 2 (Challenge) processed, context updated.") # 3. Client creates a Type 3 Authenticate message type3_message = context.create_authenticate_message() print(f"Type 3 (Authenticate) message: {base64.b64encode(type3_message).decode()}") # This Type 3 message would then be sent to the server for authentication.
Debug
Known issues
breakingThe `Ntlm` class (e.g., `ntlm_auth.ntlm.Ntlm`) was deprecated in version 1.2.0 in favor of `NtlmContext`. `NtlmContext` provides a more flexible and generic interface for NTLM authentication beyond just HTTP.
fix
Migrate your code to use `ntlm_auth.ntlm.NtlmContext` instead of `ntlm_auth.ntlm.Ntlm`. Review the `NtlmContext` documentation for new methods and properties.
affects: >=1.2.0
gotchaFor optimal performance, especially when dealing with numerous or large NTLM authentication flows, install the optional `cryptography` dependency. Without it, RC4 cipher calls will be significantly slower.
fix
Install the library with the `ntlm_context` extra: `pip install ntlm-auth[ntlm_context]`.
affects: All versions
gotchaPython 3.3 support was dropped in version 1.2.0. Additionally, specific Python 3.0, 3.1, and 3.2 versions are explicitly not supported. Ensure your Python environment meets the `requires_python` criteria for the installed version.
fix
Upgrade your Python environment to a supported version (e.g., Python 3.6+ is generally safe, but always check current PyPI metadata for `requires_python`).
affects: >=1.2.0
gotchaVersions prior to 1.5.0 do not include features like `mic_present` property, `sign`, `verify`, and `reset_rc4_state` functions on the `NtlmContext`. If your application requires these advanced NTLM capabilities, ensure you are on version 1.5.0 or later.
fix
Upgrade to `ntlm-auth` version 1.5.0 or newer to access the latest NTLM functionality.
affects: <1.5.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'ntlm_auth'
The 'ntlm-auth' library or one of its required sub-modules is not installed in the Python environment being used.
fix
Install the library using pip: `pip install ntlm-auth`
ImportError: No module named cryptography
The 'cryptography' package, a transitive dependency required by 'ntlm-auth' (often via 'pyspnego' which 'ntlm-auth' depends on), is missing from your Python environment. This usually happens when 'ntlm-auth' or 'requests-ntlm' is installed, but the 'cryptography' dependency failed to install correctly or was removed.
fix
Install the missing dependency: `pip install cryptography`
ntlm: requested auth method is ntlm, but requests_ntlm is not installed
This error typically occurs in contexts like Ansible when NTLM authentication is specified, but the 'requests-ntlm' Python package (which internally uses 'ntlm-auth') is not installed or not accessible by the Python interpreter that the application is running with.
fix
Install the 'requests-ntlm' package: `pip install requests-ntlm`
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url:
This HTTP status code indicates that the NTLM authentication handshake failed, often due to incorrect credentials (username, password, domain), misconfigured server-side NTLM settings, or issues with NTLM versions (e.g., server expecting NTLMv2 but client attempts NTLMv1).
fix
Verify the username, password, and domain are correct. Ensure the server supports the NTLM compatibility level being used by 'ntlm-auth' (default is NTLMv2). Check server logs for more specific NTLM authentication failure details.
Upgrade
Version history
1.5.0latest on PyPI · released Jun 16, 2020
Audit
Dependencies
cryptographyoptionalOptional dependency for faster RC4 cipher calls, significantly improving performance for encryption/decryption operations.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
ntlm-auth — pip install ntlm-auth · libregistry