From cdd3d182b5ea3d6e04ccbc5d032e192ee3d70e3e Mon Sep 17 00:00:00 2001 From: TalAloni Date: Fri, 13 Nov 2020 15:32:16 +0200 Subject: [PATCH] Added SMB2TransformHeader implementation --- .../SMB2/Enums/SMB2TransformHeaderFlags.cs | 10 ++++ SMBLibrary/SMB2/SMB2TransformHeader.cs | 57 +++++++++++++++++++ SMBLibrary/SMBLibrary.VS2005.csproj | 2 + 3 files changed, 69 insertions(+) create mode 100644 SMBLibrary/SMB2/Enums/SMB2TransformHeaderFlags.cs create mode 100644 SMBLibrary/SMB2/SMB2TransformHeader.cs diff --git a/SMBLibrary/SMB2/Enums/SMB2TransformHeaderFlags.cs b/SMBLibrary/SMB2/Enums/SMB2TransformHeaderFlags.cs new file mode 100644 index 0000000..70dbd28 --- /dev/null +++ b/SMBLibrary/SMB2/Enums/SMB2TransformHeaderFlags.cs @@ -0,0 +1,10 @@ +using System; + +namespace SMBLibrary.SMB2 +{ + [Flags] + public enum SMB2TransformHeaderFlags : ushort + { + Encrypted = 0x0001, + } +} diff --git a/SMBLibrary/SMB2/SMB2TransformHeader.cs b/SMBLibrary/SMB2/SMB2TransformHeader.cs new file mode 100644 index 0000000..7c39dfb --- /dev/null +++ b/SMBLibrary/SMB2/SMB2TransformHeader.cs @@ -0,0 +1,57 @@ +/* Copyright (C) 2020 Tal Aloni . All rights reserved. + * + * You can redistribute this program and/or modify it under the terms of + * the GNU Lesser Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + */ +using System; +using Utilities; + +namespace SMBLibrary.SMB2 +{ + /// + /// Used by the client or server when sending encrypted messages. only valid for the SMB 3.x dialect family. + /// + public class SMB2TransformHeader + { + public const int Length = 52; + public const int SignatureOffset = 4; + + public static readonly byte[] ProtocolSignature = new byte[] { 0xFD, 0x53, 0x4D, 0x42 }; + + private byte[] ProtocolId; // 4 bytes, 0xFD followed by "SMB" + public byte[] Signature; // 16 bytes + public byte[] Nonce; // 16 bytes + public uint OriginalMessageSize; + public ushort Reserved; + public SMB2TransformHeaderFlags Flags; // EncryptionAlgorithm in SMB 3.0 / 3.0.2 where the only possible value is SMB2_ENCRYPTION_AES128_CCM = 0x0001 + public ulong SessionId; + + public SMB2TransformHeader() + { + ProtocolId = ProtocolSignature; + } + + public SMB2TransformHeader(byte[] buffer, int offset) + { + ProtocolId = ByteReader.ReadBytes(buffer, offset + 0, 4); + Signature = ByteReader.ReadBytes(buffer, offset + 4, 16); + Nonce = ByteReader.ReadBytes(buffer, offset + 20, 16); + OriginalMessageSize = LittleEndianConverter.ToUInt32(buffer, offset + 36); + Reserved = LittleEndianConverter.ToUInt16(buffer, offset + 40); + Flags = (SMB2TransformHeaderFlags)LittleEndianConverter.ToUInt16(buffer, offset + 42); + SessionId = LittleEndianConverter.ToUInt64(buffer, offset + 44); + } + + public void WriteBytes(byte[] buffer, int offset) + { + ByteWriter.WriteBytes(buffer, offset + 0, ProtocolId); + ByteWriter.WriteBytes(buffer, offset + 4, Signature); + ByteWriter.WriteBytes(buffer, offset + 20, Nonce); + LittleEndianWriter.WriteUInt32(buffer, offset + 36, OriginalMessageSize); + LittleEndianWriter.WriteUInt16(buffer, offset + 40, Reserved); + LittleEndianWriter.WriteUInt16(buffer, offset + 42, (ushort)Flags); + LittleEndianWriter.WriteUInt64(buffer, offset + 44, SessionId); + } + } +} diff --git a/SMBLibrary/SMBLibrary.VS2005.csproj b/SMBLibrary/SMBLibrary.VS2005.csproj index a35e8ee..8256a72 100644 --- a/SMBLibrary/SMBLibrary.VS2005.csproj +++ b/SMBLibrary/SMBLibrary.VS2005.csproj @@ -575,11 +575,13 @@ + +