Beispiel #1
0
        protected static int ReadInteger(RdpPacket packet)
        {
            if (packet.ReadByte() != 2)
            {
                throw new Exception("Data Error!");
            }
            int num2 = packet.ReadByte();

            byte[] buffer = new byte[4];

            switch (num2)
            {
            case 4:
                packet.Read(buffer, 0, 4);
                return(BitConverter.ToInt32(buffer, 0));

            case 3:
                packet.Read(buffer, 0, 3);
                return(BitConverter.ToInt32(buffer, 0));

            case 2:
                packet.Read(buffer, 0, 2);
                return(BitConverter.ToInt32(buffer, 0));
            }

            packet.Read(buffer, 0, 1);

            return(BitConverter.ToInt32(buffer, 0));
        }
Beispiel #2
0
        private static void ProcesssResponse(RdpPacket packet, byte[] ServerPublicKey)
        {
            ASN1.ReadTag(packet, ASN1.SequenceTag(0), "TSRequest");
            ASN1.ReadTag(packet, ASN1.ContextTag(0), "CTX_Version");

            if (ASN1.ReadInteger(packet) < 2)
            {
                throw new Exception("TSRequest version not supported!");
            }

            ASN1.CloseTag(packet, "CTX_Version");
            byte[] buffer = null;
            int    num2   = ASN1.ReadTag(packet, "Tag");

            if (num2 == ASN1.ContextTag(1))
            {
                ASN1.ReadTag(packet, ASN1.SequenceTag(0), "NegTokens");
                ASN1.ReadTag(packet, ASN1.SequenceTag(0), "NegTokens2");
                ASN1.ReadTag(packet, ASN1.ContextTag(0), "CTX_OctetString");
                m_ChallengeMsg = new byte[ASN1.ReadTag(packet, ASN1.OctetStringTag(), "OctetString")];
                packet.Read(m_ChallengeMsg, 0, m_ChallengeMsg.Length);
                ASN1.CloseTag(packet, "OctetString");
                ASN1.CloseTag(packet, "CTX_OctetString");
                ASN1.CloseTag(packet, "NegTokens2");
                ASN1.CloseTag(packet, "NegTokens");
            }
            else if (num2 == ASN1.ContextTag(3))
            {
                buffer = new byte[ASN1.ReadTag(packet, ASN1.OctetStringTag(), "OctetString")];
                packet.Read(buffer, 0, buffer.Length);
                ASN1.CloseTag(packet, "OctetString");
            }

            ASN1.CloseTag(packet, "Tag");
            ASN1.CloseTag(packet, "TSRequest");

            if (buffer != null)
            {
                byte[] buffer2 = m_NTLMAuthenticate.DecryptMessage(buffer);
                buffer2[0] = (byte)(buffer2[0] - 1);
                if (!NTLM.CompareArray(buffer2, ServerPublicKey))
                {
                    throw new Exception("Unable to verify the server's public key!");
                }
                buffer2[0] = (byte)(buffer2[0] + 1);
                SendTSRequest(null, WriteTSCredentials(), null);
                m_bAuthenticated = true;
            }
            else
            {
                ReadNegoToken(m_ChallengeMsg, ServerPublicKey);
            }
        }
Beispiel #3
0
 public void copyToByteArray(RdpPacket packet)
 {
     byte[] buffer = new byte[packet.Length];
     packet.Position = 0L;
     packet.Read(buffer, 0, (int)packet.Length);
     this.Write(buffer, 0, buffer.Length);
 }
Beispiel #4
0
        internal static void Write(RdpPacket data)
        {
            data.Position = 0L;
            byte[] buffer = new byte[data.Length];
            data.Read(buffer, 0, (int)data.Length);

            Network.Send(buffer);
        }
Beispiel #5
0
        private static void Send(RdpPacket packet)
        {
            packet.Position = 0L;
            byte[] buffer = new byte[packet.Length];
            packet.Read(buffer, 0, (int)packet.Length);
            NTLM.DumpHex(buffer, (int)packet.Length, "Send");

            Network.Send(buffer);
        }
