mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-05-04 20:57:49 +02:00
Added proper Length property to NetBIOS session packets
This commit is contained in:
parent
801719172d
commit
62d240d166
7 changed files with 61 additions and 18 deletions
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
@ -35,5 +34,13 @@ namespace SMBLibrary.NetBios
|
||||||
|
|
||||||
return base.GetBytes();
|
return base.GetBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int Length
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return HeaderLength + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
@ -30,5 +29,13 @@ namespace SMBLibrary.NetBios
|
||||||
this.Trailer = new byte[0];
|
this.Trailer = new byte[0];
|
||||||
return base.GetBytes();
|
return base.GetBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int Length
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return HeaderLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
@ -30,5 +29,13 @@ namespace SMBLibrary.NetBios
|
||||||
this.Trailer = new byte[0];
|
this.Trailer = new byte[0];
|
||||||
return base.GetBytes();
|
return base.GetBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int Length
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return HeaderLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
@ -16,11 +15,12 @@ namespace SMBLibrary.NetBios
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class SessionPacket
|
public abstract class SessionPacket
|
||||||
{
|
{
|
||||||
|
public const int HeaderLength = 4;
|
||||||
public const int MaxSessionPacketLength = 131075;
|
public const int MaxSessionPacketLength = 131075;
|
||||||
|
|
||||||
public SessionPacketTypeName Type;
|
public SessionPacketTypeName Type;
|
||||||
public byte Flags;
|
public byte Flags;
|
||||||
public int Length; // 2 bytes + length extension bit
|
private int TrailerLength; // 2 bytes + length extension bit
|
||||||
public byte[] Trailer;
|
public byte[] Trailer;
|
||||||
|
|
||||||
public SessionPacket()
|
public SessionPacket()
|
||||||
|
@ -31,30 +31,37 @@ namespace SMBLibrary.NetBios
|
||||||
{
|
{
|
||||||
Type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset + 0);
|
Type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset + 0);
|
||||||
Flags = ByteReader.ReadByte(buffer, offset + 1);
|
Flags = ByteReader.ReadByte(buffer, offset + 1);
|
||||||
Length = (Flags & 0x01) << 16 | BigEndianConverter.ToUInt16(buffer, offset + 2);
|
TrailerLength = (Flags & 0x01) << 16 | BigEndianConverter.ToUInt16(buffer, offset + 2);
|
||||||
|
Trailer = ByteReader.ReadBytes(buffer, offset + 4, TrailerLength);
|
||||||
this.Trailer = ByteReader.ReadBytes(buffer, offset + 4, Length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual byte[] GetBytes()
|
public virtual byte[] GetBytes()
|
||||||
{
|
{
|
||||||
Length = this.Trailer.Length;
|
TrailerLength = this.Trailer.Length;
|
||||||
if (Length > 0x1FFFF)
|
if (TrailerLength > 0x1FFFF)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Invalid NBT packet length");
|
throw new ArgumentException("Invalid NBT packet length");
|
||||||
}
|
}
|
||||||
|
|
||||||
Flags = Convert.ToByte(Length > 0xFFFF);
|
Flags = Convert.ToByte(TrailerLength > 0xFFFF);
|
||||||
|
|
||||||
byte[] buffer = new byte[4 + Trailer.Length];
|
byte[] buffer = new byte[HeaderLength + Trailer.Length];
|
||||||
ByteWriter.WriteByte(buffer, 0, (byte)this.Type);
|
ByteWriter.WriteByte(buffer, 0, (byte)Type);
|
||||||
ByteWriter.WriteByte(buffer, 1, Flags);
|
ByteWriter.WriteByte(buffer, 1, Flags);
|
||||||
BigEndianWriter.WriteUInt16(buffer, 2, (ushort)(Length & 0xFFFF));
|
BigEndianWriter.WriteUInt16(buffer, 2, (ushort)(TrailerLength & 0xFFFF));
|
||||||
ByteWriter.WriteBytes(buffer, 4, this.Trailer);
|
ByteWriter.WriteBytes(buffer, 4, Trailer);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual int Length
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return HeaderLength + Trailer.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static SessionPacket GetSessionPacket(byte[] buffer, int offset)
|
public static SessionPacket GetSessionPacket(byte[] buffer, int offset)
|
||||||
{
|
{
|
||||||
SessionPacketTypeName type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset);
|
SessionPacketTypeName type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset);
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
@ -39,5 +38,15 @@ namespace SMBLibrary.NetBios
|
||||||
ByteWriter.WriteBytes(this.Trailer, part1.Length, part2);
|
ByteWriter.WriteBytes(this.Trailer, part1.Length, part2);
|
||||||
return base.GetBytes();
|
return base.GetBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int Length
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
byte[] part1 = NetBiosUtils.EncodeName(CalledName, String.Empty);
|
||||||
|
byte[] part2 = NetBiosUtils.EncodeName(CallingName, String.Empty);
|
||||||
|
return HeaderLength + part1.Length + part2.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
using Utilities;
|
using Utilities;
|
||||||
|
|
||||||
namespace SMBLibrary.NetBios
|
namespace SMBLibrary.NetBios
|
||||||
|
@ -37,5 +36,13 @@ namespace SMBLibrary.NetBios
|
||||||
BigEndianWriter.WriteUInt16(this.Trailer, 4, Port);
|
BigEndianWriter.WriteUInt16(this.Trailer, 4, Port);
|
||||||
return base.GetBytes();
|
return base.GetBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int Length
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return HeaderLength + 6;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue