mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-07-26 02:38:15 +02:00
SMB2Command: Implemented signing in GetCommandChainBytes
This commit is contained in:
parent
5d9fa0fb6e
commit
f693f7ff77
1 changed files with 22 additions and 1 deletions
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.SMB2
|
namespace SMBLibrary.SMB2
|
||||||
|
@ -121,6 +122,14 @@ namespace SMBLibrary.SMB2
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] GetCommandChainBytes(List<SMB2Command> commands)
|
public static byte[] GetCommandChainBytes(List<SMB2Command> commands)
|
||||||
|
{
|
||||||
|
return GetCommandChainBytes(commands, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <param name="sessionKey">
|
||||||
|
/// command will be signed using this key if (not null and) SMB2_FLAGS_SIGNED is set.
|
||||||
|
/// </param>
|
||||||
|
public static byte[] GetCommandChainBytes(List<SMB2Command> commands, byte[] sessionKey)
|
||||||
{
|
{
|
||||||
int totalLength = 0;
|
int totalLength = 0;
|
||||||
for (int index = 0; index < commands.Count; index++)
|
for (int index = 0; index < commands.Count; index++)
|
||||||
|
@ -143,12 +152,24 @@ namespace SMBLibrary.SMB2
|
||||||
{
|
{
|
||||||
SMB2Command command = commands[index];
|
SMB2Command command = commands[index];
|
||||||
int commandLength = command.Length;
|
int commandLength = command.Length;
|
||||||
int paddedLength = (int)Math.Ceiling((double)commandLength / 8) * 8;
|
int paddedLength;
|
||||||
if (index < commands.Count - 1)
|
if (index < commands.Count - 1)
|
||||||
{
|
{
|
||||||
|
paddedLength = (int)Math.Ceiling((double)commandLength / 8) * 8;
|
||||||
command.Header.NextCommand = (uint)paddedLength;
|
command.Header.NextCommand = (uint)paddedLength;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
paddedLength = commandLength;
|
||||||
|
}
|
||||||
command.WriteBytes(buffer, offset);
|
command.WriteBytes(buffer, offset);
|
||||||
|
if (command.Header.IsSigned && sessionKey != null)
|
||||||
|
{
|
||||||
|
// [MS-SMB2] Any padding at the end of the message MUST be used in the hash computation.
|
||||||
|
byte[] signature = new HMACSHA256(sessionKey).ComputeHash(buffer, offset, paddedLength);
|
||||||
|
// [MS-SMB2] The first 16 bytes of the hash MUST be copied into the 16-byte signature field of the SMB2 Header.
|
||||||
|
ByteWriter.WriteBytes(buffer, offset + SMB2Header.SignatureOffset, signature, 16);
|
||||||
|
}
|
||||||
offset += paddedLength;
|
offset += paddedLength;
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue