예제 #1
0
        public static TDSMessage CreateFromFirstPacket(TDSPacket firstPacket)
        {
            switch (firstPacket.Header.Type)
            {
            case HeaderType.SQLBatch:
                return(new SQLBatchMessage(firstPacket));

            case HeaderType.AttentionSignal:
                return(new AttentionMessage(firstPacket));

            case HeaderType.RPC:
                return(new RPCRequestMessage(firstPacket));

            default:
                return(new TDSMessage(firstPacket));
            }
        }
예제 #2
0
        public void BuildMessage(HeaderType messageType, byte[] lPayLoad)
        {
            var packet_size = 4096 - 8;

            Packets.Clear();

            for (var lower = 0; lower < lPayLoad.Length; lower += packet_size)
            {
                var payloadSize = lPayLoad.Length - lower;
                if (payloadSize > packet_size)
                {
                    payloadSize = packet_size;
                }

                // Create new TDS header
                var header = new TDSHeader();
                header.Type = messageType;
                if (lPayLoad.Length - lower <= packet_size)
                {
                    header.StatusBitMask = StatusBitMask.END_OF_MESSAGE;
                }
                else
                {
                    header.StatusBitMask = StatusBitMask.NORMAL;
                }
                header.PayloadSize = payloadSize;

                var payload = new byte[payloadSize];
                Array.Copy(lPayLoad, lower, payload, 0, payloadSize);

                // Create new TDS packet with new TDS header and payload
                var newPacket = new TDSPacket(header.Data, payload, payloadSize);

                // Add packet into message
                Packets.Add(newPacket);
            }
        }
예제 #3
0
        protected virtual void ClientBridgeThread()
        {
            try
            {
                byte[] bBuffer   = null;
                byte[] bHeader   = new byte[Header.TDSHeader.HEADER_SIZE];
                int    iReceived = 0;

                Message.TDSMessage tdsMessage = null;

                while ((iReceived = SocketCouple.ClientBridgeSocket.Receive(bHeader, Header.TDSHeader.HEADER_SIZE, SocketFlags.None)) > 0)
                //while ((iReceived = sc.InputSocket.Receive(bBuffer, SocketFlags.None)) > 0)
                {
                    Header.TDSHeader header = new Header.TDSHeader(bHeader);

                    int iMinBufferSize = Math.Max(0x1000, header.LengthIncludingHeader + 1);
                    if ((bBuffer == null) || (bBuffer.Length < iMinBufferSize))
                    {
                        bBuffer = new byte[iMinBufferSize];
                    }

                    //Console.WriteLine(header.Type);

                    if (header.Type == (HeaderType)23)
                    {
                        iReceived = SocketCouple.ClientBridgeSocket.Receive(bBuffer, 0, 0x1000 - Header.TDSHeader.HEADER_SIZE, SocketFlags.None);
                    }
                    else if (header.PayloadSize > 0)
                    {
                        //Console.WriteLine("\t{0:N0} bytes package", header.LengthIncludingHeader);
                        SocketCouple.ClientBridgeSocket.Receive(bBuffer, 0, header.PayloadSize, SocketFlags.None);
                    }
                    TDSPacket tdsPacket = new TDSPacket(bHeader, bBuffer, header.PayloadSize);
                    OnTDSPacketReceived(tdsPacket);

                    if (tdsMessage == null)
                    {
                        tdsMessage = Message.TDSMessage.CreateFromFirstPacket(tdsPacket);
                    }
                    else
                    {
                        tdsMessage.Packets.Add(tdsPacket);
                    }

                    if ((header.StatusBitMask & StatusBitMask.END_OF_MESSAGE) == StatusBitMask.END_OF_MESSAGE)
                    {
                        OnTDSMessageReceived(tdsMessage);
                        tdsMessage = null;
                    }

                    SocketCouple.BridgeSQLSocket.Send(bHeader, bHeader.Length, SocketFlags.None);

                    if (header.Type == (HeaderType)23)
                    {
                        SocketCouple.BridgeSQLSocket.Send(bBuffer, iReceived, SocketFlags.None);
                    }
                    else
                    {
                        // METHOD: Converts unicode bytestream to string, modifies the DBName from the query, & converts it back
                        // Doesn't work probably due to off-by-one errors during conversions

                        // string temp = Encoding.Unicode.GetString(bBuffer);
                        // bBuffer = Encoding.Unicode.GetBytes(temp.Replace("alpha", "bravo"));

                        // Unicode representation
                        // Console.WriteLine("BYTES: " + BitConverter.ToString(bBuffer));

                        // Unicode - String conversion
                        // Console.WriteLine("BYTES TO STRING: " + Encoding.Unicode.GetString(bBuffer));

                        SocketCouple.BridgeSQLSocket.Send(bBuffer, header.PayloadSize, SocketFlags.None);
                    }

                    //sc.OutputSocket.Send(bBuffer, header.LengthIncludingHeader, SocketFlags.None);
                    //sc.OutputSocket.Send(bBuffer, iReceived, SocketFlags.None);
                }
            }
            catch (Exception e)
            {
                OnBridgeException(ConnectionType.ClientBridge, e);
            }

            OnConnectionDisconnected(ConnectionType.ClientBridge);
            //Console.WriteLine("Closing InputThread");
        }
예제 #4
0
 public SQLBatchMessage(TDSPacket firstPacket)
     : base(firstPacket)
 {
 }
예제 #5
0
        protected virtual void ClientBridgeThread()
        {
            try
            {
                byte[] bBuffer   = null;
                byte[] bHeader   = new byte[Header.TDSHeader.HEADER_SIZE];
                int    iReceived = 0;

                Message.TDSMessage tdsMessage = null;

                while ((iReceived = SocketCouple.ClientBridgeSocket.Receive(bHeader, Header.TDSHeader.HEADER_SIZE, SocketFlags.None)) > 0)
                //while ((iReceived = sc.InputSocket.Receive(bBuffer, SocketFlags.None)) > 0)
                {
                    Header.TDSHeader header = new Header.TDSHeader(bHeader);

                    int iMinBufferSize = Math.Max(0x1000, header.LengthIncludingHeader + 1);
                    if ((bBuffer == null) || (bBuffer.Length < iMinBufferSize))
                    {
                        bBuffer = new byte[iMinBufferSize];
                    }

                    //Console.WriteLine(header.Type);

                    if (header.Type == (HeaderType)23)
                    {
                        iReceived = SocketCouple.ClientBridgeSocket.Receive(bBuffer, 0, 0x1000 - Header.TDSHeader.HEADER_SIZE, SocketFlags.None);
                    }
                    else if (header.PayloadSize > 0)
                    {
                        //Console.WriteLine("\t{0:N0} bytes package", header.LengthIncludingHeader);
                        SocketCouple.ClientBridgeSocket.Receive(bBuffer, 0, header.PayloadSize, SocketFlags.None);
                    }
                    TDSPacket tdsPacket = new TDSPacket(bHeader, bBuffer, header.PayloadSize);
                    OnTDSPacketReceived(tdsPacket);

                    if (tdsMessage == null)
                    {
                        tdsMessage = Message.TDSMessage.CreateFromFirstPacket(tdsPacket);
                    }
                    else
                    {
                        tdsMessage.Packets.Add(tdsPacket);
                    }

                    if ((header.StatusBitMask & StatusBitMask.END_OF_MESSAGE) == StatusBitMask.END_OF_MESSAGE)
                    {
                        OnTDSMessageReceived(tdsMessage);
                        tdsMessage = null;
                    }

                    SocketCouple.BridgeSQLSocket.Send(bHeader, bHeader.Length, SocketFlags.None);

                    if (header.Type == (HeaderType)23)
                    {
                        SocketCouple.BridgeSQLSocket.Send(bBuffer, iReceived, SocketFlags.None);
                    }
                    else
                    {
                        SocketCouple.BridgeSQLSocket.Send(bBuffer, header.PayloadSize, SocketFlags.None);
                    }

                    //sc.OutputSocket.Send(bBuffer, header.LengthIncludingHeader, SocketFlags.None);
                    //sc.OutputSocket.Send(bBuffer, iReceived, SocketFlags.None);
                }
            }
            catch (Exception e)
            {
                OnBridgeException(ConnectionType.ClientBridge, e);
            }

            OnConnectionDisconnected(ConnectionType.ClientBridge);
            //Console.WriteLine("Closing InputThread");
        }
예제 #6
0
 public RPCRequestMessage(TDSPacket firtsPacket)
     : base(firtsPacket)
 {
 }
예제 #7
0
 public AttentionMessage(TDSPacket firtsPacket)
     : base(firtsPacket)
 {
 }