Beispiel #6
0
        private static byte[] WriteTSCredentials()
        {
            RdpPacket packet = new RdpPacket();

            ASN1.WriteTag(packet, ASN1.SequenceTag(0), "SEQ_TSCRED");
            ASN1.WriteTag(packet, ASN1.ContextTag(0), "CTX_credType");
            ASN1.WriteInteger(packet, 1);
            ASN1.CloseTag(packet, "CTX_credType");
            ASN1.WriteTag(packet, ASN1.ContextTag(1), "CTX_credentials");
            ASN1.WriteTag(packet, ASN1.OctetStringTag(), "CTX_OctetString");
            ASN1.WriteTag(packet, ASN1.SequenceTag(0), "SEQ_Credentials");
            ASN1.WriteTag(packet, ASN1.ContextTag(0), "CTX_domain");
            ASN1.WriteTag(packet, ASN1.OctetStringTag(), "OctectString");
            byte[] bytes = Encoding.Unicode.GetBytes(Options.Domain);
            packet.Write(bytes, 0, bytes.Length);
            ASN1.CloseTag(packet, "OctectString");
            ASN1.CloseTag(packet, "CTX_domain");
            ASN1.WriteTag(packet, ASN1.ContextTag(1), "CTX_user");
            ASN1.WriteTag(packet, ASN1.OctetStringTag(), "OctectString");
            byte[] buffer = Encoding.Unicode.GetBytes(Options.Username);
            packet.Write(buffer, 0, buffer.Length);
            ASN1.CloseTag(packet, "OctectString");
            ASN1.CloseTag(packet, "CTX_user");
            ASN1.WriteTag(packet, ASN1.ContextTag(2), "CTX_password");
            ASN1.WriteTag(packet, ASN1.OctetStringTag(), "OctectString");
            byte[] buffer3 = Encoding.Unicode.GetBytes(Options.Password);
            packet.Write(buffer3, 0, buffer3.Length);
            ASN1.CloseTag(packet, "OctectString");
            ASN1.CloseTag(packet, "CTX_password");
            ASN1.CloseTag(packet, "SEQ_Credentials");
            ASN1.CloseTag(packet, "CTX_OctetString");
            ASN1.CloseTag(packet, "CTX_credentials");
            ASN1.CloseTag(packet, "SEQ_TSCRED");
            byte[] buffer4 = new byte[packet.Length];
            packet.Position = 0L;
            packet.Read(buffer4, 0, buffer4.Length);

            return(m_NTLMAuthenticate.EncryptMessage(buffer4));
        }
Beispiel #7
0
        public byte[] Negotiate()
        {
            RdpPacket packet   = new RdpPacket();
            uint      num      = (((((((0xe2000000 | NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY) | NTLMSSP_NEGOTIATE_ALWAYS_SIGN) | NTLMSSP_NEGOTIATE_NTLM) | NTLMSSP_NEGOTIATE_SEAL) | NTLMSSP_NEGOTIATE_SIGN) | NTLMSSP_REQUEST_TARGET) | NTLMSSP_NEGOTIATE_OEM) | NTLMSSP_NEGOTIATE_UNICODE;
            int       position = (int)packet.Position;

            packet.WriteString("NTLMSSP", false);
            packet.WriteByte(0);
            packet.WriteLittleEndian32(1);
            packet.WriteLittleEndian32(num);
            int num3 = ((int)packet.Position) - position;

            num3 += 8;
            num3 += 8;

            if ((num & 0x2000000) != 0)
            {
                num3 += 8;
            }

            packet.WriteLittleEndian16((short)0);
            packet.WriteLittleEndian16((short)0);
            packet.WriteLittleEndian32(0);
            packet.WriteLittleEndian16((short)0);
            packet.WriteLittleEndian16((short)0);
            packet.WriteLittleEndian32(0);

            if ((num & 0x2000000) != 0)
            {
                this.WriteVersion(packet);
            }

            packet.Position     = 0L;
            this.m_NegotiateMsg = new byte[packet.Length];
            packet.Read(this.m_NegotiateMsg, 0, this.m_NegotiateMsg.Length);

            return(this.m_NegotiateMsg);
        }
Beispiel #8
0
            public byte[] Serialise()
            {
                RdpPacket packet = new RdpPacket();

                if (this.NbDomainName.length > 0)
                {
                    packet.WriteLittleEndian16((short)2);
                    packet.WriteLittleEndian16((short)this.NbDomainName.length);
                    packet.Write(this.NbDomainName.value, 0, this.NbDomainName.length);
                }
                if (this.NbComputerName.length > 0)
                {
                    packet.WriteLittleEndian16((short)1);
                    packet.WriteLittleEndian16((short)this.NbComputerName.length);
                    packet.Write(this.NbComputerName.value, 0, this.NbComputerName.length);
                }
                if (this.DnsDomainName.length > 0)
                {
                    packet.WriteLittleEndian16((short)4);
                    packet.WriteLittleEndian16((short)this.DnsDomainName.length);
                    packet.Write(this.DnsDomainName.value, 0, this.DnsDomainName.length);
                }
                if (this.DnsComputerName.length > 0)
                {
                    packet.WriteLittleEndian16((short)3);
                    packet.WriteLittleEndian16((short)this.DnsComputerName.length);
                    packet.Write(this.DnsComputerName.value, 0, this.DnsComputerName.length);
                }
                if (this.DnsTreeName.length > 0)
                {
                    packet.WriteLittleEndian16((short)5);
                    packet.WriteLittleEndian16((short)this.DnsTreeName.length);
                    packet.Write(this.DnsTreeName.value, 0, this.DnsTreeName.length);
                }
                if (this.Timestamp.length > 0)
                {
                    packet.WriteLittleEndian16((short)7);
                    packet.WriteLittleEndian16((short)this.Timestamp.length);
                    packet.Write(this.Timestamp.value, 0, this.Timestamp.length);
                }
                if (this.Flags != 0)
                {
                    packet.WriteLittleEndian16((short)6);
                    packet.WriteLittleEndian16((short)4);
                    packet.WriteLittleEndian32(this.Flags);
                }
                if (this.Restrictions.length > 0)
                {
                    packet.WriteLittleEndian16((short)8);
                    packet.WriteLittleEndian16((short)this.Restrictions.length);
                    packet.Write(this.Restrictions.value, 0, this.Restrictions.length);
                }
                if (this.ChannelBindings.length > 0)
                {
                    packet.WriteLittleEndian16((short)10);
                    packet.WriteLittleEndian16((short)this.ChannelBindings.length);
                    packet.Write(this.ChannelBindings.value, 0, this.ChannelBindings.length);
                }
                if (this.TargetName.value != null)
                {
                    packet.WriteLittleEndian16((short)9);
                    packet.WriteLittleEndian16((short)this.TargetName.length);
                    packet.Write(this.TargetName.value, 0, this.TargetName.length);
                }
                packet.WriteLittleEndian16((short)0);
                packet.WriteLittleEndian16((short)0);
                packet.WritePadding(8);
                byte[] buffer = new byte[packet.Length];
                packet.Position = 0L;
                packet.Read(buffer, 0, buffer.Length);
                return(buffer);
            }
