示例#1
0
文件: Core.cs 项目: AKllX/Gemini
 private static void startSNMP()
 {
     //Config. SNMP
     //Communities Comuns: cr e public
     SnmpSharpNet.OctetString community = new SnmpSharpNet.OctetString("public");
     SNMPParameters = new AgentParameters(community)
     {
         Version = SnmpVersion.Ver2
     };
 }
示例#2
0
文件: Core.cs 项目: lailasr/Gemini
        public static void Start()
        {
            SnmpSharpNet.OctetString community = new SnmpSharpNet.OctetString("cr");
            SNMPParameters         = new AgentParameters(community);
            SNMPParameters.Version = SnmpVersion.Ver2;
            if (SNMPParameters != null)
            {
                Console.WriteLine("Parâmetros iniciados com sucesso");
            }

            DNP3Manager = DNP3ManagerFactory.CreateManager(4, new PrintingLogAdapter());
        }
示例#3
0
 /// <summary>Copy constructor.</summary>
 /// <param name="second">The object to copy</param>
 public IpAddress(OctetString second) : this(second.GetData())
 {
 }
示例#4
0
        /// <summary>
        /// Build SNMP discovery response packet.
        /// </summary>
        /// <remarks>
        /// Manager application has to be able to respond to discovery requests to be able to handle
        /// SNMPv3 INFORM notifications.
        ///
        /// In an INFORM packet, engineId value is set to the manager stations id (unlike all other requests
        /// where agent is the authoritative SNMP engine). For the agent to discover appropriate manager engine
        /// id, boots and time values (required for authentication and privacy packet handling), manager has to
        /// be able to respond to the discovery request.
        /// </remarks>
        /// <param name="messageId">Message id from the received discovery packet</param>
        /// <param name="requestId">Request id from the received discovery packets Pdu</param>
        /// <param name="engineId">Local engine id</param>
        /// <param name="engineBoots">Number of times local SNMP engine has been restarted</param>
        /// <param name="engineTime">Time since the engine was started in seconds</param>
        /// <param name="unknownEngineIdCount">Number of discovery packets received by the local SNMP engine</param>
        /// <returns>SNMP v3 packet properly formatted as a response to a discovery request</returns>
        public static SnmpV3Packet DiscoveryResponse(Int32 messageId, Int32 requestId, OctetString engineId, Int32 engineBoots, Int32 engineTime, Int32 unknownEngineIdCount)
        {
            SnmpV3Packet packet = new SnmpV3Packet();

            packet.Pdu.Type      = PduType.Report;
            packet.Pdu.RequestId = requestId;
            packet.Pdu.VbList.Add(SnmpConstants.usmStatsUnknownEngineIDs, new Integer32(unknownEngineIdCount));
            // discovery response is a report packet. We don't want to receive reports about a report
            packet.MsgFlags.Reportable = false;
            packet.SetEngineId(engineId);
            packet.MessageId       = messageId;
            packet.USM.EngineBoots = engineBoots;
            packet.USM.EngineTime  = engineTime;
            return(packet);
        }
示例#5
0
        /// <summary>
        /// Encode SNMP version 3 packet
        /// </summary>
        /// <param name="authKey">Authentication key (not password)</param>
        /// <param name="privKey">Privacy key (not password)</param>
        /// <remarks>
        /// Before encoding the packet into a byte array you need to ensure all required information is
        /// set. Examples of required information is request type, Vbs (Oid + values pairs), USM settings including
        /// SecretName, authentication method and secret (if needed), privacy method and secret (if needed), etc.
        /// </remarks>
        /// <returns>Byte array BER encoded SNMP packet.</returns>
        public byte[] Encode(byte[] authKey, byte[] privKey)
        {
            MutableByte buffer = new MutableByte();
            // encode the global message data sequence header information

            MutableByte globalMessageData = new MutableByte();

            // if message id is 0 then generate a new, random message id
            if (_messageId.Value == 0)
            {
                Random rand = new Random();
                _messageId.Value = rand.Next(1, Int32.MaxValue);
            }

            // encode message id
            _messageId.Encode(globalMessageData);

            // encode max message size
            _maxMessageSize.Encode(globalMessageData);

            // message flags
            MsgFlags.Encode(globalMessageData);

            // security model code
            _securityModel.Value = USM.Type;
            _securityModel.Encode(globalMessageData);

            // add global message data to the main buffer
            // encode sequence header and add data
            AsnType.BuildHeader(buffer, SnmpConstants.SMI_SEQUENCE, globalMessageData.Length);
            buffer.Append(globalMessageData);

            MutableByte packetHeader = new MutableByte(buffer);

            // before going down this road, check if this is a discovery packet
            OctetString savedUserName  = new OctetString();
            bool        privacy        = MsgFlags.Privacy;
            bool        authentication = MsgFlags.Authentication;
            bool        reportable     = MsgFlags.Reportable;

            if (USM.EngineId.Length <= 0)
            {
                // save USM settings prior to encoding a Discovery packet
                savedUserName.Set(USM.SecurityName);
                USM.SecurityName.Reset(); // delete security name for discovery packets
                MsgFlags.Authentication = false;
                MsgFlags.Privacy        = false;
                MsgFlags.Reportable     = true;
            }

            USM.Encode(buffer);

            if (USM.EngineId.Length <= 0)
            {
                // restore saved USM values
                USM.SecurityName.Set(savedUserName);
                MsgFlags.Authentication = authentication;
                MsgFlags.Privacy        = privacy;
                MsgFlags.Reportable     = reportable;
            }

            // Check if privacy encryption is required
            MutableByte encodedPdu = new MutableByte();

            if (MsgFlags.Privacy && USM.EngineId.Length > 0)
            {
                IPrivacyProtocol privacyProtocol = PrivacyProtocol.GetInstance(USM.Privacy);
                if (privacyProtocol == null)
                {
                    throw new SnmpException(SnmpException.UnsupportedPrivacyProtocol, "Specified privacy protocol is not supported.");
                }

                // Get BER encoded ScopedPdu
                MutableByte unencryptedPdu = new MutableByte();
                ScopedPdu.Encode(unencryptedPdu);

                // we have to expand the key
                IAuthenticationDigest auth = Authentication.GetInstance(USM.Authentication);
                if (auth == null)
                {
                    throw new SnmpException(SnmpException.UnsupportedNoAuthPriv, "Invalid authentication protocol. noAuthPriv mode not supported.");
                }

                byte[] encryptedBuffer = privacyProtocol.Encrypt(unencryptedPdu, 0, unencryptedPdu.Length, privKey, USM.EngineBoots, USM.EngineTime, out byte[] privacyParameters, auth);

                USM.PrivacyParameters.Set(privacyParameters);
                OctetString encryptedOctetString = new OctetString(encryptedBuffer);
                encryptedOctetString.Encode(encodedPdu);
                // now redo packet encoding
                buffer.Reset();
                buffer.Set(packetHeader);
                USM.Encode(buffer);
                int preEncodedLength = encodedPdu.Length;
                buffer.Append(encodedPdu);
                if (_maxMessageSize.Value != 0)
                {
                    // verify compliance with maximum message size
                    if ((encodedPdu.Length - preEncodedLength) > _maxMessageSize)
                    {
                        throw new SnmpException(SnmpException.MaximumMessageSizeExceeded, "ScopedPdu exceeds maximum message size.");
                    }
                }
            }
            else
            {
                ScopedPdu.Encode(encodedPdu);
                buffer.Append(encodedPdu);
            }

            base.Encode(buffer);

            if (MsgFlags.Authentication && USM.EngineId.Length > 0)
            {
                USM.Authenticate(authKey, ref buffer);
                // Now re-encode the packet with the authentication information
                USM.Encode(packetHeader);
                packetHeader.Append(encodedPdu);
                base.Encode(packetHeader);
                buffer = packetHeader;
            }
            return(buffer);
        }
示例#6
0
        /// <summary>
        /// Decode SNMP version 3 packet. This method will perform authentication check and decode privacy protected <see cref="ScopedPdu"/>. This method will
        /// not check for the timeliness of the packet, correct engine boot value or engine id because it does not have a reference to the engine time prior to this call.
        /// </summary>
        /// <param name="berBuffer">BER encoded SNMP version 3 packet buffer</param>
        /// <param name="length">Buffer length</param>
        /// <param name="authKey">Authentication key (not password)</param>
        /// <param name="privKey">Privacy key (not password)</param>
        public int Decode(byte[] berBuffer, int length, byte[] authKey, byte[] privKey)
        {
            MutableByte buffer = new MutableByte(berBuffer, length);

            int offset = 0;

            // let base class parse first sequence and SNMP version number
            offset = base.Decode(buffer, length);

            // check for correct SNMP protocol version
            if (_protocolVersion != (int)SnmpVersion.Ver3)
            {
                throw new SnmpInvalidVersionException("Expecting SNMP version 3.");
            }

            // now grab the global message data sequence header information
            byte asnType = AsnType.ParseHeader(buffer, ref offset, out int len);

            if (asnType != SnmpConstants.SMI_SEQUENCE)
            {
                throw new SnmpDecodingException("Invalid sequence type in global message data sequence.");
            }

            // check that packet size can accommodate the length specified in the header
            if (len > (buffer.Length - offset))
            {
                throw new OverflowException("Packet is too small to contain the data described in the header.");
            }

            // retrieve message id
            offset = _messageId.Decode(buffer, offset);

            // max message size
            offset = _maxMessageSize.Decode(buffer, offset);

            // message flags
            offset = MsgFlags.Decode(buffer, offset);

            // verify that a valid authentication/privacy configuration is present in the packet
            if (MsgFlags.Authentication == false && MsgFlags.Privacy == true)
            {
                throw new SnmpException(SnmpException.UnsupportedNoAuthPriv, "SNMP version 3 noAuthPriv security combination is not supported.");
            }

            // security model code
            offset = _securityModel.Decode(buffer, offset);

            // we only support USM. code = 0x03
            if (_securityModel.Value != USM.Type)
            {
                throw new SnmpException(SnmpException.UnsupportedSecurityModel, "Class only support SNMP Version 3 User Security Model.");
            }

            // parse user security model
            offset = USM.Decode(buffer, offset);

            // Authenticate message if authentication flag is set and packet is not a discovery packet
            if (MsgFlags.Authentication && USM.EngineId.Length > 0)
            {
                // Authenticate packet
                if (USM.AuthenticationParameters.Length != 12)
                {
                    throw new SnmpAuthenticationException("Invalid authentication parameter field length.");
                }

                if (!USM.IsAuthentic(authKey, buffer))
                {
                    throw new SnmpAuthenticationException("Authentication of the incoming packet failed.");
                }
            }

            // Decode ScopedPdu if it is privacy protected and packet is not a discovery packet
            if (MsgFlags.Privacy && USM.EngineId.Length > 0)
            {
                IPrivacyProtocol privacyProtocol = PrivacyProtocol.GetInstance(USM.Privacy);
                if (privacyProtocol == null)
                {
                    throw new SnmpException(SnmpException.UnsupportedPrivacyProtocol, "Privacy protocol requested is not supported.");
                }
                if (USM.PrivacyParameters.Length != privacyProtocol.PrivacyParametersLength)
                {
                    throw new SnmpException(SnmpException.InvalidPrivacyParameterLength, "Invalid privacy parameters field length.");
                }

                // Initialize a temporary OctetString class to hold encrypted ScopedPdu
                OctetString encryptedScopedPdu = new OctetString();
                offset = encryptedScopedPdu.Decode(buffer, offset);

                // decode encrypted packet
                byte[] decryptedScopedPdu = privacyProtocol.Decrypt(encryptedScopedPdu, 0, encryptedScopedPdu.Length, privKey, USM.EngineBoots, USM.EngineTime, USM.PrivacyParameters);
                int    tempOffset         = 0;
                offset = ScopedPdu.Decode(decryptedScopedPdu, tempOffset);
            }
            else
            {
                offset = ScopedPdu.Decode(buffer, offset);
            }

            return(offset);
        }