示例#1
0
        private bool NegotiateNTLanManagerDialect()
        {
            if (m_transport == SMBTransportType.NetBiosOverTCP)
            {
                SessionRequestPacket sessionRequest = new SessionRequestPacket();
                sessionRequest.CalledName  = NetBiosUtils.GetMSNetBiosName("*SMBSERVER", NetBiosSuffix.FileServiceService);;
                sessionRequest.CallingName = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService);
                TrySendPacket(m_clientSocket, sessionRequest);
            }
            NegotiateRequest request = new NegotiateRequest();

            request.Dialects.Add(NTLanManagerDialect);
            TrySendMessage(m_clientSocket, request);
            SMB1Message reply = WaitForMessage(CommandName.SMB_COM_NEGOTIATE);

            if (reply == null)
            {
                return(false);
            }

            if (reply.Commands[0] is NegotiateResponse)
            {
                NegotiateResponse response = (NegotiateResponse)reply.Commands[0];
                return(true);
            }
            else if (reply.Commands[0] is NegotiateResponseExtended)
            {
                NegotiateResponseExtended response = (NegotiateResponseExtended)reply.Commands[0];
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#2
0
        public bool Connect(IPAddress serverAddress, SMBTransportType transport, bool forceExtendedSecurity, string serverName = null)
        {
            m_transport = transport;
            if (!m_isConnected)
            {
                m_forceExtendedSecurity = forceExtendedSecurity;
                int port;
                port = transport == SMBTransportType.NetBiosOverTCP ? NetBiosOverTCPPort : DirectTCPPort;

                if (!ConnectSocket(serverAddress, port))
                {
                    return(false);
                }

                if (transport == SMBTransportType.NetBiosOverTCP)
                {
                    SessionRequestPacket sessionRequest = new SessionRequestPacket();
                    sessionRequest.CalledName  = NetBiosUtils.GetMSNetBiosName(serverName ?? "*SMBSERVER", NetBiosSuffix.FileServiceService);
                    sessionRequest.CallingName = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService);
                    TrySendPacket(m_clientSocket, sessionRequest);

                    SessionPacket sessionResponsePacket = WaitForSessionResponsePacket();
                    if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                    {
                        m_clientSocket.Disconnect(false);
                        if (!ConnectSocket(serverAddress, port))
                        {
                            return(false);
                        }

                        NameServiceClient nameServiceClient = new NameServiceClient(serverAddress);
                        serverName = nameServiceClient.GetServerName();
                        if (serverName == null)
                        {
                            return(false);
                        }

                        sessionRequest.CalledName = serverName;
                        TrySendPacket(m_clientSocket, sessionRequest);

                        sessionResponsePacket = WaitForSessionResponsePacket();
                        if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                        {
                            return(false);
                        }
                    }
                }

                bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity);
                if (!supportsDialect)
                {
                    m_clientSocket.Close();
                }
                else
                {
                    m_isConnected = true;
                }
            }
            return(m_isConnected);
        }
