Exemple #1
0
        public async Task InjectPacketAsync(byte[] packetBuffer, PacketInjectionArgs args)
        {
            byte[] outputBuffer = new byte[sizeof(int)];
            byte[] inputBuffer  = new byte[packetBuffer.Length + 6 * sizeof(int) + sizeof(ulong)];
            int    currentIndex = 0;

            byte[] appIdBytes      = _shadowRegisterContext.SeralizeAppIdToByteArray();
            byte[] layerBytes      = BitConverter.GetBytes((int)args.Layer);
            byte[] directionBytes  = BitConverter.GetBytes((int)args.Direction);
            byte[] addrFamilyBytes = BitConverter.GetBytes((int)args.AddrFamily);
            byte[] sizeBytes       = BitConverter.GetBytes(packetBuffer.Length);
            byte[] identifierBytes = BitConverter.GetBytes(args.Identifier);
            byte[] fragmentIndex   = BitConverter.GetBytes(args.FragmentIndex);
            appIdBytes.CopyTo(inputBuffer, currentIndex);
            currentIndex += 4;
            layerBytes.CopyTo(inputBuffer, currentIndex);
            currentIndex += 4;
            directionBytes.CopyTo(inputBuffer, currentIndex);
            currentIndex += 4;
            addrFamilyBytes.CopyTo(inputBuffer, currentIndex);
            currentIndex += 4;
            sizeBytes.CopyTo(inputBuffer, currentIndex);
            currentIndex += 4;
            identifierBytes.CopyTo(inputBuffer, currentIndex);
            currentIndex += sizeof(ulong);
            fragmentIndex.CopyTo(inputBuffer, currentIndex);
            currentIndex += sizeof(int);
            packetBuffer.CopyTo(inputBuffer, currentIndex);

            try
            {
                await _shadowDevice.SendIOControlAsync(IOCTLs.IOCTLShadowDriverInjectPacket, inputBuffer, outputBuffer);
            }
            catch (NullReferenceException)
            {
                throw new ShadowFilterException(0xC0090040);
            }
            catch (Exception)
            {
                throw;
            }

            var status = BitConverter.ToUInt32(outputBuffer, 0);

            if (status != 0)
            {
                HandleError(status);
            }
        }
Exemple #2
0
        private async void InqueueIOCTLForFurtherNotification()
        {
            var outputBuffer = new byte[2000];

            var  inputBuffer = _shadowRegisterContext.SeralizeAppIdToByteArray();
            uint ioctlResult = 0;

            while (_isQueueingContinue)
            {
                try
                {
                    ioctlResult = await _shadowDevice.SendIOControlAsync(IOCTLs.IOCTLShadowDriverQueueNotification, inputBuffer, outputBuffer);
                }
                catch (NullReferenceException)
                {
                    _isQueueingContinue = false;
                    //throw new ShadowFilterException(0xC0090040);
                }
                catch (Exception)
                {
                    _isQueueingContinue = false;
                    //System.Diagnostics.Debug.WriteLine(exception.Message);
                }

                uint status = BitConverter.ToUInt32(outputBuffer, 0);

                if (status != 0)
                {
                    _isQueueingContinue = false;
                    //if (status != 1)
                    //{
                    //    HandleError(status);
                    //}
                }


                int currentIndex = sizeof(int);
                var packetSize   = BitConverter.ToInt64(outputBuffer, currentIndex);

#if DEBUG
                System.Diagnostics.Debug.WriteLine("Packet received, size is {0}.", packetSize);
#endif
                if (packetSize > 0)
                {
                    currentIndex += sizeof(long);
                    var identifier = BitConverter.ToUInt64(outputBuffer, currentIndex);
                    currentIndex += sizeof(ulong);
                    packetSize   -= sizeof(ulong);
                    var totalFragCount = BitConverter.ToInt32(outputBuffer, currentIndex);
                    currentIndex += sizeof(int);
                    packetSize   -= sizeof(int);
                    var fragIndex = BitConverter.ToInt32(outputBuffer, currentIndex);
                    currentIndex += sizeof(int);
                    packetSize   -= sizeof(int);
                    var offsetLength = BitConverter.ToUInt32(outputBuffer, currentIndex);
                    currentIndex += sizeof(uint);
                    packetSize   -= sizeof(uint);
                    FilteringLayer layer = (FilteringLayer)BitConverter.ToInt32(outputBuffer, currentIndex);
                    currentIndex += sizeof(FilteringLayer);
                    packetSize   -= sizeof(FilteringLayer);
                    NetPacketDirection direction = (NetPacketDirection)BitConverter.ToInt32(outputBuffer, currentIndex);
                    currentIndex += sizeof(NetPacketDirection);
                    packetSize   -= sizeof(NetPacketDirection);
                    byte[] packetBuffer = new byte[packetSize];
                    Array.Copy(outputBuffer, currentIndex, packetBuffer, 0, packetSize);
                    CapturedPacketArgs args = new CapturedPacketArgs(identifier, packetSize, totalFragCount, fragIndex, layer, direction);
                    if (_isFilteringStarted)
                    {
                        byte[] newPacket = PacketReceived?.Invoke(packetBuffer, args);

                        if (newPacket != null)
                        {
                            // Modify packet
                            PacketInjectionArgs injectArgs = new PacketInjectionArgs
                            {
                                AddrFamily    = IpAddrFamily.IPv4,
                                Direction     = args.Direction,
                                FragmentIndex = args.FragmentIndex,
                                Identifier    = args.Identifier,
                                Layer         = args.Layer,
                            };

                            try
                            {
                                await InjectPacketAsync(newPacket, injectArgs);
                            }
                            catch (Exception)
                            {
                                System.Diagnostics.Debug.WriteLine("Dfsdf");
                            }
                        }
                    }
                }
            }
            return;
        }