コード例 #1
0
        //htonl(0x7F000001); // 127.0.0.1
        public void Append(IPEndPoint source, IPEndPoint destination, ProtocolType protocol, byte[] payload, DateTime?timestamp = null)
        {
            PcapItemHeader packetHeader;
            Ethernet       packetEthernet;
            IPv4           packetIP;
            UDP            packetUDP;

            byte[] sourceBytes, destinationBytes;

            byte[] payloadItemHeader, payloadEthernet, payloadIP, payloadUDP, payloadData;
            int    index;

            /* UDP */
            packetUDP  = new UDP((UInt16)source.Port, (UInt16)destination.Port, (UInt16)payload.Length);
            payloadUDP = packetUDP.GetBytes();

            /* ipv4 */
            packetIP  = new IPv4(source.Address, destination.Address, protocol, (UInt16)(payloadUDP.Length + payload.Length));
            payloadIP = packetIP.GetBytes();

            /* ethernet */
            sourceBytes      = source.Address.GetAddressBytes();
            destinationBytes = destination.Address.GetAddressBytes();
            packetEthernet   = new Ethernet(new byte[3] {
                sourceBytes[1], sourceBytes[2], sourceBytes[3]
            }, new byte[3] {
                destinationBytes[1], destinationBytes[2], destinationBytes[3]
            });
            payloadEthernet = packetEthernet.GetBytes();

            /* pcap packet header */
            if (timestamp == null)
            {
                timestamp = DateTime.UtcNow;
            }
            packetHeader      = new PcapItemHeader((DateTime)timestamp, (UInt32)(payloadEthernet.Length + payloadIP.Length + payloadUDP.Length + payload.Length));
            payloadItemHeader = packetHeader.GetBytes();

            payloadData = new byte[payloadItemHeader.Length + payloadEthernet.Length + payloadIP.Length + payloadUDP.Length + payload.Length];

            index = 0;
            payloadItemHeader.CopyTo(payloadData, index);
            index += payloadItemHeader.Length;

            payloadEthernet.CopyTo(payloadData, index);
            index += payloadEthernet.Length;

            payloadIP.CopyTo(payloadData, index);
            index += payloadIP.Length;

            payloadUDP.CopyTo(payloadData, index);
            index += payloadUDP.Length;

            payload.CopyTo(payloadData, index);
            index += payload.Length;

            content.Write(payloadData, 0, payloadData.Length);
        }