示例#3
0
        private bool NegotiateDialect(bool forceExtendedSecurity)
        {
            if (m_transport == SMBTransportType.NetBiosOverTCP)
            {
                SessionRequestPacket sessionRequest = new SessionRequestPacket();
                sessionRequest.CalledName  = NetBiosUtils.GetMSNetBiosName("*SMBSERVER", NetBiosSuffix.FileServiceService);;
                sessionRequest.CallingName = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService);
                TrySendPacket(m_clientSocket, sessionRequest);
            }
            NegotiateRequest request = new NegotiateRequest();

            request.Dialects.Add(NTLanManagerDialect);

            TrySendMessage(request);
            SMB1Message reply = WaitForMessage(CommandName.SMB_COM_NEGOTIATE);

            if (reply == null)
            {
                return(false);
            }

            if (reply.Commands[0] is NegotiateResponse && !forceExtendedSecurity)
            {
                NegotiateResponse response = (NegotiateResponse)reply.Commands[0];
                m_unicode    = ((response.Capabilities & Capabilities.Unicode) > 0);
                m_largeFiles = ((response.Capabilities & Capabilities.LargeFiles) > 0);
                bool ntSMB        = ((response.Capabilities & Capabilities.NTSMB) > 0);
                bool rpc          = ((response.Capabilities & Capabilities.RpcRemoteApi) > 0);
                bool ntStatusCode = ((response.Capabilities & Capabilities.NTStatusCode) > 0);
                m_infoLevelPassthrough = ((response.Capabilities & Capabilities.InfoLevelPassthrough) > 0);
                m_largeRead            = ((response.Capabilities & Capabilities.LargeRead) > 0);
                m_largeWrite           = ((response.Capabilities & Capabilities.LargeWrite) > 0);
                m_serverMaxBufferSize  = response.MaxBufferSize;
                m_maxMpxCount          = Math.Min(response.MaxMpxCount, ClientMaxMpxCount);
                m_serverChallenge      = response.Challenge;
                return(ntSMB && rpc && ntStatusCode);
            }
            else if (reply.Commands[0] is NegotiateResponseExtended)
            {
                NegotiateResponseExtended response = (NegotiateResponseExtended)reply.Commands[0];
                m_unicode    = ((response.Capabilities & Capabilities.Unicode) > 0);
                m_largeFiles = ((response.Capabilities & Capabilities.LargeFiles) > 0);
                bool ntSMB        = ((response.Capabilities & Capabilities.NTSMB) > 0);
                bool rpc          = ((response.Capabilities & Capabilities.RpcRemoteApi) > 0);
                bool ntStatusCode = ((response.Capabilities & Capabilities.NTStatusCode) > 0);
                m_infoLevelPassthrough = ((response.Capabilities & Capabilities.InfoLevelPassthrough) > 0);
                m_largeRead            = ((response.Capabilities & Capabilities.LargeRead) > 0);
                m_largeWrite           = ((response.Capabilities & Capabilities.LargeWrite) > 0);
                m_serverMaxBufferSize  = response.MaxBufferSize;
                m_maxMpxCount          = Math.Min(response.MaxMpxCount, ClientMaxMpxCount);
                m_securityBlob         = response.SecurityBlob;
                return(ntSMB && rpc && ntStatusCode);
            }
            else
            {
                return(false);
            }
        }
        private void ProcessSessionRequestPacket(IPacket packet)
        {
            SessionRequestPacket p = (SessionRequestPacket)packet;

            if (AuthQueue.CheckAuth(p.Username, p.SessionID))
            {
                SendPacket(new SessionResponsePacket(p.Username, SessionResponse.Ok));
            }
            else if (AuthQueue.IsAlreadyLoggedIn(p.Username))
            {
                SendPacket(new SessionResponsePacket(p.Username, SessionResponse.AlreadyLoggedIn));
            }
            else
            {
                SendPacket(new SessionResponsePacket(p.Username, SessionResponse.Wrong));
            }
        }
示例#5
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void Ssn139()
        {
            Name calledName = new Name(Address.FirstCalledName(), 0x20, null);

            do
            {
                Socket = new SocketEx(AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);

                //TCPローカルポートは、毎回空いているものを使う。
                //https://blogs.msdn.microsoft.com/dgorti/2005/09/18/only-one-usage-of-each-socket-address-protocolnetwork-addressport-is-normally-permitted/
                Socket.Bind(new IPEndPoint(LocalAddr, 0));

                Socket.Connect(new IPEndPoint(IPAddress.Parse(Address.GetHostAddress()),
                                              139),
                               SmbConstants.ConnTimeout);

                Socket.SoTimeOut = SmbConstants.SoTimeout;

                Out = Socket.GetOutputStream();
                In  = Socket.GetInputStream();
                SessionServicePacket ssp = new SessionRequestPacket(calledName,
                                                                    NbtAddress.GetLocalName());
                Out.Write(Sbuf, 0, ssp.WriteWireFormat(Sbuf, 0));
                if (Readn(In, Sbuf, 0, 4) < 4)
                {
                    try
                    {
                        //Socket.`Close` method deleted
                        //Socket.Close();
                        Socket.Dispose();
                    }
                    catch (IOException)
                    {
                    }
                    throw new SmbException("EOF during NetBIOS session request");
                }
                switch (Sbuf[0] & 0xFF)
                {
                case SessionServicePacket.PositiveSessionResponse:
                {
                    if (Log.Level >= 4)
                    {
                        Log.WriteLine("session established ok with " + Address);
                    }
                    return;
                }

                case SessionServicePacket.NegativeSessionResponse:
                {
                    int errorCode = In.Read() & 0xFF;
                    switch (errorCode)
                    {
                    case NbtException.CalledNotPresent:
                    case NbtException.NotListeningCalled:
                    {
                        //Socket.`Close` method deleted
                        //Socket.Close();
                        Socket.Dispose();
                        break;
                    }

                    default:
                    {
                        Disconnect(true);
                        throw new NbtException(NbtException.ErrSsnSrvc,
                                               errorCode);
                    }
                    }
                    break;
                }

                case -1:
                {
                    Disconnect(true);
                    throw new NbtException(NbtException.ErrSsnSrvc,
                                           NbtException.ConnectionRefused);
                }

                default:
                {
                    Disconnect(true);
                    throw new NbtException(NbtException.ErrSsnSrvc, 0);
                }
                }
            }while ((calledName.name = Address.NextCalledName()) != null);
            throw new IOException("Failed to establish session with " + Address);
        }
