Ejemplo n.º 1
0
        /// <summary>
        /// Open an NT object with a specified type.
        /// </summary>
        /// <param name="typename">The name of the type to open (e.g. Event). If null the method will try and lookup the appropriate type.</param>
        /// <param name="path">The path to the object to open.</param>
        /// <param name="root">A root directory to open from.</param>
        /// <param name="access">Generic access rights to the object.</param>
        /// <param name="attributes">Attributes to open the object.</param>
        /// <param name="security_quality_of_service">Security quality of service.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The opened object.</returns>
        /// <exception cref="NtException">Thrown if an error occurred opening the object.</exception>
        public static NtResult <NtObject> OpenWithType(string typename, string path, NtObject root,
                                                       AttributeFlags attributes, AccessMask access, SecurityQualityOfService security_quality_of_service, bool throw_on_error)
        {
            using (var obj_attr = new ObjectAttributes(path, attributes, root, security_quality_of_service, null)) {
                if (typename == null)
                {
                    typename = NtDirectory.GetDirectoryEntryType(path, root);
                }

                // Brute force the open.
                if (typename == null)
                {
                    foreach (var nttype in NtType.GetTypes().Where(t => t.CanOpen))
                    {
                        var result = nttype.Open(obj_attr, access, false);
                        if (result.IsSuccess)
                        {
                            return(result);
                        }
                    }

                    return(NtStatus.STATUS_OBJECT_TYPE_MISMATCH.CreateResultFromError <NtObject>(true));
                }

                NtType type = NtType.GetTypeByName(typename, false);
                if (type != null && type.CanOpen)
                {
                    return(type.Open(obj_attr, access, throw_on_error));
                }
                else
                {
                    return(NtStatus.STATUS_OBJECT_TYPE_MISMATCH.CreateResultFromError <NtObject>(true));
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Open an NT object with a specified type.
 /// </summary>
 /// <param name="typename">The name of the type to open (e.g. Event). If null the method will try and lookup the appropriate type.</param>
 /// <param name="path">The path to the object to open.</param>
 /// <param name="root">A root directory to open from.</param>
 /// <param name="access">Generic access rights to the object.</param>
 /// <param name="attributes">Attributes to open the object.</param>
 /// <param name="security_quality_of_service">Security quality of service.</param>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The opened object.</returns>
 /// <exception cref="NtException">Thrown if an error occurred opening the object.</exception>
 public static NtResult <NtObject> OpenWithType(string typename, string path, NtObject root,
                                                AttributeFlags attributes, AccessMask access, SecurityQualityOfService security_quality_of_service, bool throw_on_error)
 {
     using (var obj_attr = new ObjectAttributes(path, attributes, root, security_quality_of_service, null))
     {
         NtType type = NtType.GetTypeByName(typename ?? NtDirectory.GetDirectoryEntryType(path, root), false);
         return(OpenWithType(type, obj_attr, access, throw_on_error));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Open an NT object with a specified type.
        /// </summary>
        /// <param name="typename">The name of the type to open (e.g. Event). If null the method will try and lookup the appropriate type.</param>
        /// <param name="path">The path to the object to open.</param>
        /// <param name="root">A root directory to open from.</param>
        /// <param name="access">Generic access rights to the object.</param>
        /// <returns>The opened object.</returns>
        /// <exception cref="NtException">Thrown if an error occurred opening the object.</exception>
        /// <exception cref="ArgumentException">Thrown if type of resource couldn't be found.</exception>
        public static NtObject OpenWithType(string typename, string path, NtObject root, GenericAccessRights access)
        {
            if (typename == null)
            {
                typename = NtDirectory.GetDirectoryEntryType(path, root);
                if (typename == null)
                {
                    throw new ArgumentException(String.Format("Can't find type for path {0}", path));
                }
            }

            switch (typename.ToLower())
            {
            case "device":
                return(NtFile.Open(path, root, (FileAccessRights)access, FileShareMode.None, FileOpenOptions.None));

            case "file":
                return(NtFile.Open(path, root, (FileAccessRights)access, FileShareMode.Read | FileShareMode.Write | FileShareMode.Delete, FileOpenOptions.None));

            case "event":
                return(NtEvent.Open(path, root, (EventAccessRights)access));

            case "directory":
                return(NtDirectory.Open(path, root, (DirectoryAccessRights)access));

            case "symboliclink":
                return(NtSymbolicLink.Open(path, root, (SymbolicLinkAccessRights)access));

            case "mutant":
                return(NtMutant.Open(path, root, (MutantAccessRights)access));

            case "semaphore":
                return(NtSemaphore.Open(path, root, (SemaphoreAccessRights)access));

            case "section":
                return(NtSection.Open(path, root, (SectionAccessRights)access));

            case "job":
                return(NtJob.Open(path, root, (JobAccessRights)access));

            case "key":
                return(NtKey.Open(path, root, (KeyAccessRights)access));

            default:
                throw new ArgumentException(String.Format("Can't open type {0}", typename));
            }
        }
        /// <summary>
        /// Open an NT object with a specified type.
        /// </summary>
        /// <param name="typename">The name of the type to open (e.g. Event). If null the method will try and lookup the appropriate type.</param>
        /// <param name="path">The path to the object to open.</param>
        /// <param name="root">A root directory to open from.</param>
        /// <param name="access">Generic access rights to the object.</param>
        /// <returns>The opened object.</returns>
        /// <exception cref="NtException">Thrown if an error occurred opening the object.</exception>
        /// <exception cref="ArgumentException">Thrown if type of resource couldn't be found.</exception>
        public static NtObject OpenWithType(string typename, string path, NtObject root, AccessMask access)
        {
            if (typename == null)
            {
                typename = NtDirectory.GetDirectoryEntryType(path, root);
                if (typename == null)
                {
                    throw new ArgumentException(String.Format("Can't find type for path {0}", path));
                }
            }

            NtType type = NtType.GetTypeByName(typename, false);

            if (type != null && type.CanOpen)
            {
                return(type.Open(path, root, access));
            }
            else
            {
                throw new ArgumentException(String.Format("Can't open type {0}", typename));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the NT type from a path.
        /// </summary>
        /// <param name="path">The object manager path.</param>
        /// <param name="root">Optional root object.</param>
        /// <returns>The NT type. Returns null if not available or unknown.</returns>
        public static NtType GetTypeFromPath(string path, NtObject root)
        {
            NtType type = root?.NtType;

            // If a file or a key root then that's what the target must end up being.
            switch (type?.Name)
            {
            case "File":
            case "Key":
                return(type);
            }

            string type_name = NtDirectory.GetDirectoryEntryType(path, root);

            if (type_name != null)
            {
                return(GetTypeByName(type_name, true));
            }
            string full_path = path;

            if (root != null)
            {
                string root_path = root.FullPath;
                full_path = $@"{(root_path == @"\" ? string.Empty : root_path)}\{full_path}";
            }
            if (full_path.Equals(@"\REGISTRY", StringComparison.OrdinalIgnoreCase) ||
                full_path.StartsWith(@"\REGISTRY\", StringComparison.OrdinalIgnoreCase))
            {
                return(GetTypeByType <NtKey>());
            }
            if (full_path.StartsWith(@"\??\") ||
                full_path.StartsWith(@"\GLOBAL??\", StringComparison.OrdinalIgnoreCase) ||
                full_path.StartsWith(@"\Device\", StringComparison.OrdinalIgnoreCase))
            {
                return(GetTypeByType <NtFile>());
            }
            return(null);
        }