2

前のスレッドに続いて、UDP リスナーと C# の BitArray を使用して、Sonicwall ファイアウォールから IPFIX データをデコードすることに近づいているようです。

BitArray にデータを取得するようになりましたが、意味がありません。配列にバイナリが表示されると思っていましたが、値が大きく異なります。たとえば、8 ~ 10 文字の正と負の文字があり、配列のカウントは常に異なります。

ビットを取得するために使用しているコードの主要部分は次のとおりです。

byte[] bytes = listener.Receive(ref _myEndPoint);

BitArray bitarray = new BitArray(bytes);

IPFIX データをデコードする方法や、役立つポインターを知っている人はいますか?

前もって感謝します、

ジェームズ


Chris 提供のコードからの出力:

byte[] バイト = {0x00, 0x0A, 0x04, 0xB4, 0x4D, 0xAE, 0x8F, 0xF4, 0x00, 0x6D, 0x8F, 0xC7, 0x16, 0x2B, 0xFC, 0x00, 0x01, 0x01, 0x04, 0xA4, 0x4 0x8F、0xD2、0x8D、0xCC、0xC8、0x20、0x00、0x26、0x99、0xD4、0x5F、0xD7、0x30、0x37、0xA6、0xD6、0xBE、0xB9、0x51、0x90、0xF3、0x4、0x4、0x4、0x4 0x84、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x03、0x00、0x00、0x00、0x02、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x38、0x69、0x00、0x50、0x00、0x00、0x00、0x06、0x00、0x00、0x07、0xb3、0x00、0x00、0x00、0x04、0x00、0x00、0x03、0x42、0x00、0x00、0x00、0x02、0x000、0x00 0x00、0x03、0xC7、0x00、0x00、0x00、0x01、0x00、0x00、0x01、0x72、0x4D、0xAE、0x8F、0xD2、0x4D、0xAE、0x8F、0xF4、0x00、0x0、0x0、0x0、0x0、0x31 0x10、0x8F、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x82、0x00、0x00、0x82、 0x00、0x00、0x01、0x4D、0xAE、0x8F、0xEE、0x8E、0x42、0xF9、0xA0、0x30、0x37、0xA6、0xD6、0xBE、0xB9、0x00、0x50、0x56、0x90、0x10 0x42、0xfe、0x51、0x90、0xf3、0x46、0x51、0x90、0xf3、0x41、0x51、0x90、0xf3、0x41、0x00、0x00、0x00、0x02、0x00、0x00、0x00、0x00、0x00、0x01、0x01、0x01、0x01、0x01、0x01、0x01、0x01 0x00、0x00、0x00、0x00、0x00、0xc5、0x85、0x00、0x50、0x00、0x00、0x00、0x18、0x00、0x00、0x09、0x6e、0x00、0x00、0x00、0x29、0x00、0x00、0xdf、0xe1 0x00、0x00、0x00、0x0B、0x00、0x00、0x04、0x86、0x00、0x00、0x00、0x14、0x00、0x00、0x6E、0x65、0x4D、0xAE、0x8F、0xEE、0x4D、0x4F、0x0F、0x0F 0x31、0x06、0x00、0x00、0x00、0x10、0x93、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x82、0x00、0x00、0x01、0x4d、0xae、0x8f、0xf3、0x8e、0x5b、0x17、0xa0、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x0a、0x00、0x01、0x55、0xc0、0xa8、0x6f、0x37、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x01、0x10、0x00 0x0a、0x01、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x0a、0xf8、0x23、0x8c、0x00、0x00、0x00、0x01、0x00、0x00、0x00、0x00、0x00、0x00、0x00 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x01、0x00、0x00、0x00、0x30、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x4d、0xae、0x8f、0xf3、0x8f、0xf3、 0x4D、0xAE、0x8F、0xF4、0x00、0x01、0x06、0x00、0x00、0x00、0xC0、0x2E、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x44、0x00、0x00、0x00、0x4D、0xAE、0x8F、0xBE、0x8E、0xA0、0x53、0xA0、0xA7、0xA7、0xA7 0x2D, 0x01, 0x30, 0x37, 0xA6, 0xD6, 0xBE, 0xB9, 0x0A, 0x00, 0x01, 0x5A, 0x4A, 0xC9, 0x75, 0xF7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00、0x00、0x01、0x00、0x00、0x00、0x02、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0xc3、0x8f、0x00、0x50、0x00、0x00、0x06、0x00、0x00、0x06、0x00、0x00、 0x02、0xa4、0x00、0x00、0x00、0x04、0x00、0x00、0x04、0x07、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x4D、0xAE、0x8F、0xBE、0x4D、0xAE、0x8F、0xF4、0x00、0x03、0x06、0x00、0x00、0x00、0xC0、0x17、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x82、0x00、0x00、0x01、0x4D、0xAE、0x8、D、0x8、0x8F、0 0x20、0x1C、0xC1、0xDE、0xA7、0x2D、0x01、0x30、0x37、0xA6、0xD6、0xBE、0xB9、0x0A、0x00、0x01、0x5A、0x5D、0xB8、0xDD、0x85、0x0、0x0、0x0 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x01、0x00、0x00、0x00、0x02、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0xc3、0x77、0x00、0x50、0x00、0x77 0x00、0x00、0x04、0x00、0x00、0x00、0xbe、0x00、0x00、0x00、0x02、0x00、0x00、0x00、0x00、0x62、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x00、0x00、0x4D、0xAE、0x8F、0xB6、0x4D、0xAE、0x8F、0xF4、0x00、0x03、0x06、0x00、0x00、0x00、0xc0、0x17、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x82、0x00、0x00、0x00、0x4d、0xae、 0x8F、0xB6、0x8E、0x58、0xD0、0xE0、0x1C、0xC1、0xDE、0xA7、0x2D、0x01、0x30、0x37、0xA6、0xD6、0xBEDD、0xB9、0x0A、0x00、0x01、0x8、0x5、0x5 0xe8、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x01、0x00、0x00、0x00、0x02、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0xc3、0x72、0x00、0x50、0x00、0x00、0x00、0x0d、0x00、0x00、0x04、0x86、0x00、0x00、0x00、0x17、0x00、0x00、0x77、0x77、0x00、0x00、0x00、0x00、0x00 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x4D、0xAE、0x8F、0xB6、0x4D、0xAE、0x8F、0xf4、0x00、0x03、0x06、0x00、0x00、0x00、0xc0、0x17、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x84、0x00、0x00、0x84、 0x00、0x00、0x01、0x4D、0xAE、0x8F、0xB5、0x8E、0x95、0xA4、0x60、0x1C、0xC1、0xDE、0xA7、0x2D、0x01、0x30、0x37、0xA6、0xB6、0xA9、0xB9 0x01、0x5a、0x40、0x98、0xd0、0x42、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x01、0x00、0x00、0x00、0x02、0x00、0x00、0x00 0x00、0x00、0x00、0x00、0x00、0xc3、0x6e、0x00、0x50、0x00、0x00、0x00、0x07、0x00、0x00、0x05、0x7e、0x00、0x00、0x00、0x03、0x00、0x00、0x02、0x88 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x4D、0xAE、0x8f、0xb5、0x4d、0xae、0x8f、0xf4、0x00、0x03、0x06、0x00、0x00、0x00、0xc0、0x17、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x84、0x00、0x00、0x01、0x4D、0xAE、0x8F、0xB4、0x8E、0xDA、0xAB、0xE0、0x1C、0xC1、0xDE、0xA7、0x2D、0x30、0x30、0x30、0x30 0xd6、0xbe、0xb9、0x0a、0x00、0x01、0x5a、0xd1、0x55、0x8f、0x60、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x01、0x00、0x00、0x00、0x01、0x00、 0x00、0x02、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0xc3、0x5f、0x00、0x50、0x00、0x00、0x00、0x05、0x00、0x00、0x02、0x54、0x00、0x00、0x00、 0x03、0x00、0x00、0x01、0x11、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x4D、0xAE、0x8F、0xB4、0x4D、0xAE、0x8F、0xF4、0x00、0x03、0x06、0x00、0x00、0x00、0xC0、0x17、0x00、0x0、0x0、0x0、0x0、0x00 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、 0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x82、0x00、0x00、0x01};