示例#6
0
        public bool Connect(IPAddress serverAddress, SMBTransportType transport)
        {
            // Sometimes underline socket is disconnected, but m_isConnected flag is still true.
            // This cause the caller try to reuse the client and fail on all calls.
            if (m_clientSocket != null && !m_clientSocket.Connected)
            {
                // Since reconnect doesn't work for now, return false directly make response faster
                return(false);
            }

            m_transport = transport;
            if (!m_isConnected)
            {
                int port;
                if (transport == SMBTransportType.NetBiosOverTCP)
                {
                    port = NetBiosOverTCPPort;
                }
                else
                {
                    port = DirectTCPPort;
                }

                if (!ConnectSocket(serverAddress, port))
                {
                    return(false);
                }

                if (transport == SMBTransportType.NetBiosOverTCP)
                {
                    SessionRequestPacket sessionRequest = new SessionRequestPacket();
                    sessionRequest.CalledName  = NetBiosUtils.GetMSNetBiosName("*SMBSERVER", NetBiosSuffix.FileServiceService);
                    sessionRequest.CallingName = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService);
                    TrySendPacket(m_clientSocket, sessionRequest);

                    SessionPacket sessionResponsePacket = WaitForSessionResponsePacket();
                    if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                    {
                        m_clientSocket.Disconnect(false);
                        if (!ConnectSocket(serverAddress, port))
                        {
                            return(false);
                        }

                        NameServiceClient nameServiceClient = new NameServiceClient(serverAddress);
                        string            serverName        = nameServiceClient.GetServerName();
                        if (serverName == null)
                        {
                            return(false);
                        }

                        sessionRequest.CalledName = serverName;
                        TrySendPacket(m_clientSocket, sessionRequest);

                        sessionResponsePacket = WaitForSessionResponsePacket();
                        if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                        {
                            return(false);
                        }
                    }
                }

                bool supportsDialect = NegotiateDialect();
                if (!supportsDialect)
                {
                    m_clientSocket.Close();
                }
                else
                {
                    m_isConnected = true;
                }
            }
            return(m_isConnected);
        }
