Ejemplo n.º 1
0
        public static void PacketReceived(PacketInfo packetInfo, ref byte[] data /*, ref ServerCommand serverResponse*/)
        {
            Packet.Directions direction;
            int ID;

            TotalSize += packetInfo.Size;

            switch (packetInfo.FunctionID)
            {
            case Functions.CODE_RECV:
            case Functions.CODE_RECVFROM:
            case Functions.CODE_WS2RECV:
            case Functions.CODE_WS2RECVFROM:
            case Functions.CODE_WSARECV:
            case Functions.CODE_WSARECVFROM:
            case Functions.CODE_PR_RECV:
            case Functions.CODE_PR_READ:
            case Functions.CODE_SSLDECRYPTPACKET:
            case Functions.CODE_DECRYPTMESSAGE:
            case Functions.CODE_SSL_READ:
                direction          = Packet.Directions.In;
                TotalSizeReceived += packetInfo.Size;
                break;

            case Functions.CODE_SEND:
            case Functions.CODE_SENDTO:
            case Functions.CODE_WS2SEND:
            case Functions.CODE_WS2SENDTO:
            case Functions.CODE_WSASEND:
            case Functions.CODE_WSASENDTO:
            case Functions.CODE_PR_SEND:
            case Functions.CODE_PR_WRITE:
            case Functions.CODE_SSLENCRYPTPACKET:
            case Functions.CODE_ENCRYPTMESSAGE:
            case Functions.CODE_SSL_WRITE:
                direction      = Packet.Directions.Out;
                TotalSizeSent += packetInfo.Size;
                break;

            default:
                throw new IndexOutOfRangeException();
            }

            var functionFlag = FilterManager.GetFilterActionFlagForFunction(packetInfo.FunctionID);

            // If a matching breakpoint type filter is active, we open a new window to edit the data
            if (IsFilteringActived && FilterManager.CheckPacketBreak(data, packetInfo.Size, functionFlag))
            {
                var pckt           = new Packet(packetInfo.FunctionID, packetInfo.SocketId, packetInfo.LocalIp, packetInfo.LocalPort, packetInfo.RemoteIp, packetInfo.RemotePort, data, direction);
                var showPacketForm = new ShowPacketForm(0, pckt, true);
                showPacketForm.ShowDialog();
                if (showPacketForm.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    data            = showPacketForm.NewPacketData;
                    packetInfo.Size = showPacketForm.NewPacketSize;
                }
                // Send the new data to the DLL
                DllCommunication.WriteCommandToCmdMMF(ServerCodes.SCODE_SETPACKET, data, packetInfo.Size);
            }

            if (!IsCaptureEnabled) // Capture disabled, return
            {
                return;
            }

            // Don't log Functions, Ips, or Ports that are not activated in the program's settings menu
            if (!LogFunctions.HasFlag(FilterManager.GetFilterActionFlagForFunction(packetInfo.FunctionID)))
            {
                return;
            }
            if ((Settings.LocalIpChecked && packetInfo.LocalIp != (uint)Settings.LocalIp) ||
                (Settings.LocalPortChecked && packetInfo.LocalPort != Settings.LocalPort) ||
                (Settings.RemoteIpChecked && packetInfo.RemoteIp != (uint)Settings.RemoteIp) ||
                (Settings.RemotePortChecked && packetInfo.RemotePort != Settings.RemotePort))
            {
                return;
            }


            IsModifiedList = true;
            var packet = new Packet(packetInfo.FunctionID, packetInfo.SocketId, packetInfo.LocalIp, packetInfo.LocalPort, packetInfo.RemoteIp, packetInfo.RemotePort, data, direction);

            lock (LockingVar)
            {
                ID = PacketList.Count;
                PacketList.Add(packet);
                Program.mainForm.AddPacket(packet);
            }

            if (IsFilteringActived && FilterManager.CheckPacketWatch(data, packetInfo.Size, functionFlag))
            {
                Watch.Add(ID);
            }
            // Don't log ignored packets
            if (IsFilteringActived && FilterManager.CheckPacketIgnore(data, packetInfo.Size, functionFlag))
            {
                return;
            }

            Both.Add(ID);

            switch (direction)
            {
            case Packet.Directions.In:
                Received.Add(ID);
                break;

            case Packet.Directions.Out:
                Sent.Add(ID);
                break;
            }

            SmartRefresh();
        }
Ejemplo n.º 2
0
        public static void StartPacketReaderMMF()
        {
            int nodeBufferSize = sizeof(byte) * _pktMmfBufferSize;

            SharedMemory.CircularBuffer packetMMF;
            if (!CheckMMFileExists(_pktMmfName))
            {
                packetMMF = new SharedMemory.CircularBuffer(_pktMmfName, _pktMmfNodeCount, nodeBufferSize);
            }
            else
            {
                packetMMF = new SharedMemory.CircularBuffer(_pktMmfName);
            }

            byte[] writtenData = new byte[_pktMmfBufferSize];

            //int threadCount = 0;
            Action reader = () =>
            {
                //int myThreadIndex = Interlocked.Increment(ref threadCount);
                //Output.outMsgBox("Started thread {0}: ", threadCount);

                for (; ;)
                {
                    //var t1 = Environment.TickCount; Debug.WriteLine("packetMMF.Read(writtenData) {0}", ID);
                    //var t2 = Environment.TickCount; if (t2-t1 > 0) Debug.WriteLine("ANTES de packetMMF.Read {0} - diff: {1}", ID, t2-t1);

                    //The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely.
                    int amount = packetMMF.Read(writtenData, 0, -1);

                    //var t3 = Environment.TickCount; if (t3 - t2 > 0) Debug.WriteLine("DESPUES de server.Read {0} - diff: {1}", ID, t3 - t2);

                    if (amount == 0)
                    {
                        throw new Exception("Read 0 bytes from queue!");
                    }

                    Output.outString("Read data: {0}", BitConverter.ToString(writtenData));

                    // PacketInfo HEADER
                    var packetInfo = new PacketInfo();

                    var piSize = Marshal.SizeOf(packetInfo);
                    var piData = new byte[piSize];

                    // Reads the piSize bytes corresponding to the packetInfo
                    Buffer.BlockCopy(writtenData, 0, piData, 0, piSize);

                    // Converts the bytes to a packetInfo
                    IntPtr ptr = Marshal.AllocHGlobal(piSize);
                    Marshal.Copy(piData, 0, ptr, piSize);
                    packetInfo = (PacketInfo)Marshal.PtrToStructure(ptr, packetInfo.GetType());

                    var cSize = packetInfo.Size;
                    var cData = new byte[cSize];

                    // Packet size can not be bigger than buffer size
                    Debug.Assert(cSize < _pktMmfBufferSize);

                    Buffer.BlockCopy(writtenData, piSize, cData, 0, cSize);

                    PacketManager.PacketReceived(packetInfo, ref cData);

                    //var t4 = Environment.TickCount; if (t4- t3 > 0) Debug.WriteLine("DESPUES de PacketManager.PacketReceived {0} - diff: {1}", ID, t4 - t3);
                }
            };

            // Start reader thread
            ThreadPool.QueueUserWorkItem(o => { reader(); });
        }