Trouble with SNMP V3 Creating a Server and Client (SharpSnmpLib)

562 views Asked by At

I am using SharpSnmpLib to create an snmp server and client. I have got V1 and V2 working with the code below but am having trouble with V3 as it is returning a Malformed message.

My server creates a listener per device. This is my server:

GetMessageHandler handler = new GetMessageHandler();
GetBulkMessageHandler bulkMessageHandler = new GetBulkMessageHandler();

HandlerMapping hm = new HandlerMapping("v1", "GET", handler);
HandlerMapping hm2 = new HandlerMapping("v1", "GETNEXT", handler);
HandlerMapping hm3 = new HandlerMapping("v2", "GET", handler);
HandlerMapping hm4 = new HandlerMapping("v2", "GETNEXT", handler);
HandlerMapping hm5 = new HandlerMapping("v2, v3", "GETBULK", handler);
HandlerMapping hm6 = new HandlerMapping("v3", "GETBULK", bulkMessageHandler);

List<HandlerMapping> hmList = new List<HandlerMapping> {hm, hm2, hm3, hm4, hm5, hm6};

List <IMembershipProvider> memberships = new List<IMembershipProvider>();

MessageHandlerFactory mhf = new MessageHandlerFactory(hmList.ToArray());

Version1MembershipProvider v1 = new Version1MembershipProvider(new OctetString(community), new OctetString(community));
Version2MembershipProvider v2 = new Version2MembershipProvider(new OctetString(community), new OctetString(community));
Version3MembershipProvider v3 = new Version3MembershipProvider();

memberships.Add(v1);
memberships.Add(v2);
memberships.Add(v3);

ComposedMembershipProvider membershipProviders = new ComposedMembershipProvider(memberships.ToArray());

SnmpApplicationFactory factory = new SnmpApplicationFactory(store, membershipProviders, mhf);

EngineGroup eg = new EngineGroup();

Listener l = new Listener();

l.MessageReceived += l_MessageReceived;
l.ExceptionRaised += l_ExceptionRaised;

engine = new SnmpEngine(factory, l, eg);

engine.ExceptionRaised += engine_ExceptionRaised;

engine.Listener.ClearBindings();

engine.Listener.AddBinding(new System.Net.IPEndPoint("127.0.0.1", 161));

engine.Start();

This is my client:

private IPrivacyProvider CreateAuthProvider()
{
    Levels securityLevel = Levels.Authentication  | Levels.Privacy | Levels.Reportable;
    
    IAuthenticationProvider auth = MD5AuthenticationProvider(new OctetString("abc1234"));
    
    IPrivacyProvider privAuth;
    
    if ((securityLevel & Levels.Privacy) == Levels.Privacy)
    {
        if (DESPrivacyProvider.IsSupported)
        {
            privAuth = new DESPrivacyProvider(new OctetString("abc1234"), auth);
        }
        else
        {
            privAuth = null;
        }
    }
    else
    {
        privAuth = new DefaultPrivacyProvider(auth);
    }

    return privAuth;
}


IPrivacyProvider privAuth;

privAuth = CreateAuthProvider();

Discovery discoveryAuth = Messenger.GetNextDiscovery(SnmpType.GetBulkRequestPdu);
ReportMessage reportMessgae = discoveryAuth.GetResponse(config.Timeout, new IPEndPoint(127.0.0.1, 161));

var serverEndPoint = new IPEndPoint(127.0.0.1, 161);

GetBulkRequestMessage message = new GetBulkRequestMessage(VersionCode.V3, 
   0, 
   0, 
   new OctetString("newUser"),
   0, 
   10, 
   new ObjectIdentifier("1.3.6.1.4.1.37805.1.1.0.1"), 
   privAuth, 
   Messenger.MaxMessageSize, 
   reportMessgae);
ISnmpMessage response = message.GetResponse(10000, serverEndPoint);

if (response.Pdu().ErrorStatus.ToInt32() != 0)
{
   throw ErrorException.Create(
       "Error in response of SNMP GET Bulk Request for V3",
       address,
       response);
}

It would be great if someone could explain why this works and I can clearly get V1 and V2 data but am not getting any V3 data. It looks like the connection is alive when I debug for V3 but I'm not doing something correctly (maybe decrypting?)

1

There are 1 answers

0
Marthinus Elliott On

Just play around with the providers, or try match what the snmpd config has.

Code that worked for me.

OctetString authPassword = new OctetString(thePassword);
OctetString prvPassword = new OctetString(thePassword);            
MD5AuthenticationProvider md5Providor = new MD5AuthenticationProvider(authPassword);
IPrivacyProvider privacyProvider = new AESPrivacyProvider(prvPassword, md5Providor);

Remember to update your "Registry" with the username and privacy options if your not using the built in 'GetResponse' from the sharpsnmplib

var registry = new UserRegistry();
if (request.Version == VersionCode.V3)
{
    registry.Add(request.Parameters.UserName, request.Privacy);
}

I have my own implementation of getresponse, so I was getting "Malformed PDU" because I didnt update registry