示例#7
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void Ssn139()
        {
            Name calledName = new Name(Address.FirstCalledName(), 0x20, null
                                       );

            do
            {
                Socket = new SocketEx(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                if (LocalAddr != null)
                {
                    Socket.Bind2(new IPEndPoint(LocalAddr, LocalPort));
                }
                Socket.Connect(Address.GetHostAddress(), 139);
                //Socket.Connect(new IPEndPoint(IPAddress.Parse(Address.GetHostAddress()), 139), SmbConstants.ConnTimeout);
                Socket.SoTimeOut = SmbConstants.SoTimeout;

                Out = Socket.GetOutputStream();
                In  = Socket.GetInputStream();
                SessionServicePacket ssp = new SessionRequestPacket(calledName, NbtAddress.GetLocalName
                                                                        ());
                Out.Write(Sbuf, 0, ssp.WriteWireFormat(Sbuf, 0));
                if (Readn(In, Sbuf, 0, 4) < 4)
                {
                    try
                    {
                        Socket.Dispose();
                    }
                    catch (IOException)
                    {
                    }
                    throw new SmbException("EOF during NetBIOS session request");
                }
                switch (Sbuf[0] & 0xFF)
                {
                case SessionServicePacket.PositiveSessionResponse:
                {
                    if (Log.Level >= 4)
                    {
                        Log.WriteLine("session established ok with " + Address);
                    }
                    return;
                }

                case SessionServicePacket.NegativeSessionResponse:
                {
                    int errorCode = In.Read() & 0xFF;
                    switch (errorCode)
                    {
                    case NbtException.CalledNotPresent:
                    case NbtException.NotListeningCalled:
                    {
                        Socket.Dispose();
                        break;
                    }

                    default:
                    {
                        Disconnect(true);
                        throw new NbtException(NbtException.ErrSsnSrvc, errorCode);
                    }
                    }
                    break;
                }

                case -1:
                {
                    Disconnect(true);
                    throw new NbtException(NbtException.ErrSsnSrvc, NbtException.ConnectionRefused
                                           );
                }

                default:
                {
                    Disconnect(true);
                    throw new NbtException(NbtException.ErrSsnSrvc, 0);
                }
                }
            }while ((calledName.name = Address.NextCalledName()) != null);
            throw new IOException("Failed to establish session with " + Address);
        }
示例#8
0
        private bool Connect(IPAddress serverAddress, SMBTransportType transport, bool forceExtendedSecurity)
        {
            m_transport = transport;
            if (IsConnected)
            {
                return(true);
            }

            m_forceExtendedSecurity = forceExtendedSecurity;
            int port = transport == SMBTransportType.NetBiosOverTcp ? NetBiosOverTCPPort : DirectTCPPort;

            ConnectSocket(serverAddress, port);

            if (transport == SMBTransportType.NetBiosOverTcp)
            {
                SessionRequestPacket sessionRequest = new SessionRequestPacket
                {
                    CalledName  = NetBiosUtils.GetMSNetBiosName("*SMBSERVER", NetBiosSuffix.FileServiceService),
                    CallingName = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService)
                };
                SendPacket(m_clientSocket !, sessionRequest);

                SessionPacket?sessionResponsePacket = WaitForSessionResponsePacket();
                if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                {
                    m_clientSocket?.Shutdown(SocketShutdown.Both);
                    m_clientSocket?.Dispose();
                    m_clientSocket = null;

                    ConnectSocket(serverAddress, port);

                    NameServiceClient nameServiceClient = new NameServiceClient(serverAddress);
                    string?           serverName        = nameServiceClient.GetServerName();
                    if (serverName == null)
                    {
                        return(false);
                    }

                    sessionRequest.CalledName = serverName;
                    SendPacket(m_clientSocket, sessionRequest);

                    sessionResponsePacket = WaitForSessionResponsePacket();
                    if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                    {
                        return(false);
                    }
                }
            }

            bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity);

            if (!supportsDialect)
            {
                m_clientSocket?.Shutdown(SocketShutdown.Both);
                m_clientSocket?.Dispose();
                m_clientSocket = null;
            }
            else
            {
                m_isConnected = true;
            }
            return(m_isConnected);
        }
示例#9
0
        public async Task <bool> ConnectAsync(IPAddress serverAddress, SMBTransportType transport, CancellationToken cancellationToken)
        {
            m_transport = transport;
            if (!m_isConnected)
            {
                int port;
                if (transport == SMBTransportType.NetBiosOverTCP)
                {
                    port = NetBiosOverTCPPort;
                }
                else
                {
                    port = DirectTCPPort;
                }

                if (!ConnectSocket(serverAddress, port))
                {
                    return(false);
                }

                if (transport == SMBTransportType.NetBiosOverTCP)
                {
                    SessionRequestPacket sessionRequest = new SessionRequestPacket();
                    sessionRequest.CalledName  = NetBiosUtils.GetMSNetBiosName("*SMBSERVER", NetBiosSuffix.FileServiceService);
                    sessionRequest.CallingName = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService);
                    await TrySendPacketAsync(m_clientSocket, sessionRequest, cancellationToken);

                    SessionPacket sessionResponsePacket = WaitForSessionResponsePacket();
                    if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                    {
                        m_clientSocket.Disconnect(false);
                        if (!ConnectSocket(serverAddress, port))
                        {
                            return(false);
                        }

                        NameServiceClient nameServiceClient = new NameServiceClient(serverAddress);
                        string            serverName        = nameServiceClient.GetServerName();
                        if (serverName == null)
                        {
                            return(false);
                        }

                        sessionRequest.CalledName = serverName;
                        await TrySendPacketAsync(m_clientSocket, sessionRequest, cancellationToken);

                        sessionResponsePacket = WaitForSessionResponsePacket();
                        if (!(sessionResponsePacket is PositiveSessionResponsePacket))
                        {
                            return(false);
                        }
                    }
                }

                bool supportsDialect = await NegotiateDialectAsync(cancellationToken);

                if (!supportsDialect)
                {
                    m_clientSocket.Close();
                }
                else
                {
                    m_isConnected = true;
                }
            }
            return(m_isConnected);
        }