RPCPDU.cs 3.8 KB

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