/// <summary> /// Build a Smb2Packet from a byte array /// </summary> /// <param name="data">The byte array</param> /// <param name="consumedLen">The consumed data length</param> /// <param name="expectedLen">The expected data length</param> internal override void FromBytes(byte[] data, out int consumedLen, out int expectedLen) { consumedLen = 0; this.NegotiateContext_ENCRYPTION = null; this.NegotiateContext_PREAUTH = null; this.Header = TypeMarshal.ToStruct <Packet_Header>(data, ref consumedLen); byte[] tempData = data.Skip(consumedLen).ToArray(); this.PayLoad = Smb2Utility.UnmarshalStructure <NEGOTIATE_Response>(tempData); consumedLen += Marshal.SizeOf(this.PayLoad); if (this.PayLoad.SecurityBufferLength > 0) { this.Buffer = data.Skip(this.PayLoad.SecurityBufferOffset).Take(this.PayLoad.SecurityBufferLength).ToArray(); consumedLen += this.Buffer.Length; } while (data.Length > consumedLen) { // Skip padding int paddingLen = 8 - (consumedLen) % 8; if (paddingLen != 8) { if (data.Length - consumedLen <= paddingLen) { break; } consumedLen += paddingLen; } if (data.Length - consumedLen < 8) { break; } SMB2_NEGOTIATE_CONTEXT_Type_Values contextType = (SMB2_NEGOTIATE_CONTEXT_Type_Values)BitConverter.ToUInt16(data, consumedLen); if (contextType == SMB2_NEGOTIATE_CONTEXT_Type_Values.SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { if (this.NegotiateContext_PREAUTH != null) { throw new Exception("More than one SMB2_PREAUTH_INTEGRITY_CAPABILITIES are present."); } this.NegotiateContext_PREAUTH = TypeMarshal.ToStruct <SMB2_PREAUTH_INTEGRITY_CAPABILITIES>(data, ref consumedLen); } else if (contextType == SMB2_NEGOTIATE_CONTEXT_Type_Values.SMB2_ENCRYPTION_CAPABILITIES) { if (this.NegotiateContext_ENCRYPTION != null) { throw new Exception("More than one SMB2_ENCRYPTION_CAPABILITIES are present."); } this.NegotiateContext_ENCRYPTION = TypeMarshal.ToStruct <SMB2_ENCRYPTION_CAPABILITIES>(data, ref consumedLen); } else { throw new Exception(string.Format("Unknow Negotiate Context: {0}.", (ushort)contextType)); } } expectedLen = 0; }
/// <summary> /// Build a Smb2Packet from a byte array /// </summary> /// <param name="data">The byte array</param> /// <param name="consumedLen">The consumed data length</param> /// <param name="expectedLen">The expected data length</param> internal override void FromBytes(byte[] data, out int consumedLen, out int expectedLen) { consumedLen = 0; this.Header = TypeMarshal.ToStruct <Packet_Header>(data, ref consumedLen); byte[] tempData = data.Skip(consumedLen).ToArray(); this.PayLoad = Smb2Utility.UnmarshalStructure <NEGOTIATE_Request>(tempData); consumedLen += Marshal.SizeOf(this.PayLoad); this.Buffer = data.Skip(consumedLen).ToArray(); //Dialects + Padding + NegotiateContextList if (PayLoad.DialectCount > 0) { this.Dialects = Smb2Utility.UnmarshalStructArray <DialectRevision>(Buffer, PayLoad.DialectCount); consumedLen += PayLoad.DialectCount * sizeof(DialectRevision); } while (data.Length > consumedLen) { // Skip padding int paddingLen = 8 - (consumedLen) % 8; if (paddingLen != 8) { if (data.Length - consumedLen <= paddingLen) { break; } consumedLen += paddingLen; } if (data.Length - consumedLen < 8) { break; } SMB2_NEGOTIATE_CONTEXT_Type_Values contextType = (SMB2_NEGOTIATE_CONTEXT_Type_Values)BitConverter.ToUInt16(data, consumedLen); if (contextType == SMB2_NEGOTIATE_CONTEXT_Type_Values.SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { this.NegotiateContext_PREAUTH = TypeMarshal.ToStruct <SMB2_PREAUTH_INTEGRITY_CAPABILITIES>(data, ref consumedLen); } else if (contextType == SMB2_NEGOTIATE_CONTEXT_Type_Values.SMB2_ENCRYPTION_CAPABILITIES) { this.NegotiateContext_ENCRYPTION = TypeMarshal.ToStruct <SMB2_ENCRYPTION_CAPABILITIES>(data, ref consumedLen); } else if (contextType == SMB2_NEGOTIATE_CONTEXT_Type_Values.SMB2_COMPRESSION_CAPABILITIES) { this.NegotiateContext_COMPRESSION = TypeMarshal.ToStruct <SMB2_COMPRESSION_CAPABILITIES>(data, ref consumedLen); } else if (contextType == SMB2_NEGOTIATE_CONTEXT_Type_Values.SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { this.NegotiateContext_NETNAME = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID.Unmarshal(data, ref consumedLen); } } expectedLen = 0; }