/// <summary>
        /// update the context with response packet
        /// </summary>
        /// <param name="connection">the connection of endpoint</param>
        /// <param name="packet">the packet to update the context</param>
        private void ResponsePacketUpdateRoleContext(SmbServerConnection connection, SmbPacket packet)
        {
            SmbHeader smbHeader = packet.SmbHeader;

            SmbPacket requestPacket = connection.GetRequestPacket(smbHeader.Mid);
            if (requestPacket == null)
            {
                return;
            }

            switch (smbHeader.Command)
            {
                case SmbCommand.SMB_COM_SESSION_SETUP_ANDX:
                    if (smbHeader.Uid == 0)
                    {
                        break;
                    }
                    else
                    {
                        SmbServerSession session = new SmbServerSession();
                        session.Uid = smbHeader.Uid;
                        session.AuthenticationState = SessionState.Complete;
                        session.Connection = connection;
                        session.SessionKey = connection.GssApi.SessionKey;

                        connection.AddSession(session);
                    }

                    break;

                case SmbCommand.SMB_COM_LOGOFF_ANDX:
                    if (requestPacket.SmbHeader.Uid == 0)
                    {
                        break;
                    }
                    else
                    {
                        connection.RemoveSession(connection.GetSession(requestPacket.SmbHeader.Uid));
                    }

                    break;

                case SmbCommand.SMB_COM_TREE_CONNECT_ANDX:
                    {
                        SmbTreeConnectAndxRequestPacket request = requestPacket as SmbTreeConnectAndxRequestPacket;

                        SmbServerTreeConnect treeconnect = new SmbServerTreeConnect();
                        treeconnect.TreeId = smbHeader.Tid;
                        treeconnect.Session = connection.GetSession(smbHeader.Uid);
                        if (treeconnect.Session == null)
                        {
                            break;
                        }
                        if ((request.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) != 0)
                        {
                            treeconnect.Path = Encoding.Unicode.GetString(request.SmbData.Path);
                        }
                        else
                        {
                            treeconnect.Path = Encoding.ASCII.GetString(request.SmbData.Path);
                        }
                        treeconnect.Path = treeconnect.Path.TrimEnd('\0');
                        treeconnect.Session.AddTreeConnect(treeconnect);
                    }

                    break;

                case SmbCommand.SMB_COM_TREE_DISCONNECT:
                    if (requestPacket.SmbHeader.Uid != 0)
                    {
                        SmbServerSession session = connection.GetSession(requestPacket.SmbHeader.Uid);
                        if (session == null)
                        {
                            break;
                        }
                        session.RemoveTreeConnect(session.GetTreeConnect(requestPacket.SmbHeader.Tid));
                    }

                    break;

                case SmbCommand.SMB_COM_NT_CREATE_ANDX:
                    {
                        SmbNtCreateAndxResponsePacket response = packet as SmbNtCreateAndxResponsePacket;
                        SmbNtCreateAndxRequestPacket request = requestPacket as SmbNtCreateAndxRequestPacket;

                        SmbServerOpen open = new SmbServerOpen();
                        open.SmbFid = response.SmbParameters.FID;
                        open.PathName = SmbMessageUtils.GetString(request.SmbData.FileName,
                            SmbFlags2.SMB_FLAGS2_UNICODE == (request.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE));
                        open.PathName = open.PathName.TrimEnd('\0');
                        open.Session = connection.GetSession(smbHeader.Uid);
                        open.TreeConnect = connection.GetTreeConnect(smbHeader.Tid);
                        if (open.TreeConnect == null)
                        {
                            break;
                        }
                        open.TreeConnect.AddOpen(open);
                    }

                    break;

                case SmbCommand.SMB_COM_OPEN_ANDX:
                    {
                        SmbOpenAndxResponsePacket response = packet as SmbOpenAndxResponsePacket;
                        SmbOpenAndxRequestPacket request = requestPacket as SmbOpenAndxRequestPacket;

                        SmbServerOpen open = new SmbServerOpen();
                        open.SmbFid = response.SmbParameters.FID;
                        open.PathName = SmbMessageUtils.GetString(request.SmbData.FileName,
                            SmbFlags2.SMB_FLAGS2_UNICODE == (request.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE));
                        open.Session = connection.GetSession(smbHeader.Uid);
                        open.TreeConnect = connection.GetTreeConnect(smbHeader.Tid);
                        if (open.TreeConnect == null)
                        {
                            break;
                        }
                        open.TreeConnect.AddOpen(open);
                    }

                    break;

                case SmbCommand.SMB_COM_CLOSE:
                    {
                        SmbCloseRequestPacket closeRequest = requestPacket as SmbCloseRequestPacket;

                        SmbServerTreeConnect treeconnect = connection.GetTreeConnect(requestPacket.SmbHeader.Tid);
                        if (treeconnect == null)
                        {
                            break;
                        }

                        treeconnect.RemoveOpen(treeconnect.GetOpen(closeRequest.SmbParameters.FID));
                    }

                    break;

                default:
                    break;
            }

            connection.RemoveRequestPacket(packet);
        }
Exemple #2
0
        /// <summary>
        /// update the context with response packet
        /// </summary>
        /// <param name="connection">the connection of endpoint</param>
        /// <param name="packet">the packet to update the context</param>
        private void ResponsePacketUpdateRoleContext(SmbServerConnection connection, SmbPacket packet)
        {
            SmbHeader smbHeader = packet.SmbHeader;

            SmbPacket requestPacket = connection.GetRequestPacket(smbHeader.Mid);

            if (requestPacket == null)
            {
                return;
            }

            switch (smbHeader.Command)
            {
            case SmbCommand.SMB_COM_SESSION_SETUP_ANDX:
                if (smbHeader.Uid == 0)
                {
                    break;
                }
                else
                {
                    SmbServerSession session = new SmbServerSession();
                    session.Uid = smbHeader.Uid;
                    session.AuthenticationState = SessionState.Complete;
                    session.Connection          = connection;
                    session.SessionKey          = connection.GssApi.SessionKey;

                    connection.AddSession(session);
                }

                break;

            case SmbCommand.SMB_COM_LOGOFF_ANDX:
                if (requestPacket.SmbHeader.Uid == 0)
                {
                    break;
                }
                else
                {
                    connection.RemoveSession(connection.GetSession(requestPacket.SmbHeader.Uid));
                }

                break;

            case SmbCommand.SMB_COM_TREE_CONNECT_ANDX:
            {
                SmbTreeConnectAndxRequestPacket request = requestPacket as SmbTreeConnectAndxRequestPacket;

                SmbServerTreeConnect treeconnect = new SmbServerTreeConnect();
                treeconnect.TreeId  = smbHeader.Tid;
                treeconnect.Session = connection.GetSession(smbHeader.Uid);
                if (treeconnect.Session == null)
                {
                    break;
                }
                if ((request.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) != 0)
                {
                    treeconnect.Path = Encoding.Unicode.GetString(request.SmbData.Path);
                }
                else
                {
                    treeconnect.Path = Encoding.ASCII.GetString(request.SmbData.Path);
                }
                treeconnect.Path = treeconnect.Path.TrimEnd('\0');
                treeconnect.Session.AddTreeConnect(treeconnect);
            }

            break;

            case SmbCommand.SMB_COM_TREE_DISCONNECT:
                if (requestPacket.SmbHeader.Uid != 0)
                {
                    SmbServerSession session = connection.GetSession(requestPacket.SmbHeader.Uid);
                    if (session == null)
                    {
                        break;
                    }
                    session.RemoveTreeConnect(session.GetTreeConnect(requestPacket.SmbHeader.Tid));
                }

                break;

            case SmbCommand.SMB_COM_NT_CREATE_ANDX:
            {
                SmbNtCreateAndxResponsePacket response = packet as SmbNtCreateAndxResponsePacket;
                SmbNtCreateAndxRequestPacket  request  = requestPacket as SmbNtCreateAndxRequestPacket;

                SmbServerOpen open = new SmbServerOpen();
                open.SmbFid   = response.SmbParameters.FID;
                open.PathName = SmbMessageUtils.GetString(request.SmbData.FileName,
                                                          SmbFlags2.SMB_FLAGS2_UNICODE == (request.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE));
                open.PathName    = open.PathName.TrimEnd('\0');
                open.Session     = connection.GetSession(smbHeader.Uid);
                open.TreeConnect = connection.GetTreeConnect(smbHeader.Tid);
                if (open.TreeConnect == null)
                {
                    break;
                }
                open.TreeConnect.AddOpen(open);
            }

            break;

            case SmbCommand.SMB_COM_OPEN_ANDX:
            {
                SmbOpenAndxResponsePacket response = packet as SmbOpenAndxResponsePacket;
                SmbOpenAndxRequestPacket  request  = requestPacket as SmbOpenAndxRequestPacket;

                SmbServerOpen open = new SmbServerOpen();
                open.SmbFid   = response.SmbParameters.FID;
                open.PathName = SmbMessageUtils.GetString(request.SmbData.FileName,
                                                          SmbFlags2.SMB_FLAGS2_UNICODE == (request.SmbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE));
                open.Session     = connection.GetSession(smbHeader.Uid);
                open.TreeConnect = connection.GetTreeConnect(smbHeader.Tid);
                if (open.TreeConnect == null)
                {
                    break;
                }
                open.TreeConnect.AddOpen(open);
            }

            break;

            case SmbCommand.SMB_COM_CLOSE:
            {
                SmbCloseRequestPacket closeRequest = requestPacket as SmbCloseRequestPacket;

                SmbServerTreeConnect treeconnect = connection.GetTreeConnect(requestPacket.SmbHeader.Tid);
                if (treeconnect == null)
                {
                    break;
                }

                treeconnect.RemoveOpen(treeconnect.GetOpen(closeRequest.SmbParameters.FID));
            }

            break;

            default:
                break;
            }

            connection.RemoveRequestPacket(packet);
        }