RPCPDU.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Text;
  10. using Utilities;
  11. namespace SMBLibrary.RPC
  12. {
  13. /// <summary>
  14. /// See DCE 1.1: Remote Procedure Call, Chapter 12.6 - Connection-oriented RPC PDUs
  15. /// </summary>
  16. public abstract class RPCPDU
  17. {
  18. public const int CommonFieldsLength = 16;
  19. // The common header fields, which appear in all (connection oriented) PDU types:
  20. public byte VersionMajor; // rpc_vers
  21. public byte VersionMinor; // rpc_vers_minor
  22. public PacketTypeName PacketType;
  23. public PacketFlags Flags;
  24. public DataRepresentationFormat DataRepresentation;
  25. public ushort FragmentLength; // The length of the entire PDU
  26. public ushort AuthLength;
  27. public uint CallID;
  28. public RPCPDU()
  29. {
  30. VersionMajor = 5;
  31. VersionMinor = 0;
  32. }
  33. public RPCPDU(byte[] buffer, int offset)
  34. {
  35. VersionMajor = ByteReader.ReadByte(buffer, offset + 0);
  36. VersionMinor = ByteReader.ReadByte(buffer, offset + 1);
  37. PacketType = (PacketTypeName)ByteReader.ReadByte(buffer, offset + 2);
  38. Flags = (PacketFlags)ByteReader.ReadByte(buffer, offset + 3);
  39. DataRepresentation = new DataRepresentationFormat(buffer, offset + 4);
  40. FragmentLength = LittleEndianConverter.ToUInt16(buffer, offset + 8);
  41. AuthLength = LittleEndianConverter.ToUInt16(buffer, offset + 10);
  42. CallID = LittleEndianConverter.ToUInt32(buffer, offset + 12);
  43. }
  44. public abstract byte[] GetBytes();
  45. public void WriteCommonFieldsBytes(byte[] buffer)
  46. {
  47. ByteWriter.WriteByte(buffer, 0, VersionMajor);
  48. ByteWriter.WriteByte(buffer, 1, VersionMinor);
  49. ByteWriter.WriteByte(buffer, 2, (byte)PacketType);
  50. ByteWriter.WriteByte(buffer, 3, (byte)Flags);
  51. DataRepresentation.WriteBytes(buffer, 4);
  52. LittleEndianWriter.WriteUInt16(buffer, 8, FragmentLength);
  53. LittleEndianWriter.WriteUInt16(buffer, 10, AuthLength);
  54. LittleEndianWriter.WriteUInt32(buffer, 12, CallID);
  55. }
  56. public static RPCPDU GetPDU(byte[] buffer, int offset)
  57. {
  58. PacketTypeName packetType = (PacketTypeName)ByteReader.ReadByte(buffer, 2);
  59. switch (packetType)
  60. {
  61. case PacketTypeName.Request:
  62. return new RequestPDU(buffer, offset);
  63. case PacketTypeName.Response:
  64. return new ResponsePDU(buffer, offset);
  65. case PacketTypeName.Fault:
  66. return new FaultPDU(buffer, offset);
  67. case PacketTypeName.Bind:
  68. return new BindPDU(buffer, offset);
  69. case PacketTypeName.BindAck:
  70. return new BindAckPDU(buffer, offset);
  71. default:
  72. throw new NotImplementedException();
  73. }
  74. }
  75. }
  76. }