SMB1Message.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace SMBLibrary.SMB1
  13. {
  14. /// <summary>
  15. /// Each message has a single header and either a single command or multiple batched (AndX) commands.
  16. /// Multiple command requests or responses can be sent in a single message.
  17. /// </summary>
  18. public class SMB1Message
  19. {
  20. public SMB1Header Header;
  21. public List<SMB1Command> Commands = new List<SMB1Command>();
  22. public SMB1Message()
  23. {
  24. Header = new SMB1Header();
  25. }
  26. public SMB1Message(byte[] buffer)
  27. {
  28. Header = new SMB1Header(buffer);
  29. SMB1Command command = SMB1Command.ReadCommand(buffer, SMB1Header.Length, Header.Command, Header);
  30. Commands.Add(command);
  31. while(command is SMBAndXCommand)
  32. {
  33. SMBAndXCommand andXCommand = (SMBAndXCommand)command;
  34. if (andXCommand.AndXCommand == CommandName.SMB_COM_NO_ANDX_COMMAND)
  35. {
  36. break;
  37. }
  38. command = SMB1Command.ReadCommand(buffer, andXCommand.AndXOffset, andXCommand.AndXCommand, Header);
  39. Commands.Add(command);
  40. }
  41. }
  42. public byte[] GetBytes()
  43. {
  44. if (Commands.Count == 0)
  45. {
  46. throw new ArgumentException("Invalid command sequence");
  47. }
  48. for (int index = 0; index < Commands.Count - 1; index++)
  49. {
  50. if (!(Commands[index] is SMBAndXCommand))
  51. {
  52. throw new ArgumentException("Invalid command sequence");
  53. }
  54. }
  55. SMB1Command lastCommand = Commands[Commands.Count - 1];
  56. if (lastCommand is SMBAndXCommand)
  57. {
  58. ((SMBAndXCommand)lastCommand).AndXCommand = CommandName.SMB_COM_NO_ANDX_COMMAND;
  59. }
  60. List<byte[]> sequence = new List<byte[]>();
  61. int length = SMB1Header.Length;
  62. byte[] commandBytes;
  63. for (int index = 0; index < Commands.Count - 1; index++)
  64. {
  65. SMBAndXCommand andXCommand = (SMBAndXCommand)Commands[index];
  66. andXCommand.AndXCommand = Commands[index + 1].CommandName;
  67. commandBytes = Commands[index].GetBytes(Header.UnicodeFlag);
  68. ushort nextOffset = (ushort)(length + commandBytes.Length);
  69. SMBAndXCommand.WriteAndXOffset(commandBytes, 0, nextOffset);
  70. sequence.Add(commandBytes);
  71. length += commandBytes.Length;
  72. }
  73. commandBytes = lastCommand.GetBytes(Header.UnicodeFlag);
  74. sequence.Add(commandBytes);
  75. length += commandBytes.Length;
  76. Header.Command = Commands[0].CommandName;
  77. byte[] buffer = new byte[length];
  78. Header.WriteBytes(buffer, 0);
  79. int offset = SMB1Header.Length;
  80. foreach (byte[] bytes in sequence)
  81. {
  82. ByteWriter.WriteBytes(buffer, ref offset, bytes);
  83. }
  84. return buffer;
  85. }
  86. public static SMB1Message GetSMB1Message(byte[] buffer)
  87. {
  88. if (!SMB1Header.IsValidSMB1Header(buffer))
  89. {
  90. throw new InvalidRequestException("Invalid SMB header signature");;
  91. }
  92. return new SMB1Message(buffer);
  93. }
  94. }
  95. }