PySNMP Crypto Extensions
pysnmpcrypto provides strong cryptographic support for the PySNMP library, enhancing security for SNMPv3 operations. It integrates seamlessly with PySNMP's security framework by leveraging PyCryptodome. The current version is 0.1.0, with releases typically tied to updates in its core dependencies, PySNMP and PyCryptodome.
Warnings
- gotcha pysnmpcrypto is an extension for PySNMP. It requires `pysnmp` to be installed and specifically requires PySNMP version `4.4.0` or higher to function correctly and provide access to the extended cryptographic algorithms.
- gotcha The library depends on `pycryptodome` for its cryptographic operations. If `pycryptodome` is not installed, pysnmpcrypto will not be able to provide the strong encryption algorithms, potentially leading to errors when attempting to use protocols like SHA512 or AES256.
- gotcha pysnmpcrypto primarily enhances SNMPv3 security. While PySNMP supports other SNMP versions, the strong authentication and privacy protocols provided by this library are specific to SNMPv3's User-based Security Model (USM).
- gotcha Python version compatibility: pysnmpcrypto requires Python `>=3.8` and `<4.0`. Ensure your environment matches this requirement, as PySNMP itself might have broader compatibility, but this extension is more specific.
- breaking As a relatively new library (v0.1.0), future major versions might introduce breaking changes to how it integrates with PySNMP or to its exposed (albeit limited) API. Keep an eye on the GitHub repository for release notes.
Install
-
pip install pysnmpcrypto
Imports
- Side-effect import
import pysnmpcrypto.hl.api
Quickstart
from pysnmp.hlapi import *
import pysnmpcrypto.hl.api # This import enables strong crypto support
# 1. SNMP Engine and User Definition
snmp_engine = SnmpEngine()
# Define an SNMPv3 user with strong authentication (SHA512) and privacy (AES256).
# These protocols are enabled by 'pysnmpcrypto'.
# In a real scenario, 'authKey' and 'privKey' should be strong, unique values.
auth_key = b'myAuthPassword123'
priv_key = b'myPrivPassword456'
user_data = UsmUserData(
userName='testUser',
authKey=auth_key,
privKey=priv_key,
authProtocol=usmHMACSHA512AuthProtocol, # Requires pysnmpcrypto
privProtocol=usmAES256PrivProtocol # Requires pysnmpcrypto
)
# 2. Target Agent Definition (using a public test server for demonstration)
# Note: 'testUser' must be configured on the target agent with matching keys/protocols.
target = UdpTransportTarget(('demo.snmplabs.com', 161), timeout=1, retries=0)
# 3. Context Data
context = ContextData()
# 4. Object to query (e.g., sysDescr.0)
oid = ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
# 5. Perform the SNMP GET operation
print(f"Attempting to query {oid} using SNMPv3 with SHA512/AES256 (requires pysnmpcrypto)...")
error_indication, error_status, error_index, var_binds = next(
getCmd(snmp_engine, user_data, target, context, oid)
)
# 6. Process the response
if error_indication:
print(f"Error: {error_indication}")
elif error_status:
print(f"Error: {error_status.prettyPrint()} at {error_index and var_binds[int(error_index) - 1][0] or '?'}")
else:
for var_bind in var_binds:
print(f"{var_bind.prettyPrint()}")
# The key takeaway is that usmHMACSHA512AuthProtocol and usmAES256PrivProtocol
# become available for use after 'import pysnmpcrypto.hl.api'.
# A successful connection depends on the target agent supporting the user and protocols.