private IEnumerable <string> GetObjectList(string typename)
        {
            HashSet <string> walked = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            HashSet <string> names  = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            try
            {
                ObjectDirectory basedir = ObjectNamespace.OpenDirectory(null, "\\");
                UpdateObjectList(typename, walked, basedir, names);
            }
            catch (NtException)
            {
            }

            try
            {
                ObjectDirectory sessiondir = ObjectNamespace.OpenSessionDirectory();
                UpdateObjectList(typename, walked, sessiondir, names);
            }
            catch (NtException)
            {
            }

            List <string> ret = new List <string>(names);

            ret.Sort();

            return(ret);
        }
Beispiel #2
0
        internal virtual void InitNamespace(IObjectNamespaceSetting spaceSetting, string parentName, Dictionary <ObjectNamespace, IObjectNamespaceSetting> list)
        {
            var spaceName = spaceSetting.Name;

            if (!string.IsNullOrEmpty(parentName))
            {
                spaceName = parentName + "." + spaceName;
            }
            if (!this.objectNamespaces.ContainsKey(spaceName))
            {
                var space = new ObjectNamespace(spaceName);
                this.objectNamespaces.Add(spaceName, space);
                list.Add(space, spaceSetting);
            }
            var spaceSettings = spaceSetting.ObjectNamespaces;

            if (spaceSettings == null || spaceSettings.Length <= 0)
            {
                return;
            }
            foreach (var setting in spaceSettings)
            {
                this.InitNamespace(setting, spaceName, list);
            }
        }
        private void UpdateObjectList(string typename, HashSet <string> walked, ObjectDirectory dir, HashSet <string> names)
        {
            if (walked.Contains(dir.FullPath.ToLower()))
            {
                return;
            }

            walked.Add(dir.FullPath.ToLower());

            try
            {
                foreach (ObjectDirectoryEntry entry in dir.Entries)
                {
                    try
                    {
                        if (entry.TypeName.Equals(typename, StringComparison.OrdinalIgnoreCase))
                        {
                            names.Add(entry.FullPath);
                        }
                        else if (entry.IsDirectory)
                        {
                            UpdateObjectList(typename, walked, ObjectNamespace.OpenDirectory(null, entry.FullPath), names);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
        }
        private static void PopulateRootNamespace(ObjectRootNamespace root, List <string> namespaces)
        {
            //Manually apply the first sub namespace....
            ObjectNamespace targetSub = root.SubNamespaces.FirstOrDefault(ns => ns.Name == namespaces[0]);

            if (targetSub == null)
            {
                targetSub = new ObjectNamespace
                {
                    Name = namespaces[0]
                };

                root.SubNamespaces.Add(targetSub);
            }

            //Now recursively apply the namespaces for this object.
            List <string> adjustedSubNamespaces = namespaces.Skip(1).ToList();

            foreach (string subName in adjustedSubNamespaces)
            {
                ObjectNamespace newSub = targetSub.SubNamespaces.FirstOrDefault(ns => ns.Name == subName);

                if (newSub == null)
                {
                    newSub = new ObjectNamespace
                    {
                        Name = subName
                    };

                    targetSub.SubNamespaces.Add(newSub);
                }

                targetSub = newSub;
            }
        }
Beispiel #5
0
        internal virtual void InitConstAliases(ObjectNamespace space, IObjectNamespaceSetting setting)
        {
            if (setting.ConstAliases == null || setting.ConstAliases.Length <= 0)
            {
                return;
            }

            var aliases       = space.ConstAliases;
            var aliasesGlobal = this.globalObjectNamespace.ConstAliases;

            var spaceName = space.Name;
            var preName   = spaceName;

            if (!string.IsNullOrEmpty(preName))
            {
                preName += ".";
            }

            foreach (var constSetting in setting.ConstAliases)
            {
                var name  = constSetting.Name;
                var value = Convert.ChangeType(constSetting.Value, TypeHelper.CreateType(this.GetTypeNameInternal(constSetting.TypeName, spaceName), true));
                aliases.Add(name, value);
                if (space != this.globalObjectNamespace)
                {
                    aliasesGlobal.Add(preName + name, value);
                }
            }
        }
Beispiel #6
0
        internal virtual void InitTypeAliases(ObjectNamespace space, IObjectNamespaceSetting setting)
        {
            if (setting.TypeAliases == null || setting.TypeAliases.Length <= 0)
            {
                return;
            }

            var aliases       = space.TypeAliases;
            var aliasesGlobal = this.globalObjectNamespace.TypeAliases;

            var preName = space.Name;

            if (!string.IsNullOrEmpty(preName))
            {
                preName += ".";
            }

            foreach (var typeSetting in setting.TypeAliases)
            {
                var name  = typeSetting.Name;
                var value = typeSetting.TypeName;
                aliases.Add(name, value);
                if (space != this.globalObjectNamespace)
                {
                    aliasesGlobal.Add(preName + name, value);
                }
            }
        }
Beispiel #7
0
        //初始化对象
        private void InitObjectAliases(ObjectNamespace space, IObjectNamespaceSetting setting, Func <Type, IObjectContainer> predicate)
        {
            if (space == null || setting == null || setting.Objects == null || setting.Objects.Length <= 0)
            {
                return;
            }

            foreach (var objectSetting in setting.Objects)
            {
                var objectType = this.GetOrCreateType(objectSetting.TypeName);
                if (objectType == null)
                {
                    continue;
                }
                var objectName = objectSetting.Name;
                var container  = predicate(objectType);
                if (container == null)
                {
                    var lifetimeTypeName = objectSetting.Lifetime;
                    if (!string.IsNullOrEmpty(lifetimeTypeName))
                    {
                        var lifetimeType = this.GetOrCreateType(lifetimeTypeName);
                        container = (IObjectContainer)TypeHelper.CreateObject(lifetimeType, typeof(IObjectContainer), false);
                    }
                }
                if (container == null)
                {
                    container = ObjectContainer.CreateDefault();
                }
                space.AddObject(objectName, container);                //GetObject(aliasName)时可返回
                space.AddObject(objectType, container);                //GetObject<>时可返回

                IObjectBuilder objectBuilder = null;
                var            builderName   = objectSetting.Builder;
                if (!string.IsNullOrEmpty(builderName))
                {
                    objectBuilder = ((IObjectService)this).GetObject <IObjectBuilder>(builderName);
                }
                if (objectBuilder == null)
                {
                    objectBuilder = this.ObjectBuilder;
                }
                var description = ObjectDescription.CreateFromSetting(this, objectType, objectSetting);
                container.Init(description, objectBuilder);

                //查找此类型实现的接口
                var interfaces = objectType.GetInterfaces();
                if (interfaces.Length > 0)
                {
                    foreach (var ifType in interfaces)
                    {
                        if (ifType.IsDefined(typeof(ServiceAttribute), true))
                        {
                            space.AddObject(ifType, container);
                        }
                    }
                }
            }
        }
Beispiel #8
0
 static string GetSymlinkTarget(ObjectDirectoryEntry entry)
 {
     try
     {
         return(ObjectNamespace.ReadSymlink(entry.FullPath));
     }
     catch (System.ComponentModel.Win32Exception)
     {
         return("");
     }
 }
 static string GetSymlinkTarget(ObjectDirectoryEntry entry)
 {
     try
     {
         return(ObjectNamespace.ReadSymlink(entry.FullPath));
     }
     catch (NtException)
     {
         return("");
     }
 }
        static List <string> FindDeviceObjects(IEnumerable <string> names)
        {
            Queue <string>   dumpList     = new Queue <string>(names);
            HashSet <string> dumpedDirs   = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            List <string>    totalEntries = new List <string>();

            while (dumpList.Count > 0)
            {
                string name = dumpList.Dequeue();
                try
                {
                    ObjectDirectory directory = ObjectNamespace.OpenDirectory(null, name);

                    if (!dumpedDirs.Contains(directory.FullPath))
                    {
                        dumpedDirs.Add(directory.FullPath);
                        List <ObjectDirectoryEntry> sortedEntries = new List <ObjectDirectoryEntry>(directory.Entries);
                        sortedEntries.Sort();

                        string base_name = name.TrimEnd('\\');

                        IEnumerable <ObjectDirectoryEntry> objs = sortedEntries;

                        if (_recursive)
                        {
                            foreach (ObjectDirectoryEntry entry in sortedEntries.Where(d => d.IsDirectory))
                            {
                                dumpList.Enqueue(entry.FullPath);
                            }
                        }

                        totalEntries.AddRange(objs.Where(e => e.TypeName.Equals("device", StringComparison.OrdinalIgnoreCase)).Select(e => e.FullPath));
                    }
                }
                catch (NtException ex)
                {
                    int error = NtRtl.RtlNtStatusToDosError(ex.Status);
                    if (NtRtl.RtlNtStatusToDosError(ex.Status) == 6)
                    {
                        // Add name in case it's an absolute name, not in a directory
                        totalEntries.Add(name);
                    }
                    else
                    {
                    }
                }
            }

            return(totalEntries);
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            bool show_help = false;

            int pid = Process.GetCurrentProcess().Id;

            OptionSet opts = new OptionSet()
            {
                { "r", "Recursive tree directory listing",
                  v => _recursive = v != null },
                { "sddl", "Print full SDDL security descriptors", v => _print_sddl = v != null },
                { "p|pid=", "Specify a PID of a process to impersonate when checking", v => pid = int.Parse(v.Trim()) },
                { "w", "Show only write permissions granted", v => _show_write_only = v != null },
                { "k=", String.Format("Filter on a specific directory right [{0}]",
                                      String.Join(",", Enum.GetNames(typeof(DirectoryAccessRights)))), v => _dir_rights |= ParseRight(v, typeof(DirectoryAccessRights)) },
                { "s=", String.Format("Filter on a standard right [{0}]",
                                      String.Join(",", Enum.GetNames(typeof(StandardAccessRights)))), v => _dir_rights |= ParseRight(v, typeof(StandardAccessRights)) },
                { "x=", "Specify a base path to exclude from recursive search", v => _walked.Add(v.ToLower()) },
                { "t=", "Specify a type of object to include", v => _type_filter.Add(v.ToLower()) },
                { "h|help", "show this message and exit", v => show_help = v != null },
            };

            List <string> paths = opts.Parse(args);

            if (show_help || (paths.Count == 0))
            {
                ShowHelp(opts);
            }
            else
            {
                try
                {
                    _token = NativeBridge.OpenProcessToken(pid);

                    foreach (string path in paths)
                    {
                        ObjectDirectory dir = ObjectNamespace.OpenDirectory(path);

                        DumpDirectory(dir);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Beispiel #12
0
        internal virtual void InitObjectAliases(ObjectNamespace space, IObjectNamespaceSetting spaceSetting, Func <Type, ILifetimeContainer> predicate)
        {
            if (spaceSetting.ObjectSettings == null || spaceSetting.ObjectSettings.Length <= 0)
            {
                return;
            }

            var spaceName = space.Name;
            var preName   = spaceName;

            if (!string.IsNullOrEmpty(preName))
            {
                preName += ".";
            }

            foreach (var objectSetting in spaceSetting.ObjectSettings)
            {
                var objectTypeName = this.GetTypeNameInternal(objectSetting.TypeName, spaceName);
                var objectType     = TypeHelper.CreateType(objectTypeName, true);
                var objectName     = objectSetting.Name;
                var container      = predicate(objectType);
                if (container == null)
                {
                    if (objectSetting.Lifetime != null && !string.IsNullOrEmpty(objectSetting.Lifetime.TypeName))
                    {
                        var lifetimeContainerTypeName = this.GetTypeNameInternal(objectSetting.Lifetime.TypeName, spaceName);
                        container = (ILifetimeContainer)TypeHelper.CreateObject(lifetimeContainerTypeName, typeof(ILifetimeContainer), true);
                    }
                    else
                    {
                        container = LifetimeContainer.CreateDefault();
                    }
                    this.globalObjectNamespace.TypedObjects.Add(objectType, container);
                }
                space.TypedObjects.Add(objectType, container);
                space.ObjectAliases.Add(objectName, container);
                if (this.globalObjectNamespace != space)
                {
                    this.globalObjectNamespace.ObjectAliases.Add(preName + objectName, container);
                }

                container.Init(objectSetting, this.ObjectBuilder);
            }
        }
Beispiel #13
0
        static void DumpDirectory(ObjectDirectory dir)
        {
            if (_walked.Contains(dir.FullPath.ToLower()))
            {
                return;
            }

            _walked.Add(dir.FullPath.ToLower());

            try
            {
                CheckAccess(dir.FullPath, dir.SecurityDescriptor, NtType.GetTypeByName("Directory"));

                if (_recursive)
                {
                    foreach (ObjectDirectoryEntry entry in dir.Entries)
                    {
                        try
                        {
                            if (entry.IsDirectory)
                            {
                                using (ObjectDirectory newdir = ObjectNamespace.OpenDirectory(dir, entry.ObjectName))
                                {
                                    DumpDirectory(newdir);
                                }
                            }
                            else
                            {
                                CheckAccess(entry.FullPath, entry.SecurityDescriptor, NtType.GetTypeByName(entry.TypeName));
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.Error.WriteLine("Error opening {0} {1}", entry.FullPath, ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error dumping directory {0} {1}", dir.FullPath, ex.Message);
            }
        }
        static Dictionary <string, string> FindSymlinks()
        {
            Queue <string>              dumpList   = new Queue <string>(new string[] { "\\" });
            HashSet <string>            dumpedDirs = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            Dictionary <string, string> symlinks   = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            while (dumpList.Count > 0)
            {
                string name = dumpList.Dequeue();
                try
                {
                    ObjectDirectory directory = ObjectNamespace.OpenDirectory(null, name);

                    if (!dumpedDirs.Contains(directory.FullPath))
                    {
                        dumpedDirs.Add(directory.FullPath);
                        List <ObjectDirectoryEntry> sortedEntries = new List <ObjectDirectoryEntry>(directory.Entries);
                        sortedEntries.Sort();

                        string base_name = name.TrimEnd('\\');

                        IEnumerable <ObjectDirectoryEntry> objs = sortedEntries;

                        foreach (ObjectDirectoryEntry entry in sortedEntries.Where(d => d.IsDirectory))
                        {
                            dumpList.Enqueue(entry.FullPath);
                        }

                        foreach (ObjectDirectoryEntry entry in sortedEntries.Where(d => d.IsSymlink))
                        {
                            symlinks[GetSymlinkTarget(entry)] = entry.FullPath;
                        }
                    }
                }
                catch (NtException)
                {
                }
            }

            return(symlinks);
        }
Beispiel #15
0
        static void DumpDirectories(IEnumerable <string> names)
        {
            Queue <Tuple <ObjectDirectory, string> > dumpList
                = new Queue <Tuple <ObjectDirectory, string> >(names.Select(s => new Tuple <ObjectDirectory, string>(null, s)));
            HashSet <string>            dumpedDirs   = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            List <ObjectDirectoryEntry> totalEntries = new List <ObjectDirectoryEntry>();

            while (dumpList.Count > 0)
            {
                Tuple <ObjectDirectory, string> name = dumpList.Dequeue();
                try
                {
                    using (ObjectDirectory directory = ObjectNamespace.OpenDirectory(name.Item1, name.Item2))
                    {
                        if (!dumpedDirs.Contains(directory.FullPath))
                        {
                            dumpedDirs.Add(directory.FullPath);
                            List <ObjectDirectoryEntry> sortedEntries = new List <ObjectDirectoryEntry>(directory.Entries);
                            sortedEntries.Sort();

                            string base_name = name.Item2.TrimEnd('\\');

                            IEnumerable <ObjectDirectoryEntry> objs = sortedEntries;

                            if (recursive)
                            {
                                foreach (ObjectDirectoryEntry entry in sortedEntries.Where(d => d.IsDirectory))
                                {
                                    dumpList.Enqueue(new Tuple <ObjectDirectory, string>(directory.Duplicate(), entry.ObjectName));
                                }
                            }

                            if (typeFilter.Count > 0)
                            {
                                objs = objs.Where(e => typeFilter.Contains(e.TypeName.ToLower()));
                            }

                            switch (format)
                            {
                            case OutputFormat.NameOnly:
                                OutputNameOnly(directory, objs);
                                break;

                            case OutputFormat.TypeGroup:
                                totalEntries.AddRange(objs);
                                break;

                            case OutputFormat.None:
                            default:
                                OutputNone(directory, objs);
                                break;
                            }
                        }
                    }
                }
                catch (Win32Exception ex)
                {
                    Console.Error.WriteLine("Error querying {0} - {1}", name.Item2, ex.Message);
                }
            }

            switch (format)
            {
            case OutputFormat.TypeGroup:
                OutputTypeGroup(totalEntries);
                break;
            }
        }
Beispiel #16
0
 /// <summary>
 /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use in hashing algorithms and data structures like a hash table.
 /// </summary>
 /// <returns>
 /// A hash code for the current <see cref="T:System.Object"></see>.
 /// </returns>
 public override int GetHashCode()
 {
     return
         ((ObjectName.GetHashCode() * 397) +
          (ObjectNamespace.GetHashCode()));
 }