Example #1
0
 /// <summary>
 /// Deserializes a Type 2 message instance from the specified buffer
 /// of bytes.
 /// </summary>
 /// <param name="buffer">The buffer containing a sequence of bytes
 /// representing an NTLM Type 2 message.</param>
 /// <returns>An initialized instance of the Type2 class.</returns>
 /// <exception cref="SerializationException">Thrown if an error occurs
 /// during deserialization of the Type 2 message.</exception>
 static internal Type2Message Deserialize(byte[] buffer)
 {
     try {
         Type2Message t2 = new Type2Message();
         using (var ms = new MemoryStream(buffer)) {
             using (var r = new BinaryReader(ms)) {
                 if (r.ReadASCIIString(8) != signature)
                 {
                     throw new InvalidDataException("Invalid signature.");
                 }
                 if (r.ReadInt32() != (int)type)
                 {
                     throw new InvalidDataException("Unexpected message type.");
                 }
                 int targetLength = r.ReadInt16(), targetSpace =
                     r.ReadInt16(), targetOffset = r.ReadInt32();
                 t2.Flags     = (Flags)r.ReadInt32();
                 t2.Challenge = r.ReadBytes(8);
                 // Figure out, which of the several versions of Type 2 we're
                 // dealing with.
                 t2.Version = GetType2Version(targetOffset);
                 if (t2.Version > Type2Version.Version1)
                 {
                     t2.Context = r.ReadInt64();
                     // Read the target information security buffer
                     int informationLength = r.ReadInt16(), informationSpace =
                         r.ReadInt16(), informationOffset = r.ReadInt32();
                     t2.RawTargetInformation = new byte[informationLength];
                     Array.Copy(buffer, informationOffset,
                                t2.RawTargetInformation, 0, informationLength);
                     // Version 3 has an additional OS version structure.
                     if (t2.Version > Type2Version.Version2)
                     {
                         t2.OSVersion = ReadOSVersion(r);
                     }
                 }
                 t2.TargetName = GetTargetName(r.ReadBytes(targetLength),
                                               (t2.Flags & Flags.NegotiateUnicode) == Flags.NegotiateUnicode);
                 if (t2.Version > Type2Version.Version1)
                 {
                     t2.TargetInformation = ReadTargetInformation(r);
                 }
             }
         }
         return(t2);
     } catch (Exception e) {
         throw new SerializationException("NTLM Type 2 message could not be " +
                                          "deserialized.", e);
     }
 }
Example #2
0
		/// <summary>
		/// Deserializes a Type 2 message instance from the specified buffer
		/// of bytes.
		/// </summary>
		/// <param name="buffer">The buffer containing a sequence of bytes
		/// representing an NTLM Type 2 message.</param>
		/// <returns>An initialized instance of the Type2 class.</returns>
		/// <exception cref="SerializationException">Thrown if an error occurs
		/// during deserialization of the Type 2 message.</exception>
		static internal Type2Message Deserialize(byte[] buffer) {
			try {
				Type2Message t2 = new Type2Message();
				using (var ms = new MemoryStream(buffer)) {
					using (var r = new BinaryReader(ms)) {
						if (r.ReadASCIIString(8) != signature)
							throw new InvalidDataException("Invalid signature.");
						if (r.ReadInt32() != (int) type)
							throw new InvalidDataException("Unexpected message type.");
						int targetLength = r.ReadInt16(), targetSpace =
							r.ReadInt16(), targetOffset = r.ReadInt32();
						t2.Flags = (Flags) r.ReadInt32();
						t2.Challenge = r.ReadBytes(8);
						// Figure out, which of the several versions of Type 2 we're
						// dealing with.
						t2.Version = GetType2Version(targetOffset);
						if (t2.Version > Type2Version.Version1) {
							t2.Context = r.ReadInt64();
							// Read the target information security buffer
							int informationLength = r.ReadInt16(), informationSpace =
								r.ReadInt16(), informationOffset = r.ReadInt32();
							t2.RawTargetInformation = new byte[informationLength];
							Array.Copy(buffer, informationOffset,
								t2.RawTargetInformation, 0, informationLength);
							// Version 3 has an additional OS version structure.
							if (t2.Version > Type2Version.Version2)
								t2.OSVersion = ReadOSVersion(r);
						}
						t2.TargetName = GetTargetName(r.ReadBytes(targetLength),
							(t2.Flags & Flags.NegotiateUnicode) == Flags.NegotiateUnicode);
						if (t2.Version > Type2Version.Version1) {
							t2.TargetInformation = ReadTargetInformation(r);
						}
					}
				}
				return t2;
			} catch (Exception e) {
				throw new SerializationException("NTLM Type 2 message could not be " +
					"deserialized.", e);
			}
		}