/// <summary> /// Covert to a byte array /// </summary> /// <returns>The byte array</returns> public override byte[] ToBytes() { byte[] messageData = TypeMarshal.ToBytes(this.Header); messageData = messageData.Concat(Smb2Utility.MarshalStructure(this.PayLoad)).ToArray(); if (this.Buffer != null) { messageData = messageData.Concat(this.Buffer).ToArray(); } if (NegotiateContext_PREAUTH != null) { // 8-byte align Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_PREAUTH_INTEGRITY_CAPABILITIES>(NegotiateContext_PREAUTH.Value)).ToArray(); } if (NegotiateContext_ENCRYPTION != null) { // 8-byte align Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_ENCRYPTION_CAPABILITIES>(NegotiateContext_ENCRYPTION.Value)).ToArray(); } return(messageData); }
/// <summary> /// Covert to a byte array /// </summary> /// <returns>The byte array</returns> public override byte[] ToBytes() { byte[] messageData = TypeMarshal.ToBytes(this.Header); messageData = messageData.Concat(Smb2Utility.MarshalStructure(this.PayLoad)).ToArray(); if (this.Dialects != null & this.Dialects.Length > 0) { messageData = messageData.Concat(Smb2Utility.MarshalStructArray(Dialects)).ToArray(); } if (NegotiateContext_PREAUTH != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_PREAUTH_INTEGRITY_CAPABILITIES>(NegotiateContext_PREAUTH.Value)).ToArray(); } if (NegotiateContext_ENCRYPTION != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_ENCRYPTION_CAPABILITIES>(NegotiateContext_ENCRYPTION.Value)).ToArray(); } if (NegotiateContext_COMPRESSION != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_COMPRESSION_CAPABILITIES>(NegotiateContext_COMPRESSION.Value)).ToArray(); } if (NegotiateContext_NETNAME != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(NegotiateContext_NETNAME.Marshal()).ToArray(); } if (NegotiateContext_SIGNING != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_SIGNING_CAPABILITIES>(NegotiateContext_SIGNING.Value)).ToArray(); } if (NegotiateContext_RDMA != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_RDMA_TRANSFORM_CAPABILITIES>(NegotiateContext_RDMA.Value)).ToArray(); } if (NegotiateContext_TRANSPORT != null) { Smb2Utility.Align8(ref messageData); messageData = messageData.Concat(TypeMarshal.ToBytes <SMB2_TRANSPORT_CAPABILITIES>(NegotiateContext_TRANSPORT.Value)).ToArray(); } return(messageData); }
/// <summary> /// Convert an Smb2ErrorResponsePacket to a byte array /// </summary> /// <returns>The byte array</returns> public override byte[] ToBytes() { byte[] messageData = TypeMarshal.ToBytes(this.Header); messageData = messageData.Concat(TypeMarshal.ToBytes <ushort>(this.PayLoad.StructureSize)).ToArray(); messageData = messageData.Concat(TypeMarshal.ToBytes <byte>(this.PayLoad.ErrorContextCount)).ToArray(); messageData = messageData.Concat(TypeMarshal.ToBytes <byte>(this.PayLoad.Reserved)).ToArray(); messageData = messageData.Concat(TypeMarshal.ToBytes <uint>(this.PayLoad.ByteCount)).ToArray(); if (this.PayLoad.ErrorContextCount > 0) //smb311 { byte[] temp = new byte[] { }; for (int i = 0; i < this.PayLoad.ErrorContextCount; i++) { temp = temp.Concat(TypeMarshal.ToBytes <uint>(this.PayLoad.ErrorContextErrorData[i].ErrorDataLength)).ToArray(); temp = temp.Concat(TypeMarshal.ToBytes <Error_Id>(this.PayLoad.ErrorContextErrorData[i].ErrorId)).ToArray(); switch (this.Header.Status) { case Smb2Status.STATUS_STOPPED_ON_SYMLINK: temp = temp.Concat(TypeMarshal.ToBytes <Symbolic_Link_Error_Response>(this.PayLoad.ErrorContextErrorData[i].ErrorData.SymbolicLinkErrorResponse)).ToArray(); break; case Smb2Status.STATUS_BAD_NETWORK_NAME: if (this.PayLoad.ErrorContextErrorData[i].ErrorId == Error_Id.ERROR_ID_SHARE_REDIRECT) { temp = temp.Concat(TypeMarshal.ToBytes <Share_Redirect_Error_Context_Response>(this.PayLoad.ErrorContextErrorData[i].ErrorData.ShareRedirectErrorContextResponse)).ToArray(); } break; case Smb2Status.STATUS_BUFFER_TOO_SMALL: temp = temp.Concat(this.PayLoad.ErrorContextErrorData[i].ErrorData.BufferTooSmallErrorResponse).ToArray(); break; } } Smb2Utility.Align8(ref temp); messageData = messageData.Concat(temp).ToArray(); } else { messageData = messageData.Concat(this.PayLoad.ErrorData).ToArray(); } return(messageData.ToArray()); }
private void ReplacePacketByInvalidCreateContext(Smb2Packet packet) { Smb2CreateRequestPacket request = packet as Smb2CreateRequestPacket; if (request == null) { return; } string name = null; byte[] dataBuffer = null; if (Create_ContextType == CreateContextType.InvalidCreateContext) { // <235> Section 3.3.5.9: Windows Vista, Windows Server 2008, Windows 7, Windows Server 2008 R2, Windows 8, and Windows Server 2012 ignore create contexts having a NameLength // greater than 4 and ignores create contexts with length of 4 that are not specified in section 2.2.13.2. // So use three characters' name here name = "Inv"; } else if (Create_ContextType == CreateContextType.InvalidCreateContextSize) { // Use SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST to test InvalidCreateContextSize since it contains a data buffer. name = CreateContextNames.SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST; var createQueryMaximalAccessRequestStruct = new CREATE_QUERY_MAXIMAL_ACCESS_REQUEST { Timestamp = new _FILETIME() }; dataBuffer = TypeMarshal.ToBytes(createQueryMaximalAccessRequestStruct); } else { throw new ArgumentException("contextType"); } var nameBuffer = Encoding.ASCII.GetBytes(name); var createContextStruct = new CREATE_CONTEXT(); createContextStruct.Buffer = new byte[0]; createContextStruct.NameOffset = 16; createContextStruct.NameLength = (ushort)nameBuffer.Length; createContextStruct.Buffer = createContextStruct.Buffer.Concat(nameBuffer).ToArray(); if (dataBuffer != null && dataBuffer.Length > 0) { Smb2Utility.Align8(ref createContextStruct.Buffer); createContextStruct.DataOffset = (ushort)(16 + createContextStruct.Buffer.Length); createContextStruct.DataLength = (uint)dataBuffer.Length; createContextStruct.Buffer = createContextStruct.Buffer.Concat(dataBuffer).ToArray(); } byte[] createContextValuesBuffer = new byte[0]; Smb2Utility.Align8(ref createContextValuesBuffer); createContextValuesBuffer = createContextValuesBuffer.Concat(TypeMarshal.ToBytes(createContextStruct)).ToArray(); if (Create_ContextType == CreateContextType.InvalidCreateContextSize) { // Change DataLength to invalid here, after marshalling. createContextValuesBuffer[12] += 1; } Smb2Utility.Align8(ref request.Buffer); request.PayLoad.CreateContextsOffset = (uint)(request.BufferOffset + request.Buffer.Length); request.PayLoad.CreateContextsLength = (uint)createContextValuesBuffer.Length; request.Buffer = request.Buffer.Concat(createContextValuesBuffer).ToArray(); }