Beispiel #9
0
            public void Parse(RdpPacket packet)
            {
                NTLM.AV_ID av_id;
                byte[]     buffer = null;
                do
                {
                    av_id = (NTLM.AV_ID)packet.ReadLittleEndian16();
                    int count = packet.ReadLittleEndian16();
                    if (count > 0)
                    {
                        if (av_id != NTLM.AV_ID.MsvAvFlags)
                        {
                            buffer = new byte[count];
                            packet.Read(buffer, 0, count);
                        }
                        else
                        {
                            this.Flags = packet.ReadLittleEndian32();
                        }
                    }
                    switch (av_id)
                    {
                    case NTLM.AV_ID.MsvAvNbComputerName:
                        this.NbComputerName.length = count;
                        this.NbComputerName.value  = buffer;
                        this.sNbComputerName       = Encoding.Unicode.GetString(this.NbComputerName.value, 0, this.NbComputerName.value.Length);
                        break;

                    case NTLM.AV_ID.MsvAvNbDomainName:
                        this.NbDomainName.length = count;
                        this.NbDomainName.value  = buffer;
                        this.sNbDomainName       = Encoding.Unicode.GetString(this.NbDomainName.value, 0, this.NbDomainName.value.Length);
                        break;

                    case NTLM.AV_ID.MsvAvDnsComputerName:
                        this.DnsComputerName.length = count;
                        this.DnsComputerName.value  = buffer;
                        this.sDnsComputerName       = Encoding.Unicode.GetString(this.DnsComputerName.value, 0, this.DnsComputerName.value.Length);
                        break;

                    case NTLM.AV_ID.MsvAvDnsDomainName:
                        this.DnsDomainName.length = count;
                        this.DnsDomainName.value  = buffer;
                        this.sDnsDomainName       = Encoding.Unicode.GetString(this.DnsDomainName.value, 0, this.DnsDomainName.value.Length);
                        break;

                    case NTLM.AV_ID.MsvAvDnsTreeName:
                        this.DnsTreeName.length = count;
                        this.DnsTreeName.value  = buffer;
                        break;

                    case NTLM.AV_ID.MsvAvTimestamp:
                        this.Timestamp.length = count;
                        this.Timestamp.value  = buffer;
                        break;

                    case NTLM.AV_ID.MsvAvRestrictions:
                        this.Restrictions.length = count;
                        this.Restrictions.value  = buffer;
                        break;

                    case NTLM.AV_ID.MsvAvTargetName:
                        this.TargetName.length = count;
                        this.TargetName.value  = buffer;
                        break;

                    case NTLM.AV_ID.MsvChannelBindings:
                        this.ChannelBindings.length = count;
                        this.ChannelBindings.value  = buffer;
                        break;
                    }
                }while (av_id != NTLM.AV_ID.MsvAvEOL);
            }
