Exemplo n.º 1
0
        /// <summary>
        /// Add a packet to this send queue. The PcapHeader defines the packet length.
        /// </summary>
        /// <param name="packet">The packet bytes to add</param>
        /// <param name="pcapHdr">The pcap header of the packet</param>
        /// <returns>True if success, else false</returns>
        internal bool AddInternal(byte[] packet, PcapHeader pcapHdr)
        {
            if (m_queue == IntPtr.Zero)
            {
                throw new PcapException("Can't add packet, this queue is disposed");
            }

            // the header defines the size to send
            if (pcapHdr.CaptureLength > packet.Length)
            {
                var error = string.Format("pcapHdr.CaptureLength of {0} > packet.Length {1}",
                                          pcapHdr.CaptureLength, packet.Length);
                throw new System.InvalidOperationException(error);
            }

            //Marshal packet
            IntPtr pktPtr;

            pktPtr = Marshal.AllocHGlobal(packet.Length);
            Marshal.Copy(packet, 0, pktPtr, packet.Length);

            //Marshal header
            IntPtr hdrPtr = pcapHdr.MarshalToIntPtr();

            int res = SafeNativeMethods.pcap_sendqueue_queue(m_queue, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);

            return(res != -1);
        }
Exemplo n.º 2
0
        private void loadPcapContent(BinaryReader binaryReader, bool dontList)
        {
            PcapHeader pcapHeader = PcapHeader.read(binaryReader);

            while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
            {
                curPacket++;

                if (binaryReader.BaseStream.Length - binaryReader.BaseStream.Position < 16)
                {
                    //MessageBox.Show("Stream cut short (packet " + curPacket + "), stopping read: " + (binaryReader.BaseStream.Length - binaryReader.BaseStream.Position));
                    break;
                }

                PcapRecordHeader recordHeader = PcapRecordHeader.read(binaryReader);

                if (recordHeader.inclLen > 50000)
                {
                    //MessageBox.Show("Enormous packet (packet " + curPacket + "), stopping read: " + recordHeader.inclLen);
                    break;
                }

                // Make sure there's enough room for an ethernet header
                if (recordHeader.inclLen < 14)
                {
                    binaryReader.BaseStream.Position += recordHeader.inclLen;
                    continue;
                }

                if (readPacketRecordData(binaryReader, recordHeader.inclLen, recordHeader.tsSec, curPacket, dontList) == 2)
                {
                    break;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet to add</param>
        /// <param name="seconds">The 'seconds' part of the packet's timestamp</param>
        /// <param name="microseconds">The 'microseconds' part of the packet's timestamp</param>
        /// <returns>True if success, else false</returns>
        public bool Add(byte[] packet, int seconds, int microseconds)
        {
            var header = new PcapHeader((uint)seconds, (uint)microseconds,
                                        (uint)packet.Length, (uint)packet.Length);

            return(this.Add(packet, header));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet bytes to add</param>
        /// <returns>True if success, else false</returns>
        public bool Add(byte[] packet)
        {
            PcapHeader hdr = new PcapHeader();

            hdr.CaptureLength = (uint)packet.Length;
            return(this.AddInternal(packet, hdr));
        }
Exemplo n.º 5
0
 /// <summary>
 ///  should be overriden by upper classes
 /// </summary>
 public virtual void OnOffsetChanged()
 {
     if (PcapHeader != null)
     {
         PcapHeader = new PcapHeader(PcapHeader.Seconds, PcapHeader.MicroSeconds, _bytes.Length, _bytes.Length);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet to add</param>
        /// <returns>True if success, else false</returns>
        public bool Add(RawCapture packet)
        {
            var data    = packet.Data;
            var timeval = packet.Timeval;
            var header  = new PcapHeader((uint)timeval.Seconds, (uint)timeval.MicroSeconds,
                                         (uint)data.Length, (uint)data.Length);

            return(this.AddInternal(data, header));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet to add</param>
        /// <returns>True if success, else false</returns>
        public bool Add(Kavprot.Packets.RawPacket packet)
        {
            var data    = packet.Data;
            var timeval = packet.Timeval;
            var header  = new PcapHeader(timeval.Seconds, timeval.MicroSeconds,
                                         (uint)data.Length, (uint)data.Length);

            return(this.AddInternal(data, header));
        }
Exemplo n.º 8
0
    public static PcapHeader read(BinaryReader binaryReader)
    {
        PcapHeader newObj = new PcapHeader();

        newObj.magicNumber  = binaryReader.ReadUInt32();
        newObj.versionMajor = binaryReader.ReadUInt16();
        newObj.versionMinor = binaryReader.ReadUInt16();
        newObj.thisZone     = binaryReader.ReadUInt32();
        newObj.sigFigs      = binaryReader.ReadUInt32();
        newObj.snapLen      = binaryReader.ReadUInt32();
        newObj.network      = binaryReader.ReadUInt32();
        return(newObj);
    }
Exemplo n.º 9
0
        private static List <PacketRecord> loadPcapContent(BinaryReader binaryReader, ref bool abort)
        {
            List <PacketRecord> results = new List <PacketRecord>();

            /*PcapHeader pcapHeader = */
            PcapHeader.read(binaryReader);

            int curPacket = 0;

            while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
            {
                if (abort)
                {
                    break;
                }

                curPacket++;

                if (binaryReader.BaseStream.Length - binaryReader.BaseStream.Position < 16)
                {
                    //MessageBox.Show("Stream cut short (packet " + curPacket + "), stopping read: " + (binaryReader.BaseStream.Length - binaryReader.BaseStream.Position));
                    break;
                }

                PcapRecordHeader recordHeader = PcapRecordHeader.read(binaryReader);

                if (recordHeader.inclLen > 50000)
                {
                    //MessageBox.Show("Enormous packet (packet " + curPacket + "), stopping read: " + recordHeader.inclLen);
                    break;
                }

                // Make sure there's enough room for an ethernet header
                if (recordHeader.inclLen < 14)
                {
                    binaryReader.BaseStream.Position += recordHeader.inclLen;
                    continue;
                }

                var packetRecord = readPacketRecordData(binaryReader, recordHeader.inclLen, recordHeader.tsSec, curPacket);

                if (packetRecord == null)
                {
                    break;
                }

                results.Add(packetRecord);
            }

            return(results);
        }
Exemplo n.º 10
0
        public PcapFile(string filename)
        {
            byte[] initData;

            this.filename = filename;
            content       = new MemoryStream();

            if (File.Exists(this.filename))
            {
                initData = File.ReadAllBytes(filename);
                content.Write(initData, 0, initData.Length);
            }
            else
            {
                /* pcap file header */
                PcapHeader fileHeader;
                fileHeader = new PcapHeader(1);
                initData   = fileHeader.GetBytes();
                content.Write(initData, 0, initData.Length);
                Save();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Prints the time and length of each received packet
        /// </summary>
        private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var time = e.Packet.Timeval.Date;
            var len  = e.Packet.Data.Length;
            var aa   = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);

            if (aa.PayloadPacket is IPv4Packet iPPacket)
            {
                var header = new PcapHeader((uint)e.Packet.Timeval.Seconds, (uint)e.Packet.Timeval.MicroSeconds,
                                            (uint)e.Packet.Data.Length, (uint)e.Packet.Data.Length);
                if (iPPacket.Protocol == PacketDotNet.ProtocolType.Tcp)
                {
                    var tcpPacket = (iPPacket.PayloadPacket as TcpPacket).PayloadData;
                    for (int i = 0; i < tcpPacket.Length; i++)
                    {
                        if (tcpPacket[i] > 31 && tcpPacket[i] < 127)
                        {
                            Console.Write(System.Text.Encoding.UTF8.GetString(BitConverter.GetBytes(tcpPacket[i])));
                        }
                    }
                }
                Console.WriteLine("\r\n");
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Add a packet to this send queue.
 /// </summary>
 /// <param name="packet">The packet bytes to add</param>
 /// <param name="pcapHdr">The pcap header of the packet</param>
 /// <returns>True if success, else false</returns>
 internal bool Add(byte[] packet, PcapHeader pcapHdr)
 {
     return(this.AddInternal(packet, pcapHdr));
 }
Exemplo n.º 13
0
 /// <summary>
 ///  should be overriden by upper classes
 /// </summary>
 public virtual void OnOffsetChanged()
 {
     if(PcapHeader!=null)
     {
         PcapHeader = new PcapHeader(PcapHeader.Seconds,
                                     PcapHeader.MicroSeconds,
                                     (uint)_bytes.Length,
                                     (uint)_bytes.Length);
     }
 }
Exemplo n.º 14
0
        private static List <PacketRecord> loadPcapPacketRecords(BinaryReader binaryReader, bool asMessages, ref bool abort)
        {
            List <PacketRecord> results = new List <PacketRecord>();

            /*PcapHeader pcapHeader = */
            PcapHeader.read(binaryReader);

            int curPacket = 0;

            Dictionary <ulong, PacketRecord> incompletePacketMap = new Dictionary <ulong, PacketRecord>();

            while (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
            {
                if (abort)
                {
                    break;
                }

                PcapRecordHeader recordHeader;
                try
                {
                    recordHeader = readPcapRecordHeader(binaryReader, curPacket);

                    if (recordHeader == null)
                    {
                        continue;
                    }
                }
                catch (InvalidDataException e)
                {
                    break;
                }

                long packetStartPos = binaryReader.BaseStream.Position;

                try
                {
                    if (asMessages)
                    {
                        if (!readMessageData(binaryReader, recordHeader.inclLen, recordHeader.tsSec, results, incompletePacketMap))
                        {
                            break;
                        }
                    }
                    else
                    {
                        var packetRecord = readPacketData(binaryReader, recordHeader.inclLen, recordHeader.tsSec, curPacket);

                        if (packetRecord == null)
                        {
                            break;
                        }

                        results.Add(packetRecord);
                    }

                    curPacket++;
                }
                catch (Exception e)
                {
                    binaryReader.BaseStream.Position += recordHeader.inclLen - (binaryReader.BaseStream.Position - packetStartPos);
                }
            }

            return(results);
        }
Exemplo n.º 15
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));
        }