PySNMP Crypto Extensions

0.1.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to enable and use strong cryptographic protocols (like SHA512 for authentication and AES256 for privacy) for SNMPv3 within PySNMP by simply importing `pysnmpcrypto.hl.api`. It sets up an SNMPv3 user with these protocols and attempts to perform a GET operation against a public test agent. For this to fully succeed, the target agent must be configured with a matching user and supported security levels.

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.

view raw JSON →