Beispiel #10
0
        private byte[] Authenticate(byte[] lmChallengeResponse, byte[] ntChallengeResponse, string sDomainName, string sUser, string sWorkstation, byte[] EncryptedRandomSessionKey, byte[] ExportedSessionKey, bool bGenerateMIC)
        {
            RdpPacket packet = new RdpPacket();
            uint      flags  = (
                (((((0xe2800000 | NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY) |
                    NTLMSSP_NEGOTIATE_ALWAYS_SIGN) | NTLMSSP_NEGOTIATE_NTLM) |
                  NTLMSSP_NEGOTIATE_SEAL) | NTLMSSP_NEGOTIATE_SIGN) |
                NTLMSSP_REQUEST_TARGET) | NTLMSSP_NEGOTIATE_UNICODE;

            DumpFlags(flags);
            int position = (int)packet.Position;

            packet.WriteString("NTLMSSP", false);
            packet.WriteByte(0);
            packet.WriteLittleEndian32(3);
            int num3 = ((int)packet.Position) - position;

            num3 += 8;
            num3 += 8;
            num3 += 8;
            num3 += 8;
            num3 += 8;
            num3 += 8;
            num3 += 4;

            if ((flags & 0x2000000) != 0)
            {
                num3 += 8;
            }

            if (bGenerateMIC)
            {
                num3 += 0x10;
            }

            byte[] bytes   = Encoding.Unicode.GetBytes(sDomainName);
            byte[] buffer  = Encoding.Unicode.GetBytes(sUser);
            byte[] buffer3 = Encoding.Unicode.GetBytes(sWorkstation);
            int    num4    = num3;
            int    num5    = num4 + bytes.Length;
            int    num6    = num5 + buffer.Length;
            int    num7    = num6 + buffer3.Length;
            int    num8    = num7 + lmChallengeResponse.Length;
            int    num9    = num8 + ntChallengeResponse.Length;

            packet.WriteLittleEndian16((ushort)lmChallengeResponse.Length);
            packet.WriteLittleEndian16((ushort)lmChallengeResponse.Length);
            packet.WriteLittleEndian32(num7);
            num3 += lmChallengeResponse.Length;
            packet.WriteLittleEndian16((ushort)ntChallengeResponse.Length);
            packet.WriteLittleEndian16((ushort)ntChallengeResponse.Length);
            packet.WriteLittleEndian32(num8);
            num3 += ntChallengeResponse.Length;
            packet.WriteLittleEndian16((ushort)bytes.Length);
            packet.WriteLittleEndian16((ushort)bytes.Length);
            packet.WriteLittleEndian32(num4);
            num3 += bytes.Length;
            packet.WriteLittleEndian16((ushort)buffer.Length);
            packet.WriteLittleEndian16((ushort)buffer.Length);
            packet.WriteLittleEndian32(num5);
            num3 += buffer.Length;
            packet.WriteLittleEndian16((ushort)buffer3.Length);
            packet.WriteLittleEndian16((ushort)buffer3.Length);
            packet.WriteLittleEndian32(num6);
            num3 += buffer3.Length;
            packet.WriteLittleEndian16((ushort)EncryptedRandomSessionKey.Length);
            packet.WriteLittleEndian16((ushort)EncryptedRandomSessionKey.Length);
            packet.WriteLittleEndian32(num9);
            num3 += EncryptedRandomSessionKey.Length;
            packet.WriteLittleEndian32(flags);

            if ((flags & 0x2000000) != 0)
            {
                this.WriteVersion(packet);
            }

            long num10 = packet.Position;

            if (bGenerateMIC)
            {
                packet.WritePadding(0x10);
            }

            packet.Write(bytes, 0, bytes.Length);
            packet.Write(buffer, 0, buffer.Length);
            packet.Write(buffer3, 0, buffer3.Length);
            packet.Write(lmChallengeResponse, 0, lmChallengeResponse.Length);
            packet.Write(ntChallengeResponse, 0, ntChallengeResponse.Length);
            packet.Write(EncryptedRandomSessionKey, 0, EncryptedRandomSessionKey.Length);

            if (bGenerateMIC)
            {
                packet.Position = 0L;
                byte[] buffer4 = new byte[packet.Length];
                packet.Read(buffer4, 0, buffer4.Length);
                HMACT64 hmact = new HMACT64(ExportedSessionKey);
                hmact.update(this.m_NegotiateMsg);
                hmact.update(this.m_ChallengeMsg);
                hmact.update(buffer4);
                byte[] buffer5 = hmact.digest();
                packet.Position = num10;
                packet.Write(buffer5, 0, buffer5.Length);
            }

            packet.Position = 0L;
            byte[] buffer6 = new byte[packet.Length];
            packet.Read(buffer6, 0, buffer6.Length);

            return(buffer6);
        }
