123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. 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 System.Collections.Generic;
- using Utilities;
- namespace SMBLibrary.SMB2
- {
- public abstract class SMB2Command
- {
- public SMB2Header Header;
- public SMB2Command(SMB2CommandName commandName)
- {
- Header = new SMB2Header(commandName);
- }
- public SMB2Command(byte[] buffer, int offset)
- {
- Header = new SMB2Header(buffer, offset);
- }
- public void WriteBytes(byte[] buffer, int offset)
- {
- Header.WriteBytes(buffer, offset);
- WriteCommandBytes(buffer, offset + SMB2Header.Length);
- }
- public abstract void WriteCommandBytes(byte[] buffer, int offset);
- public byte[] GetBytes()
- {
- byte[] buffer = new byte[this.Length];
- WriteBytes(buffer, 0);
- return buffer;
- }
- public SMB2CommandName CommandName
- {
- get
- {
- return Header.Command;
- }
- }
- public int Length
- {
- get
- {
- return SMB2Header.Length + CommandLength;
- }
- }
- public abstract int CommandLength
- {
- get;
- }
- public static SMB2Command ReadRequest(byte[] buffer, int offset)
- {
- SMB2CommandName Command = (SMB2CommandName)LittleEndianConverter.ToUInt16(buffer, offset + 12);
- switch (Command)
- {
- case SMB2CommandName.Negotiate:
- return new NegotiateRequest(buffer, offset);
- case SMB2CommandName.SessionSetup:
- return new SessionSetupRequest(buffer, offset);
- case SMB2CommandName.Logoff:
- return new LogoffRequest(buffer, offset);
- case SMB2CommandName.TreeConnect:
- return new TreeConnectRequest(buffer, offset);
- case SMB2CommandName.TreeDisconnect:
- return new TreeDisconnectRequest(buffer, offset);
- case SMB2CommandName.Create:
- return new CreateRequest(buffer, offset);
- case SMB2CommandName.Close:
- return new CloseRequest(buffer, offset);
- case SMB2CommandName.Flush:
- return new FlushRequest(buffer, offset);
- case SMB2CommandName.Read:
- return new ReadRequest(buffer, offset);
- case SMB2CommandName.Write:
- return new WriteRequest(buffer, offset);
- case SMB2CommandName.Lock:
- return new LockRequest(buffer, offset);
- case SMB2CommandName.IOCtl:
- return new IOCtlRequest(buffer, offset);
- case SMB2CommandName.Cancel:
- return new CancelRequest(buffer, offset);
- case SMB2CommandName.Echo:
- return new EchoRequest(buffer, offset);
- case SMB2CommandName.QueryDirectory:
- return new QueryDirectoryRequest(buffer, offset);
- case SMB2CommandName.ChangeNotify:
- return new ChangeNotifyRequest(buffer, offset);
- case SMB2CommandName.QueryInfo:
- return new QueryInfoRequest(buffer, offset);
- case SMB2CommandName.SetInfo:
- return new SetInfoRequest(buffer, offset);
- default:
- throw new System.IO.InvalidDataException("Invalid SMB2 command in buffer");
- }
- }
- public static List<SMB2Command> ReadRequestChain(byte[] buffer, int offset)
- {
- List<SMB2Command> result = new List<SMB2Command>();
- SMB2Command command;
- do
- {
- command = ReadRequest(buffer, offset);
- result.Add(command);
- offset += (int)command.Header.NextCommand;
- }
- while (command.Header.NextCommand != 0);
- return result;
- }
- public static byte[] GetCommandChainBytes(List<SMB2Command> commands)
- {
- int totalLength = 0;
- for (int index = 0; index < commands.Count; index++)
- {
- // Any subsequent SMB2 header MUST be 8-byte aligned
- int length = commands[index].Length;
- if (index < commands.Count - 1)
- {
- int paddedLength = (int)Math.Ceiling((double)length / 8) * 8;
- totalLength += paddedLength;
- }
- else
- {
- totalLength += length;
- }
- }
- byte[] buffer = new byte[totalLength];
- int offset = 0;
- for (int index = 0; index < commands.Count; index++)
- {
- SMB2Command command = commands[index];
- int commandLength = command.Length;
- int paddedLength = (int)Math.Ceiling((double)commandLength / 8) * 8;
- if (index < commands.Count - 1)
- {
- command.Header.NextCommand = (uint)paddedLength;
- }
- command.WriteBytes(buffer, offset);
- offset += paddedLength;
- }
- return buffer;
- }
- }
- }
|