4

1 に答える 1

4

受信しているバイト パケットの 1 つのダンプを提供していただけますか? 配列を以下のコードに渡すと、出力ウィンドウにダンプされます。これは、生のパケットを確認するのに役立ちます。

    private static string PrintByteArray(byte[] bytes){
        return "byte[] bytes = {0x" + BitConverter.ToString(bytes).Replace("-", ", 0x") + "};";
    }

@Paul Sasik がデータの解析に関する彼の回答で何について話していたか理解できますか? 構造は、彼がリンクしたRFC 仕様にあります。パケットの最初の 2 バイト (16 ビット) がバージョン番号になります。私は仕様を読んでいませんが、バイトはリトルエンディアンとビッグエンディアンの 2 つの異なる方法で書き込むことができます。したがって、仕様によると、最初の 2 バイトは0x00 0x0aorのようなものでなければなりません0x0a 0x00

編集

ビット対バイトの観点から考えないでください。後者は前者の 8 つのコレクションです。

仕様によると、最初の 16 ビット (2 バイト) は、0x000aお持ちのものと一致するバージョン番号です。次に、次の 2 バイトがメッセージ全体のバイト単位の長さであると言います (実際にはオクテットと同じです)。あなたのデータには0x04b41204バイト配列の長さを正確に表す 10 進数があります。次のフィールドは、1970 年 1 月 1 日 (別名 Unix/Posix 時間) からのエクスポート時間を秒単位で表す 4 バイトのフィールドです。このサイトによると、10 進数で0x4dae8ff4はです。以下のコードを使用して、メッセージ ヘッダーを解析できます。うまくいけば、これで生データの解析に取り掛かることができます。1,303,285,748Wed, 20 Apr 2011 07:49:08 GMT

    private static IPFIX ParseMessageHeader(byte[] bytes)
    {
        IPFIX ret = new IPFIX();
        ret.Version = ToUInt16BigEndian(bytes, 0);
        ret.Length = ToUInt16BigEndian(bytes, 2);
        ret.ExportTime = (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(ToUInt32BigEndian(bytes, 4));
        ret.SequenceNumber = ToUInt32BigEndian(bytes, 8);
        ret.ObservationDomainID = ToUInt32BigEndian(bytes, 12);
        ret.Sets = new List<Set>();
        Int32 CurOctet = 16;
        Set S;
        while (true)
        {
            S = new Set();
            S.SetId = ToUInt16BigEndian(bytes, CurOctet);
            S.Length = ToUInt16BigEndian(bytes, CurOctet + 2);
            S.data = bytes.Skip(CurOctet).Take(S.Length).ToArray();
            ret.Sets.Add(S);
            CurOctet += S.Length;
            if (CurOctet >= ret.Length)
            {
                break;
            }
        }
        
        return ret;
    }

    //These two functions are from here http://snipplr.com/view/15179/adapt-systembitconverter-to-handle-big-endian-network-byte-ordering-in-order-to-create-number-types-from-bytes-and-viceversa/
    //BitConverter.ToUInt16 would parse the results in "little endian" order so 0x000a would actually be parsed as 0x0a00 and give you 2,560 instead of 10.
    //The spec says that everything should be in "big endian" (also known as "network order"
    public static UInt16 ToUInt16BigEndian(byte[] value, int startIndex)
    {
        return System.BitConverter.ToUInt16(value.Reverse().ToArray(), value.Length - sizeof(UInt16) - startIndex);
    }
    public static UInt32 ToUInt32BigEndian(byte[] value, int startIndex)
    {
        return System.BitConverter.ToUInt32(value.Reverse().ToArray(), value.Length - sizeof(UInt32) - startIndex);
    }
    struct IPFIX
    {
        public UInt16 Version;
        public UInt16 Length;
        public DateTime ExportTime;
        public UInt32 SequenceNumber;
        public UInt32 ObservationDomainID;
        public List<Set> Sets;
    }
    struct Set
    {
        public UInt16 SetId;
        public UInt16 Length;
        public byte[] data;
        public SetType SetType
        {
            get
            {
                if (SetId == 2) return SetType.TemplateSet;
                if (SetId == 3) return SetType.OptionTemplate;
                if (SetId > 255) return SetType.DataSet;
                throw new ArgumentOutOfRangeException("SetId", "SetId not in expected range of 2, 3 or >255");
            }
        }

    }
    enum SetType { TemplateSet, OptionTemplate, DataSet };

注意すべきことの1つは、「エンディアンネス」です。Windows は既定で値をリトル エンディアンとして解析しますが、ここでのすべての値は仕様ごとにビッグ エンディアンとして解析する必要があります。通常の BitConvertor クラスはここでは機能しないため、必要に応じて機能するように調整する 2 つのヘルパー関数が上にあります。intまた、通常の s の代わりに and を使用してUInt16いることがわかりますUInt32。それも仕様次第。anInt16は負の数と正の数の両方を保持しますが、 anUInt16にはどちらの概念もありません (ただし、通常は正と見なされます)。

于 2011-04-19T16:39:01.397 に答える