예제 #1
0
파일: Program.cs 프로젝트: PeterG75/reg2inf
        static void Main(string[] args)
        {
            string inputDir  = @"C:\IT-Repos\GetLumiaBSP\GetLumiaBSP\bin\Debug\out\Registry";
            string outputDir = @"E:\Happanero_8.1_ARM64\L950XL";

            var files = System.IO.Directory.EnumerateFiles(inputDir, "*.reg", System.IO.SearchOption.TopDirectoryOnly).ToList();

            foreach (var file in files)
            {
                var reg = RegImporter.GetRegistry(file);

                if (reg == null)
                {
                    Console.WriteLine($"(reg2inf) Cannot read {file}");
                    continue;
                }

                var inf = Reg2Inf.GenerateBaseInf(reg);

                if (inf == null)
                {
                    Console.WriteLine($"(reg2inf) Error extracting from {file}");
                    continue;
                }

                if (inf.Infs.Count > 0)
                {
                    var result = Reg2Inf.ExportInf(inf, file.Split('\\').Last(), outputDir);
                    if (result)
                    {
                        Console.WriteLine($"(reg2inf) Exported to {file.Split('\\').Last().Replace(".reg", "")}");
                    }
                    else
                    {
                        Console.WriteLine($"(reg2inf) Failed to export to {file.Split('\\').Last().Replace(".reg", "")} folder");
                    }
                }
            }
            Console.ReadKey();
        }
예제 #2
0
        public static BaseInf GenerateBaseInf(Dictionary <string, Hashtable> reg, bool suppressWUDFSupportErrorAsTest = false)
        {
            try
            {
                BaseInf baseInf = new BaseInf
                {
                    Infs         = new List <Inf>(),
                    ExternalRegs = new List <string>(),
                    ParsingLog   = ""
                };

                var infFiles = GetDriverInfFiles(reg);

                if (!suppressWUDFSupportErrorAsTest)
                {
                    var wudf = GetDriverWUDF(reg);
                    if (wudf.Count > 0)
                    {
                        Console.WriteLine("(reg2inf) WUDF Driver is not supported yet");
                        return(baseInf);
                    }
                }

                infFiles.ForEach(infFile =>
                {
                    Inf inf = new Inf
                    {
                        DeviceIDs      = new List <KeyValuePair <string, Descriptor> >(),
                        Configurations = new List <Configuration>(),
                        Strings        = new List <KeyValuePair <string, string> >(),
                        Services       = new List <Service>(),
                        InternalRegs   = new List <string>(),
                        Manufacturer   = ""
                    };

                    var infName = infFile.Key.GetPartAfterTo("DRIVERINFFILES");
                    inf.InfName = infName;

                    var packageName = infFile.Value["Active"] as string;
                    inf.PackageName = packageName.Replace("\"", "");

                    var configuration = infFile.Value["Configurations"] as string;

                    Version version = new Version
                    {
                        CatalogFile = infName.ToLower().Replace(".inf", ".cat"),
                        Signature   = "$WINDOWS NT$",
                        SourceFiles = new List <string>()
                    };

                    var Guids = GetDriverGuids(reg);
                    Guids.ForEach(guid =>
                    {
                        foreach (DictionaryEntry infNameForGuid in guid.Value)
                        {
                            if ((infNameForGuid.Key as string).ToUpper() == infName.ToUpper())
                            {
                                version.ClassGuid = guid.Key.GetPartAfterTo(@"DEVICEIDS");
                                version.Class     = Helper.GetClassFromGuid(version.ClassGuid);
                                break;
                            }
                        }
                    });

                    var Ids = GetDriverIds(reg);
                    Ids.ForEach(id =>
                    {
                        foreach (DictionaryEntry infNameForId in id.Value)
                        {
                            if ((infNameForId.Key as string).ToUpper() == infName.ToUpper())
                            {
                                var deviceId = id.Key.GetPartAfterTo(@"DEVICEIDS");

                                //SEARCH FOR DESCRIPTOR
                                var descs       = GetDriverPackagesDescriptors(reg, inf.PackageName, deviceId);
                                Descriptor desc = new Descriptor
                                {
                                    Manufacturer  = (descs[0].Value["Manufacturer"] as string).Replace("\"", ""),
                                    Description   = (descs[0].Value["Description"] as string).Replace("\"", ""),
                                    Configuration = (descs[0].Value["Configuration"] as string).Replace("\"", "")
                                };

                                inf.Manufacturer = (descs[0].Value["Manufacturer"] as string).Replace("\"", "");

                                inf.DeviceIDs.Add(new KeyValuePair <string, Descriptor>(deviceId, desc));

                                //SEARCH FOR CONFIGURATION
                                var confs = GetDriverPackagesConfigurations(reg, inf.PackageName, desc.Configuration);
                                confs.ForEach(conf =>
                                {
                                    var config = new Configuration
                                    {
                                        Properties   = new List <string>(),
                                        Keys         = new List <string>(),
                                        ServiceName  = (conf.Value["Service"] as string).Replace("\"", ""),
                                        FromDeviceID = deviceId
                                    };

                                    desc.LinkedServiceName = (conf.Value["Service"] as string).Replace("\"", "");

                                    var props = GetDriverPackagesConfigurationProperties(reg, inf.PackageName, desc.Configuration);


                                    config.Properties.AddRange(Helper.GetConvertedRegKeysIntoInfKeysForProperties(props));

                                    var keys = GetDriverPackagesConfigurationGeneric(reg, inf.PackageName, desc.Configuration);

                                    config.Keys.AddRange(Helper.GetConvertedRegKeysIntoInfKeys(keys, RegHive.HKR, desc.Configuration.ToUpper(), baseInf));

                                    inf.Configurations.Add(config);
                                });

                                break;
                            }
                        }
                    });

                    var servicesName = inf.Configurations.Select(x => x.ServiceName).Distinct().ToList();

                    var strings = GetDriverPackagesStrings(reg, inf.PackageName);
                    strings.ForEach(@string =>
                    {
                        foreach (DictionaryEntry item in @string.Value)
                        {
                            inf.Strings.Add(new KeyValuePair <string, string>(item.Key as string, item.Value as string));
                        }
                    });

                    servicesName.ForEach(serviceName =>
                    {
                        var service = GetServiceByName(reg, serviceName);
                        if (service.Count > 0)
                        {
                            var serv    = new Service();
                            serv.AddReg = new List <string>();

                            serv.ServiceName = serviceName;

                            if (service[0].Value.ContainsKey("DisplayName"))
                            {
                                var displayName = (service[0].Value["DisplayName"] as string).Replace("\"", "");
                                if (displayName.Contains(",") && displayName.Contains(";"))
                                {
                                    var onlyDisplayName = displayName.Split(',')[1];
                                    serv.DisplayName    = onlyDisplayName.Split(';')[0];

                                    var stringsFound = inf.Strings.Where(x => x.Key.ToLower() == serv.DisplayName.Replace("%", "").ToLower()).ToList();
                                    if (stringsFound.Count == 0)
                                    {
                                        inf.Strings.Add(new KeyValuePair <string, string>(onlyDisplayName.Split(';')[0].Replace("%", ""), "\"" + onlyDisplayName.Split(';')[1] + "\""));
                                    }
                                }
                                else
                                {
                                    serv.DisplayName = displayName;
                                }
                            }

                            if (service[0].Value.ContainsKey("ErrorControl"))
                            {
                                var errorControl  = (service[0].Value["ErrorControl"] as string).Replace("dword:", "", StringComparison.InvariantCultureIgnoreCase);
                                serv.ErrorControl = Convert.ToInt32(errorControl, 16).ToString();
                            }

                            if (service[0].Value.ContainsKey("ImagePath"))
                            {
                                var imagePath    = (service[0].Value["ImagePath"] as string);
                                serv.ServicePath = Helper.StringArrayHexToString(Helper.CleanSplitHexString(imagePath));
                            }

                            if (service[0].Value.ContainsKey("Start"))
                            {
                                var startType  = (service[0].Value["Start"] as string).Replace("dword:", "", StringComparison.InvariantCultureIgnoreCase);
                                serv.StartType = Convert.ToInt32(startType, 16).ToString();
                            }

                            if (service[0].Value.ContainsKey("Type"))
                            {
                                var serviceType  = (service[0].Value["Type"] as string).Replace("dword:", "", StringComparison.InvariantCultureIgnoreCase);
                                serv.ServiceType = Convert.ToInt32(serviceType, 16).ToString();
                            }

                            if (service[0].Value.ContainsKey("Group"))
                            {
                                var group           = (service[0].Value["Group"] as string);
                                serv.LoadOrderGroup = group.Replace("\"", "");
                            }

                            if (service[0].Value.ContainsKey("Tag"))
                            {
                                var tag  = (service[0].Value["Tag"] as string).Replace("dword:", "", StringComparison.InvariantCultureIgnoreCase);
                                serv.Tag = Convert.ToInt32(tag, 16).ToString();
                            }

                            var hash = new Hashtable();
                            foreach (DictionaryEntry item in service[0].Value)
                            {
                                if (!((item.Key as string) == "Owners" || (item.Key as string) == "DisplayName" || (item.Key as string) == "ErrorControl" || (item.Key as string) == "ImagePath" ||
                                      (item.Key as string) == "Start" || (item.Key as string) == "Type" || (item.Key as string) == "Group" || (item.Key as string) == "Tag"))
                                {
                                    hash.Add(item.Key, item.Value);
                                }
                            }

                            var regToAdd = GetRegsByServiceName(reg, serviceName);

                            serv.AddReg.AddRange(Helper.GetConvertedRegKeysIntoInfKeys(regToAdd, RegHive.HKR, serviceName, baseInf));

                            var wdf = GetWDFByServiceName(reg, serviceName);

                            if (wdf.Count > 0)
                            {
                                serv.IsWdf = true;
                                serv.KmdfLibraryVersion = (wdf[0].Value["KmdfLibraryVersion"] as string).ToString();
                            }


                            if (hash.Count > 0)
                            {
                                serv.AddReg.AddRange(Helper.GetConvertedRegKeysIntoInfKeys(new List <KeyValuePair <string, Hashtable> >()
                                {
                                    new KeyValuePair <string, Hashtable>(service[0].Key, hash)
                                }, RegHive.HKR, serviceName, baseInf));
                            }
                            inf.Services.Add(serv);
                        }
                    });

                    var DriverPackage = GetDriverPackages(reg, inf.PackageName);
                    DriverPackage.ForEach(package =>
                    {
                        if (package.Key.ToUpper().EndsWith(inf.PackageName.ToUpper()))
                        {
                            version.DriverVer = Helper.GetProperDriverVer(Helper.StringToByteArray(Helper.CleanHexString(package.Value["Version"] as string)));
                            version.Provider  = package.Value["Provider"] as string;
                        }
                    });

                    var pnpLockDownFiles = GetPnpLockDownFiles(reg);
                    pnpLockDownFiles.ForEach(file =>
                    {
                        var ownerString = file.Value["Owners"] as string;

                        if (ownerString != null)
                        {
                            var owner = Helper.StringArrayHexToString(Helper.CleanSplitHexString(ownerString));
                            if (owner.ToLower() == inf.InfName.ToLower())
                            {
                                version.PnpLockDown = true;
                                version.SourceFiles.Add(file.Key.GetPartAfterTo("PNPLOCKDOWNFILES"));
                            }
                        }
                    });

                    inf.Version = version;
                    baseInf.Infs.Add(inf);
                });

                Dictionary <string, Hashtable> debloatedReg = reg;

                baseInf.Infs.ForEach(inf => inf.Services.ForEach(service => debloatedReg = RegImporter.DebloatReg(debloatedReg, BloatType.SPECIFIC_SERVICE, $@"\{service.ServiceName}")));

                debloatedReg = RegImporter.DebloatReg(debloatedReg, BloatType.ALL);

                if (baseInf.Infs.Count == 1)
                {
                    var list = debloatedReg.ToList();
                    baseInf.Infs[0].InternalRegs.AddRange(Helper.GetConvertedRegKeysIntoInfKeys(list, RegHive.AUTO_DETECT, "", baseInf));
                    //Internal
                }
                else if (baseInf.Infs.Count > 1)
                {
                    var list = debloatedReg.ToList();
                    baseInf.ExternalRegs.AddRange(Helper.GetConvertedRegKeysIntoInfKeys(list, RegHive.AUTO_DETECT, "", baseInf));
                    //ExternalRegs
                }

                return(baseInf);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }