/// <summary>
        /// Expect client to connect share "$IPC", tcp or netbios connect is not included
        /// </summary>
        /// <param name="timeout">timeout</param>
        /// <returns>The client endpoint</returns>
        public override FsEndpoint ExpectConnectIpcShare(TimeSpan timeout)
        {
            CifsServerPerConnection   connection;
            SmbNegotiateRequestPacket negotiateRequest = this.cifsServer.ExpectPacket(
                timeout, out connection) as SmbNegotiateRequestPacket;

            //ushort uid = (ushort)Interlocked.Increment(ref nextUid);

            SmbNegotiateResponsePacket negotiateResponse =
                this.cifsServer.CreateNegotiateResponse(connection, negotiateRequest, SecurityModes.NONE);

            this.cifsServer.SendPacket(negotiateResponse, connection);

            SmbSessionSetupAndxRequestPacket sessionSetupRequest =
                this.cifsServer.ExpectPacket(timeout, out connection) as SmbSessionSetupAndxRequestPacket;

            SmbSessionSetupAndxResponsePacket sessionSetupResponse =
                this.cifsServer.CreateSessionSetupAndxResponse(connection, sessionSetupRequest, ActionValues.NONE,
                                                               null);

            this.cifsServer.SendPacket(sessionSetupResponse, connection);

            SmbTreeConnectAndxRequestPacket treeConnectRequest =
                this.cifsServer.ExpectPacket(timeout, out connection) as SmbTreeConnectAndxRequestPacket;


            SmbTreeConnectAndxResponsePacket treeConnectResponse =
                this.cifsServer.CreateTreeConnectAndxResponse(connection, treeConnectRequest, OptionalSupport.NONE,
                                                              IPC_SERVICE_NAME, null);

            this.cifsServer.SendPacket(treeConnectResponse, connection);

            return(this.fsEndpoints[connection.Identity]);
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbSessionSetupAndxResponsePacket(SmbSessionSetupAndxResponsePacket packet)
            : base(packet)
        {
            this.InitDefaultValue();
            this.smbParameters.WordCount = packet.SmbParameters.WordCount;
            this.smbParameters.AndXCommand = packet.SmbParameters.AndXCommand;
            this.smbParameters.AndXReserved = packet.SmbParameters.AndXReserved;
            this.smbParameters.AndXOffset = packet.SmbParameters.AndXOffset;
            this.smbParameters.Action = packet.SmbParameters.Action;
            this.smbData.ByteCount = packet.SmbData.ByteCount;

            if (packet.smbData.Pad != null)
            {
                this.smbData.Pad = new byte[packet.smbData.Pad.Length];
                Array.Copy(packet.smbData.Pad, this.smbData.Pad, packet.smbData.Pad.Length);
            }
            else
            {
                this.smbData.Pad = new byte[0];
            }

            if (packet.smbData.NativeOS != null)
            {
                this.smbData.NativeOS = new byte[packet.smbData.NativeOS.Length];
                Array.Copy(packet.smbData.NativeOS, this.smbData.NativeOS, packet.smbData.NativeOS.Length);
            }
            else
            {
                this.smbData.NativeOS = new byte[0];
            }

            if (packet.smbData.NativeLanMan != null)
            {
                this.smbData.NativeLanMan = new byte[packet.smbData.NativeLanMan.Length];
                Array.Copy(packet.smbData.NativeLanMan, this.smbData.NativeLanMan, packet.smbData.NativeLanMan.Length);
            }
            else
            {
                this.smbData.NativeLanMan = new byte[0];
            }

            if (packet.smbData.PrimaryDomain != null)
            {
                this.smbData.PrimaryDomain = new byte[packet.smbData.PrimaryDomain.Length];
                Array.Copy(packet.smbData.PrimaryDomain,
                    this.smbData.PrimaryDomain, packet.smbData.PrimaryDomain.Length);
            }
            else
            {
                this.smbData.PrimaryDomain = new byte[0];
            }
        }
        /// <summary>
        /// Deep copy constructor.
        /// </summary>
        public SmbSessionSetupAndxResponsePacket(SmbSessionSetupAndxResponsePacket packet)
            : base(packet)
        {
            this.InitDefaultValue();
            this.smbParameters.WordCount    = packet.SmbParameters.WordCount;
            this.smbParameters.AndXCommand  = packet.SmbParameters.AndXCommand;
            this.smbParameters.AndXReserved = packet.SmbParameters.AndXReserved;
            this.smbParameters.AndXOffset   = packet.SmbParameters.AndXOffset;
            this.smbParameters.Action       = packet.SmbParameters.Action;
            this.smbData.ByteCount          = packet.SmbData.ByteCount;

            if (packet.smbData.Pad != null)
            {
                this.smbData.Pad = new byte[packet.smbData.Pad.Length];
                Array.Copy(packet.smbData.Pad, this.smbData.Pad, packet.smbData.Pad.Length);
            }
            else
            {
                this.smbData.Pad = new byte[0];
            }

            if (packet.smbData.NativeOS != null)
            {
                this.smbData.NativeOS = new byte[packet.smbData.NativeOS.Length];
                Array.Copy(packet.smbData.NativeOS, this.smbData.NativeOS, packet.smbData.NativeOS.Length);
            }
            else
            {
                this.smbData.NativeOS = new byte[0];
            }

            if (packet.smbData.NativeLanMan != null)
            {
                this.smbData.NativeLanMan = new byte[packet.smbData.NativeLanMan.Length];
                Array.Copy(packet.smbData.NativeLanMan, this.smbData.NativeLanMan, packet.smbData.NativeLanMan.Length);
            }
            else
            {
                this.smbData.NativeLanMan = new byte[0];
            }

            if (packet.smbData.PrimaryDomain != null)
            {
                this.smbData.PrimaryDomain = new byte[packet.smbData.PrimaryDomain.Length];
                Array.Copy(packet.smbData.PrimaryDomain,
                           this.smbData.PrimaryDomain, packet.smbData.PrimaryDomain.Length);
            }
            else
            {
                this.smbData.PrimaryDomain = new byte[0];
            }
        }
示例#4
0
        /// <summary>
        /// Set up connection with server.
        /// Including 4 steps: 1. Tcp connection 2. Negotiation 3. SessionSetup 4. TreeConnect in order
        /// </summary>
        /// <param name="server">server name of ip address</param>
        /// <param name="client">client name of ip address</param>
        /// <param name="domain">user's domain</param>
        /// <param name="userName">user's name</param>
        /// <param name="password">user's password</param>
        /// <param name="timeout">The pending time to get server's response in step 2, 3 or 4</param>
        /// <exception cref="System.Net.ProtocolViolationException">Fail to set up connection with server</exception>
        public override void Connect(
            string server,
            string client,
            string domain,
            string userName,
            string password,
            TimeSpan timeout,
            SecurityPackageType securityPackage = SecurityPackageType.Ntlm,
            bool useServerToken = false)
        {
            this.cifsClient.Connect(server, client);

            SmbPacket request, response;
            uint      status;

            // Negotiate:
            SMB_Dialect dialectLM21 = new SMB_Dialect();

            dialectLM21.BufferFormat  = dialectBufferFormat;
            dialectLM21.DialectString = dialectLanMan21;
            SMB_Dialect dialectNTLM = new SMB_Dialect();

            dialectNTLM.BufferFormat  = dialectBufferFormat;
            dialectNTLM.DialectString = dialectNtLanMan;
            request = this.cifsClient.CreateNegotiateRequest(new SMB_Dialect[] { dialectLM21, dialectNTLM });
            this.cifsClient.SendPacket(request);
            response = this.cifsClient.ExpectPacket(timeout);
            status   = (response as SmbNegotiateResponsePacket).SmbHeader.Status;

            if (status != 0)
            {
                throw new ProtocolViolationException("Negotiate Failed. ErrorCode: " + status);
            }

            // Session setup:
            CifsUserAccount userAccount = new CifsUserAccount(domain, userName, password);

            request = this.cifsClient.CreateSessionSetupRequest(userAccount, nativeOS, nativeLanMan);
            this.cifsClient.SendPacket(request);
            response = this.cifsClient.ExpectPacket(timeout);
            SmbSessionSetupAndxResponsePacket sessionSetupResponse = response as SmbSessionSetupAndxResponsePacket;

            status = sessionSetupResponse.SmbHeader.Status;

            if (status != 0)
            {
                throw new ProtocolViolationException("Session Setup Failed. ErrorCode: " + status);
            }
            this.uid = sessionSetupResponse.SmbHeader.Uid;

            // Tree connect:
            string ipcAddress = "\\\\" + server + '\\' + ipcConnectString;

            request = this.cifsClient.CreateTreeConnectAndxRequest(this.uid, TreeConnectAndxFlags.NONE,
                                                                   ipcAddress, treeConnectService, null, null);
            this.cifsClient.SendPacket(request);
            response = this.cifsClient.ExpectPacket(timeout);
            SmbTreeConnectAndxResponsePacket treeConnectResponse = response as SmbTreeConnectAndxResponsePacket;

            status = treeConnectResponse.SmbHeader.Status;

            if (status != 0)
            {
                throw new ProtocolViolationException("Tree Connect Failed. ErrorCode: " + status);
            }
            this.treeId = treeConnectResponse.SmbHeader.Tid;
        }
        public SmbSessionSetupAndxResponsePacket CreateSessionSetupAndxResponse(
            CifsServerPerConnection connection,
            SmbSessionSetupAndxRequestPacket request,
            ActionValues action,
            SmbPacket andxPacket)
        {
            SmbSessionSetupAndxResponsePacket response = new SmbSessionSetupAndxResponsePacket();
            SmbHeader smbHeader = CifsMessageUtils.CreateSmbHeader(connection, request);
            smbHeader.Uid = connection.GenerateUID();
            response.SmbHeader = smbHeader;

            bool isUnicode = (response.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE)
                == SmbFlags2.SMB_FLAGS2_UNICODE;
            byte[] oS = CifsMessageUtils.ToSmbStringBytes(CifsMessageUtils.NATIVE_OS, isUnicode);
            byte[] lanMan = CifsMessageUtils.ToSmbStringBytes(CifsMessageUtils.NATIVE_LANMAN, isUnicode);
            byte[] domain = CifsMessageUtils.ToSmbStringBytes(this.context.DomainName, isUnicode);
            int padOffset = Marshal.SizeOf(response.SmbParameters) + sizeof(ushort);
            SMB_COM_SESSION_SETUP_ANDX_Response_SMB_Data smbData = response.SmbData;
            smbData.Pad = new byte[(padOffset + 3) & ~3];
            smbData.NativeOS = oS;
            smbData.NativeLanMan = lanMan;
            smbData.PrimaryDomain = domain;
            smbData.ByteCount = (ushort)(((padOffset + 3) & ~3) + oS.Length + lanMan.Length + domain.Length);
            response.SmbData = smbData;

            SMB_COM_SESSION_SETUP_ANDX_Response_SMB_Parameters smbParameters = response.SmbParameters;
            smbParameters.AndXCommand =
                andxPacket != null ? andxPacket.SmbHeader.Command : SmbCommand.SMB_COM_NO_ANDX_COMMAND;
            smbParameters.AndXReserved = 0x00;
            smbParameters.AndXOffset = (ushort)(response.HeaderSize + Marshal.SizeOf(response.SmbParameters)
                    + Marshal.SizeOf(response.SmbData));
            smbParameters.Action = action;
            smbParameters.WordCount = (byte)(TypeMarshal.GetBlockMemorySize(smbParameters) / 2);
            response.SmbParameters = smbParameters;

            response.AndxPacket = andxPacket;
            response.UpdateHeader();

            return response;
        }