예제 #8
0
 public TDSMessage(TDSPacket firtsPacket)
 {
     Packets.Add(firtsPacket);
 }
예제 #9
0
        protected virtual void ClientBridgeThread()
        {
            try
            {
                byte[]     bBuffer    = null;
                var        bHeader    = new byte[TDSHeader.HEADER_SIZE];
                var        iReceived  = 0;
                TDSMessage tdsMessage = null;

                while ((iReceived =
                            SocketCouple.ClientBridgeSocket.Receive(bHeader, TDSHeader.HEADER_SIZE, SocketFlags.None)) > 0)
                //while ((iReceived = sc.InputSocket.Receive(bBuffer, SocketFlags.None)) > 0)
                {
                    var header = new TDSHeader(bHeader);

                    var iMinBufferSize = Math.Max(0x1000, header.LengthIncludingHeader + 1);
                    if (bBuffer == null || bBuffer.Length < iMinBufferSize)
                    {
                        bBuffer = new byte[iMinBufferSize];
                    }

                    //Console.WriteLine(header.Type);

                    if (header.Type == (HeaderType)23)
                    {
                        iReceived = SocketCouple.ClientBridgeSocket.Receive(bBuffer, 0, 0x1000 - TDSHeader.HEADER_SIZE,
                                                                            SocketFlags.None);
                    }
                    else if (header.PayloadSize > 0)
                    {
                        SocketCouple.ClientBridgeSocket.Receive(bBuffer, 0, header.PayloadSize, SocketFlags.None);
                    }
                    var tdsPacket = new TDSPacket(bHeader, bBuffer, header.PayloadSize);
                    OnTDSPacketReceived(tdsPacket);

                    if (tdsMessage == null)
                    {
                        tdsMessage = TDSMessage.CreateFromFirstPacket(tdsPacket);
                    }
                    else
                    {
                        tdsMessage.Packets.Add(tdsPacket);
                    }


                    if (!(tdsMessage is SQLBatchMessage)) // By Calvin: If message is not SQL command, do not intercept
                    {
                        SocketCouple.BridgeSQLSocket.Send(bHeader, bHeader.Length, SocketFlags.None);
                        if (header.Type == (HeaderType)23)
                        {
                            SocketCouple.BridgeSQLSocket.Send(bBuffer, iReceived, SocketFlags.None);
                        }
                        else
                        {
                            SocketCouple.BridgeSQLSocket.Send(bBuffer, header.PayloadSize, SocketFlags.None);
                        }
                    }

                    if ((header.StatusBitMask & StatusBitMask.END_OF_MESSAGE) == StatusBitMask.END_OF_MESSAGE)
                    {
                        OnTDSMessageReceived(tdsMessage);

                        // By Calvin: If message is SQL command, intercept
                        if (tdsMessage is SQLBatchMessage)
                        {
                            //Console.WriteLine("REQUEST");
                            //Console.WriteLine(System.Text.Encoding.Unicode.GetString(tdsMessage.Packets[0].Payload));
                            //Console.WriteLine(BitConverter.ToString(tdsMessage.Packets[0].Payload));

                            // Modify it here
                            var b = (SQLBatchMessage)tdsMessage;
                            if (b.GetBatchText().Contains("showmethemoney"))
                            {
                                SQLModifier.ChangeSQL(b.GetBatchText(), this);
                            }
                            else
                            {
                                foreach (var packet in b.Packets)
                                {
                                    //Console.WriteLine(packet.Payload.Length + " | " +packet.Header.PayloadSize);
                                    SocketCouple.BridgeSQLSocket.Send(packet.Header.Data, packet.Header.Data.Length,
                                                                      SocketFlags.None);
                                    SocketCouple.BridgeSQLSocket.Send(packet.Payload, packet.Header.PayloadSize,
                                                                      SocketFlags.None);
                                }
                            }
                        }
                        tdsMessage = null;
                    }
                }
            }
            catch (Exception e)
            {
                OnBridgeException(ConnectionType.ClientBridge, e);
            }

            OnConnectionDisconnected(ConnectionType.ClientBridge);
            //Console.WriteLine("Closing InputThread");
        }
예제 #10
0
 protected virtual void OnTDSPacketReceived(TDSPacket packet)
 {
     BridgeAcceptor.OnTDSMessageReceived(this, packet);
 }