コード例 #2
0
        //htonl(0x7F000001); // 127.0.0.1
        public void Append(IPEndPoint source, IPEndPoint destination, ProtocolType protocol, byte[] payload, DateTime?timestamp = null)
        {
            PcapItemHeader packetHeader;
            Ethernet       packetEthernet;
            IPv4           packetIP;

            byte[] sourceBytes, destinationBytes;

            byte[] payloadItemHeader, payloadEthernet, payloadIP, payloadTransport, payloadData;
            int    index;

            switch (protocol)
            {
            case ProtocolType.Udp:
                UDP packetUDP;
                packetUDP        = new UDP((UInt16)source.Port, (UInt16)destination.Port, (UInt16)payload.Length);
                payloadTransport = packetUDP.GetBytes();
                break;

            case ProtocolType.Tcp:
                TCP    packetTCP;
                UInt16 connection1, connection2;

                UInt64 seqAck;
                UInt32 seq, ack;

                connection1 = tcpConnectionId(source, destination);
                connection2 = tcpConnectionId(destination, source);
                if (tcpWindows.ContainsKey(connection1))
                {
                    seqAck = tcpWindows[connection1];
                    seq    = (UInt32)(seqAck >> 32);
                    ack    = (UInt32)seqAck;
                }
                else
                {
                    seq    = 1;
                    ack    = 1;
                    seqAck = ((UInt64)seq << 32) | ack;
                    tcpWindows.Add(connection1, seqAck);
                }

                packetTCP = new TCP((UInt16)source.Port, (UInt16)destination.Port, seq, ack);

                seq   += (UInt32)payload.Length;
                seqAck = ((UInt64)seq << 32) | ack;
                tcpWindows[connection1] = seqAck;

                if (tcpWindows.ContainsKey(connection2))
                {
                    seqAck = tcpWindows[connection2];
                    seq    = (UInt32)(seqAck >> 32);
                    ack    = (UInt32)payload.Length;

                    seqAck = ((UInt64)seq << 32) | ack;
                    tcpWindows[connection2] = seqAck;
                }
                else
                {
                    seq = 1;
                    ack = (UInt32)payload.Length;

                    seqAck = ((UInt64)seq << 32) | ack;
                    tcpWindows.Add(connection2, seqAck);
                }

                payloadTransport = packetTCP.GetBytes();
                break;

            case ProtocolType.IP:
                payloadTransport = new byte[] { };
                break;

            default:
                throw new System.ArgumentException("This protocol is not supported", "protocol");
            }


            /* ipv4 */
            packetIP  = new IPv4(source.Address, destination.Address, protocol, (UInt16)(payloadTransport.Length + payload.Length));
            payloadIP = packetIP.GetBytes();

            /* ethernet */
            sourceBytes      = source.Address.GetAddressBytes();
            destinationBytes = destination.Address.GetAddressBytes();
            packetEthernet   = new Ethernet(new byte[3] {
                sourceBytes[1], sourceBytes[2], sourceBytes[3]
            }, new byte[3] {
                destinationBytes[1], destinationBytes[2], destinationBytes[3]
            });
            payloadEthernet = packetEthernet.GetBytes();

            /* pcap packet header */
            if (timestamp == null)
            {
                timestamp = DateTime.UtcNow;
            }
            packetHeader      = new PcapItemHeader((DateTime)timestamp, (UInt32)(payloadEthernet.Length + payloadIP.Length + payloadTransport.Length + payload.Length));
            payloadItemHeader = packetHeader.GetBytes();

            payloadData = new byte[payloadItemHeader.Length + payloadEthernet.Length + payloadIP.Length + payloadTransport.Length + payload.Length];

            index = 0;
            payloadItemHeader.CopyTo(payloadData, index);
            index += payloadItemHeader.Length;

            payloadEthernet.CopyTo(payloadData, index);
            index += payloadEthernet.Length;

            payloadIP.CopyTo(payloadData, index);
            index += payloadIP.Length;

            payloadTransport.CopyTo(payloadData, index);
            index += payloadTransport.Length;

            payload.CopyTo(payloadData, index);
            index += payload.Length;

            if (payloadData.Length != packetHeader.length + 16)
            {
                throw new Exception("Error of PCap. Possible bug");
            }

            content.Write(payloadData, 0, payloadData.Length);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            PcapHeader     fileHeader;
            PcapItemHeader packetHeader;
            Ethernet       packetEthernet;
            IPv4           packetIP;
            UDP            packetUDP;
            UInt16         destPort;

            byte[]   payloadFileHeader, payloadItemHeader, payloadEthernet, payloadIP, payloadUDP, payloadData;
            DateTime captureTime;

            byte[] payload;
            int    index;

            string source, dest;

            if (args.Length < 2)
            {
                usage();
                return;
            }

            if (args.Length > 2)
            {
                if (!UInt16.TryParse(args[2], out destPort))
                {
                    Console.WriteLine(String.Format("Error: The value {0} is not a valid port number!", args[2]));
                    usage();
                    return;
                }
            }
            else
            {
                destPort = 0;
            }

            source = args[0];
            dest   = args[1];



            if (!File.Exists(source))
            {
                Console.WriteLine(String.Format("Error: File {0} does not exists!", source));
            }
            if (File.Exists(dest))
            {
                Console.WriteLine(String.Format("Error: File {0} already exists! Please delete it first.", dest));
            }

            try
            {
                /* load payload */
                payloadData = File.ReadAllBytes(source);
                captureTime = File.GetLastWriteTimeUtc(source);

                /* UDP */
                packetUDP  = new UDP(destPort, (UInt16)payloadData.Length);
                payloadUDP = packetUDP.GetBytes();

                /* ipv4 */
                packetIP  = new IPv4((UInt16)(payloadUDP.Length + payloadData.Length));
                payloadIP = packetIP.GetBytes();

                /* ethernet */
                packetEthernet  = new Ethernet(1);
                payloadEthernet = packetEthernet.GetBytes();

                /* pcap packet header */
                packetHeader      = new PcapItemHeader(captureTime, (UInt32)(payloadEthernet.Length + payloadIP.Length + payloadUDP.Length + payloadData.Length));
                payloadItemHeader = packetHeader.GetBytes();

                /* pcap file header */
                fileHeader        = new PcapHeader(1);
                payloadFileHeader = fileHeader.GetBytes();

                payload = new byte[payloadFileHeader.Length + payloadItemHeader.Length + payloadEthernet.Length + payloadIP.Length + payloadUDP.Length + payloadData.Length];

                index = 0;
                payloadFileHeader.CopyTo(payload, index);
                index += payloadFileHeader.Length;

                payloadItemHeader.CopyTo(payload, index);
                index += payloadItemHeader.Length;

                payloadEthernet.CopyTo(payload, index);
                index += payloadEthernet.Length;

                payloadIP.CopyTo(payload, index);
                index += payloadIP.Length;

                payloadUDP.CopyTo(payload, index);
                index += payloadUDP.Length;

                payloadData.CopyTo(payload, index);
                index += payloadData.Length;

                File.WriteAllBytes(dest, payload);
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Error: Unable to convert '{0}' to '{1}'!", source, dest));
                Console.WriteLine(e.ToString());
                return;
            }

            Console.WriteLine(String.Format("'{0}' -> '{1}'", source, dest));
        }