Beispiel #1
0
        static void HandleFiles(string NewDrive, string Output)
        {
            Console.WriteLine("(bspExtractor) " + "Processing files...");
            var packages = GetPackages(NewDrive);

            foreach (var packagename in packages)
            {
                var othername = packagename.Replace(".dsm.xml", "");

                if (File.Exists(NewDrive + @"\Windows\servicing\Packages\" + packagename))
                {
                    string path = NewDrive + @"\Windows\servicing\Packages\" + packagename;

                    othername = packagename.Split('~').First();

                    Stream stream = File.OpenRead(path);

                    XmlSerializer   serializer = new XmlSerializer(typeof(XmlMum.Assembly));
                    XmlMum.Assembly package    = (XmlMum.Assembly)serializer.Deserialize(stream);

                    stream.Close();

                    var entries     = package.Package.CustomInformation.File;
                    var catpath     = (@"\Windows\servicing\Packages\" + packagename).Replace(".mum", ".cat");
                    var catfilepath = catpath.Replace("$(runtime.bootdrive)", "").Replace("$(runtime.system32)", @"Windows\System32").Replace("$(runtime.systemroot)", "Windows").Replace("$(runtime.drivers)", @"Windows\System32\Drivers").Replace("$(runtime.programfiles)", "Program Files");

                    if (catfilepath == @"\update.mum")
                    {
                        catfilepath = @"\Windows\servicing\Packages\" + packagename;
                    }

                    if (catfilepath.EndsWith(@"\update.cat"))
                    {
                        catfilepath = catfilepath.Replace(@"\update.cat", @"\" + packagename.Replace(".mum", ".cat"));
                    }

                    if (catfilepath.EndsWith(".manifest") && !catfilepath.Contains(@"\"))
                    {
                        if (!catfilepath.Contains(".deployment_"))
                        {
                            DirectoryCopy(NewDrive + @"\Windows\WinSxS\" + catfilepath.Replace(".manifest", ""), Output + @"\Files\" + othername + @"\Windows\WinSxS\" + catfilepath.Replace(".manifest", ""), true);
                        }
                        catfilepath = @"\Windows\WinSxS\Manifests\" + catfilepath;
                    }

                    if (!Directory.Exists(Output + @"\Files\" + othername + string.Join("\\", catfilepath.Split('\\').Reverse().Skip(1).Reverse())))
                    {
                        Directory.CreateDirectory(Output + @"\Files\" + othername + @"\" + string.Join("\\", catfilepath.Split('\\').Reverse().Skip(1).Reverse()));
                    }
                    try
                    {
                        File.Copy(NewDrive + @"\" + catfilepath, Output + @"\Files\" + othername + @"\" + catfilepath);
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("(bspExtractor) " + ex.Message);
                        Console.ResetColor();
                    }

                    foreach (var entry in entries)
                    {
                        string outfolder = othername;
                        if (!string.IsNullOrEmpty(entry.SourcePackage))
                        {
                            outfolder = entry.SourcePackage;
                        }

                        var filepath = entry.Name.Replace("$(runtime.bootdrive)", "").Replace("$(runtime.system32)", @"Windows\System32").Replace("$(runtime.systemroot)", "Windows").Replace("$(runtime.drivers)", @"Windows\System32\Drivers").Replace("$(runtime.programfiles)", "Program Files");

                        if (filepath == @"\update.mum")
                        {
                            filepath = @"\Windows\servicing\Packages\" + packagename;
                        }

                        if (filepath.EndsWith(@"\update.cat"))
                        {
                            filepath = filepath.Replace(@"\update.cat", @"\" + packagename.Replace(".mum", ".cat"));
                        }

                        if (filepath.EndsWith(".manifest") && !filepath.Contains(@"\"))
                        {
                            if (!filepath.Contains(".deployment_"))
                            {
                                //Console.WriteLine("(bspExtractor) " + "Copying " + @"\Windows\WinSxS\" + filepath.Replace(".manifest", "") + "...");
                                DirectoryCopy(NewDrive + @"\Windows\WinSxS\" + filepath.Replace(".manifest", ""), Output + @"\Files\" + outfolder + @"\Windows\WinSxS\" + filepath.Replace(".manifest", ""), true);
                            }
                            filepath = @"\Windows\WinSxS\Manifests\" + filepath;
                        }

                        if (!Directory.Exists(Output + @"\Files\" + outfolder + @"\" + string.Join("\\", filepath.Split('\\').Reverse().Skip(1).Reverse())))
                        {
                            Directory.CreateDirectory(Output + @"\Files\" + outfolder + @"\" + string.Join("\\", filepath.Split('\\').Reverse().Skip(1).Reverse()));
                        }
                        try
                        {
                            File.Copy(NewDrive + @"\" + filepath, Output + @"\Files\" + outfolder + @"\" + filepath);
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("(bspExtractor) " + ex.Message);
                            Console.ResetColor();
                        }
                    }
                }
                else if (File.Exists(NewDrive + @"\Windows\Packages\DsmFiles\" + packagename))
                {
                    string path = NewDrive + @"\Windows\Packages\DsmFiles\" + packagename;

                    var proc = new Process();
                    proc.StartInfo = new ProcessStartInfo("7za.exe", "x " + path)
                    {
                        WindowStyle = ProcessWindowStyle.Hidden
                    };
                    proc.Start();
                    proc.WaitForExit();

                    Stream stream = File.OpenRead(packagename.Replace(".xml", "")); //File.OpenRead(path);//

                    XmlSerializer  serializer = new XmlSerializer(typeof(XmlDsm.Package));
                    XmlDsm.Package package    = (XmlDsm.Package)serializer.Deserialize(stream);

                    stream.Close();

                    File.Delete(packagename.Replace(".xml", ""));

                    var entries = package.Files.FileEntry;

                    foreach (var entry in entries)
                    {
                        string outfolder = othername;
                        if (!string.IsNullOrEmpty(entry.SourcePackage))
                        {
                            outfolder = entry.SourcePackage;
                        }

                        if (!Directory.Exists(Output + @"\Files\" + outfolder + @"\" + string.Join("\\", entry.DevicePath.Split('\\').Reverse().Skip(1).Reverse())))
                        {
                            Directory.CreateDirectory(Output + @"\Files\" + outfolder + @"\" + string.Join("\\", entry.DevicePath.Split('\\').Reverse().Skip(1).Reverse()));
                        }
                        try
                        {
                            File.Copy(NewDrive + entry.DevicePath, Output + @"\Files\" + outfolder + @"\" + entry.DevicePath);
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("(bspExtractor) " + ex.Message);
                            Console.ResetColor();
                        }
                    }
                }
            }
        }
Beispiel #2
0
        static void HandleReg(string NewDrive, string Output)
        {
            var packages = GetPackages(NewDrive);

            Console.WriteLine("(bspExtractor) " + "Preparing files for registry import file generation...");
            foreach (var packagename in packages)
            {
                if (File.Exists(NewDrive + @"\Windows\servicing\Packages\" + packagename))
                {
                    string path = NewDrive + @"\Windows\servicing\Packages\" + packagename;

                    Stream stream = File.OpenRead(path);

                    XmlSerializer   serializer = new XmlSerializer(typeof(XmlMum.Assembly));
                    XmlMum.Assembly package    = (XmlMum.Assembly)serializer.Deserialize(stream);

                    stream.Close();

                    var entries = package.Package.CustomInformation.File.Where(x => x.Name.ToLower().EndsWith(".manifest") && !x.Name.ToLower().Contains("deployment"));

                    foreach (var entry in entries)
                    {
                        var filepath = entry.Name.Replace("$(runtime.bootdrive)", "").Replace("$(runtime.system32)", @"Windows\System32").Replace("$(runtime.systemroot)", "Windows").Replace("$(runtime.drivers)", @"Windows\System32\Drivers").Replace("$(runtime.programfiles)", "Program Files");

                        if (filepath == @"\update.mum")
                        {
                            filepath = @"\Windows\servicing\Packages\" + packagename;
                        }

                        if (filepath.EndsWith(@"\update.cat"))
                        {
                            filepath = filepath.Replace(@"\update.cat", @"\" + packagename.Replace(".mum", ".cat"));
                        }

                        if (filepath.EndsWith(".manifest") && !filepath.Contains(@"\"))
                        {
                            filepath = @"\Windows\WinSxS\Manifests\" + filepath;
                        }

                        if (!Directory.Exists(Output + @"\Registry\cbs\"))
                        {
                            Directory.CreateDirectory(Output + @"\Registry\cbs\");
                        }
                        try
                        {
                            File.Copy(NewDrive + @"\" + filepath, Output + @"\Registry\cbs\" + entry.Name);
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("(bspExtractor) " + ex.Message);
                            Console.ResetColor();
                        }
                    }
                }
                else if (File.Exists(NewDrive + @"\Windows\Packages\DsmFiles\" + packagename))
                {
                    string path = NewDrive + @"\Windows\Packages\DsmFiles\" + packagename;

                    var proc = new Process();
                    proc.StartInfo = new ProcessStartInfo("7za.exe", "x " + path)
                    {
                        WindowStyle = ProcessWindowStyle.Hidden
                    };
                    proc.Start();
                    proc.WaitForExit();

                    Stream stream = File.OpenRead(packagename.Replace(".xml", "")); //File.OpenRead(path);//

                    XmlSerializer  serializer = new XmlSerializer(typeof(XmlDsm.Package));
                    XmlDsm.Package package    = (XmlDsm.Package)serializer.Deserialize(stream);

                    stream.Close();

                    File.Delete(packagename.Replace(".xml", ""));

                    var entries = package.Files.FileEntry.Where(x => x.DevicePath.ToLower().Contains(@"windows\packages\registryfiles\"));

                    foreach (var entry in entries)
                    {
                        if (!Directory.Exists(Output + @"\Registry\spkg\"))
                        {
                            Directory.CreateDirectory(Output + @"\Registry\spkg\");
                        }
                        try
                        {
                            File.Copy(NewDrive + entry.DevicePath, Output + @"\Registry\spkg\" + entry.DevicePath.Split('\\').Last());
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("(bspExtractor) " + ex.Message);
                            Console.ResetColor();
                        }
                    }
                }
            }

            if (Directory.Exists(Output + @"\Registry\cbs\"))
            {
                Directory.CreateDirectory(Output + @"\Registry\cbs\out");
                Console.WriteLine("(bspExtractor) " + "Processing registry files...");
                foreach (var file in Directory.EnumerateFiles(Output + @"\Registry\cbs\"))
                {
                    var proc = new Process
                    {
                        StartInfo = new ProcessStartInfo("SxSExpand.exe", file + " " + Output + @"\Registry\cbs\out")
                        {
                            CreateNoWindow  = true,
                            UseShellExecute = false
                        }
                    };
                    proc.Start();
                    proc.WaitForExit();
                    File.Delete(file);
                }

                if (Directory.Exists(Output + @"\Registry\cbs\out"))
                {
                    foreach (var file in Directory.EnumerateFiles(Output + @"\Registry\cbs\out\"))
                    {
                        HandleCbsFile(file, Output + @"\Registry\");
                    }
                    Console.WriteLine("(bspExtractor) " + "Cleaning up...");
                    Directory.Delete(Output + @"\Registry\cbs\", true);
                }
            }

            if (Directory.Exists(Output + @"\Registry\spkg\"))
            {
                Console.WriteLine("(bspExtractor) " + "Processing registry files...");
                Directory.CreateDirectory(Output + @"\Registry\spkg\out\");
                foreach (var file in Directory.EnumerateFiles(Output + @"\Registry\spkg\"))
                {
                    var proc = new Process();
                    proc.StartInfo = new ProcessStartInfo("7za.exe", "x " + file + " -o" + Output + @"\Registry\spkg\out -aou")
                    {
                        WindowStyle = ProcessWindowStyle.Hidden
                    };
                    proc.Start();
                    proc.WaitForExit();
                    File.Delete(file);

                    //File.Copy(file, Output + @"\Registry\spkg\out\" + file.Split('\\').Last());
                }

                Console.WriteLine("(bspExtractor) " + "Reading files...");
                if (Directory.Exists(Output + @"\Registry\spkg\out"))
                {
                    foreach (var file in Directory.EnumerateFiles(Output + @"\Registry\spkg\"))
                    {
                        //var file = string.Join("\\", file2.Split('\\').Reverse().Skip(1).Reverse()) + @"\out\" + string.Join("\\", file2.Split('\\').Last());
                        var content = KeyNameReplace(File.ReadAllText(file));
                        File.WriteAllText(file, content);

                        if (content.Contains("; RegistrySource="))
                        {
                            foreach (var part in content.Split(new string[] { "; RegistrySource=" }, StringSplitOptions.None).Skip(1))
                            {
                                var src = part.Split('\n').First().Replace("\r", "").Replace("_1", "");

                                var newcontent = "; RegistrySource=" + file.Split('\\').Last().Replace("_1", "") + ".reg" + string.Join("\n", part.Split('\n').Skip(1));

                                if (File.Exists(Output + @"\Registry\" + src))
                                {
                                    File.AppendAllText(Output + @"\Registry\" + src, newcontent, Encoding.ASCII);
                                    continue;
                                }
                                newcontent = "Windows Registry Editor Version 5.00" + "\r\n" + newcontent;
                                File.WriteAllText(Output + @"\Registry\" + src, newcontent, Encoding.ASCII);
                            }
                        }
                        else
                        {
                            if (!File.Exists(Output + @"\Registry\" + file.Split('\\').Last().Replace("_1", "") + ".reg"))
                            {
                                File.Move(file, Output + @"\Registry\" + file.Split('\\').Last().Replace("_1", "") + ".reg");
                            }
                            else
                            {
                                File.AppendAllText(Output + @"\Registry\" + file.Split('\\').Last().Replace("_1", "") + ".reg", File.ReadAllText(file).Replace("Windows Registry Editor Version 5.00", "; RegistrySource=" + file.Split('\\').Last().Replace("_1", "") + ".reg"), Encoding.ASCII);
                            }
                        }
                    }
                    Console.WriteLine("(bspExtractor) " + "Cleaning up...");
                    Directory.Delete(Output + @"\Registry\spkg\", true);
                }
            }
        }