Beispiel #11
0
        public byte[] ProcessChallenge(byte[] Challenge)
        {
            byte[]    bytes;
            RdpPacket packet = new RdpPacket();

            this.m_ChallengeMsg = Challenge;
            packet.Write(Challenge, 0, Challenge.Length);
            packet.Position = 0L;
            long position = packet.Position;

            if (packet.ReadString(8) != "NTLMSSP\0")
            {
                throw new Exception("Invalid negotiation token!");
            }

            if (packet.ReadLittleEndian32() != 2)
            {
                throw new Exception("Expected challenge!");
            }

            int count = packet.ReadLittleEndian16();

            packet.ReadLittleEndian16();
            int  num4  = packet.ReadLittleEndian32();
            uint flags = (uint)packet.ReadLittleEndian32();

            DumpFlags(flags);
            byte[] buffer = new byte[8];
            packet.Read(buffer, 0, 8);
            DumpHex(buffer, buffer.Length, "Server Challenge");
            byte[] buffer2 = new byte[8];
            packet.Read(buffer2, 0, 8);
            int num5 = packet.ReadLittleEndian16();

            packet.ReadLittleEndian16();
            int num6 = packet.ReadLittleEndian32();

            if ((flags & 0x2000000) != 0)
            {
                byte[] buffer3 = new byte[8];
                packet.Read(buffer3, 0, 8);
            }

            if ((flags & 0x20000000) == 0)
            {
                throw new Exception("Strong Encryption not supported by server");
            }

            byte[] buffer4 = null;

            if (count > 0)
            {
                buffer4         = new byte[count];
                packet.Position = position + num4;
                packet.Read(buffer4, 0, count);
                Encoding.Unicode.GetString(buffer4, 0, buffer4.Length);
            }

            AV_PAIRS av_pairs = new AV_PAIRS();

            byte[] buffer5 = null;

            if (num5 <= 0)
            {
                throw new Exception("No TargetInfo!");
            }

            packet.Position = position + num6;
            buffer5         = new byte[num5];
            packet.Read(buffer5, 0, num5);
            packet = new RdpPacket();
            packet.Write(buffer5, 0, buffer5.Length);
            packet.Position = 0L;
            av_pairs.Parse(packet);

            buffer5 = av_pairs.Serialise();

            byte[] data = nTOWFv2(this.m_sDomain, this.m_sUsername, this.m_sPassword);

            if (Network.Logger != null)
            {
                if (Network.Logger.Reading)
                {
                    data = this.m_Socket.GetBlob(PacketLogger.PacketType.NTLM_ResponseKeyNT);
                }
                else
                {
                    this.m_Socket.AddBlob(PacketLogger.PacketType.NTLM_ResponseKeyNT, data);
                }
            }

            byte[] blob = new byte[8];
            RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();

            provider.GetBytes(blob);

            if (Network.Logger != null)
            {
                if (Network.Logger.Reading)
                {
                    blob = this.m_Socket.GetBlob(PacketLogger.PacketType.NTLM_ClientChallenge);
                }
                else
                {
                    this.m_Socket.AddBlob(PacketLogger.PacketType.NTLM_ClientChallenge, blob);
                }
            }

            DumpHex(blob, blob.Length, "Client Challenge");
            byte[] buffer8 = getLMv2Response(data, buffer, blob);
            DumpHex(buffer8, buffer8.Length, "LM Response");

            if (this.m_bNTLMv2)
            {
                Array.Clear(buffer8, 0, buffer8.Length);
            }

            bool bGenerateMIC = false;

            if ((av_pairs.Timestamp.length <= 0) || !this.m_bNTLMv2)
            {
                bytes = BitConverter.GetBytes(DateTime.UtcNow.ToFileTimeUtc());
            }
            else
            {
                bytes        = av_pairs.Timestamp.value;
                bGenerateMIC = true;
                av_pairs.ProcessForNTLMv2();
                buffer5 = av_pairs.Serialise();
            }

            DumpHex(buffer5, buffer5.Length, "targetinfo");
            byte[] keyExchangeKey = null;
            byte[] buffer11       = getNTLMv2Response(data, buffer, blob, bytes, buffer5, out keyExchangeKey);
            DumpHex(buffer11, buffer11.Length, "NTLMv2 Response");

            if (Network.Logger != null)
            {
                if (Network.Logger.Reading)
                {
                    keyExchangeKey = this.m_Socket.GetBlob(PacketLogger.PacketType.NTLM_KeyExchangeKey);
                }
                else
                {
                    this.m_Socket.AddBlob(PacketLogger.PacketType.NTLM_KeyExchangeKey, keyExchangeKey);
                }
            }

            byte[] encryptedRandomSessionKey = null;
            byte[] buffer13 = null;
            buffer13 = new byte[0x10];
            provider.GetBytes(buffer13);

            if (Network.Logger != null)
            {
                if (Network.Logger.Reading)
                {
                    buffer13 = this.m_Socket.GetBlob(PacketLogger.PacketType.NTLM_ExportedSessionKey);
                }
                else
                {
                    this.m_Socket.AddBlob(PacketLogger.PacketType.NTLM_ExportedSessionKey, buffer13);
                }
            }

            encryptedRandomSessionKey = new byte[0x10];
            RC4 rc = new RC4();

            rc.engineInitEncrypt(keyExchangeKey);
            encryptedRandomSessionKey = rc.crypt(buffer13);

            if ((flags & 0x40000000) == 0)
            {
                encryptedRandomSessionKey = new byte[0];
                buffer13 = keyExchangeKey;
            }

            this.InitSignKeys(buffer13);

            return(this.Authenticate(buffer8, buffer11, this.m_sDomain, this.m_sUsername, this.m_sWorkstation, encryptedRandomSessionKey, buffer13, bGenerateMIC));
        }