/// <summary> /// Constructor /// </summary> /// <param name="transportType">The transport type of the connection</param> /// <param name="remoteEndpoint">The remote endpoint</param> /// <param name="sessionId">Netbios session id</param> /// <param name="endpointId">Identify this endpoint</param> internal Smb2Endpoint(Smb2TransportType transportType, IPEndPoint remoteEndpoint, byte sessionId, int endpointId) { this.transportType = transportType; this.remoteEndpoint = remoteEndpoint; this.sessionId = sessionId; this.endpointId = endpointId; }
/// <summary> /// Constructor /// </summary> /// <param name="config">The config, contains information about transport type, tcp listening port and so on</param> public Smb2Server(Smb2ServerConfig config) { decoder = new Smb2Decoder(Smb2Role.Server); decoder.TransportType = config.TransportType; clientEndpoints = new List<Smb2Endpoint>(); context = new Smb2ServerContext(); context.transportType = config.TransportType; context.requireMessageSigning = config.RequireMessageSigning; context.isDfsCapable = config.IsDfsCapable; transportType = config.TransportType; if (transportType == Smb2TransportType.NetBios) { NetbiosTransportConfig netbiosConfig = new NetbiosTransportConfig(); netbiosConfig.BufferSize = Smb2Consts.MaxNetbiosBufferSize; netbiosConfig.LocalNetbiosName = config.LocalNetbiosName; netbiosConfig.MaxNames = Smb2Consts.MaxNames; netbiosConfig.MaxSessions = Smb2Consts.MaxSessions; netbiosConfig.Type = StackTransportType.Netbios; netbiosConfig.Role = Role.Server; transport = new TransportStack(netbiosConfig, decoder.Smb2DecodePacketCallback); } else if (transportType == Smb2TransportType.Tcp) { SocketTransportConfig socketConfig = new SocketTransportConfig(); socketConfig.BufferSize = Smb2Consts.MaxNetbiosBufferSize; socketConfig.MaxConnections = Smb2Consts.MaxConnectionNumer; socketConfig.LocalIpAddress = IPAddress.Any; socketConfig.LocalIpPort = config.ServerTcpListeningPort; socketConfig.Role = Role.Server; socketConfig.Type = StackTransportType.Tcp; transport = new TransportStack(socketConfig, decoder.Smb2DecodePacketCallback); } else { throw new ArgumentException("config contains invalid transport type", "config"); } }
public Smb2Packet DecodeTransportPayload( byte[] messageBytes, Smb2Role role, Smb2TransportType transportType, bool ignoreCompoundFlag, out int consumedLength, out int expectedLength ) { //tcp transport will prefix 4 bytes length in the beginning. and netbios won't do this. if (transportType == Smb2TransportType.Tcp) { if (messageBytes.Length < Smb2Consts.TcpPrefixedLenByteCount) { consumedLength = 0; expectedLength = 4; return(null); } //in the header of tcp payload, there are 4 bytes(in fact only 3 bytes are used) which indicate //the length of smb2 int dataLenShouldHave = (messageBytes[1] << 16) + (messageBytes[2] << 8) + messageBytes[3]; if (dataLenShouldHave > (messageBytes.Length - Smb2Consts.TcpPrefixedLenByteCount)) { consumedLength = 0; expectedLength = Smb2Consts.TcpPrefixedLenByteCount + dataLenShouldHave; return(null); } byte[] smb2Message = new byte[dataLenShouldHave]; Array.Copy(messageBytes, Smb2Consts.TcpPrefixedLenByteCount, smb2Message, 0, smb2Message.Length); Smb2Packet packet = DecodeCompletePacket( smb2Message, role, ignoreCompoundFlag, 0, 0, out consumedLength, out expectedLength); // Here we ignore the consumedLength returned by DecodeCompletePacket(), there may be some tcp padding data // at the end which we are not interested. consumedLength = dataLenShouldHave + Smb2Consts.TcpPrefixedLenByteCount; return(packet); } else { Smb2Packet packet = DecodeCompletePacket( messageBytes, role, ignoreCompoundFlag, 0, 0, out consumedLength, out expectedLength); //Some packet has unknown padding data at the end. consumedLength = messageBytes.Length; return(packet); } }
public Smb2Packet DecodeTransportPayload( byte[] messageBytes, Smb2Role role, Smb2TransportType transportType, bool ignoreCompoundFlag, out int consumedLength, out int expectedLength ) { //tcp transport will prefix 4 bytes length in the beginning. and netbios won't do this. if (transportType == Smb2TransportType.Tcp) { if (messageBytes.Length < Smb2Consts.TcpPrefixedLenByteCount) { consumedLength = 0; expectedLength = 4; return null; } //in the header of tcp payload, there are 4 bytes(in fact only 3 bytes are used) which indicate //the length of smb2 int dataLenShouldHave = (messageBytes[1] << 16) + (messageBytes[2] << 8) + messageBytes[3]; if (dataLenShouldHave > (messageBytes.Length - Smb2Consts.TcpPrefixedLenByteCount)) { consumedLength = 0; expectedLength = Smb2Consts.TcpPrefixedLenByteCount + dataLenShouldHave; return null; } byte[] smb2Message = new byte[dataLenShouldHave]; Array.Copy(messageBytes, Smb2Consts.TcpPrefixedLenByteCount, smb2Message, 0, smb2Message.Length); Smb2Packet packet = DecodeCompletePacket( smb2Message, role, ignoreCompoundFlag, 0, 0, out consumedLength, out expectedLength); // Here we ignore the consumedLength returned by DecodeCompletePacket(), there may be some tcp padding data // at the end which we are not interested. consumedLength = dataLenShouldHave + Smb2Consts.TcpPrefixedLenByteCount; return packet; } else { Smb2Packet packet = DecodeCompletePacket( messageBytes, role, ignoreCompoundFlag, 0, 0, out consumedLength, out expectedLength); //Some packet has unknown padding data at the end. consumedLength = messageBytes.Length; return packet; } }