Skip to content

Unable to get device information using SNMP V3, an error occurred "Incoming message could not be authenticated!" #122

@bensonwang186

Description

@bensonwang186

I used SNMP V3 to get device information. When I used it, an error occurred "Incoming message could not be authenticated!"
My code:

import asyncio
import threading
from puresnmp import V1, V2C, V3, PyWrapper, Client, Auth, Priv

result_list = []

MAX_WORKS = 255
TIMEOUT = 1  # second
RETRIES = 1

V1_COMMUNITY = 'public'
V3_USERNAME = 'test'
V3_AUTH_KEY = b'aaaaaaaaaaaaaaaa'
V3_PRIV_KEY = b'bbbbbbbbbbbbbbbb'
V3_AUTH_METHOD = 'md5'  # md5、sha1
V3_PRIV_METHOD = 'aes'  # aes、des

SNMP_V1 = 'SNMP v1'
SNMP_V3 = 'SNMP v3'

OID_sysDescr = '.1.3.6.1.2.1.1.1.0'
OID_sysObjectID = '.1.3.6.1.2.1.1.2.0'
OID_upsIdentManufacturer = '.1.3.6.1.2.1.33.1.1.1.0'
OID_upsIdentMode = '.1.3.6.1.2.1.33.1.1.2.0'

lock = threading.Lock()


def get_device_info(ip, snmp_version):
    oids = [OID_sysDescr, OID_sysObjectID, OID_upsIdentManufacturer, OID_upsIdentMode]
    between_callback(ip, oids, snmp_version)


async def get_snmp_oid(ip, oids, snmp_version):
    output = (len(oids) + 1) * [None]
    credentials = None

    if snmp_version == SNMP_V1:
        credentials = V2C(V1_COMMUNITY)

    elif snmp_version == SNMP_V3:
        credentials = V3(
            username=V3_USERNAME,
            auth=Auth(V3_AUTH_KEY, V3_AUTH_METHOD),  # md5、sha1
            priv=Priv(V3_PRIV_KEY, V3_PRIV_METHOD)  # aes、des
        )

    client = Client(ip=ip, credentials=credentials)
    client.configure(timeout=TIMEOUT, retries=RETRIES)
    pyw_client = PyWrapper(client)

    for i in range(len(oids)):
        try:
            output[i + 1] = await pyw_client.get(oids[i])
        except Exception as e:
            continue

    allNone = all(element is None for element in output)
    if not allNone:
        output[0] = ip
        with lock:
            result_list.append(output)

    for re in result_list:
        print(re)


def between_callback(ip, oids, snmp_version):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    loop.run_until_complete(get_snmp_oid(ip, oids, snmp_version))
    loop.close()


if __name__ == '__main__':
    get_device_info('192.168.xxx.xxx', SNMP_V3)

so I went to find the error (puresnmp_plugins\security\usm.py) and found that it occurred in the "verify_authentication" faction.
Because "is_authentic" will return False, causing me to be unable to get device information.image

I tried to modify the program to make it work, so I forced "is_authentic" to True,
When my AUTH_KEY and PRIV_KEY are both correct, I can successfully obtain device information through SNMP V3.
If my AUTH_KEY or PRIV_KEY is typed incorrectly on purpose, I will still be able to determine the error and be unable to get device information.
image

The following are my questions:

  1. Is there really a problem with "verify_authentication" faction or Are there any mistakes in my code?
  2. Can I run it according to the modification method I mentioned above?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions