Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SnmpV1PDU"/> struct.
 /// </summary>
 /// <param name="pduType">Type of the pdu.</param>
 /// <param name="enterprise">The enterprise.</param>
 /// <param name="agentAddress">The agent address.</param>
 /// <param name="genericV1Trap">The generic v1 trap.</param>
 /// <param name="specificTrap">The specific trap.</param>
 /// <param name="timeStamp">The time stamp.</param>
 /// <param name="varBinds">The variable binds.</param>
 public SnmpV1PDU(PduType pduType, ObjectIdentifier enterprise, IPAddress agentAddress, GenericTrap genericV1Trap, int specificTrap, uint timeStamp, VarBind[] varBinds)
 {
     PduType       = pduType;
     VarBinds      = new ReadOnlyCollection <VarBind>((VarBind[])varBinds.Clone());
     Enterprise    = enterprise;
     AgentAddress  = agentAddress;
     GenericV1Trap = genericV1Trap;
     SpecificTrap  = specificTrap;
     TimeStamp     = timeStamp;
 }
Example #2
0
        /// <summary>
        /// Converts the Asn.1 encoded byte array to SNMP Datagram.
        /// </summary>
        /// <param name="byteSegment">The asn.1 encoded bytes.</param>
        /// <param name="timestamp">The timestamp when the packet has been received.</param>
        /// <param name="sourceIpAddress">The source IP address of the packet.</param>
        /// <returns>
        /// SnmpPacket
        /// </returns>
        /// <exception cref="System.ArgumentNullException">bytes</exception>
        /// <exception cref="System.IO.InvalidDataException">Snmp Version V3 not supported</exception>
        public static SnmpDatagram ToSnmpDatagram(
            this ArraySegment <byte> byteSegment,
            DateTimeOffset timestamp,
            string sourceIpAddress)
        {
            var bytes = byteSegment.Array;

            if (bytes == null || bytes.Length == 0)
            {
                throw new ArgumentNullException("byteSegment");
            }

            int offset = byteSegment.Offset;
            int length;

            offset = bytes.NextValueLength(offset, -1, -1, -1, out length);
            offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
            SnmpVersion snmpVersion = (SnmpVersion)bytes.ReadInteger(offset, length);

            offset += length;

            if (snmpVersion == SnmpVersion.V3)
            {
                throw new InvalidDataException("Snmp Version V3 not supported");
            }

            offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.OctetString, out length);
            string community = bytes.ReadOctetString(offset, length);

            offset += length;

            PduType pduType = (PduType)(bytes[offset++] & 0x1F);

            offset = bytes.ReadLength(offset, out length);
            if (snmpVersion == SnmpVersion.V1 && pduType == PduType.Trap)
            {
                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.ObjectIdentifier, out length);
                ObjectIdentifier oid = new ObjectIdentifier(bytes.ReadOids(offset, length));
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1SnmpTag.IpAddress, out length);
                IPAddress ipAddress = bytes.ReadIpAddress(offset);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                GenericTrap genericTrap = (GenericTrap)bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                int specificTrap = bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1SnmpTag.TimeTicks, out length);
                uint timeStamp = bytes.ReadUnsignedInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length);
                VarBind[] varBinds = bytes.ReadVarBinds(offset, length);

                return(new SnmpDatagramV1(
                           timestamp,
                           sourceIpAddress,
                           new SnmpHeader(snmpVersion, community),
                           varBinds));
            }
            else
            {
                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                int requestId = bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                SnmpErrorStatus errorStatus = (SnmpErrorStatus)bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                int errorIndex = bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length);
                VarBind[] varBinds = bytes.ReadVarBinds(offset, length);

                return(new SnmpDatagramV2C(
                           timestamp,
                           sourceIpAddress,
                           new SnmpHeader(snmpVersion, community),
                           varBinds,
                           pduType,
                           requestId,
                           errorStatus,
                           errorIndex));
            }
        }
Example #3
0
        private void ParsePacket(DateTime receivedDate, IPEndPoint receivedEP, Byte[] packet)
        {
            int commlength, miblength;

            if (packet[0] == 0xff)
            {
                Console.Error.WriteLine("Invalid Packet received from {0}", receivedEP.ToString());
                return;
            }

            //Só é valido para versão 1 do SNMP
            Int32 zeroPos = Encoding.UTF8.GetString(packet).IndexOf("\0");

            if (zeroPos > 7)
            {
                Console.Error.WriteLine("Invalid Packet rceeived from {0}", receivedEP.ToString());
                return;
            }

            using (MemoryStream stm = new MemoryStream(packet, zeroPos + 1, packet.Length - zeroPos - 1))
                using (BinaryReader pData = new BinaryReader(stm))
                {
                    datatype   = (DataType)pData.ReadByte();
                    commlength = Convert.ToInt16(pData.ReadByte());
                    community  = Encoding.UTF8.GetString(pData.ReadBytes(commlength));
                    pData.ReadByte(); //i have no idea
                    miblength = Convert.ToInt16(pData.ReadByte());

                    switch (datatype)
                    {
                    case DataType.trap:   //Trap PDU Item 4.1.6.  The Trap-PDU RFC 1157
                        //https://docs.microsoft.com/pt-br/windows/desktop/SecCertEnroll/about-object-identifier
                        pData.ReadByte(); //i have no idea
                        Int32 oIDLen = Convert.ToInt16(pData.ReadByte());
                        enterprise = OidByteArrayToString(pData.ReadBytes(oIDLen));

                        pData.ReadByte(); //i have no idea
                        Int32 agentAddrLen = Convert.ToInt16(pData.ReadByte());

                        agentAddr = new IPAddress(pData.ReadBytes(agentAddrLen));

                        pData.ReadByte(); //i have no idea
                        pData.ReadByte(); //i have no idea

                        genericTrap = (GenericTrap)pData.ReadByte();

                        pData.ReadByte(); //i have no idea
                        pData.ReadByte(); //i have no idea

                        specificTrap = Convert.ToInt32(pData.ReadByte());

                        //VarBindList

                        pData.ReadBytes(5);

                        while (pData.BaseStream.Position < pData.BaseStream.Length - 2)
                        {
                            pData.ReadByte(); //i have no idea
                            Int32 varBindLen = Convert.ToInt16(pData.ReadByte());
                            pData.ReadByte(); //i have no idea

                            oIDLen = Convert.ToInt16(pData.ReadByte());
                            String varBindOID = OidByteArrayToString(pData.ReadBytes(oIDLen));

                            pData.ReadByte(); //i have no idea

                            Int32  valueLen = Convert.ToInt16(pData.ReadByte());
                            Byte[] bValue   = pData.ReadBytes(valueLen);
                            String value    = Encoding.UTF8.GetString(bValue);

                            varBindList.Add(new SNMPTrapVarBind(varBindOID, value));
                        }

                        break;

                    default:
#if DEBUG
                        Console.Error.WriteLine("DataType não reconhecido: {0}", datatype);
#endif
                        break;
                    }
                }
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SnmpDatagram"/> class.
 /// </summary>
 /// <param name="pduType">Type of the pdu.</param>
 /// <param name="snmpVersion">The SNMP version.</param>
 /// <param name="community">The community.</param>
 /// <param name="enterprise">The enterprise.</param>
 /// <param name="agentAddress">The agent address.</param>
 /// <param name="genericV1Trap">The generic v1 trap.</param>
 /// <param name="specificTrap">The specific trap.</param>
 /// <param name="timeStamp">The time stamp.</param>
 /// <param name="varBinds">The variable binds.</param>
 public SnmpDatagram(PduType pduType, SnmpVersion snmpVersion, string community, ObjectIdentifier enterprise, IPAddress agentAddress, GenericTrap genericV1Trap, int specificTrap, uint timeStamp, VarBind[] varBinds)
 {
     Header = new SnmpHeader(snmpVersion, community);
     PduV1  = new SnmpV1PDU(pduType, enterprise, agentAddress, genericV1Trap, specificTrap, timeStamp, varBinds);
     PduV2c = default(SnmpV2cPDU);
 }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SnmpDatagram"/> class.
 /// </summary>
 /// <param name="pduType">Type of the pdu.</param>
 /// <param name="snmpVersion">The SNMP version.</param>
 /// <param name="community">The community.</param>
 /// <param name="enterprise">The enterprise.</param>
 /// <param name="agentAddress">The agent address.</param>
 /// <param name="genericV1Trap">The generic v1 trap.</param>
 /// <param name="specificTrap">The specific trap.</param>
 /// <param name="timeStamp">The time stamp.</param>
 /// <param name="varBinds">The variable binds.</param>
 public SnmpDatagram(PduType pduType, SnmpVersion snmpVersion, string community, ObjectIdentifier enterprise, IPAddress agentAddress, GenericTrap genericV1Trap, int specificTrap, uint timeStamp, VarBind[] varBinds)
 {
     Header = new SnmpHeader(snmpVersion, community);
     PduV1 = new SnmpV1PDU(pduType, enterprise, agentAddress, genericV1Trap, specificTrap, timeStamp, varBinds);
     PduV2c = default(SnmpV2cPDU);
 }